Skip to content

Commit 1f82042

Browse files
author
git apple-llvm automerger
committed
Merge commit '1425c7223676' from llvm.org/master into apple/main
2 parents f0d0f16 + 1425c72 commit 1f82042

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

clang-tools-extra/clangd/SourceCode.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,12 @@ std::vector<Range> collectIdentifierRanges(llvm::StringRef Identifier,
633633
return Ranges;
634634
}
635635

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+
636642
namespace {
637643
struct NamespaceEvent {
638644
enum {

clang-tools-extra/clangd/SourceCode.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ struct SpelledWord {
248248
const LangOptions &LangOpts);
249249
};
250250

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+
251255
/// Heuristically determine namespaces visible at a point, without parsing Code.
252256
/// This considers using-directives and enclosing namespace-declarations that
253257
/// are visible (and not obfuscated) in the file itself (not headers).

clang-tools-extra/clangd/unittests/SourceCodeTests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,19 @@ TEST(SourceCodeTests, isHeaderFile) {
789789
EXPECT_TRUE(isHeaderFile("header.h", LangOpts));
790790
}
791791

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+
792805
} // namespace
793806
} // namespace clangd
794807
} // namespace clang

0 commit comments

Comments
 (0)