diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2025-05-02 07:20:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-02 07:20:02 -0400 |
commit | 85c810060e1a2a90dc4ec184b4c4275db17ef28f (patch) | |
tree | 28b3fe6f699647511cef8ef915793ef367f6ee23 /clang/lib/Basic/IdentifierTable.cpp | |
parent | 3d4f979e271d2a1fe0906f4d1b16db108838f98f (diff) | |
download | llvm-85c810060e1a2a90dc4ec184b4c4275db17ef28f.zip llvm-85c810060e1a2a90dc4ec184b4c4275db17ef28f.tar.gz llvm-85c810060e1a2a90dc4ec184b4c4275db17ef28f.tar.bz2 |
[C] Diagnose use of C++ keywords in C (#137234)
This adds a new diagnostic group, -Wc++-keyword, which is off by default
and grouped under -Wc++-compat. The diagnostic catches use of C++
keywords in C code.
This change additionally fixes an issue with -Wreserved-identifier not
diagnosing use of reserved identifiers in function parameter lists in a
function declaration which is not a definition.
Fixes https://github.com/llvm/llvm-project/issues/21898
Diffstat (limited to 'clang/lib/Basic/IdentifierTable.cpp')
-rw-r--r-- | clang/lib/Basic/IdentifierTable.cpp | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp index 16151c9..cc472c9 100644 --- a/clang/lib/Basic/IdentifierTable.cpp +++ b/clang/lib/Basic/IdentifierTable.cpp @@ -250,6 +250,32 @@ static KeywordStatus getKeywordStatus(const LangOptions &LangOpts, return CurStatus; } +static bool IsKeywordInCpp(unsigned Flags) { + while (Flags != 0) { + unsigned CurFlag = Flags & ~(Flags - 1); + Flags = Flags & ~CurFlag; + switch (static_cast<TokenKey>(CurFlag)) { + case KEYCXX: + case KEYCXX11: + case KEYCXX20: + case BOOLSUPPORT: + case WCHARSUPPORT: + case CHAR8SUPPORT: + return true; + default: + break; // Go to the next flag, try again. + } + } + return false; +} + +static void MarkIdentifierAsKeywordInCpp(IdentifierTable &Table, + StringRef Name) { + IdentifierInfo &II = Table.get(Name, tok::identifier); + II.setIsKeywordInCPlusPlus(); + II.setHandleIdentifierCase(); +} + /// AddKeyword - This method is used to associate a token ID with specific /// identifiers because they are language keywords. This causes the lexer to /// automatically map matching identifiers to specialized token codes. @@ -258,8 +284,18 @@ static void AddKeyword(StringRef Keyword, const LangOptions &LangOpts, IdentifierTable &Table) { KeywordStatus AddResult = getKeywordStatus(LangOpts, Flags); - // Don't add this keyword if disabled in this language. - if (AddResult == KS_Disabled) return; + // Don't add this keyword if disabled in this language and isn't otherwise + // special. + if (AddResult == KS_Disabled) { + // We do not consider any identifiers to be C++ keywords when in + // Objective-C because @ effectively introduces a custom grammar where C++ + // keywords can be used (and similar for selectors). We could enable this + // for Objective-C, but it would require more logic to ensure we do not + // issue compatibility diagnostics in these cases. + if (!LangOpts.ObjC && IsKeywordInCpp(Flags)) + MarkIdentifierAsKeywordInCpp(Table, Keyword); + return; + } IdentifierInfo &Info = Table.get(Keyword, AddResult == KS_Future ? tok::identifier : TokenCode); @@ -304,9 +340,11 @@ void IdentifierTable::AddKeywords(const LangOptions &LangOpts) { #define ALIAS(NAME, TOK, FLAGS) \ AddKeyword(StringRef(NAME), tok::kw_ ## TOK, \ FLAGS, LangOpts, *this); -#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ - if (LangOpts.CXXOperatorNames) \ - AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); +#define CXX_KEYWORD_OPERATOR(NAME, ALIAS) \ + if (LangOpts.CXXOperatorNames) \ + AddCXXOperatorKeyword(StringRef(#NAME), tok::ALIAS, *this); \ + else \ + MarkIdentifierAsKeywordInCpp(*this, StringRef(#NAME)); #define OBJC_AT_KEYWORD(NAME) \ if (LangOpts.ObjC) \ AddObjCKeyword(StringRef(#NAME), tok::objc_##NAME, *this); |