Skip to content

Commit 751d4da

Browse files
[clangd] Assert that the testcases in LocateSymbol.All have no diagnostics
Summary: Also fix some bugs in the testcases which this exposed. Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D72066
1 parent 57835d0 commit 751d4da

File tree

1 file changed

+62
-22
lines changed

1 file changed

+62
-22
lines changed

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

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ TEST(LocateSymbol, All) {
253253
)cpp",
254254

255255
R"cpp(// Function definition via pointer
256-
int [[foo]](int) {}
256+
void [[foo]](int) {}
257257
int main() {
258258
auto *X = &^foo;
259259
}
@@ -270,7 +270,7 @@ TEST(LocateSymbol, All) {
270270
struct Foo { int [[x]]; };
271271
int main() {
272272
Foo bar;
273-
bar.^x;
273+
(void)bar.^x;
274274
}
275275
)cpp",
276276

@@ -281,13 +281,6 @@ TEST(LocateSymbol, All) {
281281
};
282282
)cpp",
283283

284-
R"cpp(// Field, GNU old-style field designator
285-
struct Foo { int [[x]]; };
286-
int main() {
287-
Foo bar = { ^x : 1 };
288-
}
289-
)cpp",
290-
291284
R"cpp(// Field, field designator
292285
struct Foo { int [[x]]; };
293286
int main() {
@@ -322,19 +315,11 @@ TEST(LocateSymbol, All) {
322315

323316
R"cpp(// Namespace
324317
namespace $decl[[ns]] {
325-
struct Foo { static void bar(); }
318+
struct Foo { static void bar(); };
326319
} // namespace ns
327320
int main() { ^ns::Foo::bar(); }
328321
)cpp",
329322

330-
R"cpp(// Macro
331-
#define MACRO 0
332-
#define [[MACRO]] 1
333-
int main() { return ^MACRO; }
334-
#define MACRO 2
335-
#undef macro
336-
)cpp",
337-
338323
R"cpp(// Macro
339324
class TTT { public: int a; };
340325
#define [[FF]](S) if (int b = S.a) {}
@@ -352,7 +337,7 @@ TEST(LocateSymbol, All) {
352337

353338
R"cpp(// Symbol concatenated inside macro (not supported)
354339
int *pi;
355-
#define POINTER(X) p # X;
340+
#define POINTER(X) p ## X;
356341
int i = *POINTER(^i);
357342
)cpp",
358343

@@ -433,18 +418,18 @@ TEST(LocateSymbol, All) {
433418
)cpp",
434419

435420
R"cpp(// No implicit constructors
436-
class X {
421+
struct X {
437422
X(X&& x) = default;
438423
};
439-
X [[makeX]]() {}
424+
X $decl[[makeX]]();
440425
void foo() {
441426
auto x = m^akeX();
442427
}
443428
)cpp",
444429

445430
R"cpp(
446431
struct X {
447-
X& [[operator]]++() {}
432+
X& $decl[[operator]]++();
448433
};
449434
void foo(X& x) {
450435
+^+x;
@@ -529,6 +514,61 @@ TEST(LocateSymbol, All) {
529514
// parsing.
530515
TU.ExtraArgs.push_back("-fno-delayed-template-parsing");
531516

517+
auto AST = TU.build();
518+
for (auto &D : AST.getDiagnostics())
519+
ADD_FAILURE() << D;
520+
ASSERT_TRUE(AST.getDiagnostics().empty()) << Test;
521+
522+
auto Results = locateSymbolAt(AST, T.point());
523+
524+
if (!WantDecl) {
525+
EXPECT_THAT(Results, IsEmpty()) << Test;
526+
} else {
527+
ASSERT_THAT(Results, ::testing::SizeIs(1)) << Test;
528+
EXPECT_EQ(Results[0].PreferredDeclaration.range, *WantDecl) << Test;
529+
llvm::Optional<Range> GotDef;
530+
if (Results[0].Definition)
531+
GotDef = Results[0].Definition->range;
532+
EXPECT_EQ(WantDef, GotDef) << Test;
533+
}
534+
}
535+
}
536+
537+
// LocateSymbol test cases that produce warnings.
538+
// These are separated out from All so that in All we can assert
539+
// that there are no diagnostics.
540+
TEST(LocateSymbol, Warnings) {
541+
const char *Tests[] = {
542+
R"cpp(// Field, GNU old-style field designator
543+
struct Foo { int [[x]]; };
544+
int main() {
545+
Foo bar = { ^x : 1 };
546+
}
547+
)cpp",
548+
549+
R"cpp(// Macro
550+
#define MACRO 0
551+
#define [[MACRO]] 1
552+
int main() { return ^MACRO; }
553+
#define MACRO 2
554+
#undef macro
555+
)cpp",
556+
};
557+
558+
for (const char *Test : Tests) {
559+
Annotations T(Test);
560+
llvm::Optional<Range> WantDecl;
561+
llvm::Optional<Range> WantDef;
562+
if (!T.ranges().empty())
563+
WantDecl = WantDef = T.range();
564+
if (!T.ranges("decl").empty())
565+
WantDecl = T.range("decl");
566+
if (!T.ranges("def").empty())
567+
WantDef = T.range("def");
568+
569+
TestTU TU;
570+
TU.Code = T.code();
571+
532572
auto AST = TU.build();
533573
auto Results = locateSymbolAt(AST, T.point());
534574

0 commit comments

Comments
 (0)