diff options
author | serge-sans-paille <sguelton@redhat.com> | 2020-12-09 09:26:27 +0100 |
---|---|---|
committer | serge-sans-paille <sguelton@redhat.com> | 2021-05-04 11:19:01 +0200 |
commit | b83b23275b745287bf9d3d72a93b593119f53f75 (patch) | |
tree | 631e684005609ed7cd0709d5d805aaddb5edf365 /clang/lib/Basic/IdentifierTable.cpp | |
parent | 46fa214a6f24549d83a69793b7e14585c2eefa2b (diff) | |
download | llvm-b83b23275b745287bf9d3d72a93b593119f53f75.zip llvm-b83b23275b745287bf9d3d72a93b593119f53f75.tar.gz llvm-b83b23275b745287bf9d3d72a93b593119f53f75.tar.bz2 |
Introduce -Wreserved-identifier
Warn when a declaration uses an identifier that doesn't obey the reserved
identifier rule from C and/or C++.
Differential Revision: https://reviews.llvm.org/D93095
Diffstat (limited to 'clang/lib/Basic/IdentifierTable.cpp')
-rw-r--r-- | clang/lib/Basic/IdentifierTable.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/clang/lib/Basic/IdentifierTable.cpp b/clang/lib/Basic/IdentifierTable.cpp index cedc94a..e591530 100644 --- a/clang/lib/Basic/IdentifierTable.cpp +++ b/clang/lib/Basic/IdentifierTable.cpp @@ -273,6 +273,39 @@ bool IdentifierInfo::isCPlusPlusKeyword(const LangOptions &LangOpts) const { return !isKeyword(LangOptsNoCPP); } +ReservedIdentifierStatus +IdentifierInfo::isReserved(const LangOptions &LangOpts) const { + StringRef Name = getName(); + + // '_' is a reserved identifier, but its use is so common (e.g. to store + // ignored values) that we don't warn on it. + if (Name.size() <= 1) + return ReservedIdentifierStatus::NotReserved; + + // [lex.name] p3 + if (Name[0] == '_') { + + // Each name that begins with an underscore followed by an uppercase letter + // or another underscore is reserved. + if (Name[1] == '_') + return ReservedIdentifierStatus::StartsWithDoubleUnderscore; + + if ('A' <= Name[1] && Name[1] <= 'Z') + return ReservedIdentifierStatus:: + StartsWithUnderscoreFollowedByCapitalLetter; + + // This is a bit misleading: it actually means it's only reserved if we're + // at global scope because it starts with an underscore. + return ReservedIdentifierStatus::StartsWithUnderscoreAtGlobalScope; + } + + // Each name that contains a double underscore (__) is reserved. + if (LangOpts.CPlusPlus && Name.contains("__")) + return ReservedIdentifierStatus::ContainsDoubleUnderscore; + + return ReservedIdentifierStatus::NotReserved; +} + tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const { // We use a perfect hash function here involving the length of the keyword, // the first and third character. For preprocessor ID's there are no |