11
11
// /
12
12
// ===----------------------------------------------------------------------===//
13
13
14
+ #include " clang/Basic/AttributeCommonInfo.h"
15
+ #include " clang/Basic/Attributes.h"
14
16
#include " clang/Basic/CharInfo.h"
15
17
#include " clang/Basic/DirectoryEntry.h"
16
18
#include " clang/Basic/FileManager.h"
@@ -97,7 +99,8 @@ SourceRange Preprocessor::DiscardUntilEndOfDirective(Token &Tmp) {
97
99
enum MacroDiag {
98
100
MD_NoWarn, // > Not a reserved identifier
99
101
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
101
104
};
102
105
103
106
// / Enumerates possible %select values for the pp_err_elif_after_else and
@@ -173,6 +176,22 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
173
176
return false ;
174
177
}
175
178
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
+
176
195
static MacroDiag shouldWarnOnMacroDef (Preprocessor &PP, IdentifierInfo *II) {
177
196
const LangOptions &Lang = PP.getLangOpts ();
178
197
StringRef Text = II->getName ();
@@ -182,6 +201,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
182
201
return MD_KeywordDef;
183
202
if (Lang.CPlusPlus11 && (Text == " override" || Text == " final" ))
184
203
return MD_KeywordDef;
204
+ if (isReservedCXXAttributeName (PP, II))
205
+ return MD_ReservedAttributeIdentifier;
185
206
return MD_NoWarn;
186
207
}
187
208
@@ -190,6 +211,8 @@ static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
190
211
// Do not warn on keyword undef. It is generally harmless and widely used.
191
212
if (isReservedInAllContexts (II->isReserved (Lang)))
192
213
return MD_ReservedMacro;
214
+ if (isReservedCXXAttributeName (PP, II))
215
+ return MD_ReservedAttributeIdentifier;
193
216
return MD_NoWarn;
194
217
}
195
218
@@ -365,6 +388,9 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
365
388
}
366
389
if (D == MD_ReservedMacro)
367
390
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 ();
368
394
}
369
395
370
396
// Okay, we got a good identifier.
0 commit comments