File tree Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Expand file tree Collapse file tree 3 files changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -633,6 +633,12 @@ std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
633
633
return Ranges;
634
634
}
635
635
636
+ bool isKeyword (llvm::StringRef NewName, const LangOptions &LangOpts) {
637
+ // Keywords are initialized in constructor.
638
+ clang::IdentifierTable KeywordsTable (LangOpts);
639
+ return KeywordsTable.find (NewName) != KeywordsTable.end ();
640
+ }
641
+
636
642
namespace {
637
643
struct NamespaceEvent {
638
644
enum {
Original file line number Diff line number Diff line change @@ -248,6 +248,10 @@ struct SpelledWord {
248
248
const LangOptions &LangOpts);
249
249
};
250
250
251
+ // / Return true if the \p TokenName is in the list of reversed keywords of the
252
+ // / language.
253
+ bool isKeyword (llvm::StringRef TokenName, const LangOptions &LangOpts);
254
+
251
255
// / Heuristically determine namespaces visible at a point, without parsing Code.
252
256
// / This considers using-directives and enclosing namespace-declarations that
253
257
// / are visible (and not obfuscated) in the file itself (not headers).
Original file line number Diff line number Diff line change @@ -789,6 +789,19 @@ TEST(SourceCodeTests, isHeaderFile) {
789
789
EXPECT_TRUE (isHeaderFile (" header.h" , LangOpts));
790
790
}
791
791
792
+ TEST (SourceCodeTests, isKeywords) {
793
+ LangOptions LangOpts;
794
+ LangOpts.CPlusPlus20 = true ;
795
+ EXPECT_TRUE (isKeyword (" int" , LangOpts));
796
+ EXPECT_TRUE (isKeyword (" return" , LangOpts));
797
+ EXPECT_TRUE (isKeyword (" co_await" , LangOpts));
798
+
799
+ // these are identifiers (not keywords!) with special meaning in some
800
+ // contexts.
801
+ EXPECT_FALSE (isKeyword (" final" , LangOpts));
802
+ EXPECT_FALSE (isKeyword (" override" , LangOpts));
803
+ }
804
+
792
805
} // namespace
793
806
} // namespace clangd
794
807
} // namespace clang
You can’t perform that action at this time.
0 commit comments