18 #include "clang/Basic/SourceLocation.h" 19 #include "clang/Basic/SourceManager.h" 20 #include "llvm/Support/Error.h" 21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 27 using ::testing::ElementsAreArray;
29 class IgnoreDiagnostics :
public DiagnosticsConsumer {
31 std::vector<Diag> Diagnostics)
override {}
35 const char *Tests[] = {
36 R
"cpp( // Single statement in a function body. 38 [[[[int v = [[1^00]]]];]] 44 // int v = (10 + 2) * (a + a); 45 [[[[int v = [[[[([[[[10^]] + 2]])]] * (a + a)]]]];]] 48 R"cpp( // Function call. 49 int add(int x, int y) { return x + y; } 51 // int res = add(11, 22); 52 [[[[int res = [[add([[1^1]], 22)]]]];]] 55 R"cpp( // Tricky macros. 58 // int var = (4 + 15 MUL 6 + 10); 59 [[[[int var = [[[[([[4 + [[1^5]]]] MUL]] 6 + 10)]]]];]] 62 R"cpp( // Cursor inside a macro. 63 #define HASH(x) ((x) % 10) 65 [[[[int a = [[HASH([[[[2^3]] + 34]])]]]];]] 68 R"cpp( // Cursor on a macro. 69 #define HASH(x) ((x) % 10) 71 [[[[int a = [[HA^SH(23)]]]];]] 74 R"cpp( // Multiple declaration. 76 [[[[int var1, var^2]], var3;]] 79 R"cpp( // Before comment. 82 [[[[int var2 = [[[[var1]]^ /*some comment*/ + 41]]]];]] 88 R
"cpp( // Single statement in TU. 91 R"cpp( // Cursor at end of VarDecl. 95 R
"cpp( // Cursor in between spaces. 102 struct AAA { struct BBB { static int ccc(); };}; 104 // int x = AAA::BBB::ccc(); 105 [[[[int x = [[[[AAA::BBB::c^cc]]()]]]];]] 109 struct AAA { struct BBB { static int ccc(); };}; 111 // int x = AAA::BBB::ccc(); 112 [[[[int x = [[[[[[[[[[AA^A]]::]]BBB::]]ccc]]()]]]];]] 115 R"cpp( // Inside struct. 116 struct A { static int a(); }; 119 [[return [[[[1^1]] + 2]]]]; 129 // int x = nsa::nsb::ccc(); 130 [[[[int x = [[[[nsa::nsb::cc^c]]()]]]];]] 138 for (
const char *Test : Tests) {
139 auto T = Annotations(Test);
142 ElementsAreArray(T.ranges()))
147 TEST(SemanticSelection, RunViaClangDServer) {
150 MockCompilationDatabase CDB;
154 FS.Files[FooH] = R
"cpp( 156 #define HASH(x) ((x) % 10) 160 const char *SourceContents = R
"cpp( 162 [[void bar(int& inp) [[{ 163 // inp = HASH(foo(inp)); 164 [[inp = [[HASH([[foo([[in^p]])]])]]]]; 167 Annotations SourceAnnotations(SourceContents); 168 FS.Files[FooCpp] = SourceAnnotations.code(); 169 Server.addDocument(FooCpp, SourceAnnotations.code()); 172 ASSERT_TRUE(
bool(Ranges))
173 <<
"getSemanticRange returned an error: " << Ranges.takeError();
174 EXPECT_THAT(*Ranges, ElementsAreArray(SourceAnnotations.ranges()));
llvm::Expected< std::vector< Range > > runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos)
llvm::StringRef PathRef
A typedef to represent a ref to file path.
static Options optsForTest()
TEST(BackgroundQueueTest, Priority)
std::string testPath(PathRef File)
virtual void onDiagnosticsReady(PathRef File, std::vector< Diag > Diagnostics)=0
Called by ClangdServer when Diagnostics for File are ready.
llvm::Expected< std::vector< Range > > getSemanticRanges(ParsedAST &AST, Position Pos)
Returns the list of all interesting ranges around the Position Pos.
static TestTU withCode(llvm::StringRef Code)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
IgnoreDiagnostics DiagConsumer