TokensTest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //===- TokensTest.cpp -----------------------------------------------------===//
  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 "clang/Tooling/Syntax/Tokens.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/AST/Expr.h"
  11. #include "clang/Basic/Diagnostic.h"
  12. #include "clang/Basic/DiagnosticIDs.h"
  13. #include "clang/Basic/DiagnosticOptions.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/FileSystemOptions.h"
  16. #include "clang/Basic/LLVM.h"
  17. #include "clang/Basic/LangOptions.h"
  18. #include "clang/Basic/SourceLocation.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Basic/TokenKinds.def"
  21. #include "clang/Basic/TokenKinds.h"
  22. #include "clang/Frontend/CompilerInstance.h"
  23. #include "clang/Frontend/FrontendAction.h"
  24. #include "clang/Frontend/Utils.h"
  25. #include "clang/Lex/Lexer.h"
  26. #include "clang/Lex/PreprocessorOptions.h"
  27. #include "clang/Lex/Token.h"
  28. #include "clang/Tooling/Tooling.h"
  29. #include "llvm/ADT/ArrayRef.h"
  30. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  31. #include "llvm/ADT/None.h"
  32. #include "llvm/ADT/Optional.h"
  33. #include "llvm/ADT/STLExtras.h"
  34. #include "llvm/ADT/StringRef.h"
  35. #include "llvm/Support/FormatVariadic.h"
  36. #include "llvm/Support/MemoryBuffer.h"
  37. #include "llvm/Support/VirtualFileSystem.h"
  38. #include "llvm/Support/raw_os_ostream.h"
  39. #include "llvm/Support/raw_ostream.h"
  40. #include "llvm/Testing/Support/Annotations.h"
  41. #include "llvm/Testing/Support/SupportHelpers.h"
  42. #include <cassert>
  43. #include <cstdlib>
  44. #include <gmock/gmock.h>
  45. #include <gtest/gtest.h>
  46. #include <memory>
  47. #include <ostream>
  48. #include <string>
  49. using namespace clang;
  50. using namespace clang::syntax;
  51. using llvm::ValueIs;
  52. using ::testing::AllOf;
  53. using ::testing::Contains;
  54. using ::testing::ElementsAre;
  55. using ::testing::Matcher;
  56. using ::testing::Not;
  57. using ::testing::StartsWith;
  58. namespace {
  59. // Checks the passed ArrayRef<T> has the same begin() and end() iterators as the
  60. // argument.
  61. MATCHER_P(SameRange, A, "") {
  62. return A.begin() == arg.begin() && A.end() == arg.end();
  63. }
  64. // Matchers for syntax::Token.
  65. MATCHER_P(Kind, K, "") { return arg.kind() == K; }
  66. MATCHER_P2(HasText, Text, SourceMgr, "") {
  67. return arg.text(*SourceMgr) == Text;
  68. }
  69. /// Checks the start and end location of a token are equal to SourceRng.
  70. MATCHER_P(RangeIs, SourceRng, "") {
  71. return arg.location() == SourceRng.first &&
  72. arg.endLocation() == SourceRng.second;
  73. }
  74. class TokenCollectorTest : public ::testing::Test {
  75. public:
  76. /// Run the clang frontend, collect the preprocessed tokens from the frontend
  77. /// invocation and store them in this->Buffer.
  78. /// This also clears SourceManager before running the compiler.
  79. void recordTokens(llvm::StringRef Code) {
  80. class RecordTokens : public ASTFrontendAction {
  81. public:
  82. explicit RecordTokens(TokenBuffer &Result) : Result(Result) {}
  83. bool BeginSourceFileAction(CompilerInstance &CI) override {
  84. assert(!Collector && "expected only a single call to BeginSourceFile");
  85. Collector.emplace(CI.getPreprocessor());
  86. return true;
  87. }
  88. void EndSourceFileAction() override {
  89. assert(Collector && "BeginSourceFileAction was never called");
  90. Result = std::move(*Collector).consume();
  91. }
  92. std::unique_ptr<ASTConsumer>
  93. CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
  94. return llvm::make_unique<ASTConsumer>();
  95. }
  96. private:
  97. TokenBuffer &Result;
  98. llvm::Optional<TokenCollector> Collector;
  99. };
  100. constexpr const char *FileName = "./input.cpp";
  101. FS->addFile(FileName, time_t(), llvm::MemoryBuffer::getMemBufferCopy(""));
  102. // Prepare to run a compiler.
  103. if (!Diags->getClient())
  104. Diags->setClient(new IgnoringDiagConsumer);
  105. std::vector<const char *> Args = {"tok-test", "-std=c++03", "-fsyntax-only",
  106. FileName};
  107. auto CI = createInvocationFromCommandLine(Args, Diags, FS);
  108. assert(CI);
  109. CI->getFrontendOpts().DisableFree = false;
  110. CI->getPreprocessorOpts().addRemappedFile(
  111. FileName, llvm::MemoryBuffer::getMemBufferCopy(Code).release());
  112. CompilerInstance Compiler;
  113. Compiler.setInvocation(std::move(CI));
  114. Compiler.setDiagnostics(Diags.get());
  115. Compiler.setFileManager(FileMgr.get());
  116. Compiler.setSourceManager(SourceMgr.get());
  117. this->Buffer = TokenBuffer(*SourceMgr);
  118. RecordTokens Recorder(this->Buffer);
  119. ASSERT_TRUE(Compiler.ExecuteAction(Recorder))
  120. << "failed to run the frontend";
  121. }
  122. /// Record the tokens and return a test dump of the resulting buffer.
  123. std::string collectAndDump(llvm::StringRef Code) {
  124. recordTokens(Code);
  125. return Buffer.dumpForTests();
  126. }
  127. // Adds a file to the test VFS.
  128. void addFile(llvm::StringRef Path, llvm::StringRef Contents) {
  129. if (!FS->addFile(Path, time_t(),
  130. llvm::MemoryBuffer::getMemBufferCopy(Contents))) {
  131. ADD_FAILURE() << "could not add a file to VFS: " << Path;
  132. }
  133. }
  134. /// Add a new file, run syntax::tokenize() on it and return the results.
  135. std::vector<syntax::Token> tokenize(llvm::StringRef Text) {
  136. // FIXME: pass proper LangOptions.
  137. return syntax::tokenize(
  138. SourceMgr->createFileID(llvm::MemoryBuffer::getMemBufferCopy(Text)),
  139. *SourceMgr, LangOptions());
  140. }
  141. // Specialized versions of matchers that hide the SourceManager from clients.
  142. Matcher<syntax::Token> HasText(std::string Text) const {
  143. return ::HasText(Text, SourceMgr.get());
  144. }
  145. Matcher<syntax::Token> RangeIs(llvm::Annotations::Range R) const {
  146. std::pair<SourceLocation, SourceLocation> Ls;
  147. Ls.first = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID())
  148. .getLocWithOffset(R.Begin);
  149. Ls.second = SourceMgr->getLocForStartOfFile(SourceMgr->getMainFileID())
  150. .getLocWithOffset(R.End);
  151. return ::RangeIs(Ls);
  152. }
  153. /// Finds a subrange in O(n * m).
  154. template <class T, class U, class Eq>
  155. llvm::ArrayRef<T> findSubrange(llvm::ArrayRef<U> Subrange,
  156. llvm::ArrayRef<T> Range, Eq F) {
  157. for (auto Begin = Range.begin(); Begin < Range.end(); ++Begin) {
  158. auto It = Begin;
  159. for (auto ItSub = Subrange.begin();
  160. ItSub != Subrange.end() && It != Range.end(); ++ItSub, ++It) {
  161. if (!F(*ItSub, *It))
  162. goto continue_outer;
  163. }
  164. return llvm::makeArrayRef(Begin, It);
  165. continue_outer:;
  166. }
  167. return llvm::makeArrayRef(Range.end(), Range.end());
  168. }
  169. /// Finds a subrange in \p Tokens that match the tokens specified in \p Query.
  170. /// The match should be unique. \p Query is a whitespace-separated list of
  171. /// tokens to search for.
  172. llvm::ArrayRef<syntax::Token>
  173. findTokenRange(llvm::StringRef Query, llvm::ArrayRef<syntax::Token> Tokens) {
  174. llvm::SmallVector<llvm::StringRef, 8> QueryTokens;
  175. Query.split(QueryTokens, ' ', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
  176. if (QueryTokens.empty()) {
  177. ADD_FAILURE() << "will not look for an empty list of tokens";
  178. std::abort();
  179. }
  180. // An equality test for search.
  181. auto TextMatches = [this](llvm::StringRef Q, const syntax::Token &T) {
  182. return Q == T.text(*SourceMgr);
  183. };
  184. // Find a match.
  185. auto Found =
  186. findSubrange(llvm::makeArrayRef(QueryTokens), Tokens, TextMatches);
  187. if (Found.begin() == Tokens.end()) {
  188. ADD_FAILURE() << "could not find the subrange for " << Query;
  189. std::abort();
  190. }
  191. // Check that the match is unique.
  192. if (findSubrange(llvm::makeArrayRef(QueryTokens),
  193. llvm::makeArrayRef(Found.end(), Tokens.end()), TextMatches)
  194. .begin() != Tokens.end()) {
  195. ADD_FAILURE() << "match is not unique for " << Query;
  196. std::abort();
  197. }
  198. return Found;
  199. };
  200. // Specialized versions of findTokenRange for expanded and spelled tokens.
  201. llvm::ArrayRef<syntax::Token> findExpanded(llvm::StringRef Query) {
  202. return findTokenRange(Query, Buffer.expandedTokens());
  203. }
  204. llvm::ArrayRef<syntax::Token> findSpelled(llvm::StringRef Query,
  205. FileID File = FileID()) {
  206. if (!File.isValid())
  207. File = SourceMgr->getMainFileID();
  208. return findTokenRange(Query, Buffer.spelledTokens(File));
  209. }
  210. // Data fields.
  211. llvm::IntrusiveRefCntPtr<DiagnosticsEngine> Diags =
  212. new DiagnosticsEngine(new DiagnosticIDs, new DiagnosticOptions);
  213. IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FS =
  214. new llvm::vfs::InMemoryFileSystem;
  215. llvm::IntrusiveRefCntPtr<FileManager> FileMgr =
  216. new FileManager(FileSystemOptions(), FS);
  217. llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr =
  218. new SourceManager(*Diags, *FileMgr);
  219. /// Contains last result of calling recordTokens().
  220. TokenBuffer Buffer = TokenBuffer(*SourceMgr);
  221. };
  222. TEST_F(TokenCollectorTest, RawMode) {
  223. EXPECT_THAT(tokenize("int main() {}"),
  224. ElementsAre(Kind(tok::kw_int),
  225. AllOf(HasText("main"), Kind(tok::identifier)),
  226. Kind(tok::l_paren), Kind(tok::r_paren),
  227. Kind(tok::l_brace), Kind(tok::r_brace)));
  228. // Comments are ignored for now.
  229. EXPECT_THAT(tokenize("/* foo */int a; // more comments"),
  230. ElementsAre(Kind(tok::kw_int),
  231. AllOf(HasText("a"), Kind(tok::identifier)),
  232. Kind(tok::semi)));
  233. }
  234. TEST_F(TokenCollectorTest, Basic) {
  235. std::pair</*Input*/ std::string, /*Expected*/ std::string> TestCases[] = {
  236. {"int main() {}",
  237. R"(expanded tokens:
  238. int main ( ) { }
  239. file './input.cpp'
  240. spelled tokens:
  241. int main ( ) { }
  242. no mappings.
  243. )"},
  244. // All kinds of whitespace are ignored.
  245. {"\t\n int\t\n main\t\n (\t\n )\t\n{\t\n }\t\n",
  246. R"(expanded tokens:
  247. int main ( ) { }
  248. file './input.cpp'
  249. spelled tokens:
  250. int main ( ) { }
  251. no mappings.
  252. )"},
  253. // Annotation tokens are ignored.
  254. {R"cpp(
  255. #pragma GCC visibility push (public)
  256. #pragma GCC visibility pop
  257. )cpp",
  258. R"(expanded tokens:
  259. <empty>
  260. file './input.cpp'
  261. spelled tokens:
  262. # pragma GCC visibility push ( public ) # pragma GCC visibility pop
  263. mappings:
  264. ['#'_0, '<eof>'_13) => ['<eof>'_0, '<eof>'_0)
  265. )"}};
  266. for (auto &Test : TestCases)
  267. EXPECT_EQ(collectAndDump(Test.first), Test.second)
  268. << collectAndDump(Test.first);
  269. }
  270. TEST_F(TokenCollectorTest, Locations) {
  271. // Check locations of the tokens.
  272. llvm::Annotations Code(R"cpp(
  273. $r1[[int]] $r2[[a]] $r3[[=]] $r4[["foo bar baz"]] $r5[[;]]
  274. )cpp");
  275. recordTokens(Code.code());
  276. // Check expanded tokens.
  277. EXPECT_THAT(
  278. Buffer.expandedTokens(),
  279. ElementsAre(AllOf(Kind(tok::kw_int), RangeIs(Code.range("r1"))),
  280. AllOf(Kind(tok::identifier), RangeIs(Code.range("r2"))),
  281. AllOf(Kind(tok::equal), RangeIs(Code.range("r3"))),
  282. AllOf(Kind(tok::string_literal), RangeIs(Code.range("r4"))),
  283. AllOf(Kind(tok::semi), RangeIs(Code.range("r5"))),
  284. Kind(tok::eof)));
  285. // Check spelled tokens.
  286. EXPECT_THAT(
  287. Buffer.spelledTokens(SourceMgr->getMainFileID()),
  288. ElementsAre(AllOf(Kind(tok::kw_int), RangeIs(Code.range("r1"))),
  289. AllOf(Kind(tok::identifier), RangeIs(Code.range("r2"))),
  290. AllOf(Kind(tok::equal), RangeIs(Code.range("r3"))),
  291. AllOf(Kind(tok::string_literal), RangeIs(Code.range("r4"))),
  292. AllOf(Kind(tok::semi), RangeIs(Code.range("r5")))));
  293. }
  294. TEST_F(TokenCollectorTest, MacroDirectives) {
  295. // Macro directives are not stored anywhere at the moment.
  296. std::string Code = R"cpp(
  297. #define FOO a
  298. #include "unresolved_file.h"
  299. #undef FOO
  300. #ifdef X
  301. #else
  302. #endif
  303. #ifndef Y
  304. #endif
  305. #if 1
  306. #elif 2
  307. #else
  308. #endif
  309. #pragma once
  310. #pragma something lalala
  311. int a;
  312. )cpp";
  313. std::string Expected =
  314. "expanded tokens:\n"
  315. " int a ;\n"
  316. "file './input.cpp'\n"
  317. " spelled tokens:\n"
  318. " # define FOO a # include \"unresolved_file.h\" # undef FOO "
  319. "# ifdef X # else # endif # ifndef Y # endif # if 1 # elif 2 # else "
  320. "# endif # pragma once # pragma something lalala int a ;\n"
  321. " mappings:\n"
  322. " ['#'_0, 'int'_39) => ['int'_0, 'int'_0)\n";
  323. EXPECT_EQ(collectAndDump(Code), Expected);
  324. }
  325. TEST_F(TokenCollectorTest, MacroReplacements) {
  326. std::pair</*Input*/ std::string, /*Expected*/ std::string> TestCases[] = {
  327. // A simple object-like macro.
  328. {R"cpp(
  329. #define INT int const
  330. INT a;
  331. )cpp",
  332. R"(expanded tokens:
  333. int const a ;
  334. file './input.cpp'
  335. spelled tokens:
  336. # define INT int const INT a ;
  337. mappings:
  338. ['#'_0, 'INT'_5) => ['int'_0, 'int'_0)
  339. ['INT'_5, 'a'_6) => ['int'_0, 'a'_2)
  340. )"},
  341. // A simple function-like macro.
  342. {R"cpp(
  343. #define INT(a) const int
  344. INT(10+10) a;
  345. )cpp",
  346. R"(expanded tokens:
  347. const int a ;
  348. file './input.cpp'
  349. spelled tokens:
  350. # define INT ( a ) const int INT ( 10 + 10 ) a ;
  351. mappings:
  352. ['#'_0, 'INT'_8) => ['const'_0, 'const'_0)
  353. ['INT'_8, 'a'_14) => ['const'_0, 'a'_2)
  354. )"},
  355. // Recursive macro replacements.
  356. {R"cpp(
  357. #define ID(X) X
  358. #define INT int const
  359. ID(ID(INT)) a;
  360. )cpp",
  361. R"(expanded tokens:
  362. int const a ;
  363. file './input.cpp'
  364. spelled tokens:
  365. # define ID ( X ) X # define INT int const ID ( ID ( INT ) ) a ;
  366. mappings:
  367. ['#'_0, 'ID'_12) => ['int'_0, 'int'_0)
  368. ['ID'_12, 'a'_19) => ['int'_0, 'a'_2)
  369. )"},
  370. // A little more complicated recursive macro replacements.
  371. {R"cpp(
  372. #define ADD(X, Y) X+Y
  373. #define MULT(X, Y) X*Y
  374. int a = ADD(MULT(1,2), MULT(3,ADD(4,5)));
  375. )cpp",
  376. "expanded tokens:\n"
  377. " int a = 1 * 2 + 3 * 4 + 5 ;\n"
  378. "file './input.cpp'\n"
  379. " spelled tokens:\n"
  380. " # define ADD ( X , Y ) X + Y # define MULT ( X , Y ) X * Y int "
  381. "a = ADD ( MULT ( 1 , 2 ) , MULT ( 3 , ADD ( 4 , 5 ) ) ) ;\n"
  382. " mappings:\n"
  383. " ['#'_0, 'int'_22) => ['int'_0, 'int'_0)\n"
  384. " ['ADD'_25, ';'_46) => ['1'_3, ';'_12)\n"},
  385. // Empty macro replacement.
  386. {R"cpp(
  387. #define EMPTY
  388. #define EMPTY_FUNC(X)
  389. EMPTY
  390. EMPTY_FUNC(1+2+3)
  391. )cpp",
  392. R"(expanded tokens:
  393. <empty>
  394. file './input.cpp'
  395. spelled tokens:
  396. # define EMPTY # define EMPTY_FUNC ( X ) EMPTY EMPTY_FUNC ( 1 + 2 + 3 )
  397. mappings:
  398. ['#'_0, '<eof>'_18) => ['<eof>'_0, '<eof>'_0)
  399. )"},
  400. // File ends with a macro replacement.
  401. {R"cpp(
  402. #define FOO 10+10;
  403. int a = FOO
  404. )cpp",
  405. R"(expanded tokens:
  406. int a = 10 + 10 ;
  407. file './input.cpp'
  408. spelled tokens:
  409. # define FOO 10 + 10 ; int a = FOO
  410. mappings:
  411. ['#'_0, 'int'_7) => ['int'_0, 'int'_0)
  412. ['FOO'_10, '<eof>'_11) => ['10'_3, '<eof>'_7)
  413. )"}};
  414. for (auto &Test : TestCases)
  415. EXPECT_EQ(Test.second, collectAndDump(Test.first))
  416. << collectAndDump(Test.first);
  417. }
  418. TEST_F(TokenCollectorTest, SpecialTokens) {
  419. // Tokens coming from concatenations.
  420. recordTokens(R"cpp(
  421. #define CONCAT(a, b) a ## b
  422. int a = CONCAT(1, 2);
  423. )cpp");
  424. EXPECT_THAT(std::vector<syntax::Token>(Buffer.expandedTokens()),
  425. Contains(HasText("12")));
  426. // Multi-line tokens with slashes at the end.
  427. recordTokens("i\\\nn\\\nt");
  428. EXPECT_THAT(Buffer.expandedTokens(),
  429. ElementsAre(AllOf(Kind(tok::kw_int), HasText("i\\\nn\\\nt")),
  430. Kind(tok::eof)));
  431. // FIXME: test tokens with digraphs and UCN identifiers.
  432. }
  433. TEST_F(TokenCollectorTest, LateBoundTokens) {
  434. // The parser eventually breaks the first '>>' into two tokens ('>' and '>'),
  435. // but we choose to record them as a single token (for now).
  436. llvm::Annotations Code(R"cpp(
  437. template <class T>
  438. struct foo { int a; };
  439. int bar = foo<foo<int$br[[>>]]().a;
  440. int baz = 10 $op[[>>]] 2;
  441. )cpp");
  442. recordTokens(Code.code());
  443. EXPECT_THAT(std::vector<syntax::Token>(Buffer.expandedTokens()),
  444. AllOf(Contains(AllOf(Kind(tok::greatergreater),
  445. RangeIs(Code.range("br")))),
  446. Contains(AllOf(Kind(tok::greatergreater),
  447. RangeIs(Code.range("op"))))));
  448. }
  449. TEST_F(TokenCollectorTest, DelayedParsing) {
  450. llvm::StringLiteral Code = R"cpp(
  451. struct Foo {
  452. int method() {
  453. // Parser will visit method bodies and initializers multiple times, but
  454. // TokenBuffer should only record the first walk over the tokens;
  455. return 100;
  456. }
  457. int a = 10;
  458. struct Subclass {
  459. void foo() {
  460. Foo().method();
  461. }
  462. };
  463. };
  464. )cpp";
  465. std::string ExpectedTokens =
  466. "expanded tokens:\n"
  467. " struct Foo { int method ( ) { return 100 ; } int a = 10 ; struct "
  468. "Subclass { void foo ( ) { Foo ( ) . method ( ) ; } } ; } ;\n";
  469. EXPECT_THAT(collectAndDump(Code), StartsWith(ExpectedTokens));
  470. }
  471. TEST_F(TokenCollectorTest, MultiFile) {
  472. addFile("./foo.h", R"cpp(
  473. #define ADD(X, Y) X+Y
  474. int a = 100;
  475. #include "bar.h"
  476. )cpp");
  477. addFile("./bar.h", R"cpp(
  478. int b = ADD(1, 2);
  479. #define MULT(X, Y) X*Y
  480. )cpp");
  481. llvm::StringLiteral Code = R"cpp(
  482. #include "foo.h"
  483. int c = ADD(1, MULT(2,3));
  484. )cpp";
  485. std::string Expected = R"(expanded tokens:
  486. int a = 100 ; int b = 1 + 2 ; int c = 1 + 2 * 3 ;
  487. file './input.cpp'
  488. spelled tokens:
  489. # include "foo.h" int c = ADD ( 1 , MULT ( 2 , 3 ) ) ;
  490. mappings:
  491. ['#'_0, 'int'_3) => ['int'_12, 'int'_12)
  492. ['ADD'_6, ';'_17) => ['1'_15, ';'_20)
  493. file './foo.h'
  494. spelled tokens:
  495. # define ADD ( X , Y ) X + Y int a = 100 ; # include "bar.h"
  496. mappings:
  497. ['#'_0, 'int'_11) => ['int'_0, 'int'_0)
  498. ['#'_16, '<eof>'_19) => ['int'_5, 'int'_5)
  499. file './bar.h'
  500. spelled tokens:
  501. int b = ADD ( 1 , 2 ) ; # define MULT ( X , Y ) X * Y
  502. mappings:
  503. ['ADD'_3, ';'_9) => ['1'_8, ';'_11)
  504. ['#'_10, '<eof>'_21) => ['int'_12, 'int'_12)
  505. )";
  506. EXPECT_EQ(Expected, collectAndDump(Code))
  507. << "input: " << Code << "\nresults: " << collectAndDump(Code);
  508. }
  509. class TokenBufferTest : public TokenCollectorTest {};
  510. TEST_F(TokenBufferTest, SpelledByExpanded) {
  511. recordTokens(R"cpp(
  512. a1 a2 a3 b1 b2
  513. )cpp");
  514. // Sanity check: expanded and spelled tokens are stored separately.
  515. EXPECT_THAT(findExpanded("a1 a2"), Not(SameRange(findSpelled("a1 a2"))));
  516. // Searching for subranges of expanded tokens should give the corresponding
  517. // spelled ones.
  518. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3 b1 b2")),
  519. ValueIs(SameRange(findSpelled("a1 a2 a3 b1 b2"))));
  520. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3")),
  521. ValueIs(SameRange(findSpelled("a1 a2 a3"))));
  522. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("b1 b2")),
  523. ValueIs(SameRange(findSpelled("b1 b2"))));
  524. // Test search on simple macro expansions.
  525. recordTokens(R"cpp(
  526. #define A a1 a2 a3
  527. #define B b1 b2
  528. A split B
  529. )cpp");
  530. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3 split b1 b2")),
  531. ValueIs(SameRange(findSpelled("A split B"))));
  532. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3")),
  533. ValueIs(SameRange(findSpelled("A split").drop_back())));
  534. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("b1 b2")),
  535. ValueIs(SameRange(findSpelled("split B").drop_front())));
  536. // Ranges not fully covering macro invocations should fail.
  537. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a1 a2")), llvm::None);
  538. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("b2")), llvm::None);
  539. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a2 a3 split b1 b2")),
  540. llvm::None);
  541. // Recursive macro invocations.
  542. recordTokens(R"cpp(
  543. #define ID(x) x
  544. #define B b1 b2
  545. ID(ID(ID(a1) a2 a3)) split ID(B)
  546. )cpp");
  547. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3")),
  548. ValueIs(SameRange(findSpelled("ID ( ID ( ID ( a1 ) a2 a3 ) )"))));
  549. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("b1 b2")),
  550. ValueIs(SameRange(findSpelled("ID ( B )"))));
  551. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("a1 a2 a3 split b1 b2")),
  552. ValueIs(SameRange(findSpelled(
  553. "ID ( ID ( ID ( a1 ) a2 a3 ) ) split ID ( B )"))));
  554. // Ranges crossing macro call boundaries.
  555. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a1 a2 a3 split b1")),
  556. llvm::None);
  557. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a2 a3 split b1")),
  558. llvm::None);
  559. // FIXME: next two examples should map to macro arguments, but currently they
  560. // fail.
  561. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a2")), llvm::None);
  562. EXPECT_EQ(Buffer.spelledForExpanded(findExpanded("a1 a2")), llvm::None);
  563. // Empty macro expansions.
  564. recordTokens(R"cpp(
  565. #define EMPTY
  566. #define ID(X) X
  567. EMPTY EMPTY ID(1 2 3) EMPTY EMPTY split1
  568. EMPTY EMPTY ID(4 5 6) split2
  569. ID(7 8 9) EMPTY EMPTY
  570. )cpp");
  571. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("1 2 3")),
  572. ValueIs(SameRange(findSpelled("ID ( 1 2 3 )"))));
  573. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("4 5 6")),
  574. ValueIs(SameRange(findSpelled("ID ( 4 5 6 )"))));
  575. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("7 8 9")),
  576. ValueIs(SameRange(findSpelled("ID ( 7 8 9 )"))));
  577. // Empty mappings coming from various directives.
  578. recordTokens(R"cpp(
  579. #define ID(X) X
  580. ID(1)
  581. #pragma lalala
  582. not_mapped
  583. )cpp");
  584. EXPECT_THAT(Buffer.spelledForExpanded(findExpanded("not_mapped")),
  585. ValueIs(SameRange(findSpelled("not_mapped"))));
  586. }
  587. TEST_F(TokenBufferTest, TokensToFileRange) {
  588. addFile("./foo.h", "token_from_header");
  589. llvm::Annotations Code(R"cpp(
  590. #define FOO token_from_expansion
  591. #include "./foo.h"
  592. $all[[$i[[int]] a = FOO;]]
  593. )cpp");
  594. recordTokens(Code.code());
  595. auto &SM = *SourceMgr;
  596. // Two simple examples.
  597. auto Int = findExpanded("int").front();
  598. auto Semi = findExpanded(";").front();
  599. EXPECT_EQ(Int.range(SM), FileRange(SM.getMainFileID(), Code.range("i").Begin,
  600. Code.range("i").End));
  601. EXPECT_EQ(syntax::Token::range(SM, Int, Semi),
  602. FileRange(SM.getMainFileID(), Code.range("all").Begin,
  603. Code.range("all").End));
  604. // We don't test assertion failures because death tests are slow.
  605. }
  606. } // namespace