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/Basic/TokenKinds.h" 16 #include "clang/Format/Format.h" 17 #include "llvm/Support/Error.h" 18 #include "llvm/Support/raw_os_ostream.h" 19 #include "llvm/Testing/Support/Annotations.h" 20 #include "llvm/Testing/Support/Error.h" 21 #include "gmock/gmock.h" 22 #include "gtest/gtest.h" 23 #include <tuple> 24 25 namespace clang { 26 namespace clangd { 27 namespace { 28 29 using llvm::Failed; 30 using llvm::HasValue; 31 32 MATCHER_P2(Pos, Line, Col, "") { 33 return arg.line == int(Line) && arg.character == int(Col); 34 } 35 36 MATCHER_P(MacroName, Name, "") { return arg.Name == Name; } 37 38 /// A helper to make tests easier to read. 39 Position position(int Line, int Character) { 40 Position Pos; 41 Pos.line = Line; 42 Pos.character = Character; 43 return Pos; 44 } 45 46 TEST(SourceCodeTests, lspLength) { 47 EXPECT_EQ(lspLength(""), 0UL); 48 EXPECT_EQ(lspLength("ascii"), 5UL); 49 // BMP 50 EXPECT_EQ(lspLength("↓"), 1UL); 51 EXPECT_EQ(lspLength("¥"), 1UL); 52 // astral 53 EXPECT_EQ(lspLength(""), 2UL); 54 55 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 56 EXPECT_EQ(lspLength(""), 0UL); 57 EXPECT_EQ(lspLength("ascii"), 5UL); 58 // BMP 59 EXPECT_EQ(lspLength("↓"), 3UL); 60 EXPECT_EQ(lspLength("¥"), 2UL); 61 // astral 62 EXPECT_EQ(lspLength(""), 4UL); 63 64 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 65 EXPECT_EQ(lspLength(""), 0UL); 66 EXPECT_EQ(lspLength("ascii"), 5UL); 67 // BMP 68 EXPECT_EQ(lspLength("↓"), 1UL); 69 EXPECT_EQ(lspLength("¥"), 1UL); 70 // astral 71 EXPECT_EQ(lspLength(""), 1UL); 72 } 73 74 // The = → below are ASCII (1 byte), BMP (3 bytes), and astral (4 bytes). 75 const char File[] = R"(0:0 = 0 76 1:0 → 8 77 2:0 18)"; 78 struct Line { 79 unsigned Number; 80 unsigned Offset; 81 unsigned Length; 82 }; 83 Line FileLines[] = {Line{0, 0, 7}, Line{1, 8, 9}, Line{2, 18, 11}}; 84 85 TEST(SourceCodeTests, PositionToOffset) { 86 // line out of bounds 87 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 88 // first line 89 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)), 90 llvm::Failed()); // out of range 91 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)), 92 llvm::HasValue(0)); // first character 93 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)), 94 llvm::HasValue(3)); // middle character 95 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)), 96 llvm::HasValue(6)); // last character 97 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)), 98 llvm::HasValue(7)); // the newline itself 99 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false), 100 llvm::HasValue(7)); 101 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)), 102 llvm::HasValue(7)); // out of range 103 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false), 104 llvm::Failed()); // out of range 105 // middle line 106 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)), 107 llvm::Failed()); // out of range 108 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)), 109 llvm::HasValue(8)); // first character 110 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)), 111 llvm::HasValue(11)); // middle character 112 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false), 113 llvm::HasValue(11)); 114 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)), 115 llvm::HasValue(16)); // last character 116 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)), 117 llvm::HasValue(17)); // the newline itself 118 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)), 119 llvm::HasValue(17)); // out of range 120 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false), 121 llvm::Failed()); // out of range 122 // last line 123 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)), 124 llvm::Failed()); // out of range 125 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)), 126 llvm::HasValue(18)); // first character 127 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 3)), 128 llvm::HasValue(21)); // middle character 129 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false), 130 llvm::Failed()); // middle of surrogate pair 131 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5)), 132 llvm::HasValue(26)); // middle of surrogate pair 133 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 6), false), 134 llvm::HasValue(26)); // end of surrogate pair 135 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)), 136 llvm::HasValue(28)); // last character 137 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9)), 138 llvm::HasValue(29)); // EOF 139 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 10), false), 140 llvm::Failed()); // out of range 141 // line out of bounds 142 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 143 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed()); 144 145 // Codepoints are similar, except near astral characters. 146 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 147 // line out of bounds 148 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 149 // first line 150 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, -1)), 151 llvm::Failed()); // out of range 152 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 0)), 153 llvm::HasValue(0)); // first character 154 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 3)), 155 llvm::HasValue(3)); // middle character 156 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 6)), 157 llvm::HasValue(6)); // last character 158 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7)), 159 llvm::HasValue(7)); // the newline itself 160 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 7), false), 161 llvm::HasValue(7)); 162 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8)), 163 llvm::HasValue(7)); // out of range 164 EXPECT_THAT_EXPECTED(positionToOffset(File, position(0, 8), false), 165 llvm::Failed()); // out of range 166 // middle line 167 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, -1)), 168 llvm::Failed()); // out of range 169 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 0)), 170 llvm::HasValue(8)); // first character 171 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3)), 172 llvm::HasValue(11)); // middle character 173 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 3), false), 174 llvm::HasValue(11)); 175 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 6)), 176 llvm::HasValue(16)); // last character 177 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 7)), 178 llvm::HasValue(17)); // the newline itself 179 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8)), 180 llvm::HasValue(17)); // out of range 181 EXPECT_THAT_EXPECTED(positionToOffset(File, position(1, 8), false), 182 llvm::Failed()); // out of range 183 // last line 184 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, -1)), 185 llvm::Failed()); // out of range 186 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 0)), 187 llvm::HasValue(18)); // first character 188 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 4)), 189 llvm::HasValue(22)); // Before astral character. 190 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 5), false), 191 llvm::HasValue(26)); // after astral character 192 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 7)), 193 llvm::HasValue(28)); // last character 194 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 8)), 195 llvm::HasValue(29)); // EOF 196 EXPECT_THAT_EXPECTED(positionToOffset(File, position(2, 9), false), 197 llvm::Failed()); // out of range 198 // line out of bounds 199 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 200 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 1)), llvm::Failed()); 201 202 // Test UTF-8, where transformations are trivial. 203 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 204 EXPECT_THAT_EXPECTED(positionToOffset(File, position(-1, 2)), llvm::Failed()); 205 EXPECT_THAT_EXPECTED(positionToOffset(File, position(3, 0)), llvm::Failed()); 206 for (Line L : FileLines) { 207 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, -1)), 208 llvm::Failed()); // out of range 209 for (unsigned I = 0; I <= L.Length; ++I) 210 EXPECT_THAT_EXPECTED(positionToOffset(File, position(L.Number, I)), 211 llvm::HasValue(L.Offset + I)); 212 EXPECT_THAT_EXPECTED( 213 positionToOffset(File, position(L.Number, L.Length + 1)), 214 llvm::HasValue(L.Offset + L.Length)); 215 EXPECT_THAT_EXPECTED( 216 positionToOffset(File, position(L.Number, L.Length + 1), false), 217 llvm::Failed()); // out of range 218 } 219 } 220 221 TEST(SourceCodeTests, OffsetToPosition) { 222 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file"; 223 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line"; 224 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line"; 225 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline"; 226 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line"; 227 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char"; 228 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char"; 229 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char"; 230 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line"; 231 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline"; 232 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line"; 233 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line"; 234 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char"; 235 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 6)) << "in astral char"; 236 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 6)) << "after astral char"; 237 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 8)) << "end of last line"; 238 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 9)) << "EOF"; 239 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 9)) << "out of bounds"; 240 241 // Codepoints are similar, except near astral characters. 242 WithContextValue UTF32(kCurrentOffsetEncoding, OffsetEncoding::UTF32); 243 EXPECT_THAT(offsetToPosition(File, 0), Pos(0, 0)) << "start of file"; 244 EXPECT_THAT(offsetToPosition(File, 3), Pos(0, 3)) << "in first line"; 245 EXPECT_THAT(offsetToPosition(File, 6), Pos(0, 6)) << "end of first line"; 246 EXPECT_THAT(offsetToPosition(File, 7), Pos(0, 7)) << "first newline"; 247 EXPECT_THAT(offsetToPosition(File, 8), Pos(1, 0)) << "start of second line"; 248 EXPECT_THAT(offsetToPosition(File, 12), Pos(1, 4)) << "before BMP char"; 249 EXPECT_THAT(offsetToPosition(File, 13), Pos(1, 5)) << "in BMP char"; 250 EXPECT_THAT(offsetToPosition(File, 15), Pos(1, 5)) << "after BMP char"; 251 EXPECT_THAT(offsetToPosition(File, 16), Pos(1, 6)) << "end of second line"; 252 EXPECT_THAT(offsetToPosition(File, 17), Pos(1, 7)) << "second newline"; 253 EXPECT_THAT(offsetToPosition(File, 18), Pos(2, 0)) << "start of last line"; 254 EXPECT_THAT(offsetToPosition(File, 21), Pos(2, 3)) << "in last line"; 255 EXPECT_THAT(offsetToPosition(File, 22), Pos(2, 4)) << "before astral char"; 256 EXPECT_THAT(offsetToPosition(File, 24), Pos(2, 5)) << "in astral char"; 257 EXPECT_THAT(offsetToPosition(File, 26), Pos(2, 5)) << "after astral char"; 258 EXPECT_THAT(offsetToPosition(File, 28), Pos(2, 7)) << "end of last line"; 259 EXPECT_THAT(offsetToPosition(File, 29), Pos(2, 8)) << "EOF"; 260 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 8)) << "out of bounds"; 261 262 WithContextValue UTF8(kCurrentOffsetEncoding, OffsetEncoding::UTF8); 263 for (Line L : FileLines) { 264 for (unsigned I = 0; I <= L.Length; ++I) 265 EXPECT_THAT(offsetToPosition(File, L.Offset + I), Pos(L.Number, I)); 266 } 267 EXPECT_THAT(offsetToPosition(File, 30), Pos(2, 11)) << "out of bounds"; 268 } 269 270 TEST(SourceCodeTests, SourceLocationInMainFile) { 271 Annotations Source(R"cpp( 272 ^in^t ^foo 273 ^bar 274 ^baz ^() {} {} {} {} { }^ 275 )cpp"); 276 277 SourceManagerForFile Owner("foo.cpp", Source.code()); 278 SourceManager &SM = Owner.get(); 279 280 SourceLocation StartOfFile = SM.getLocForStartOfFile(SM.getMainFileID()); 281 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 0)), 282 HasValue(StartOfFile)); 283 // End of file. 284 EXPECT_THAT_EXPECTED( 285 sourceLocationInMainFile(SM, position(4, 0)), 286 HasValue(StartOfFile.getLocWithOffset(Source.code().size()))); 287 // Column number is too large. 288 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 1)), Failed()); 289 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(0, 100)), 290 Failed()); 291 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(4, 1)), Failed()); 292 // Line number is too large. 293 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, position(5, 0)), Failed()); 294 // Check all positions mentioned in the test return valid results. 295 for (auto P : Source.points()) { 296 size_t Offset = llvm::cantFail(positionToOffset(Source.code(), P)); 297 EXPECT_THAT_EXPECTED(sourceLocationInMainFile(SM, P), 298 HasValue(StartOfFile.getLocWithOffset(Offset))); 299 } 300 } 301 302 TEST(SourceCodeTests, CollectIdentifiers) { 303 auto Style = format::getLLVMStyle(); 304 auto IDs = collectIdentifiers(R"cpp( 305 #include "a.h" 306 void foo() { int xyz; int abc = xyz; return foo(); } 307 )cpp", 308 Style); 309 EXPECT_EQ(IDs.size(), 7u); 310 EXPECT_EQ(IDs["include"], 1u); 311 EXPECT_EQ(IDs["void"], 1u); 312 EXPECT_EQ(IDs["int"], 2u); 313 EXPECT_EQ(IDs["xyz"], 2u); 314 EXPECT_EQ(IDs["abc"], 1u); 315 EXPECT_EQ(IDs["return"], 1u); 316 EXPECT_EQ(IDs["foo"], 2u); 317 } 318 319 TEST(SourceCodeTests, CollectWords) { 320 auto Words = collectWords(R"cpp( 321 #define FIZZ_BUZZ 322 // this is a comment 323 std::string getSomeText() { return "magic word"; } 324 )cpp"); 325 std::set<StringRef> ActualWords(Words.keys().begin(), Words.keys().end()); 326 std::set<StringRef> ExpectedWords = {"define", "fizz", "buzz", "this", 327 "comment", "string", "some", "text", 328 "return", "magic", "word"}; 329 EXPECT_EQ(ActualWords, ExpectedWords); 330 } 331 332 class SpelledWordsTest : public ::testing::Test { 333 llvm::Optional<ParsedAST> AST; 334 335 llvm::Optional<SpelledWord> tryWord(const char *Text) { 336 llvm::Annotations A(Text); 337 auto TU = TestTU::withCode(A.code()); 338 AST = TU.build(); 339 auto SW = SpelledWord::touching( 340 AST->getSourceManager().getComposedLoc( 341 AST->getSourceManager().getMainFileID(), A.point()), 342 AST->getTokens(), AST->getLangOpts()); 343 if (A.ranges().size()) { 344 llvm::StringRef Want = A.code().slice(A.range().Begin, A.range().End); 345 EXPECT_EQ(Want, SW->Text) << Text; 346 } 347 return SW; 348 } 349 350 protected: 351 SpelledWord word(const char *Text) { 352 auto Result = tryWord(Text); 353 EXPECT_TRUE(Result) << Text; 354 return Result.getValueOr(SpelledWord()); 355 } 356 357 void noWord(const char *Text) { EXPECT_FALSE(tryWord(Text)) << Text; } 358 }; 359 360 TEST_F(SpelledWordsTest, HeuristicBoundaries) { 361 word("// [[^foo]] "); 362 word("// [[f^oo]] "); 363 word("// [[foo^]] "); 364 word("// [[foo^]]+bar "); 365 noWord("//^ foo "); 366 noWord("// foo ^"); 367 } 368 369 TEST_F(SpelledWordsTest, LikelyIdentifier) { 370 EXPECT_FALSE(word("// ^foo ").LikelyIdentifier); 371 EXPECT_TRUE(word("// [[^foo_bar]] ").LikelyIdentifier); 372 EXPECT_TRUE(word("// [[^fooBar]] ").LikelyIdentifier); 373 EXPECT_FALSE(word("// H^TTP ").LikelyIdentifier); 374 EXPECT_TRUE(word("// \\p [[^foo]] ").LikelyIdentifier); 375 EXPECT_TRUE(word("// @param[in] [[^foo]] ").LikelyIdentifier); 376 EXPECT_TRUE(word("// `[[f^oo]]` ").LikelyIdentifier); 377 EXPECT_TRUE(word("// bar::[[f^oo]] ").LikelyIdentifier); 378 EXPECT_TRUE(word("// [[f^oo]]::bar ").LikelyIdentifier); 379 } 380 381 TEST_F(SpelledWordsTest, Comment) { 382 auto W = word("// [[^foo]]"); 383 EXPECT_FALSE(W.PartOfSpelledToken); 384 EXPECT_FALSE(W.SpelledToken); 385 EXPECT_FALSE(W.ExpandedToken); 386 } 387 388 TEST_F(SpelledWordsTest, PartOfString) { 389 auto W = word(R"( auto str = "foo [[^bar]] baz"; )"); 390 ASSERT_TRUE(W.PartOfSpelledToken); 391 EXPECT_EQ(W.PartOfSpelledToken->kind(), tok::string_literal); 392 EXPECT_FALSE(W.SpelledToken); 393 EXPECT_FALSE(W.ExpandedToken); 394 } 395 396 TEST_F(SpelledWordsTest, DisabledSection) { 397 auto W = word(R"cpp( 398 #if 0 399 foo [[^bar]] baz 400 #endif 401 )cpp"); 402 ASSERT_TRUE(W.SpelledToken); 403 EXPECT_EQ(W.SpelledToken->kind(), tok::identifier); 404 EXPECT_EQ(W.SpelledToken, W.PartOfSpelledToken); 405 EXPECT_FALSE(W.ExpandedToken); 406 } 407 408 TEST_F(SpelledWordsTest, Macros) { 409 auto W = word(R"cpp( 410 #define ID(X) X 411 ID(int [[^i]]); 412 )cpp"); 413 ASSERT_TRUE(W.SpelledToken); 414 EXPECT_EQ(W.SpelledToken->kind(), tok::identifier); 415 EXPECT_EQ(W.SpelledToken, W.PartOfSpelledToken); 416 ASSERT_TRUE(W.ExpandedToken); 417 EXPECT_EQ(W.ExpandedToken->kind(), tok::identifier); 418 419 W = word(R"cpp( 420 #define OBJECT Expansion; 421 int [[^OBJECT]]; 422 )cpp"); 423 EXPECT_TRUE(W.SpelledToken); 424 EXPECT_FALSE(W.ExpandedToken) << "Expanded token is spelled differently"; 425 } 426 427 TEST(SourceCodeTests, VisibleNamespaces) { 428 std::vector<std::pair<const char *, std::vector<std::string>>> Cases = { 429 { 430 R"cpp( 431 // Using directive resolved against enclosing namespaces. 432 using namespace foo; 433 namespace ns { 434 using namespace bar; 435 )cpp", 436 {"ns", "", "bar", "foo", "ns::bar"}, 437 }, 438 { 439 R"cpp( 440 // Don't include namespaces we've closed, ignore namespace aliases. 441 using namespace clang; 442 using std::swap; 443 namespace clang { 444 namespace clangd {} 445 namespace ll = ::llvm; 446 } 447 namespace clang { 448 )cpp", 449 {"clang", ""}, 450 }, 451 { 452 R"cpp( 453 // Using directives visible even if a namespace is reopened. 454 // Ignore anonymous namespaces. 455 namespace foo{ using namespace bar; } 456 namespace foo{ namespace { 457 )cpp", 458 {"foo", "", "bar", "foo::bar"}, 459 }, 460 { 461 R"cpp( 462 // Mismatched braces 463 namespace foo{} 464 }}} 465 namespace bar{ 466 )cpp", 467 {"bar", ""}, 468 }, 469 { 470 R"cpp( 471 // Namespaces with multiple chunks. 472 namespace a::b { 473 using namespace c::d; 474 namespace e::f { 475 )cpp", 476 { 477 "a::b::e::f", 478 "", 479 "a", 480 "a::b", 481 "a::b::c::d", 482 "a::b::e", 483 "a::c::d", 484 "c::d", 485 }, 486 }, 487 { 488 "", 489 {""}, 490 }, 491 { 492 R"cpp( 493 // Parse until EOF 494 namespace bar{})cpp", 495 {""}, 496 }, 497 }; 498 for (const auto &Case : Cases) { 499 EXPECT_EQ(Case.second, 500 visibleNamespaces(Case.first, format::getFormattingLangOpts( 501 format::getLLVMStyle()))) 502 << Case.first; 503 } 504 } 505 506 TEST(SourceCodeTests, GetMacros) { 507 Annotations Code(R"cpp( 508 #define MACRO 123 509 int abc = MA^CRO; 510 )cpp"); 511 TestTU TU = TestTU::withCode(Code.code()); 512 auto AST = TU.build(); 513 auto CurLoc = sourceLocationInMainFile(AST.getSourceManager(), Code.point()); 514 ASSERT_TRUE(bool(CurLoc)); 515 const auto *Id = syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens()); 516 ASSERT_TRUE(Id); 517 auto Result = locateMacroAt(*Id, AST.getPreprocessor()); 518 ASSERT_TRUE(Result); 519 EXPECT_THAT(*Result, MacroName("MACRO")); 520 } 521 522 TEST(SourceCodeTests, WorksAtBeginOfFile) { 523 Annotations Code("^MACRO"); 524 TestTU TU = TestTU::withCode(Code.code()); 525 TU.HeaderCode = "#define MACRO int x;"; 526 auto AST = TU.build(); 527 auto CurLoc = sourceLocationInMainFile(AST.getSourceManager(), Code.point()); 528 ASSERT_TRUE(bool(CurLoc)); 529 const auto *Id = syntax::spelledIdentifierTouching(*CurLoc, AST.getTokens()); 530 ASSERT_TRUE(Id); 531 auto Result = locateMacroAt(*Id, AST.getPreprocessor()); 532 ASSERT_TRUE(Result); 533 EXPECT_THAT(*Result, MacroName("MACRO")); 534 } 535 536 TEST(SourceCodeTests, IsInsideMainFile) { 537 TestTU TU; 538 TU.HeaderCode = R"cpp( 539 #define DEFINE_CLASS(X) class X {}; 540 #define DEFINE_YY DEFINE_CLASS(YY) 541 542 class Header1 {}; 543 DEFINE_CLASS(Header2) 544 class Header {}; 545 )cpp"; 546 TU.Code = R"cpp( 547 #define DEFINE_MAIN4 class Main4{}; 548 class Main1 {}; 549 DEFINE_CLASS(Main2) 550 DEFINE_YY 551 class Main {}; 552 DEFINE_MAIN4 553 )cpp"; 554 TU.ExtraArgs.push_back("-DHeader=Header3"); 555 TU.ExtraArgs.push_back("-DMain=Main3"); 556 auto AST = TU.build(); 557 const auto &SM = AST.getSourceManager(); 558 auto DeclLoc = [&AST](llvm::StringRef Name) { 559 return findDecl(AST, Name).getLocation(); 560 }; 561 for (const auto *HeaderDecl : {"Header1", "Header2", "Header3"}) 562 EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM)) << HeaderDecl; 563 564 for (const auto *MainDecl : {"Main1", "Main2", "Main3", "Main4", "YY"}) 565 EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM)) << MainDecl; 566 567 // Main4 is *spelled* in the preamble, but in the main-file part of it. 568 EXPECT_TRUE(isInsideMainFile(SM.getSpellingLoc(DeclLoc("Main4")), SM)); 569 } 570 571 // Test for functions toHalfOpenFileRange and getHalfOpenFileRange 572 TEST(SourceCodeTests, HalfOpenFileRange) { 573 // Each marked range should be the file range of the decl with the same name 574 // and each name should be unique. 575 Annotations Test(R"cpp( 576 #define FOO(X, Y) int Y = ++X 577 #define BAR(X) X + 1 578 #define ECHO(X) X 579 580 #define BUZZ BAZZ(ADD) 581 #define BAZZ(m) m(1) 582 #define ADD(a) int f = a + 1; 583 template<typename T> 584 class P {}; 585 586 int main() { 587 $a[[P<P<P<P<P<int>>>>> a]]; 588 $b[[int b = 1]]; 589 $c[[FOO(b, c)]]; 590 $d[[FOO(BAR(BAR(b)), d)]]; 591 // FIXME: We might want to select everything inside the outer ECHO. 592 ECHO(ECHO($e[[int) ECHO(e]])); 593 // Shouldn't crash. 594 $f[[BUZZ]]; 595 } 596 )cpp"); 597 598 ParsedAST AST = TestTU::withCode(Test.code()).build(); 599 llvm::errs() << Test.code(); 600 const SourceManager &SM = AST.getSourceManager(); 601 const LangOptions &LangOpts = AST.getLangOpts(); 602 // Turn a SourceLocation into a pair of positions 603 auto SourceRangeToRange = [&SM](SourceRange SrcRange) { 604 return Range{sourceLocToPosition(SM, SrcRange.getBegin()), 605 sourceLocToPosition(SM, SrcRange.getEnd())}; 606 }; 607 auto CheckRange = [&](llvm::StringRef Name) { 608 const NamedDecl &Decl = findUnqualifiedDecl(AST, Name); 609 auto FileRange = toHalfOpenFileRange(SM, LangOpts, Decl.getSourceRange()); 610 SCOPED_TRACE("Checking range: " + Name); 611 ASSERT_NE(FileRange, llvm::None); 612 Range HalfOpenRange = SourceRangeToRange(*FileRange); 613 EXPECT_EQ(HalfOpenRange, Test.ranges(Name)[0]); 614 }; 615 616 CheckRange("a"); 617 CheckRange("b"); 618 CheckRange("c"); 619 CheckRange("d"); 620 CheckRange("e"); 621 CheckRange("f"); 622 } 623 624 TEST(SourceCodeTests, HalfOpenFileRangePathologicalPreprocessor) { 625 const char *Case = R"cpp( 626 #define MACRO while(1) 627 void test() { 628 [[#include "Expand.inc" 629 br^eak]]; 630 } 631 )cpp"; 632 Annotations Test(Case); 633 auto TU = TestTU::withCode(Test.code()); 634 TU.AdditionalFiles["Expand.inc"] = "MACRO\n"; 635 auto AST = TU.build(); 636 637 const auto &Func = cast<FunctionDecl>(findDecl(AST, "test")); 638 const auto &Body = cast<CompoundStmt>(Func.getBody()); 639 const auto &Loop = cast<WhileStmt>(*Body->child_begin()); 640 llvm::Optional<SourceRange> Range = toHalfOpenFileRange( 641 AST.getSourceManager(), AST.getLangOpts(), Loop->getSourceRange()); 642 ASSERT_TRUE(Range) << "Failed to get file range"; 643 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getBegin()), 644 Test.llvm::Annotations::range().Begin); 645 EXPECT_EQ(AST.getSourceManager().getFileOffset(Range->getEnd()), 646 Test.llvm::Annotations::range().End); 647 } 648 649 TEST(SourceCodeTests, IncludeHashLoc) { 650 const char *Case = R"cpp( 651 $foo^#include "foo.inc" 652 #define HEADER "bar.inc" 653 $bar^# include HEADER 654 )cpp"; 655 Annotations Test(Case); 656 auto TU = TestTU::withCode(Test.code()); 657 TU.AdditionalFiles["foo.inc"] = "int foo;\n"; 658 TU.AdditionalFiles["bar.inc"] = "int bar;\n"; 659 auto AST = TU.build(); 660 const auto &SM = AST.getSourceManager(); 661 662 FileID Foo = SM.getFileID(findDecl(AST, "foo").getLocation()); 663 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Foo, SM)), 664 Test.llvm::Annotations::point("foo")); 665 FileID Bar = SM.getFileID(findDecl(AST, "bar").getLocation()); 666 EXPECT_EQ(SM.getFileOffset(includeHashLoc(Bar, SM)), 667 Test.llvm::Annotations::point("bar")); 668 } 669 670 TEST(SourceCodeTests, GetEligiblePoints) { 671 constexpr struct { 672 const char *Code; 673 const char *FullyQualifiedName; 674 const char *EnclosingNamespace; 675 } Cases[] = { 676 {R"cpp(// FIXME: We should also mark positions before and after 677 //declarations/definitions as eligible. 678 namespace ns1 { 679 namespace a { namespace ns2 {} } 680 namespace ns2 {^ 681 void foo(); 682 namespace {} 683 void bar() {} 684 namespace ns3 {} 685 class T {}; 686 ^} 687 using namespace ns2; 688 })cpp", 689 "ns1::ns2::symbol", "ns1::ns2::"}, 690 {R"cpp( 691 namespace ns1 {^ 692 namespace a { namespace ns2 {} } 693 namespace b {} 694 namespace ns {} 695 ^})cpp", 696 "ns1::ns2::symbol", "ns1::"}, 697 {R"cpp( 698 namespace x { 699 namespace a { namespace ns2 {} } 700 namespace b {} 701 namespace ns {} 702 }^)cpp", 703 "ns1::ns2::symbol", ""}, 704 {R"cpp( 705 namespace ns1 { 706 namespace ns2 {^^} 707 namespace b {} 708 namespace ns2 {^^} 709 } 710 namespace ns1 {namespace ns2 {^^}})cpp", 711 "ns1::ns2::symbol", "ns1::ns2::"}, 712 {R"cpp( 713 namespace ns1 {^ 714 namespace ns {} 715 namespace b {} 716 namespace ns {} 717 ^} 718 namespace ns1 {^namespace ns {}^})cpp", 719 "ns1::ns2::symbol", "ns1::"}, 720 }; 721 for (auto Case : Cases) { 722 Annotations Test(Case.Code); 723 724 auto Res = getEligiblePoints( 725 Test.code(), Case.FullyQualifiedName, 726 format::getFormattingLangOpts(format::getLLVMStyle())); 727 EXPECT_THAT(Res.EligiblePoints, testing::ElementsAreArray(Test.points())) 728 << Test.code(); 729 EXPECT_EQ(Res.EnclosingNamespace, Case.EnclosingNamespace) << Test.code(); 730 } 731 } 732 733 TEST(SourceCodeTests, IdentifierRanges) { 734 Annotations Code(R"cpp( 735 class [[Foo]] {}; 736 // Foo 737 /* Foo */ 738 void f([[Foo]]* foo1) { 739 [[Foo]] foo2; 740 auto S = [[Foo]](); 741 // cross-line identifier is not supported. 742 F\ 743 o\ 744 o foo2; 745 } 746 )cpp"); 747 LangOptions LangOpts; 748 LangOpts.CPlusPlus = true; 749 EXPECT_EQ(Code.ranges(), 750 collectIdentifierRanges("Foo", Code.code(), LangOpts)); 751 } 752 753 TEST(SourceCodeTests, isHeaderFile) { 754 // Without lang options. 755 EXPECT_TRUE(isHeaderFile("foo.h")); 756 EXPECT_TRUE(isHeaderFile("foo.hh")); 757 EXPECT_TRUE(isHeaderFile("foo.hpp")); 758 759 EXPECT_FALSE(isHeaderFile("foo.cpp")); 760 EXPECT_FALSE(isHeaderFile("foo.c++")); 761 EXPECT_FALSE(isHeaderFile("foo.cxx")); 762 EXPECT_FALSE(isHeaderFile("foo.cc")); 763 EXPECT_FALSE(isHeaderFile("foo.c")); 764 EXPECT_FALSE(isHeaderFile("foo.mm")); 765 EXPECT_FALSE(isHeaderFile("foo.m")); 766 767 // With lang options 768 LangOptions LangOpts; 769 LangOpts.IsHeaderFile = true; 770 EXPECT_TRUE(isHeaderFile("string", LangOpts)); 771 // Emulate cases where there is no "-x header" flag for a .h file, we still 772 // want to treat it as a header. 773 LangOpts.IsHeaderFile = false; 774 EXPECT_TRUE(isHeaderFile("header.h", LangOpts)); 775 } 776 777 } // namespace 778 } // namespace clangd 779 } // namespace clang 780