|
59 | 59 | #include "clang/Sema/SemaWasm.h" |
60 | 60 | #include "clang/Sema/Template.h" |
61 | 61 | #include "llvm/ADT/STLForwardCompat.h" |
| 62 | +#include "llvm/ADT/SmallPtrSet.h" |
62 | 63 | #include "llvm/ADT/SmallString.h" |
63 | 64 | #include "llvm/ADT/StringExtras.h" |
64 | 65 | #include "llvm/ADT/StringSwitch.h" |
@@ -6112,17 +6113,23 @@ static bool isKeywordInCPlusPlus(const Sema &S, const IdentifierInfo *II) { |
6112 | 6113 | if (!II) |
6113 | 6114 | return false; |
6114 | 6115 |
|
6115 | | - // Clear all the C language options, set all the C++ language options, but |
6116 | | - // otherwise leave all the defaults alone. This isn't ideal because some |
6117 | | - // feature flags are language-specific, but this is a reasonable |
6118 | | - // approximation. |
6119 | | - LangOptions LO = S.getLangOpts(); |
6120 | | - LO.CPlusPlus = LO.CPlusPlus11 = LO.CPlusPlus14 = LO.CPlusPlus17 = |
6121 | | - LO.CPlusPlus20 = LO.CPlusPlus23 = LO.CPlusPlus26 = 1; |
6122 | | - LO.C99 = LO.C11 = LO.C17 = LO.C23 = LO.C2y = 0; |
6123 | | - LO.Char8 = 1; // Presume this is always a keyword in C++. |
6124 | | - LO.WChar = 1; // Presume this is always a keyword in C++. |
6125 | | - return II->isNameKeyword(LO); |
| 6116 | + // Build a static map of identifiers for all of the keywords in C++ that are |
| 6117 | + // not keywords in C. This allows us to do pointer comparisons instead of |
| 6118 | + // string comparisons when deciding whether the given identifier is a keyword |
| 6119 | + // or not. Note, this treats all keywords as being enabled, regardless of the |
| 6120 | + // setting of other language options. It intentionally disables the modules |
| 6121 | + // keywords because those are conditional keywords, so may be safe to use. |
| 6122 | + static llvm::SmallPtrSet<IdentifierInfo *, 32> Keywords; |
| 6123 | + if (Keywords.empty()) { |
| 6124 | +#define MODULES_KEYWORD(NAME) |
| 6125 | +#define KEYWORD(NAME, FLAGS) \ |
| 6126 | + Keywords.insert(&S.getPreprocessor().getIdentifierTable().get(#NAME)); |
| 6127 | +#define CXX_KEYWORD_OPERATOR(NAME, TOK) \ |
| 6128 | + Keywords.insert(&S.getPreprocessor().getIdentifierTable().get(#NAME)); |
| 6129 | +#include "clang/Basic/TokenKinds.def" |
| 6130 | + } |
| 6131 | + |
| 6132 | + return Keywords.contains(II); |
6126 | 6133 | } |
6127 | 6134 |
|
6128 | 6135 | void Sema::warnOnReservedIdentifier(const NamedDecl *D) { |
|
0 commit comments