diff options
Diffstat (limited to 'clang-tools-extra/clangd/unittests/SourceCodeTests.cpp')
-rw-r--r-- | clang-tools-extra/clangd/unittests/SourceCodeTests.cpp | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp index 8f9386e802a..d94d1c9f52b 100644 --- a/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp +++ b/clang-tools-extra/clangd/unittests/SourceCodeTests.cpp @@ -312,6 +312,45 @@ TEST(SourceCodeTests, SourceLocationInMainFile) { } } +TEST(ClangdUnitTest, GetBeginningOfIdentifier) { + std::string Preamble = R"cpp( +struct Bar { int func(); }; +#define MACRO(X) void f() { X; } +Bar* bar; + )cpp"; + // First ^ is the expected beginning, last is the search position. + for (std::string Text : std::vector<std::string>{ + "int ^f^oo();", // inside identifier + "int ^foo();", // beginning of identifier + "int ^foo^();", // end of identifier + "int foo(^);", // non-identifier + "^int foo();", // beginning of file (can't back up) + "int ^f0^0();", // after a digit (lexing at N-1 is wrong) + "int ^λλ^λ();", // UTF-8 handled properly when backing up + + // identifier in macro arg + "MACRO(bar->^func())", // beginning of identifier + "MACRO(bar->^fun^c())", // inside identifier + "MACRO(bar->^func^())", // end of identifier + "MACRO(^bar->func())", // begin identifier + "MACRO(^bar^->func())", // end identifier + "^MACRO(bar->func())", // beginning of macro name + "^MAC^RO(bar->func())", // inside macro name + "^MACRO^(bar->func())", // end of macro name + }) { + std::string WithPreamble = Preamble + Text; + Annotations TestCase(WithPreamble); + auto AST = TestTU::withCode(TestCase.code()).build(); + const auto &SourceMgr = AST.getSourceManager(); + SourceLocation Actual = getBeginningOfIdentifier( + TestCase.points().back(), SourceMgr, AST.getASTContext().getLangOpts()); + Position ActualPos = offsetToPosition( + TestCase.code(), + SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual))); + EXPECT_EQ(TestCase.points().front(), ActualPos) << Text; + } +} + TEST(SourceCodeTests, CollectIdentifiers) { auto Style = format::getLLVMStyle(); auto IDs = collectIdentifiers(R"cpp( @@ -417,8 +456,8 @@ TEST(SourceCodeTests, GetMacros) { )cpp"); TestTU TU = TestTU::withCode(Code.code()); auto AST = TU.build(); - auto Loc = getBeginningOfIdentifier(AST, Code.point(), - AST.getSourceManager().getMainFileID()); + auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(), + AST.getASTContext().getLangOpts()); auto Result = locateMacroAt(Loc, AST.getPreprocessor()); ASSERT_TRUE(Result); EXPECT_THAT(*Result, MacroName("MACRO")); |