This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rG6045cb89e5e8: Use an allow list on reserved macro identifiers (authored by serge-sans-paille). Herald added a project: clang.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D102168/new/ https://reviews.llvm.org/D102168 Files: clang/lib/Lex/PPDirectives.cpp clang/test/Preprocessor/macro-reserved.c Index: clang/test/Preprocessor/macro-reserved.c =================================================================== --- clang/test/Preprocessor/macro-reserved.c +++ 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 Index: clang/lib/Lex/PPDirectives.cpp =================================================================== --- clang/lib/Lex/PPDirectives.cpp +++ clang/lib/Lex/PPDirectives.cpp @@ -121,8 +121,49 @@ 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;
Index: clang/test/Preprocessor/macro-reserved.c =================================================================== --- clang/test/Preprocessor/macro-reserved.c +++ 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 Index: clang/lib/Lex/PPDirectives.cpp =================================================================== --- clang/lib/Lex/PPDirectives.cpp +++ clang/lib/Lex/PPDirectives.cpp @@ -121,8 +121,49 @@ 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;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits