Skip to content

Commit cf8c358

Browse files
committed
Revert "[Lex] Warn when defining or undefining any builtin macro"
This reverts commit 22e3f58. Breaks check-clang on arm, see https://reviews.llvm.org/D144654#4349954 Also reverts follow-up "[AArch64] Don't redefine _LP64 and __LP64__" This reverts commit e55d52c.
1 parent d763c6e commit cf8c358

File tree

6 files changed

+72
-83
lines changed

6 files changed

+72
-83
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ Improvements to Clang's diagnostics
285285
- Clang constexpr evaluator now prints subobject's name instead of its type in notes
286286
when a constexpr variable has uninitialized subobjects after its constructor call.
287287
(`#58601 <https://github.com/llvm/llvm-project/issues/58601>`_)
288-
- Clang now warns when any predefined macro is undefined or redefined, instead
289-
of only some of them.
290288

291289
Bug Fixes in This Version
292290
-------------------------

clang/lib/Basic/Targets/AArch64.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ void AArch64TargetInfo::getTargetDefines(const LangOptions &Opts,
340340
getTriple().isOSBinFormatELF())
341341
Builder.defineMacro("__ELF__");
342342

343+
// Target properties.
344+
if (!getTriple().isOSWindows() && getTriple().isArch64Bit()) {
345+
Builder.defineMacro("_LP64");
346+
Builder.defineMacro("__LP64__");
347+
}
348+
343349
std::string CodeModel = getTargetOpts().CodeModel;
344350
if (CodeModel == "default")
345351
CodeModel = "small";

clang/lib/Lex/PPDirectives.cpp

Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -109,52 +109,52 @@ enum PPElifDiag {
109109
PED_Elifndef
110110
};
111111

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-
153112
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
154113
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+
}
155157
StringRef Text = II->getName();
156-
if (isReservedInAllContexts(II->isReserved(Lang)))
157-
return isFeatureTestMacro(Text) ? MD_NoWarn : MD_ReservedMacro;
158158
if (II->isKeyword(Lang))
159159
return MD_KeywordDef;
160160
if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
@@ -319,6 +319,15 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
319319
return Diag(MacroNameTok, diag::err_defined_macro_name);
320320
}
321321

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+
322331
// If defining/undefining reserved identifier or a keyword, we need to issue
323332
// a warning.
324333
SourceLocation MacroNameLoc = MacroNameTok.getLocation();
@@ -3003,12 +3012,6 @@ MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
30033012
MI->setTokens(Tokens, BP);
30043013
return MI;
30053014
}
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-
30123015
/// HandleDefineDirective - Implements \#define. This consumes the entire macro
30133016
/// line then lets the caller lex the next real token.
30143017
void Preprocessor::HandleDefineDirective(
@@ -3080,9 +3083,15 @@ void Preprocessor::HandleDefineDirective(
30803083
// In Objective-C, ignore attempts to directly redefine the builtin
30813084
// definitions of the ownership qualifiers. It's still possible to
30823085
// #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() &&
30863095
isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
30873096
// Warn if it changes the tokens.
30883097
if ((!getDiagnostics().getSuppressSystemWarnings() ||
@@ -3106,9 +3115,7 @@ void Preprocessor::HandleDefineDirective(
31063115

31073116
// Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
31083117
// 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())
31123119
Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
31133120
// Macros must be identical. This means all tokens and whitespace
31143121
// separation must be the same. C99 6.10.3p2.
@@ -3188,14 +3195,6 @@ void Preprocessor::HandleUndefDirective() {
31883195
if (!MI->isUsed() && MI->isWarnIfUnused())
31893196
Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
31903197

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-
31993198
if (MI->isWarnIfUnused())
32003199
WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
32013200

clang/test/Lexer/builtin_redef.c

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
// RUN: %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -D__STDC__=1 -U__STDC_HOSTED__ -E 2>&1 | FileCheck %s --check-prefix=CHECK-OUT
2-
// RUN: %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -D__STDC__=1 -U__STDC_HOSTED__ -E 2>&1 | FileCheck %s --check-prefix=CHECK-WARN
3-
// RUN: not %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -D__STDC__=1 -U__STDC_HOSTED__ -E 2>&1 -pedantic-errors | FileCheck %s --check-prefix=CHECK-ERR
1+
// RUN: %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -E 2>&1 | FileCheck %s --check-prefix=CHECK-OUT
2+
// RUN: %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -E 2>&1 | FileCheck %s --check-prefix=CHECK-WARN
3+
// RUN: not %clang_cc1 %s -D__TIME__=1234 -U__DATE__ -E 2>&1 -pedantic-errors | FileCheck %s --check-prefix=CHECK-ERR
44

55
// CHECK-WARN: <command line>:{{.*}} warning: redefining builtin macro
6-
// CHECK-WARN-NEXT: #define __TIME__ 1234
76
// CHECK-WARN: <command line>:{{.*}} warning: undefining builtin macro
8-
// CHECK-WARN-NEXT: #undef __DATE__
9-
// CHECK-WARN: <command line>:{{.*}} warning: redefining builtin macro
10-
// CHECK-WARN-NEXT: #define __STDC__ 1
11-
// CHECK-WARN: <command line>:{{.*}} warning: undefining builtin macro
12-
// CHECK-WARN-NEXT: #undef __STDC_HOSTED__
137

148
// CHECK-ERR: <command line>:{{.*}} error: redefining builtin macro
15-
// CHECK-ERR-NEXT: #define __TIME__ 1234
16-
// CHECK-ERR: <command line>:{{.*}} error: undefining builtin macro
17-
// CHECK-ERR-NEXT: #undef __DATE__
18-
// CHECK-ERR: <command line>:{{.*}} error: redefining builtin macro
19-
// CHECK-ERR-NEXT: #define __STDC__ 1
209
// CHECK-ERR: <command line>:{{.*}} error: undefining builtin macro
21-
// CHECK-ERR-NEXT: #undef __STDC_HOSTED__
2210

2311
int n = __TIME__;
2412
__DATE__

clang/test/Preprocessor/macro-reserved.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@
66
#define __cplusplus
77
#define _HAVE_X 0
88
#define X__Y
9-
#define __STDC__ 1 // expected-warning {{redefining builtin macro}}
109

1110
#undef for
1211
#undef final
1312
#undef __HAVE_X
1413
#undef __cplusplus
1514
#undef _HAVE_X
1615
#undef X__Y
17-
#undef __STDC_HOSTED__ // expected-warning {{undefining builtin macro}}
1816

1917
// allowlisted definitions
2018
#define while while

clang/test/Preprocessor/macro-reserved.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#undef _HAVE_X
1313
#undef X__Y
1414

15-
#undef __cplusplus // expected-warning {{undefining builtin macro}}
15+
#undef __cplusplus
1616
#define __cplusplus
1717

1818
// allowlisted definitions

0 commit comments

Comments
 (0)