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"
@@ -101,7 +103,8 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(Token &Tmp) {
101103enum MacroDiag {
102104 MD_NoWarn, // > Not a reserved identifier
103105 MD_KeywordDef, // > Macro hides keyword, enabled by default
104- MD_ReservedMacro // > #define of #undef reserved id, disabled by default
106+ MD_ReservedMacro, // > #define of #undef reserved id, disabled by default
107+ MD_ReservedAttributeIdentifier
105108};
106109
107110// / Enumerates possible %select values for the pp_err_elif_after_else and
@@ -177,6 +180,20 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
177180 return false ;
178181}
179182
183+ static bool isReservedCXXAttributeName (Preprocessor &PP, IdentifierInfo *II) {
184+ const LangOptions &Lang = PP.getLangOpts ();
185+ if (Lang.CPlusPlus &&
186+ hasAttribute (AttributeCommonInfo::Syntax::AS_CXX11, /* Scope*/ nullptr , II,
187+ PP.getTargetInfo (), Lang) > 0 ) {
188+ AttributeCommonInfo::Kind AttrKind = AttributeCommonInfo::getParsedKind (
189+ II, /* Scope*/ nullptr , AttributeCommonInfo::Syntax::AS_CXX11);
190+ return !((AttrKind == AttributeCommonInfo::Kind::AT_Likely ||
191+ AttrKind == AttributeCommonInfo::Kind::AT_Unlikely) &&
192+ PP.isNextPPTokenLParen ());
193+ }
194+ return false ;
195+ }
196+
180197static MacroDiag shouldWarnOnMacroDef (Preprocessor &PP, IdentifierInfo *II) {
181198 const LangOptions &Lang = PP.getLangOpts ();
182199 StringRef Text = II->getName ();
@@ -186,6 +203,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
186203 return MD_KeywordDef;
187204 if (Lang.CPlusPlus11 && (Text == " override" || Text == " final" ))
188205 return MD_KeywordDef;
206+ if (isReservedCXXAttributeName (PP, II))
207+ return MD_ReservedAttributeIdentifier;
189208 return MD_NoWarn;
190209}
191210
@@ -194,6 +213,8 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
194213 // Do not warn on keyword undef. It is generally harmless and widely used.
195214 if (isReservedInAllContexts (II->isReserved (Lang)))
196215 return MD_ReservedMacro;
216+ if (isReservedCXXAttributeName (PP, II))
217+ return MD_ReservedAttributeIdentifier;
197218 return MD_NoWarn;
198219}
199220
@@ -369,6 +390,9 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
369390 }
370391 if (D == MD_ReservedMacro)
371392 Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_id);
393+ if (D == MD_ReservedAttributeIdentifier)
394+ Diag (MacroNameTok, diag::warn_pp_macro_is_reserved_attribute_id)
395+ << II->getName ();
372396 }
373397
374398 // Okay, we got a good identifier.
0 commit comments