@@ -109,52 +109,52 @@ enum PPElifDiag {
109
109
PED_Elifndef
110
110
};
111
111
112
- static bool isFeatureTestMacro (StringRef MacroName) {
113
- // list from:
114
- // * https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
115
- // * https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
116
- // * man 7 feature_test_macros
117
- // The list must be sorted for correct binary search.
118
- static constexpr StringRef ReservedMacro[] = {
119
- " _ATFILE_SOURCE" ,
120
- " _BSD_SOURCE" ,
121
- " _CRT_NONSTDC_NO_WARNINGS" ,
122
- " _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES" ,
123
- " _CRT_SECURE_NO_WARNINGS" ,
124
- " _FILE_OFFSET_BITS" ,
125
- " _FORTIFY_SOURCE" ,
126
- " _GLIBCXX_ASSERTIONS" ,
127
- " _GLIBCXX_CONCEPT_CHECKS" ,
128
- " _GLIBCXX_DEBUG" ,
129
- " _GLIBCXX_DEBUG_PEDANTIC" ,
130
- " _GLIBCXX_PARALLEL" ,
131
- " _GLIBCXX_PARALLEL_ASSERTIONS" ,
132
- " _GLIBCXX_SANITIZE_VECTOR" ,
133
- " _GLIBCXX_USE_CXX11_ABI" ,
134
- " _GLIBCXX_USE_DEPRECATED" ,
135
- " _GNU_SOURCE" ,
136
- " _ISOC11_SOURCE" ,
137
- " _ISOC95_SOURCE" ,
138
- " _ISOC99_SOURCE" ,
139
- " _LARGEFILE64_SOURCE" ,
140
- " _POSIX_C_SOURCE" ,
141
- " _REENTRANT" ,
142
- " _SVID_SOURCE" ,
143
- " _THREAD_SAFE" ,
144
- " _XOPEN_SOURCE" ,
145
- " _XOPEN_SOURCE_EXTENDED" ,
146
- " __STDCPP_WANT_MATH_SPEC_FUNCS__" ,
147
- " __STDC_FORMAT_MACROS" ,
148
- };
149
- return std::binary_search (std::begin (ReservedMacro), std::end (ReservedMacro),
150
- MacroName);
151
- }
152
-
153
112
static MacroDiag shouldWarnOnMacroDef (Preprocessor &PP, IdentifierInfo *II) {
154
113
const LangOptions &Lang = PP.getLangOpts ();
114
+ if (isReservedInAllContexts (II->isReserved (Lang))) {
115
+ // list from:
116
+ // - https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
117
+ // - https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
118
+ // - man 7 feature_test_macros
119
+ // The list must be sorted for correct binary search.
120
+ static constexpr StringRef ReservedMacro[] = {
121
+ " _ATFILE_SOURCE" ,
122
+ " _BSD_SOURCE" ,
123
+ " _CRT_NONSTDC_NO_WARNINGS" ,
124
+ " _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES" ,
125
+ " _CRT_SECURE_NO_WARNINGS" ,
126
+ " _FILE_OFFSET_BITS" ,
127
+ " _FORTIFY_SOURCE" ,
128
+ " _GLIBCXX_ASSERTIONS" ,
129
+ " _GLIBCXX_CONCEPT_CHECKS" ,
130
+ " _GLIBCXX_DEBUG" ,
131
+ " _GLIBCXX_DEBUG_PEDANTIC" ,
132
+ " _GLIBCXX_PARALLEL" ,
133
+ " _GLIBCXX_PARALLEL_ASSERTIONS" ,
134
+ " _GLIBCXX_SANITIZE_VECTOR" ,
135
+ " _GLIBCXX_USE_CXX11_ABI" ,
136
+ " _GLIBCXX_USE_DEPRECATED" ,
137
+ " _GNU_SOURCE" ,
138
+ " _ISOC11_SOURCE" ,
139
+ " _ISOC95_SOURCE" ,
140
+ " _ISOC99_SOURCE" ,
141
+ " _LARGEFILE64_SOURCE" ,
142
+ " _POSIX_C_SOURCE" ,
143
+ " _REENTRANT" ,
144
+ " _SVID_SOURCE" ,
145
+ " _THREAD_SAFE" ,
146
+ " _XOPEN_SOURCE" ,
147
+ " _XOPEN_SOURCE_EXTENDED" ,
148
+ " __STDCPP_WANT_MATH_SPEC_FUNCS__" ,
149
+ " __STDC_FORMAT_MACROS" ,
150
+ };
151
+ if (std::binary_search (std::begin (ReservedMacro), std::end (ReservedMacro),
152
+ II->getName ()))
153
+ return MD_NoWarn;
154
+
155
+ return MD_ReservedMacro;
156
+ }
155
157
StringRef Text = II->getName ();
156
- if (isReservedInAllContexts (II->isReserved (Lang)))
157
- return isFeatureTestMacro (Text) ? MD_NoWarn : MD_ReservedMacro;
158
158
if (II->isKeyword (Lang))
159
159
return MD_KeywordDef;
160
160
if (Lang.CPlusPlus11 && (Text.equals (" override" ) || Text.equals (" final" )))
@@ -319,6 +319,15 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
319
319
return Diag (MacroNameTok, diag::err_defined_macro_name);
320
320
}
321
321
322
+ if (isDefineUndef == MU_Undef) {
323
+ auto *MI = getMacroInfo (II);
324
+ if (MI && MI->isBuiltinMacro ()) {
325
+ // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
326
+ // and C++ [cpp.predefined]p4], but allow it as an extension.
327
+ Diag (MacroNameTok, diag::ext_pp_undef_builtin_macro);
328
+ }
329
+ }
330
+
322
331
// If defining/undefining reserved identifier or a keyword, we need to issue
323
332
// a warning.
324
333
SourceLocation MacroNameLoc = MacroNameTok.getLocation ();
@@ -3003,12 +3012,6 @@ MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
3003
3012
MI->setTokens (Tokens, BP);
3004
3013
return MI;
3005
3014
}
3006
-
3007
- static bool isObjCProtectedMacro (const IdentifierInfo *II) {
3008
- return II->isStr (" __strong" ) || II->isStr (" __weak" ) ||
3009
- II->isStr (" __unsafe_unretained" ) || II->isStr (" __autoreleasing" );
3010
- }
3011
-
3012
3015
// / HandleDefineDirective - Implements \#define. This consumes the entire macro
3013
3016
// / line then lets the caller lex the next real token.
3014
3017
void Preprocessor::HandleDefineDirective (
@@ -3080,9 +3083,15 @@ void Preprocessor::HandleDefineDirective(
3080
3083
// In Objective-C, ignore attempts to directly redefine the builtin
3081
3084
// definitions of the ownership qualifiers. It's still possible to
3082
3085
// #undef them.
3083
- if (getLangOpts ().ObjC &&
3084
- SourceMgr.getFileID (OtherMI->getDefinitionLoc ()) ==
3085
- getPredefinesFileID () &&
3086
+ auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
3087
+ return II->isStr (" __strong" ) ||
3088
+ II->isStr (" __weak" ) ||
3089
+ II->isStr (" __unsafe_unretained" ) ||
3090
+ II->isStr (" __autoreleasing" );
3091
+ };
3092
+ if (getLangOpts ().ObjC &&
3093
+ SourceMgr.getFileID (OtherMI->getDefinitionLoc ())
3094
+ == getPredefinesFileID () &&
3086
3095
isObjCProtectedMacro (MacroNameTok.getIdentifierInfo ())) {
3087
3096
// Warn if it changes the tokens.
3088
3097
if ((!getDiagnostics ().getSuppressSystemWarnings () ||
@@ -3106,9 +3115,7 @@ void Preprocessor::HandleDefineDirective(
3106
3115
3107
3116
// Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
3108
3117
// C++ [cpp.predefined]p4, but allow it as an extension.
3109
- if (OtherMI->isBuiltinMacro () ||
3110
- (SourceMgr.isWrittenInBuiltinFile (OtherMI->getDefinitionLoc ()) &&
3111
- !isFeatureTestMacro (MacroNameTok.getIdentifierInfo ()->getName ())))
3118
+ if (OtherMI->isBuiltinMacro ())
3112
3119
Diag (MacroNameTok, diag::ext_pp_redef_builtin_macro);
3113
3120
// Macros must be identical. This means all tokens and whitespace
3114
3121
// separation must be the same. C99 6.10.3p2.
@@ -3188,14 +3195,6 @@ void Preprocessor::HandleUndefDirective() {
3188
3195
if (!MI->isUsed () && MI->isWarnIfUnused ())
3189
3196
Diag (MI->getDefinitionLoc (), diag::pp_macro_not_used);
3190
3197
3191
- // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
3192
- // C++ [cpp.predefined]p4, but allow it as an extension. Don't warn if this
3193
- // is an Objective-C builtin macro though.
3194
- if ((MI->isBuiltinMacro () ||
3195
- SourceMgr.isWrittenInBuiltinFile (MI->getDefinitionLoc ())) &&
3196
- !(getLangOpts ().ObjC && isObjCProtectedMacro (II)))
3197
- Diag (MacroNameTok, diag::ext_pp_undef_builtin_macro);
3198
-
3199
3198
if (MI->isWarnIfUnused ())
3200
3199
WarnUnusedMacroLocs.erase (MI->getDefinitionLoc ());
3201
3200
0 commit comments