1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
|
//===-- SemanticSelectionTests.cpp ----------------*- C++ -*--------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "Annotations.h"
#include "ClangdServer.h"
#include "Protocol.h"
#include "SemanticSelection.h"
#include "SyncAPI.h"
#include "TestFS.h"
#include "TestTU.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <vector>
namespace clang {
namespace clangd {
namespace {
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::UnorderedElementsAreArray;
// front() is SR.range, back() is outermost range.
std::vector<Range> gatherRanges(const SelectionRange &SR) {
std::vector<Range> Ranges;
for (const SelectionRange *S = &SR; S; S = S->parent.get())
Ranges.push_back(S->range);
return Ranges;
}
std::vector<Range>
gatherFoldingRanges(llvm::ArrayRef<FoldingRange> FoldingRanges) {
std::vector<Range> Ranges;
Range NextRange;
for (const auto &R : FoldingRanges) {
NextRange.start.line = R.startLine;
NextRange.start.character = R.startCharacter;
NextRange.end.line = R.endLine;
NextRange.end.character = R.endCharacter;
Ranges.push_back(NextRange);
}
return Ranges;
}
TEST(SemanticSelection, All) {
const char *Tests[] = {
R"cpp( // Single statement in a function body.
[[void func() [[{
[[[[int v = [[1^00]]]];]]
}]]]]
)cpp",
R"cpp( // Expression
[[void func() [[{
int a = 1;
// int v = (10 + 2) * (a + a);
[[[[int v = [[[[([[[[10^]] + 2]])]] * (a + a)]]]];]]
}]]]]
)cpp",
R"cpp( // Function call.
int add(int x, int y) { return x + y; }
[[void callee() [[{
// int res = add(11, 22);
[[[[int res = [[add([[1^1]], 22)]]]];]]
}]]]]
)cpp",
R"cpp( // Tricky macros.
#define MUL ) * (
[[void func() [[{
// int var = (4 + 15 MUL 6 + 10);
[[[[int var = [[[[([[4 + [[1^5]]]] MUL]] 6 + 10)]]]];]]
}]]]]
)cpp",
R"cpp( // Cursor inside a macro.
#define HASH(x) ((x) % 10)
[[void func() [[{
[[[[int a = [[HASH([[[[2^3]] + 34]])]]]];]]
}]]]]
)cpp",
R"cpp( // Cursor on a macro.
#define HASH(x) ((x) % 10)
[[void func() [[{
[[[[int a = [[HA^SH(23)]]]];]]
}]]]]
)cpp",
R"cpp( // Multiple declaration.
[[void func() [[{
[[[[int var1, var^2]], var3;]]
}]]]]
)cpp",
R"cpp( // Before comment.
[[void func() [[{
int var1 = 1;
[[[[int var2 = [[[[var1]]^ /*some comment*/ + 41]]]];]]
}]]]]
)cpp",
// Empty file.
"[[^]]",
// FIXME: We should get the whole DeclStmt as a range.
R"cpp( // Single statement in TU.
[[int v = [[1^00]]]];
)cpp",
R"cpp( // Cursor at end of VarDecl.
[[int v = [[100]]^]];
)cpp",
// FIXME: No node found associated to the position.
R"cpp( // Cursor in between spaces.
void func() {
int v = 100 + [[^]] 100;
}
)cpp",
// Structs.
R"cpp(
struct AAA { struct BBB { static int ccc(); };};
[[void func() [[{
// int x = AAA::BBB::ccc();
[[[[int x = [[[[AAA::BBB::c^cc]]()]]]];]]
}]]]]
)cpp",
R"cpp(
struct AAA { struct BBB { static int ccc(); };};
[[void func() [[{
// int x = AAA::BBB::ccc();
[[[[int x = [[[[[[[[[[AA^A]]::]]BBB::]]ccc]]()]]]];]]
}]]]]
)cpp",
R"cpp( // Inside struct.
struct A { static int a(); };
[[struct B {
[[static int b() [[{
[[return [[[[1^1]] + 2]]]];
}]]]]
}]];
)cpp",
// Namespaces.
R"cpp(
[[namespace nsa {
[[namespace nsb {
static int ccc();
[[void func() [[{
// int x = nsa::nsb::ccc();
[[[[int x = [[[[nsa::nsb::cc^c]]()]]]];]]
}]]]]
}]]
}]]
)cpp",
};
for (const char *Test : Tests) {
auto T = Annotations(Test);
auto AST = TestTU::withCode(T.code()).build();
EXPECT_THAT(gatherRanges(llvm::cantFail(getSemanticRanges(AST, T.point()))),
ElementsAreArray(T.ranges()))
<< Test;
}
}
TEST(SemanticSelection, RunViaClangdServer) {
MockFS FS;
MockCompilationDatabase CDB;
ClangdServer Server(CDB, FS, ClangdServer::optsForTest());
auto FooH = testPath("foo.h");
FS.Files[FooH] = R"cpp(
int foo(int x);
#define HASH(x) ((x) % 10)
)cpp";
auto FooCpp = testPath("Foo.cpp");
const char *SourceContents = R"cpp(
#include "foo.h"
[[void bar(int& inp) [[{
// inp = HASH(foo(inp));
[[inp = [[HASH([[foo([[in^p]])]])]]]];
}]]]]
$empty[[^]]
)cpp";
Annotations SourceAnnotations(SourceContents);
FS.Files[FooCpp] = std::string(SourceAnnotations.code());
Server.addDocument(FooCpp, SourceAnnotations.code());
auto Ranges = runSemanticRanges(Server, FooCpp, SourceAnnotations.points());
ASSERT_TRUE(bool(Ranges))
<< "getSemanticRange returned an error: " << Ranges.takeError();
ASSERT_EQ(Ranges->size(), SourceAnnotations.points().size());
EXPECT_THAT(gatherRanges(Ranges->front()),
ElementsAreArray(SourceAnnotations.ranges()));
EXPECT_THAT(gatherRanges(Ranges->back()),
ElementsAre(SourceAnnotations.range("empty")));
}
TEST(FoldingRanges, ASTAll) {
const char *Tests[] = {
R"cpp(
#define FOO int foo() {\
int Variable = 42; \
}
// Do not generate folding range for braces within macro expansion.
FOO
// Do not generate folding range within macro arguments.
#define FUNCTOR(functor) functor
void func() {[[
FUNCTOR([](){});
]]}
// Do not generate folding range with a brace coming from macro.
#define LBRACE {
void bar() LBRACE
int X = 42;
}
)cpp",
R"cpp(
void func() {[[
int Variable = 100;
if (Variable > 5) {[[
Variable += 42;
]]} else if (Variable++)
++Variable;
else {[[
Variable--;
]]}
// Do not generate FoldingRange for empty CompoundStmts.
for (;;) {}
// If there are newlines between {}, we should generate one.
for (;;) {[[
]]}
]]}
)cpp",
R"cpp(
class Foo {
public:
Foo() {[[
int X = 1;
]]}
private:
int getBar() {[[
return 42;
]]}
// Braces are located at the same line: no folding range here.
void getFooBar() { }
};
)cpp",
};
for (const char *Test : Tests) {
auto T = Annotations(Test);
auto AST = TestTU::withCode(T.code()).build();
EXPECT_THAT(gatherFoldingRanges(llvm::cantFail(getFoldingRanges(AST))),
UnorderedElementsAreArray(T.ranges()))
<< Test;
}
}
TEST(FoldingRanges, PseudoParserWithoutLineFoldings) {
const char *Tests[] = {
R"cpp(
#define FOO int foo() {\
int Variable = 42; \
}
// Do not generate folding range for braces within macro expansion.
FOO
// Do not generate folding range within macro arguments.
#define FUNCTOR(functor) functor
void func() {[[
FUNCTOR([](){});
]]}
// Do not generate folding range with a brace coming from macro.
#define LBRACE {
void bar() LBRACE
int X = 42;
}
)cpp",
R"cpp(
void func() {[[
int Variable = 100;
if (Variable > 5) {[[
Variable += 42;
]]} else if (Variable++)
++Variable;
else {[[
Variable--;
]]}
// Do not generate FoldingRange for empty CompoundStmts.
for (;;) {}
// If there are newlines between {}, we should generate one.
for (;;) {[[
]]}
]]}
)cpp",
R"cpp(
class Foo {[[
public:
Foo() {[[
int X = 1;
]]}
private:
int getBar() {[[
return 42;
]]}
// Braces are located at the same line: no folding range here.
void getFooBar() { }
]]};
)cpp",
R"cpp(
// Range boundaries on escaped newlines.
class Foo \
\
{[[ \
public:
Foo() {[[\
int X = 1;
]]} \
]]};
)cpp",
R"cpp(
/*[[ Multi
* line
* comment
]]*/
)cpp",
R"cpp(
//[[ Comment
// 1]]
//[[ Comment
// 2]]
// No folding for single line comment.
/*[[ comment 3
]]*/
/*[[ comment 4
]]*/
/*[[ foo */
/* bar ]]*/
/*[[ foo */
// baz
/* bar ]]*/
/*[[ foo */
/* bar*/
// baz]]
//[[ foo
/* bar */]]
)cpp",
};
for (const char *Test : Tests) {
auto T = Annotations(Test);
EXPECT_THAT(gatherFoldingRanges(llvm::cantFail(getFoldingRanges(
T.code().str(), /*LineFoldingsOnly=*/false))),
UnorderedElementsAreArray(T.ranges()))
<< Test;
}
}
TEST(FoldingRanges, PseudoParserLineFoldingsOnly) {
const char *Tests[] = {
R"cpp(
void func(int a) {[[
a++;]]
}
)cpp",
R"cpp(
// Always exclude last line for brackets.
void func(int a) {[[
if(a == 1) {[[
a++;]]
} else if (a == 2){[[
a--;]]
} else { // No folding for 2 line bracketed ranges.
}]]
}
)cpp",
R"cpp(
/*[[ comment
* comment]]
*/
/* No folding for this comment.
*/
// No folding for this comment.
//[[ 2 single line comment.
// 2 single line comment.]]
//[[ >=2 line comments.
// >=2 line comments.
// >=2 line comments.]]
//[[ foo\
bar\
baz]]
/*[[ foo */
/* bar */]]
/* baz */
/*[[ foo */
/* bar]]
* This does not fold me */
//[[ foo
/* bar */]]
)cpp",
// FIXME: Support folding template arguments.
// R"cpp(
// template <[[typename foo, class bar]]> struct baz {};
// )cpp",
};
auto StripColumns = [](const std::vector<Range> &Ranges) {
std::vector<Range> Res;
for (Range R : Ranges) {
R.start.character = R.end.character = 0;
Res.push_back(R);
}
return Res;
};
for (const char *Test : Tests) {
auto T = Annotations(Test);
EXPECT_THAT(
StripColumns(gatherFoldingRanges(llvm::cantFail(
getFoldingRanges(T.code().str(), /*LineFoldingsOnly=*/true)))),
UnorderedElementsAreArray(StripColumns(T.ranges())))
<< Test;
}
}
} // namespace
} // namespace clangd
} // namespace clang
|