[clang] 6045cb8 - Use an allow list on reserved macro identifiers
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 13 00:24:10 PDT 2021
Author: serge-sans-paille
Date: 2021-05-13T09:23:47+02:00
New Revision: 6045cb89e5e8223eea4d7f9c3a3e0d8d25ff909f
URL: https://github.com/llvm/llvm-project/commit/6045cb89e5e8223eea4d7f9c3a3e0d8d25ff909f
DIFF: https://github.com/llvm/llvm-project/commit/6045cb89e5e8223eea4d7f9c3a3e0d8d25ff909f.diff
LOG: Use an allow list on reserved macro identifiers
The allow list is based on various official sources (see in-code comment).
This fixes https://bugs.llvm.org/show_bug.cgi?id=50248
Differential Revision: https://reviews.llvm.org/D102168
Added:
Modified:
clang/lib/Lex/PPDirectives.cpp
clang/test/Preprocessor/macro-reserved.c
Removed:
################################################################################
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 469c4e48525cc..a74b42a883fec 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -121,8 +121,49 @@ static bool isForModuleBuilding(Module *M, StringRef CurrentModule,
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
const LangOptions &Lang = PP.getLangOpts();
- if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved)
+ if (II->isReserved(Lang) != ReservedIdentifierStatus::NotReserved) {
+ // list from:
+ // - https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
+ // - https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
+ // - man 7 feature_test_macros
+ // The list must be sorted for correct binary search.
+ static constexpr StringRef ReservedMacro[] = {
+ "_ATFILE_SOURCE",
+ "_BSD_SOURCE",
+ "_CRT_NONSTDC_NO_WARNINGS",
+ "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES",
+ "_CRT_SECURE_NO_WARNINGS",
+ "_FILE_OFFSET_BITS",
+ "_FORTIFY_SOURCE",
+ "_GLIBCXX_ASSERTIONS",
+ "_GLIBCXX_CONCEPT_CHECKS",
+ "_GLIBCXX_DEBUG",
+ "_GLIBCXX_DEBUG_PEDANTIC",
+ "_GLIBCXX_PARALLEL",
+ "_GLIBCXX_PARALLEL_ASSERTIONS",
+ "_GLIBCXX_SANITIZE_VECTOR",
+ "_GLIBCXX_USE_CXX11_ABI",
+ "_GLIBCXX_USE_DEPRECATED",
+ "_GNU_SOURCE",
+ "_ISOC11_SOURCE",
+ "_ISOC95_SOURCE",
+ "_ISOC99_SOURCE",
+ "_LARGEFILE64_SOURCE",
+ "_POSIX_C_SOURCE",
+ "_REENTRANT",
+ "_SVID_SOURCE",
+ "_THREAD_SAFE",
+ "_XOPEN_SOURCE",
+ "_XOPEN_SOURCE_EXTENDED",
+ "__STDCPP_WANT_MATH_SPEC_FUNCS__",
+ "__STDC_FORMAT_MACROS",
+ };
+ if (std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro),
+ II->getName()))
+ return MD_NoWarn;
+
return MD_ReservedMacro;
+ }
StringRef Text = II->getName();
if (II->isKeyword(Lang))
return MD_KeywordDef;
diff --git a/clang/test/Preprocessor/macro-reserved.c b/clang/test/Preprocessor/macro-reserved.c
index 899074e799aff..f7bec04c73bf8 100644
--- a/clang/test/Preprocessor/macro-reserved.c
+++ b/clang/test/Preprocessor/macro-reserved.c
@@ -62,3 +62,6 @@
#undef X__Y
int x;
+
+#define _GNU_SOURCE // no-warning
+#define __STDC_FORMAT_MACROS // no-warning
More information about the cfe-commits
mailing list