1111// /
1212// ===----------------------------------------------------------------------===//
1313
14+ #include " clang/Basic/AttributeCommonInfo.h"
15+ #include " clang/Basic/Attributes.h"
1416#include " clang/Basic/CharInfo.h"
1517#include " clang/Basic/DirectoryEntry.h"
1618#include " clang/Basic/FileManager.h"
@@ -97,7 +99,8 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(Token &Tmp) {
9799enum MacroDiag {
98100 MD_NoWarn, // > Not a reserved identifier
99101 MD_KeywordDef, // > Macro hides keyword, enabled by default
100- MD_ReservedMacro // > #define of #undef reserved id, disabled by default
102+ MD_ReservedMacro, // > #define of #undef reserved id, disabled by default
103+ MD_ReservedAttributeIdentifier
101104};
102105
103106// / Enumerates possible %select values for the pp_err_elif_after_else and
@@ -173,6 +176,22 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
173176 return false ;
174177}
175178
179+ static bool isReservedCXXAttributeName (Preprocessor &PP, IdentifierInfo *II) {
180+ const LangOptions &Lang = PP.getLangOpts ();
181+ if (Lang.CPlusPlus &&
182+ hasAttribute (AttributeCommonInfo::AS_CXX11, /* Scope*/ nullptr , II,
183+ PP.getTargetInfo (), Lang, /* CheckPlugins*/ false ) > 0 ) {
184+ AttributeCommonInfo::AttrArgsInfo AttrArgsInfo =
185+ AttributeCommonInfo::getCXX11AttrArgsInfo (II);
186+ if (AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Required)
187+ return PP.isNextPPTokenLParen ();
188+
189+ return !PP.isNextPPTokenLParen () ||
190+ AttrArgsInfo == AttributeCommonInfo::AttrArgsInfo::Optional;
191+ }
192+ return false ;
193+ }
194+
176195static MacroDiag shouldWarnOnMacroDef (Preprocessor &PP, IdentifierInfo *II) {
177196 const LangOptions &Lang = PP.getLangOpts ();
178197 StringRef Text = II->getName ();
@@ -182,6 +201,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
182201 return MD_KeywordDef;
183202 if (Lang.CPlusPlus11 && (Text == " override" || Text == " final" ))
184203 return MD_KeywordDef;
204+ if (isReservedCXXAttributeName (PP, II))
205+ return MD_ReservedAttributeIdentifier;
185206 return MD_NoWarn;
186207}
187208
@@ -190,6 +211,8 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
190211 // Do not warn on keyword undef. It is generally harmless and widely used.
191212 if (isReservedInAllContexts (II->isReserved (Lang)))
192213 return MD_ReservedMacro;
214+ if (isReservedCXXAttributeName (PP, II))
215+ return MD_ReservedAttributeIdentifier;
193216 return MD_NoWarn;
194217}
195218
@@ -365,6 +388,9 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
365388 }
366389 if (D == MD_ReservedMacro)
367390 Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_id);
391+ if (D == MD_ReservedAttributeIdentifier)
392+ Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
393+ << II->getName ();
368394 }
369395
370396 // Okay, we got a good identifier.
0 commit comments