1 //===-- SourceCodeTests.cpp ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 #include "Annotations.h" 9 #include "Context.h" 10 #include "Protocol.h" 11 #include "SourceCode.h" 12 #include "TestTU.h" 13 #include "clang/Basic/LangOptions.h" 14 #include "clang/Basic/SourceLocation.h" 15 #include "clang/Format/Format.h" 16 #include "llvm/Support/Error.h" 17 #include "llvm/Support/raw_os_ostream.h" 18 #include "llvm/Testing/Support/Annotations.h" 19 #include "llvm/Testing/Support/Error.h" 20 #include "gmock/gmock.h" 21 #include "gtest/gtest.h" 22 23 namespace clang { 24 namespace clangd { 25 namespace { 26 27 using llvm::Failed; 28 using llvm::HasValue; 29 using ::testing::UnorderedElementsAreArray; 30 31 MATCHER_P2(Pos, Line, Col, "") { 32 return arg.line == int(Line) && arg.character == int(Col); 33 } 34 35 MATCHER_P(MacroName, Name, "") { return arg.Name == Name; } 36 37 /// A helper to make tests easier to read. 38 Position position(int line, int character) { 39 Position Pos; 40 Pos.line = line; 41 Pos.character = character; 42 return Pos; 43 } 44 45 Range range(const std::pair<int, int> p1, const std::pair<int, int> p2) { 46 Range range; 47 range.start = position(p1.first, p1.second); 48 range.end = position(p2.first, p2.second); 49 return range; 50 } 51 52 TEST(SourceCodeTests, lspLength) { 53 EXPECT_EQ(lspLength(""), 0UL); 54 EXPECT_EQ(lspLength("ascii"), 5UL); 55 // BMP 56 EXPECT_EQ(lspLength("↓"), 1UL); 57 EXPECT_EQ(lspLength("¥"), 1UL); 58 // astral 59 EXPECT_EQ(lspLength(""), 2UL); 60 61 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 62 EXPECT_EQ(lspLength(""), 0UL); 63 EXPECT_EQ(lspLength("ascii"), 5UL); 64 // BMP 65 EXPECT_EQ(lspLength("↓"), 3UL); 66 EXPECT_EQ(lspLength("¥"), 2UL); 67 // astral 68 EXPECT_EQ(lspLength(""), 4UL); 69 70 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 71 EXPECT_EQ(lspLength(""), 0UL); 72 EXPECT_EQ(lspLength("ascii"), 5UL); 73 // BMP 74 EXPECT_EQ(lspLength("↓"), 1UL); 75 EXPECT_EQ(lspLength("¥"), 1UL); 76 // astral 77 EXPECT_EQ(lspLength(""), 1UL); 78 } 79 80 // The = → below are ASCII (1 byte), BMP (3 bytes), and astral (4 bytes). 81 const char File[] = R"(0:0 = 0 82 1:0 → 8 83 2:0 18)"; 84 struct Line { 85 unsigned Number; 86 unsigned Offset; 87 unsigned Length; 88 }; 89 Line FileLines[] = {Line{0, 0, 7}, Line{1, 8, 9}, Line{2, 18, 11}}; 90 91 TEST(SourceCodeTests, PositionToOffset) { 92 // line out of bounds 93 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 94 // first line 95 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)), 96 llvm::Failed()); // out of range 97 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)), 98 llvm::HasValue(0)); // first character 99 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)), 100 llvm::HasValue(3)); // middle character 101 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)), 102 llvm::HasValue(6)); // last character 103 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)), 104 llvm::HasValue(7)); // the newline itself 105 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false), 106 llvm::HasValue(7)); 107 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)), 108 llvm::HasValue(7)); // out of range 109 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false), 110 llvm::Failed()); // out of range 111 // middle line 112 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)), 113 llvm::Failed()); // out of range 114 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)), 115 llvm::HasValue(8)); // first character 116 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)), 117 llvm::HasValue(11)); // middle character 118 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false), 119 llvm::HasValue(11)); 120 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)), 121 llvm::HasValue(16)); // last character 122 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)), 123 llvm::HasValue(17)); // the newline itself 124 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)), 125 llvm::HasValue(17)); // out of range 126 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false), 127 llvm::Failed()); // out of range 128 // last line 129 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)), 130 llvm::Failed()); // out of range 131 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)), 132 llvm::HasValue(18)); // first character 133 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 3)), 134 llvm::HasValue(21)); // middle character 135 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false), 136 llvm::Failed()); // middle of surrogate pair 137 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5)), 138 llvm::HasValue(26)); // middle of surrogate pair 139 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 6), false), 140 llvm::HasValue(26)); // end of surrogate pair 141 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)), 142 llvm::HasValue(28)); // last character 143 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9)), 144 llvm::HasValue(29)); // EOF 145 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 10), false), 146 llvm::Failed()); // out of range 147 // line out of bounds 148 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 149 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed()); 150 151 // Codepoints are similar, except near astral characters. 152 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 153 // line out of bounds 154 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 155 // first line 156 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)), 157 llvm::Failed()); // out of range 158 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)), 159 llvm::HasValue(0)); // first character 160 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)), 161 llvm::HasValue(3)); // middle character 162 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)), 163 llvm::HasValue(6)); // last character 164 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)), 165 llvm::HasValue(7)); // the newline itself 166 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false), 167 llvm::HasValue(7)); 168 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)), 169 llvm::HasValue(7)); // out of range 170 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false), 171 llvm::Failed()); // out of range 172 // middle line 173 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)), 174 llvm::Failed()); // out of range 175 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)), 176 llvm::HasValue(8)); // first character 177 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)), 178 llvm::HasValue(11)); // middle character 179 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false), 180 llvm::HasValue(11)); 181 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)), 182 llvm::HasValue(16)); // last character 183 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)), 184 llvm::HasValue(17)); // the newline itself 185 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)), 186 llvm::HasValue(17)); // out of range 187 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false), 188 llvm::Failed()); // out of range 189 // last line 190 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)), 191 llvm::Failed()); // out of range 192 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)), 193 llvm::HasValue(18)); // first character 194 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 4)), 195 llvm::HasValue(22)); // Before astral character. 196 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false), 197 llvm::HasValue(26)); // after astral character 198 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 7)), 199 llvm::HasValue(28)); // last character 200 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)), 201 llvm::HasValue(29)); // EOF 202 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9), false), 203 llvm::Failed()); // out of range 204 // line out of bounds 205 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 206 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed()); 207 208 // Test UTF-8, where transformations are trivial. 209 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 210 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 211 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 212 for (Line L : FileLines) { 213 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, -1)), 214 llvm::Failed()); // out of range 215 for (unsigned I = 0; I <= L.Length; ++I) 216 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, I)), 217 llvm::HasValue(L.Offset + I)); 218 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, L.Length+1)), 219 llvm::HasValue(L.Offset + L.Length)); 220 EXPECT_THAT_EXPECTED( 221 positionToOffset(File, position(L.Number, L.Length + 1), false), 222 llvm::Failed()); // out of range 223 } 224 } 225 226 TEST(SourceCodeTests, OffsetToPosition) { 227 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file"; 228 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line"; 229 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line"; 230 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline"; 231 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line"; 232 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char"; 233 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char"; 234 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char"; 235 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line"; 236 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline"; 237 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line"; 238 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line"; 239 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char"; 240 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 6)) << "in astral char"; 241 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 6)) << "after astral char"; 242 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 8)) << "end of last line"; 243 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 9)) << "EOF"; 244 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 9)) << "out of bounds"; 245 246 // Codepoints are similar, except near astral characters. 247 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 248 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file"; 249 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line"; 250 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line"; 251 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline"; 252 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line"; 253 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char"; 254 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char"; 255 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char"; 256 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line"; 257 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline"; 258 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line"; 259 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line"; 260 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char"; 261 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 5)) << "in astral char"; 262 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 5)) << "after astral char"; 263 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 7)) << "end of last line"; 264 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 8)) << "EOF"; 265 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 8)) << "out of bounds"; 266 267 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 268 for (Line L : FileLines) { 269 for (unsigned I = 0; I <= L.Length; ++I) 270 EXPECT_THAT(offsetToPosition(File, L.Offset + I), Pos(L.Number, I)); 271 } 272 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 11)) << "out of bounds"; 273 } 274 275 TEST(SourceCodeTests, IsRangeConsecutive) { 276 EXPECT_TRUE(isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 3}, {2, 4}))); 277 EXPECT_FALSE( 278 isRangeConsecutive(range({0, 2}, {0, 3}), range({2, 3}, {2, 4}))); 279 EXPECT_FALSE( 280 isRangeConsecutive(range({2, 2}, {2, 3}), range({2, 4}, {2, 5}))); 281 } 282 283 TEST(SourceCodeTests, SourceLocationInMainFile) { 284 Annotations Source(R"cpp( 285 ^in^t ^foo 286 ^bar 287 ^baz ^() {} {} {} {} { }^ 288 )cpp"); 289 290 SourceManagerForFile Owner("foo.cpp", Source.code()); 291 SourceManager &SM = Owner.get(); 292 293 SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID()); 294 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)), 295 HasValue(StartOfFile)); 296 // End of file. 297 EXPECT_THAT_EXPECTED( 298 sourceLocationInMainFile(SM, position(4, 0)), 299 HasValue(StartOfFile.getLocWithOffset(Source.code().size()))); 300 // Column number is too large. 301 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed()); 302 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)), 303 Failed()); 304 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed()); 305 // Line number is too large. 306 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed()); 307 // Check all positions mentioned in the test return valid results. 308 for (auto P : Source.points()) { 309 size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P)); 310 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P), 311 HasValue(StartOfFile.getLocWithOffset(Offset))); 312 } 313 } 314 315 TEST(SourceCodeTests, GetBeginningOfIdentifier) { 316 std::string Preamble = R"cpp( 317 struct Bar { int func(); }; 318 #define MACRO(X) void f() { X; } 319 Bar* bar; 320 )cpp"; 321 // First ^ is the expected beginning, last is the search position. 322 for (std::string Text : std::vector<std::string>{ 323 "int ^f^oo();", // inside identifier 324 "int ^foo();", // beginning of identifier 325 "int ^foo^();", // end of identifier 326 "int foo(^);", // non-identifier 327 "^int foo();", // beginning of file (can't back up) 328 "int ^f0^0();", // after a digit (lexing at N-1 is wrong) 329 "int ^λλ^λ();", // UTF-8 handled properly when backing up 330 331 // identifier in macro arg 332 "MACRO(bar->^func())", // beginning of identifier 333 "MACRO(bar->^fun^c())", // inside identifier 334 "MACRO(bar->^func^())", // end of identifier 335 "MACRO(^bar->func())", // begin identifier 336 "MACRO(^bar^->func())", // end identifier 337 "^MACRO(bar->func())", // beginning of macro name 338 "^MAC^RO(bar->func())", // inside macro name 339 "^MACRO^(bar->func())", // end of macro name 340 }) { 341 std::string WithPreamble = Preamble + Text; 342 Annotations TestCase(WithPreamble); 343 auto AST = TestTU::withCode(TestCase.code()).build(); 344 const auto &SourceMgr = AST.getSourceManager(); 345 SourceLocation Actual = getBeginningOfIdentifier( 346 TestCase.points().back(), SourceMgr, AST.getASTContext().getLangOpts()); 347 Position ActualPos = offsetToPosition( 348 TestCase.code(), 349 SourceMgr.getFileOffset(SourceMgr.getSpellingLoc(Actual))); 350 EXPECT_EQ(TestCase.points().front(), ActualPos) << Text; 351 } 352 } 353 354 TEST(SourceCodeTests, CollectIdentifiers) { 355 auto Style = format::getLLVMStyle(); 356 auto IDs = collectIdentifiers(R"cpp( 357 #include "a.h" 358 void foo() { int xyz; int abc = xyz; return foo(); } 359 )cpp", 360 Style); 361 EXPECT_EQ(IDs.size(), 7u); 362 EXPECT_EQ(IDs["include"], 1u); 363 EXPECT_EQ(IDs["void"], 1u); 364 EXPECT_EQ(IDs["int"], 2u); 365 EXPECT_EQ(IDs["xyz"], 2u); 366 EXPECT_EQ(IDs["abc"], 1u); 367 EXPECT_EQ(IDs["return"], 1u); 368 EXPECT_EQ(IDs["foo"], 2u); 369 } 370 371 TEST(SourceCodeTests, CollectWords) { 372 auto Words = collectWords(R"cpp( 373 #define FIZZ_BUZZ 374 // this is a comment 375 std::string getSomeText() { return "magic word"; } 376 )cpp"); 377 std::set<std::string> ActualWords(Words.keys().begin(), Words.keys().end()); 378 std::set<std::string> ExpectedWords = {"define", "fizz", "buzz", "this", 379 "comment", "string", "some", "text", 380 "return", "magic", "word"}; 381 EXPECT_EQ(ActualWords, ExpectedWords); 382 } 383 384 TEST(SourceCodeTests, VisibleNamespaces) { 385 std::vector<std::pair<const char *, std::vector<std::string>>> Cases = { 386 { 387 R"cpp( 388 // Using directive resolved against enclosing namespaces. 389 using namespace foo; 390 namespace ns { 391 using namespace bar; 392 )cpp", 393 {"ns", "", "bar", "foo", "ns::bar"}, 394 }, 395 { 396 R"cpp( 397 // Don't include namespaces we've closed, ignore namespace aliases. 398 using namespace clang; 399 using std::swap; 400 namespace clang { 401 namespace clangd {} 402 namespace ll = ::llvm; 403 } 404 namespace clang { 405 )cpp", 406 {"clang", ""}, 407 }, 408 { 409 R"cpp( 410 // Using directives visible even if a namespace is reopened. 411 // Ignore anonymous namespaces. 412 namespace foo{ using namespace bar; } 413 namespace foo{ namespace { 414 )cpp", 415 {"foo", "", "bar", "foo::bar"}, 416 }, 417 { 418 R"cpp( 419 // Mismatched braces 420 namespace foo{} 421 }}} 422 namespace bar{ 423 )cpp", 424 {"bar", ""}, 425 }, 426 { 427 R"cpp( 428 // Namespaces with multiple chunks. 429 namespace a::b { 430 using namespace c::d; 431 namespace e::f { 432 )cpp", 433 { 434 "a::b::e::f", 435 "", 436 "a", 437 "a::b", 438 "a::b::c::d", 439 "a::b::e", 440 "a::c::d", 441 "c::d", 442 }, 443 }, 444 }; 445 for (const auto& Case : Cases) { 446 EXPECT_EQ(Case.second, 447 visibleNamespaces(Case.first, format::getLLVMStyle())) 448 << Case.first; 449 } 450 } 451 452 TEST(SourceCodeTests, GetMacros) { 453 Annotations Code(R"cpp( 454 #define MACRO 123 455 int abc = MA^CRO; 456 )cpp"); 457 TestTU TU = TestTU::withCode(Code.code()); 458 auto AST = TU.build(); 459 auto Loc = getBeginningOfIdentifier(Code.point(), AST.getSourceManager(), 460 AST.getASTContext().getLangOpts()); 461 auto Result = locateMacroAt(Loc, AST.getPreprocessor()); 462 ASSERT_TRUE(Result); 463 EXPECT_THAT(*Result, MacroName("MACRO")); 464 } 465 466 TEST(SourceCodeTests, IsInsideMainFile){ 467 TestTU TU; 468 TU.HeaderCode = R"cpp( 469 #define DEFINE_CLASS(X) class X {}; 470 #define DEFINE_YY DEFINE_CLASS(YY) 471 472 class Header1 {}; 473 DEFINE_CLASS(Header2) 474 class Header {}; 475 )cpp"; 476 TU.Code = R"cpp( 477 class Main1 {}; 478 DEFINE_CLASS(Main2) 479 DEFINE_YY 480 class Main {}; 481 )cpp"; 482 TU.ExtraArgs.push_back("-DHeader=Header3"); 483 TU.ExtraArgs.push_back("-DMain=Main3"); 484 auto AST = TU.build(); 485 const auto& SM = AST.getSourceManager(); 486 auto DeclLoc = [&AST](llvm::StringRef Name) { 487 return findDecl(AST, Name).getLocation(); 488 }; 489 for (const auto *HeaderDecl : {"Header1", "Header2", "Header3"}) 490 EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM)); 491 492 for (const auto *MainDecl : {"Main1", "Main2", "Main3", "YY"}) 493 EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM)); 494 } 495 496 // Test for functions toHalfOpenFileRange and getHalfOpenFileRange 497 TEST(SourceCodeTests, HalfOpenFileRange) { 498 // Each marked range should be the file range of the decl with the same name 499 // and each name should be unique. 500 Annotations Test(R"cpp( 501 #define FOO(X, Y) int Y = ++X 502 #define BAR(X) X + 1 503 #define ECHO(X) X 504 505 #define BUZZ BAZZ(ADD) 506 #define BAZZ(m) m(1) 507 #define ADD(a) int f = a + 1; 508 template<typename T> 509 class P {}; 510 511 int main() { 512 $a[[P<P<P<P<P<int>>>>> a]]; 513 $b[[int b = 1]]; 514 $c[[FOO(b, c)]]; 515 $d[[FOO(BAR(BAR(b)), d)]]; 516 // FIXME: We might want to select everything inside the outer ECHO. 517 ECHO(ECHO($e[[int) ECHO(e]])); 518 // Shouldn't crash. 519 $f[[BUZZ]]; 520 } 521 )cpp"); 522 523 ParsedAST AST = TestTU::withCode(Test.code()).build(); 524 llvm::errs() << Test.code(); 525 const SourceManager &SM = AST.getSourceManager(); 526 const LangOptions &LangOpts = AST.getASTContext().getLangOpts(); 527 // Turn a SourceLocation into a pair of positions 528 auto SourceRangeToRange = [&SM](SourceRange SrcRange) { 529 return Range{sourceLocToPosition(SM, SrcRange.getBegin()), 530 sourceLocToPosition(SM, SrcRange.getEnd())}; 531 }; 532 auto CheckRange = [&](llvm::StringRef Name) { 533 const NamedDecl &Decl = findUnqualifiedDecl(AST, Name); 534 auto FileRange = toHalfOpenFileRange(SM, LangOpts, Decl.getSourceRange()); 535 SCOPED_TRACE("Checking range: " + Name); 536 ASSERT_NE(FileRange, llvm::None); 537 Range HalfOpenRange = SourceRangeToRange(*FileRange); 538 EXPECT_EQ(HalfOpenRange, Test.ranges(Name)[0]); 539 }; 540 541 CheckRange("a"); 542 CheckRange("b"); 543 CheckRange("c"); 544 CheckRange("d"); 545 CheckRange("e"); 546 CheckRange("f"); 547 } 548 549 TEST(SourceCodeTests, HalfOpenFileRangePathologicalPreprocessor) { 550 const char *Case = R"cpp( 551 #define MACRO while(1) 552 void test() { 553 [[#include "Expand.inc" 554 br^eak]]; 555 } 556 )cpp"; 557 Annotations Test(Case); 558 auto TU = TestTU::withCode(Test.code()); 559 TU.AdditionalFiles["Expand.inc"] = "MACRO\n"; 560 auto AST = TU.build(); 561 562 const auto &Func = cast<FunctionDecl>(findDecl(AST, "test")); 563 const auto &Body = cast<CompoundStmt>(Func.getBody()); 564 const auto &Loop = cast<WhileStmt>(*Body->child_begin()); 565 llvm::Optional<SourceRange> Range = toHalfOpenFileRange( 566 AST.getSourceManager(), AST.getASTContext().getLangOpts(), 567 Loop->getSourceRange()); 568 ASSERT_TRUE(Range) << "Failed to get file range"; 569 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getBegin()), 570 Test.llvm::Annotations::range().Begin); 571 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getEnd()), 572 Test.llvm::Annotations::range().End); 573 } 574 575 TEST(SourceCodeTests, IncludeHashLoc) { 576 const char *Case = R"cpp( 577 $foo^#include "foo.inc" 578 #define HEADER "bar.inc" 579 $bar^# include HEADER 580 )cpp"; 581 Annotations Test(Case); 582 auto TU = TestTU::withCode(Test.code()); 583 TU.AdditionalFiles["foo.inc"] = "int foo;\n"; 584 TU.AdditionalFiles["bar.inc"] = "int bar;\n"; 585 auto AST = TU.build(); 586 const auto& SM = AST.getSourceManager(); 587 588 FileID Foo = SM.getFileID(findDecl(AST, "foo").getLocation()); 589 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Foo, SM)), 590 Test.llvm::Annotations::point("foo")); 591 FileID Bar = SM.getFileID(findDecl(AST, "bar").getLocation()); 592 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Bar, SM)), 593 Test.llvm::Annotations::point("bar")); 594 } 595 596 } // namespace 597 } // namespace clangd 598 } // namespace clang 599