https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/106036
Fixes #92196 https://eel.is/c++draft/macro.names#2 > A translation unit shall not #define or #undef names lexically identical to > keywords, to the identifiers listed in Table > [4](https://eel.is/c++draft/lex.name#tab:lex.name.special), or to the > [attribute-token](https://eel.is/c++draft/dcl.attr.grammar#nt:attribute-token)s > described in [[dcl.attr]](https://eel.is/c++draft/dcl.attr), except that the > names likely and unlikely may be defined as function-like macros > ([[cpp.replace]](https://eel.is/c++draft/cpp.replace))[.](https://eel.is/c++draft/macro.names#2.sentence-1) >From 67e70c3abc2527d2f35abdc10ce2710dd9ebb918 Mon Sep 17 00:00:00 2001 From: Oleksandr T <oleksandr.taras...@outlook.com> Date: Mon, 26 Aug 2024 07:37:04 +0300 Subject: [PATCH] [Clang] restrict use of attribute names reserved by the C++ standard --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Lex/PPDirectives.cpp | 22 +++++++++++++ .../Preprocessor/macro-reserved-attrs.cpp | 33 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 clang/test/Preprocessor/macro-reserved-attrs.cpp diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 2c6c7e083b9c91..b8b76eb0ff380c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -254,6 +254,8 @@ Improvements to Clang's diagnostics compilation speed with modules. This warning is disabled by default and it needs to be explicitly enabled or by ``-Weverything``. +- Clang now diagnoses the use of attribute names reserved by the C++ standard. (#GH92196). + Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 4e77df9ec444c7..bbe1b4a640c4ac 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -177,6 +177,26 @@ static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr, return false; } +static bool isReservedAttrName(Preprocessor &PP, IdentifierInfo *II) { + const LangOptions &Lang = PP.getLangOpts(); + const StringRef Name = II->getName(); + + if (Lang.CPlusPlus26) + return Name == "indeterminate"; + if (Lang.CPlusPlus23) + return Name == "assume"; + if (Lang.CPlusPlus20) + return Name == "no_unique_address"; + if (Lang.CPlusPlus17) + return Name == "fallthrough" || Name == "nodiscard" || + Name == "maybe_unused"; + if (Lang.CPlusPlus14) + return Name == "deprecated"; + if (Lang.CPlusPlus11) + return Name == "noreturn" || Name == "carries_dependency"; + return false; +} + static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) { const LangOptions &Lang = PP.getLangOpts(); StringRef Text = II->getName(); @@ -186,6 +206,8 @@ static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) { return MD_KeywordDef; if (Lang.CPlusPlus11 && (Text == "override" || Text == "final")) return MD_KeywordDef; + if (isReservedAttrName(PP, II)) + return MD_KeywordDef; return MD_NoWarn; } diff --git a/clang/test/Preprocessor/macro-reserved-attrs.cpp b/clang/test/Preprocessor/macro-reserved-attrs.cpp new file mode 100644 index 00000000000000..b4336f801e438a --- /dev/null +++ b/clang/test/Preprocessor/macro-reserved-attrs.cpp @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx11 -pedantic -std=c++11 %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx14 -pedantic -std=c++14 %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 -pedantic -std=c++17 %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx20 -pedantic -std=c++20 %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx23 -pedantic -std=c++23 %s +// RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx26 -pedantic -std=c++26 %s + +#define noreturn 1 // cxx11-warning {{keyword is hidden by macro definition}} +#undef noreturn + +#define carries_dependency 1 // cxx11-warning {{keyword is hidden by macro definition}} +#undef carries_dependency + +#define deprecated 1 // cxx14-warning {{keyword is hidden by macro definition}} +#undef deprecated + +#define fallthrough 1 // cxx17-warning {{keyword is hidden by macro definition}} +#undef fallthrough + +#define maybe_unused 1 // cxx17-warning {{keyword is hidden by macro definition}} +#undef maybe_unused + +#define nodiscard 1 // cxx17-warning {{keyword is hidden by macro definition}} +#undef nodiscard + +#define no_unique_address 1 // cxx20-warning {{keyword is hidden by macro definition}} +#undef no_unique_address + +#define assume 1 // cxx23-warning {{keyword is hidden by macro definition}} +#undef assume + +#define indeterminate 1 // cxx26-warning {{keyword is hidden by macro definition}} +#undef indeterminate _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits