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 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
|
//===-- SourceCodeTests.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 "Context.h"
#include "Protocol.h"
#include "SourceCode.h"
#include "TestTU.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Format/Format.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/raw_os_ostream.h"
#include "llvm/Testing/Support/Error.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace clang {
namespace clangd {
namespace {
using llvm::Failed;
using llvm::HasValue;
using ::testing::UnorderedElementsAreArray;
MATCHER_P2(Pos, Line, Col, "") {
return arg.line == int(Line) && arg.character == int(Col);
}
MATCHER_P(MacroName, Name, "") { return arg.Name == Name; }
/// A helper to make tests easier to read.
Position position(int line, int character) {
Position Pos;
Pos.line = line;
Pos.character = character;
return Pos;
}
Range range(const std::pair<int, int> p1, const std::pair<int, int> p2) {
Range range;
range.start = position(p1.first, p1.second);
range.end = position(p2.first, p2.second);
return range;
}
TEST(SourceCodeTests, lspLength) {
EXPECT_EQ(lspLength(""), 0UL);
EXPECT_EQ(lspLength("ascii"), 5UL);
// BMP
EXPECT_EQ(lspLength("↓"), 1UL);
EXPECT_EQ(lspLength("¥"), 1UL);
// astral
EXPECT_EQ(lspLength("😂"), 2UL);
WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
EXPECT_EQ(lspLength(""), 0UL);
EXPECT_EQ(lspLength("ascii"), 5UL);
// BMP
EXPECT_EQ(lspLength("↓"), 3UL);
EXPECT_EQ(lspLength("¥"), 2UL);
// astral
EXPECT_EQ(lspLength("😂"), 4UL);
WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
EXPECT_EQ(lspLength(""), 0UL);
EXPECT_EQ(lspLength("ascii"), 5UL);
// BMP
EXPECT_EQ(lspLength("↓"), 1UL);
EXPECT_EQ(lspLength("¥"), 1UL);
// astral
EXPECT_EQ(lspLength("😂"), 1UL);
}
// The = → 🡆 below are ASCII (1 byte), BMP (3 bytes), and astral (4 bytes).
const char File[] = R"(0:0 = 0
1:0 → 8
2:0 🡆 18)";
struct Line {
unsigned Number;
unsigned Offset;
unsigned Length;
};
Line FileLines[] = {Line{0, 0, 7}, Line{1, 8, 9}, Line{2, 18, 11}};
TEST(SourceCodeTests, PositionToOffset) {
// line out of bounds
EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
// first line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)),
llvm::HasValue(0)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)),
llvm::HasValue(3)); // middle character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)),
llvm::HasValue(6)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)),
llvm::HasValue(7)); // the newline itself
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false),
llvm::HasValue(7));
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)),
llvm::HasValue(7)); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false),
llvm::Failed()); // out of range
// middle line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)),
llvm::HasValue(8)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)),
llvm::HasValue(11)); // middle character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false),
llvm::HasValue(11));
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)),
llvm::HasValue(16)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)),
llvm::HasValue(17)); // the newline itself
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)),
llvm::HasValue(17)); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false),
llvm::Failed()); // out of range
// last line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)),
llvm::HasValue(18)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 3)),
llvm::HasValue(21)); // middle character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false),
llvm::Failed()); // middle of surrogate pair
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5)),
llvm::HasValue(26)); // middle of surrogate pair
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 6), false),
llvm::HasValue(26)); // end of surrogate pair
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)),
llvm::HasValue(28)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9)),
llvm::HasValue(29)); // EOF
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 10), false),
llvm::Failed()); // out of range
// line out of bounds
EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed());
// Codepoints are similar, except near astral characters.
WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
// line out of bounds
EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
// first line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)),
llvm::HasValue(0)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)),
llvm::HasValue(3)); // middle character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)),
llvm::HasValue(6)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)),
llvm::HasValue(7)); // the newline itself
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false),
llvm::HasValue(7));
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)),
llvm::HasValue(7)); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false),
llvm::Failed()); // out of range
// middle line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)),
llvm::HasValue(8)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)),
llvm::HasValue(11)); // middle character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false),
llvm::HasValue(11));
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)),
llvm::HasValue(16)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)),
llvm::HasValue(17)); // the newline itself
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)),
llvm::HasValue(17)); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false),
llvm::Failed()); // out of range
// last line
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)),
llvm::Failed()); // out of range
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)),
llvm::HasValue(18)); // first character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 4)),
llvm::HasValue(22)); // Before astral character.
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false),
llvm::HasValue(26)); // after astral character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 7)),
llvm::HasValue(28)); // last character
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)),
llvm::HasValue(29)); // EOF
EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9), false),
llvm::Failed()); // out of range
// line out of bounds
EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed());
// Test UTF-8, where transformations are trivial.
WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed());
EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed());
for (Line L : FileLines) {
EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, -1)),
llvm::Failed()); // out of range
for (unsigned I = 0; I <= L.Length; ++I)
EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, I)),
llvm::HasValue(L.Offset + I));
EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, L.Length+1)),
llvm::HasValue(L.Offset + L.Length));
EXPECT_THAT_EXPECTED(
positionToOffset(File, position(L.Number, L.Length + 1), false),
llvm::Failed()); // out of range
}
}
TEST(SourceCodeTests, OffsetToPosition) {
EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file";
EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line";
EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line";
EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline";
EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line";
EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char";
EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char";
EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char";
EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line";
EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline";
EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line";
EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line";
EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char";
EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 6)) << "in astral char";
EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 6)) << "after astral char";
EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 8)) << "end of last line";
EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 9)) << "EOF";
EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 9)) << "out of bounds";
// Codepoints are similar, except near astral characters.
WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32);
EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file";
EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line";
EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line";
EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline";
EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line";
EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char";
EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char";
EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char";
EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line";
EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline";
EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line";
EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line";
EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char";
EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 5)) << "in astral char";
EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 5)) << "after astral char";
EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 7)) << "end of last line";
EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 8)) << "EOF";
EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 8)) << "out of bounds";
WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8);
for (Line L : FileLines) {
for (unsigned I = 0; I <= L.Length; ++I)
EXPECT_THAT(offsetToPosition(File, L.Offset + I), Pos(L.Number, I));
}
EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 11)) << "out of bounds";
}
TEST(SourceCodeTests, IsRangeConsecutive) {
EXPECT_TRUE(isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 3}, {2, 4})));
EXPECT_FALSE(
isRangeConsecutive(range({0, 2}, {0, 3}), range({2, 3}, {2, 4})));
EXPECT_FALSE(
isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 4}, {2, 5})));
}
TEST(SourceCodeTests, SourceLocationInMainFile) {
Annotations Source(R"cpp(
^in^t ^foo
^bar
^baz ^() {} {} {} {} { }^
)cpp");
SourceManagerForFile Owner("foo.cpp", Source.code());
SourceManager &SM = Owner.get();
SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID());
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)),
HasValue(StartOfFile));
// End of file.
EXPECT_THAT_EXPECTED(
sourceLocationInMainFile(SM, position(4, 0)),
HasValue(StartOfFile.getLocWithOffset(Source.code().size())));
// Column number is too large.
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed());
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)),
Failed());
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed());
// Line number is too large.
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed());
// Check all positions mentioned in the test return valid results.
for (auto P : Source.points()) {
size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P));
EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P),
HasValue(StartOfFile.getLocWithOffset(Offset)));
}
}
TEST(SourceCodeTests, CollectIdentifiers) {
auto Style = format::getLLVMStyle();
auto IDs = collectIdentifiers(R"cpp(
#include "a.h"
void foo() { int xyz; int abc = xyz; return foo(); }
)cpp",
Style);
EXPECT_EQ(IDs.size(), 7u);
EXPECT_EQ(IDs["include"], 1u);
EXPECT_EQ(IDs["void"], 1u);
EXPECT_EQ(IDs["int"], 2u);
EXPECT_EQ(IDs["xyz"], 2u);
EXPECT_EQ(IDs["abc"], 1u);
EXPECT_EQ(IDs["return"], 1u);
EXPECT_EQ(IDs["foo"], 2u);
}
TEST(SourceCodeTests, CollectWords) {
auto Words = collectWords(R"cpp(
#define FIZZ_BUZZ
// this is a comment
std::string getSomeText() { return "magic word"; }
)cpp");
std::set<std::string> ActualWords(Words.keys().begin(), Words.keys().end());
std::set<std::string> ExpectedWords = {"define", "fizz", "buzz", "this",
"comment", "string", "some", "text",
"return", "magic", "word"};
EXPECT_EQ(ActualWords, ExpectedWords);
}
TEST(SourceCodeTests, VisibleNamespaces) {
std::vector<std::pair<const char *, std::vector<std::string>>> Cases = {
{
R"cpp(
// Using directive resolved against enclosing namespaces.
using namespace foo;
namespace ns {
using namespace bar;
)cpp",
{"ns", "", "bar", "foo", "ns::bar"},
},
{
R"cpp(
// Don't include namespaces we've closed, ignore namespace aliases.
using namespace clang;
using std::swap;
namespace clang {
namespace clangd {}
namespace ll = ::llvm;
}
namespace clang {
)cpp",
{"clang", ""},
},
{
R"cpp(
// Using directives visible even if a namespace is reopened.
// Ignore anonymous namespaces.
namespace foo{ using namespace bar; }
namespace foo{ namespace {
)cpp",
{"foo", "", "bar", "foo::bar"},
},
{
R"cpp(
// Mismatched braces
namespace foo{}
}}}
namespace bar{
)cpp",
{"bar", ""},
},
{
R"cpp(
// Namespaces with multiple chunks.
namespace a::b {
using namespace c::d;
namespace e::f {
)cpp",
{
"a::b::e::f",
"",
"a",
"a::b",
"a::b::c::d",
"a::b::e",
"a::c::d",
"c::d",
},
},
};
for (const auto& Case : Cases) {
EXPECT_EQ(Case.second,
visibleNamespaces(Case.first, format::getLLVMStyle()))
<< Case.first;
}
}
TEST(SourceCodeTests, GetMacros) {
Annotations Code(R"cpp(
#define MACRO 123
int abc = MA^CRO;
)cpp");
TestTU TU = TestTU::withCode(Code.code());
auto AST = TU.build();
auto Loc = getBeginningOfIdentifier(AST, Code.point(),
AST.getSourceManager().getMainFileID());
auto Result = locateMacroAt(Loc, AST.getPreprocessor());
ASSERT_TRUE(Result);
EXPECT_THAT(*Result, MacroName("MACRO"));
}
TEST(SourceCodeTests, IsInsideMainFile){
TestTU TU;
TU.HeaderCode = R"cpp(
#define DEFINE_CLASS(X) class X {};
#define DEFINE_YY DEFINE_CLASS(YY)
class Header1 {};
DEFINE_CLASS(Header2)
class Header {};
)cpp";
TU.Code = R"cpp(
class Main1 {};
DEFINE_CLASS(Main2)
DEFINE_YY
class Main {};
)cpp";
TU.ExtraArgs.push_back("-DHeader=Header3");
TU.ExtraArgs.push_back("-DMain=Main3");
auto AST = TU.build();
const auto& SM = AST.getSourceManager();
auto DeclLoc = [&AST](llvm::StringRef Name) {
return findDecl(AST, Name).getLocation();
};
for (const auto *HeaderDecl : {"Header1", "Header2", "Header3"})
EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM));
for (const auto *MainDecl : {"Main1", "Main2", "Main3", "YY"})
EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM));
}
// Test for functions toHalfOpenFileRange and getHalfOpenFileRange
TEST(SourceCodeTests, HalfOpenFileRange) {
// Each marked range should be the file range of the decl with the same name
// and each name should be unique.
Annotations Test(R"cpp(
#define FOO(X, Y) int Y = ++X
#define BAR(X) X + 1
#define ECHO(X) X
#define BUZZ BAZZ(ADD)
#define BAZZ(m) m(1)
#define ADD(a) int f = a + 1;
template<typename T>
class P {};
int main() {
$a[[P<P<P<P<P<int>>>>> a]];
$b[[int b = 1]];
$c[[FOO(b, c)]];
$d[[FOO(BAR(BAR(b)), d)]];
// FIXME: We might want to select everything inside the outer ECHO.
ECHO(ECHO($e[[int) ECHO(e]]));
// Shouldn't crash.
$f[[BUZZ]];
}
)cpp");
ParsedAST AST = TestTU::withCode(Test.code()).build();
llvm::errs() << Test.code();
const SourceManager &SM = AST.getSourceManager();
const LangOptions &LangOpts = AST.getASTContext().getLangOpts();
// Turn a SourceLocation into a pair of positions
auto SourceRangeToRange = [&SM](SourceRange SrcRange) {
return Range{sourceLocToPosition(SM, SrcRange.getBegin()),
sourceLocToPosition(SM, SrcRange.getEnd())};
};
auto CheckRange = [&](llvm::StringRef Name) {
const NamedDecl &Decl = findUnqualifiedDecl(AST, Name);
auto FileRange = toHalfOpenFileRange(SM, LangOpts, Decl.getSourceRange());
SCOPED_TRACE("Checking range: " + Name);
ASSERT_NE(FileRange, llvm::None);
Range HalfOpenRange = SourceRangeToRange(*FileRange);
EXPECT_EQ(HalfOpenRange, Test.ranges(Name)[0]);
};
CheckRange("a");
CheckRange("b");
CheckRange("c");
CheckRange("d");
CheckRange("e");
CheckRange("f");
}
} // namespace
} // namespace clangd
} // namespace clang
|