[PATCH] D88810: [clangd] Add isKeyword function.
Haojian Wu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 5 01:04:13 PDT 2020
hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, arphaman.
Herald added a project: clang.
hokein requested review of this revision.
Herald added subscribers: MaskRay, ilya-biryukov.
This will be used in rename for doing basic name validation.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D88810
Files:
clang-tools-extra/clangd/SourceCode.cpp
clang-tools-extra/clangd/SourceCode.h
clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
Index: clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -789,6 +789,18 @@
EXPECT_TRUE(isHeaderFile("header.h", LangOpts));
}
+TEST(SourceCodeTests, isKeywords) {
+ LangOptions LangOpts;
+ LangOpts.CPlusPlus11 = true;
+ EXPECT_TRUE(isKeyword("int", LangOpts));
+ EXPECT_TRUE(isKeyword("return", 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
Index: clang-tools-extra/clangd/SourceCode.h
===================================================================
--- clang-tools-extra/clangd/SourceCode.h
+++ clang-tools-extra/clangd/SourceCode.h
@@ -248,6 +248,10 @@
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).
Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -633,6 +633,12 @@
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 {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D88810.296104.patch
Type: text/x-patch
Size: 2001 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20201005/6a53e63d/attachment-0001.bin>
More information about the cfe-commits
mailing list