[clangd] Add isKeyword function.

This will be used in rename for doing basic name validation.

Differential Revision: https://reviews.llvm.org/D88810
This commit is contained in:
Haojian Wu 2020-10-05 15:10:53 +02:00
parent ff86acbb79
commit 1425c72236
3 changed files with 23 additions and 0 deletions

View File

@ -633,6 +633,12 @@ std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
return Ranges;
}
bool isKeyword(llvm::StringRef NewName, const LangOptions &LangOpts) {
// Keywords are initialized in constructor.
clang::IdentifierTable KeywordsTable(LangOpts);
return KeywordsTable.find(NewName) != KeywordsTable.end();
}
namespace {
struct NamespaceEvent {
enum {

View File

@ -248,6 +248,10 @@ struct SpelledWord {
const LangOptions &LangOpts);
};
/// Return true if the \p TokenName is in the list of reversed keywords of the
/// language.
bool isKeyword(llvm::StringRef TokenName, const LangOptions &LangOpts);
/// Heuristically determine namespaces visible at a point, without parsing Code.
/// This considers using-directives and enclosing namespace-declarations that
/// are visible (and not obfuscated) in the file itself (not headers).

View File

@ -789,6 +789,19 @@ TEST(SourceCodeTests, isHeaderFile) {
EXPECT_TRUE(isHeaderFile("header.h", LangOpts));
}
TEST(SourceCodeTests, isKeywords) {
LangOptions LangOpts;
LangOpts.CPlusPlus20 = true;
EXPECT_TRUE(isKeyword("int", LangOpts));
EXPECT_TRUE(isKeyword("return", LangOpts));
EXPECT_TRUE(isKeyword("co_await", LangOpts));
// these are identifiers (not keywords!) with special meaning in some
// contexts.
EXPECT_FALSE(isKeyword("final", LangOpts));
EXPECT_FALSE(isKeyword("override", LangOpts));
}
} // namespace
} // namespace clangd
} // namespace clang