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,20 @@ 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::Syntax::AS_CXX11, /* Scope*/ nullptr , II,
183+ PP.getTargetInfo (), Lang) > 0 ) {
184+ AttributeCommonInfo::Kind AttrKind = AttributeCommonInfo::getParsedKind (
185+ II, /* Scope*/ nullptr , AttributeCommonInfo::Syntax::AS_CXX11);
186+ return !((AttrKind == AttributeCommonInfo::Kind::AT_Likely ||
187+ AttrKind == AttributeCommonInfo::Kind::AT_Unlikely) &&
188+ PP.isNextPPTokenLParen ());
189+ }
190+ return false ;
191+ }
192+
176193static MacroDiag shouldWarnOnMacroDef (Preprocessor &PP, IdentifierInfo *II) {
177194 const LangOptions &Lang = PP.getLangOpts ();
178195 StringRef Text = II->getName ();
@@ -182,6 +199,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
182199 return MD_KeywordDef;
183200 if (Lang.CPlusPlus11 && (Text == " override" || Text == " final" ))
184201 return MD_KeywordDef;
202+ if (isReservedCXXAttributeName (PP, II))
203+ return MD_ReservedAttributeIdentifier;
185204 return MD_NoWarn;
186205}
187206
@@ -190,6 +209,8 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
190209 // Do not warn on keyword undef. It is generally harmless and widely used.
191210 if (isReservedInAllContexts (II->isReserved (Lang)))
192211 return MD_ReservedMacro;
212+ if (isReservedCXXAttributeName (PP, II))
213+ return MD_ReservedAttributeIdentifier;
193214 return MD_NoWarn;
194215}
195216
@@ -365,6 +386,9 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
365386 }
366387 if (D == MD_ReservedMacro)
367388 Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_id);
389+ if (D == MD_ReservedAttributeIdentifier)
390+ Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
391+ << II->getName ();
368392 }
369393
370394 // Okay, we got a good identifier.
0 commit comments