LexerTest.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. //===- unittests/Lex/LexerTest.cpp ------ Lexer tests ---------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/Lex/Lexer.h"
  10. #include "clang/Basic/Diagnostic.h"
  11. #include "clang/Basic/DiagnosticOptions.h"
  12. #include "clang/Basic/FileManager.h"
  13. #include "clang/Basic/LangOptions.h"
  14. #include "clang/Basic/MemoryBufferCache.h"
  15. #include "clang/Basic/SourceManager.h"
  16. #include "clang/Basic/TargetInfo.h"
  17. #include "clang/Basic/TargetOptions.h"
  18. #include "clang/Lex/HeaderSearch.h"
  19. #include "clang/Lex/HeaderSearchOptions.h"
  20. #include "clang/Lex/MacroArgs.h"
  21. #include "clang/Lex/MacroInfo.h"
  22. #include "clang/Lex/ModuleLoader.h"
  23. #include "clang/Lex/Preprocessor.h"
  24. #include "clang/Lex/PreprocessorOptions.h"
  25. #include "gtest/gtest.h"
  26. using namespace clang;
  27. namespace {
  28. // The test fixture.
  29. class LexerTest : public ::testing::Test {
  30. protected:
  31. LexerTest()
  32. : FileMgr(FileMgrOpts),
  33. DiagID(new DiagnosticIDs()),
  34. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  35. SourceMgr(Diags, FileMgr),
  36. TargetOpts(new TargetOptions)
  37. {
  38. TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
  39. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  40. }
  41. std::unique_ptr<Preprocessor> CreatePP(StringRef Source,
  42. TrivialModuleLoader &ModLoader) {
  43. std::unique_ptr<llvm::MemoryBuffer> Buf =
  44. llvm::MemoryBuffer::getMemBuffer(Source);
  45. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
  46. MemoryBufferCache PCMCache;
  47. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  48. Diags, LangOpts, Target.get());
  49. std::unique_ptr<Preprocessor> PP = llvm::make_unique<Preprocessor>(
  50. std::make_shared<PreprocessorOptions>(), Diags, LangOpts, SourceMgr,
  51. PCMCache, HeaderInfo, ModLoader,
  52. /*IILookup =*/nullptr,
  53. /*OwnsHeaderSearch =*/false);
  54. PP->Initialize(*Target);
  55. PP->EnterMainSourceFile();
  56. return PP;
  57. }
  58. std::vector<Token> Lex(StringRef Source) {
  59. TrivialModuleLoader ModLoader;
  60. auto PP = CreatePP(Source, ModLoader);
  61. std::vector<Token> toks;
  62. while (1) {
  63. Token tok;
  64. PP->Lex(tok);
  65. if (tok.is(tok::eof))
  66. break;
  67. toks.push_back(tok);
  68. }
  69. return toks;
  70. }
  71. std::vector<Token> CheckLex(StringRef Source,
  72. ArrayRef<tok::TokenKind> ExpectedTokens) {
  73. auto toks = Lex(Source);
  74. EXPECT_EQ(ExpectedTokens.size(), toks.size());
  75. for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
  76. EXPECT_EQ(ExpectedTokens[i], toks[i].getKind());
  77. }
  78. return toks;
  79. }
  80. std::string getSourceText(Token Begin, Token End) {
  81. bool Invalid;
  82. StringRef Str =
  83. Lexer::getSourceText(CharSourceRange::getTokenRange(SourceRange(
  84. Begin.getLocation(), End.getLocation())),
  85. SourceMgr, LangOpts, &Invalid);
  86. if (Invalid)
  87. return "<INVALID>";
  88. return Str;
  89. }
  90. FileSystemOptions FileMgrOpts;
  91. FileManager FileMgr;
  92. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  93. DiagnosticsEngine Diags;
  94. SourceManager SourceMgr;
  95. LangOptions LangOpts;
  96. std::shared_ptr<TargetOptions> TargetOpts;
  97. IntrusiveRefCntPtr<TargetInfo> Target;
  98. };
  99. TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgument) {
  100. std::vector<tok::TokenKind> ExpectedTokens;
  101. ExpectedTokens.push_back(tok::identifier);
  102. ExpectedTokens.push_back(tok::l_paren);
  103. ExpectedTokens.push_back(tok::identifier);
  104. ExpectedTokens.push_back(tok::r_paren);
  105. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  106. "M(f(M(i)))",
  107. ExpectedTokens);
  108. EXPECT_EQ("M(i)", getSourceText(toks[2], toks[2]));
  109. }
  110. TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro) {
  111. std::vector<tok::TokenKind> ExpectedTokens;
  112. ExpectedTokens.push_back(tok::identifier);
  113. ExpectedTokens.push_back(tok::identifier);
  114. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  115. "M(M(i) c)",
  116. ExpectedTokens);
  117. EXPECT_EQ("M(i)", getSourceText(toks[0], toks[0]));
  118. }
  119. TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForBeginOfMacro) {
  120. std::vector<tok::TokenKind> ExpectedTokens;
  121. ExpectedTokens.push_back(tok::identifier);
  122. ExpectedTokens.push_back(tok::identifier);
  123. ExpectedTokens.push_back(tok::identifier);
  124. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  125. "M(c c M(i))",
  126. ExpectedTokens);
  127. EXPECT_EQ("c M(i)", getSourceText(toks[1], toks[2]));
  128. }
  129. TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForEndOfMacro) {
  130. std::vector<tok::TokenKind> ExpectedTokens;
  131. ExpectedTokens.push_back(tok::identifier);
  132. ExpectedTokens.push_back(tok::identifier);
  133. ExpectedTokens.push_back(tok::identifier);
  134. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  135. "M(M(i) c c)",
  136. ExpectedTokens);
  137. EXPECT_EQ("M(i) c", getSourceText(toks[0], toks[1]));
  138. }
  139. TEST_F(LexerTest, GetSourceTextInSeparateFnMacros) {
  140. std::vector<tok::TokenKind> ExpectedTokens;
  141. ExpectedTokens.push_back(tok::identifier);
  142. ExpectedTokens.push_back(tok::identifier);
  143. ExpectedTokens.push_back(tok::identifier);
  144. ExpectedTokens.push_back(tok::identifier);
  145. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  146. "M(c M(i)) M(M(i) c)",
  147. ExpectedTokens);
  148. EXPECT_EQ("<INVALID>", getSourceText(toks[1], toks[2]));
  149. }
  150. TEST_F(LexerTest, GetSourceTextWorksAcrossTokenPastes) {
  151. std::vector<tok::TokenKind> ExpectedTokens;
  152. ExpectedTokens.push_back(tok::identifier);
  153. ExpectedTokens.push_back(tok::l_paren);
  154. ExpectedTokens.push_back(tok::identifier);
  155. ExpectedTokens.push_back(tok::r_paren);
  156. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  157. "#define C(x) M(x##c)\n"
  158. "M(f(C(i)))",
  159. ExpectedTokens);
  160. EXPECT_EQ("C(i)", getSourceText(toks[2], toks[2]));
  161. }
  162. TEST_F(LexerTest, GetSourceTextExpandsAcrossMultipleMacroCalls) {
  163. std::vector<tok::TokenKind> ExpectedTokens;
  164. ExpectedTokens.push_back(tok::identifier);
  165. ExpectedTokens.push_back(tok::l_paren);
  166. ExpectedTokens.push_back(tok::identifier);
  167. ExpectedTokens.push_back(tok::r_paren);
  168. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  169. "f(M(M(i)))",
  170. ExpectedTokens);
  171. EXPECT_EQ("M(M(i))", getSourceText(toks[2], toks[2]));
  172. }
  173. TEST_F(LexerTest, GetSourceTextInMiddleOfMacroArgument) {
  174. std::vector<tok::TokenKind> ExpectedTokens;
  175. ExpectedTokens.push_back(tok::identifier);
  176. ExpectedTokens.push_back(tok::l_paren);
  177. ExpectedTokens.push_back(tok::identifier);
  178. ExpectedTokens.push_back(tok::r_paren);
  179. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  180. "M(f(i))",
  181. ExpectedTokens);
  182. EXPECT_EQ("i", getSourceText(toks[2], toks[2]));
  183. }
  184. TEST_F(LexerTest, GetSourceTextExpandsAroundDifferentMacroCalls) {
  185. std::vector<tok::TokenKind> ExpectedTokens;
  186. ExpectedTokens.push_back(tok::identifier);
  187. ExpectedTokens.push_back(tok::l_paren);
  188. ExpectedTokens.push_back(tok::identifier);
  189. ExpectedTokens.push_back(tok::r_paren);
  190. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  191. "#define C(x) x\n"
  192. "f(C(M(i)))",
  193. ExpectedTokens);
  194. EXPECT_EQ("C(M(i))", getSourceText(toks[2], toks[2]));
  195. }
  196. TEST_F(LexerTest, GetSourceTextOnlyExpandsIfFirstTokenInMacro) {
  197. std::vector<tok::TokenKind> ExpectedTokens;
  198. ExpectedTokens.push_back(tok::identifier);
  199. ExpectedTokens.push_back(tok::l_paren);
  200. ExpectedTokens.push_back(tok::identifier);
  201. ExpectedTokens.push_back(tok::identifier);
  202. ExpectedTokens.push_back(tok::r_paren);
  203. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  204. "#define C(x) c x\n"
  205. "f(C(M(i)))",
  206. ExpectedTokens);
  207. EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
  208. }
  209. TEST_F(LexerTest, GetSourceTextExpandsRecursively) {
  210. std::vector<tok::TokenKind> ExpectedTokens;
  211. ExpectedTokens.push_back(tok::identifier);
  212. ExpectedTokens.push_back(tok::identifier);
  213. ExpectedTokens.push_back(tok::l_paren);
  214. ExpectedTokens.push_back(tok::identifier);
  215. ExpectedTokens.push_back(tok::r_paren);
  216. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  217. "#define C(x) c M(x)\n"
  218. "C(f(M(i)))",
  219. ExpectedTokens);
  220. EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
  221. }
  222. TEST_F(LexerTest, LexAPI) {
  223. std::vector<tok::TokenKind> ExpectedTokens;
  224. ExpectedTokens.push_back(tok::l_square);
  225. ExpectedTokens.push_back(tok::identifier);
  226. ExpectedTokens.push_back(tok::r_square);
  227. ExpectedTokens.push_back(tok::l_square);
  228. ExpectedTokens.push_back(tok::identifier);
  229. ExpectedTokens.push_back(tok::r_square);
  230. ExpectedTokens.push_back(tok::identifier);
  231. ExpectedTokens.push_back(tok::identifier);
  232. ExpectedTokens.push_back(tok::identifier);
  233. ExpectedTokens.push_back(tok::identifier);
  234. std::vector<Token> toks = CheckLex("#define M(x) [x]\n"
  235. "#define N(x) x\n"
  236. "#define INN(x) x\n"
  237. "#define NOF1 INN(val)\n"
  238. "#define NOF2 val\n"
  239. "M(foo) N([bar])\n"
  240. "N(INN(val)) N(NOF1) N(NOF2) N(val)",
  241. ExpectedTokens);
  242. SourceLocation lsqrLoc = toks[0].getLocation();
  243. SourceLocation idLoc = toks[1].getLocation();
  244. SourceLocation rsqrLoc = toks[2].getLocation();
  245. CharSourceRange macroRange = SourceMgr.getExpansionRange(lsqrLoc);
  246. SourceLocation Loc;
  247. EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
  248. EXPECT_EQ(Loc, macroRange.getBegin());
  249. EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  250. EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  251. EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
  252. EXPECT_EQ(Loc, macroRange.getEnd());
  253. EXPECT_TRUE(macroRange.isTokenRange());
  254. CharSourceRange range = Lexer::makeFileCharRange(
  255. CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts);
  256. EXPECT_TRUE(range.isInvalid());
  257. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc),
  258. SourceMgr, LangOpts);
  259. EXPECT_TRUE(range.isInvalid());
  260. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  261. SourceMgr, LangOpts);
  262. EXPECT_TRUE(!range.isTokenRange());
  263. EXPECT_EQ(range.getAsRange(),
  264. SourceRange(macroRange.getBegin(),
  265. macroRange.getEnd().getLocWithOffset(1)));
  266. StringRef text = Lexer::getSourceText(
  267. CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  268. SourceMgr, LangOpts);
  269. EXPECT_EQ(text, "M(foo)");
  270. SourceLocation macroLsqrLoc = toks[3].getLocation();
  271. SourceLocation macroIdLoc = toks[4].getLocation();
  272. SourceLocation macroRsqrLoc = toks[5].getLocation();
  273. SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
  274. SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
  275. SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
  276. range = Lexer::makeFileCharRange(
  277. CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc),
  278. SourceMgr, LangOpts);
  279. EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
  280. range.getAsRange());
  281. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc),
  282. SourceMgr, LangOpts);
  283. EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
  284. range.getAsRange());
  285. macroRange = SourceMgr.getExpansionRange(macroLsqrLoc);
  286. range = Lexer::makeFileCharRange(
  287. CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc),
  288. SourceMgr, LangOpts);
  289. EXPECT_EQ(SourceRange(macroRange.getBegin(), macroRange.getEnd().getLocWithOffset(1)),
  290. range.getAsRange());
  291. text = Lexer::getSourceText(
  292. CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
  293. SourceMgr, LangOpts);
  294. EXPECT_EQ(text, "[bar");
  295. SourceLocation idLoc1 = toks[6].getLocation();
  296. SourceLocation idLoc2 = toks[7].getLocation();
  297. SourceLocation idLoc3 = toks[8].getLocation();
  298. SourceLocation idLoc4 = toks[9].getLocation();
  299. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts));
  300. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts));
  301. EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts));
  302. EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
  303. }
  304. TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) {
  305. std::vector<Token> toks =
  306. Lex("#define helper1 0\n"
  307. "void helper2(const char *, ...);\n"
  308. "#define M1(a, ...) helper2(a, ##__VA_ARGS__)\n"
  309. "#define M2(a, ...) M1(a, helper1, ##__VA_ARGS__)\n"
  310. "void f1() { M2(\"a\", \"b\"); }");
  311. // Check the file corresponding to the "helper1" macro arg in M2.
  312. //
  313. // The lexer used to report its size as 31, meaning that the end of the
  314. // expansion would be on the *next line* (just past `M2("a", "b")`). Make
  315. // sure that we get the correct end location (the comma after "helper1").
  316. SourceLocation helper1ArgLoc = toks[20].getLocation();
  317. EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U);
  318. }
  319. TEST_F(LexerTest, DontOverallocateStringifyArgs) {
  320. TrivialModuleLoader ModLoader;
  321. auto PP = CreatePP("\"StrArg\", 5, 'C'", ModLoader);
  322. llvm::BumpPtrAllocator Allocator;
  323. std::array<IdentifierInfo *, 3> ParamList;
  324. MacroInfo *MI = PP->AllocateMacroInfo({});
  325. MI->setIsFunctionLike();
  326. MI->setParameterList(ParamList, Allocator);
  327. EXPECT_EQ(3u, MI->getNumParams());
  328. EXPECT_TRUE(MI->isFunctionLike());
  329. Token Eof;
  330. Eof.setKind(tok::eof);
  331. std::vector<Token> ArgTokens;
  332. while (1) {
  333. Token tok;
  334. PP->Lex(tok);
  335. if (tok.is(tok::eof)) {
  336. ArgTokens.push_back(Eof);
  337. break;
  338. }
  339. if (tok.is(tok::comma))
  340. ArgTokens.push_back(Eof);
  341. else
  342. ArgTokens.push_back(tok);
  343. }
  344. auto MacroArgsDeleter = [&PP](MacroArgs *M) { M->destroy(*PP); };
  345. std::unique_ptr<MacroArgs, decltype(MacroArgsDeleter)> MA(
  346. MacroArgs::create(MI, ArgTokens, false, *PP), MacroArgsDeleter);
  347. Token Result = MA->getStringifiedArgument(0, *PP, {}, {});
  348. EXPECT_EQ(tok::string_literal, Result.getKind());
  349. EXPECT_STREQ("\"\\\"StrArg\\\"\"", Result.getLiteralData());
  350. Result = MA->getStringifiedArgument(1, *PP, {}, {});
  351. EXPECT_EQ(tok::string_literal, Result.getKind());
  352. EXPECT_STREQ("\"5\"", Result.getLiteralData());
  353. Result = MA->getStringifiedArgument(2, *PP, {}, {});
  354. EXPECT_EQ(tok::string_literal, Result.getKind());
  355. EXPECT_STREQ("\"'C'\"", Result.getLiteralData());
  356. #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
  357. EXPECT_DEATH(MA->getStringifiedArgument(3, *PP, {}, {}),
  358. "Invalid argument number!");
  359. #endif
  360. }
  361. TEST_F(LexerTest, IsNewLineEscapedValid) {
  362. auto hasNewLineEscaped = [](const char *S) {
  363. return Lexer::isNewLineEscaped(S, S + strlen(S) - 1);
  364. };
  365. EXPECT_TRUE(hasNewLineEscaped("\\\r"));
  366. EXPECT_TRUE(hasNewLineEscaped("\\\n"));
  367. EXPECT_TRUE(hasNewLineEscaped("\\\r\n"));
  368. EXPECT_TRUE(hasNewLineEscaped("\\\n\r"));
  369. EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r"));
  370. EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r\n"));
  371. EXPECT_FALSE(hasNewLineEscaped("\\\r\r"));
  372. EXPECT_FALSE(hasNewLineEscaped("\\\r\r\n"));
  373. EXPECT_FALSE(hasNewLineEscaped("\\\n\n"));
  374. EXPECT_FALSE(hasNewLineEscaped("\r"));
  375. EXPECT_FALSE(hasNewLineEscaped("\n"));
  376. EXPECT_FALSE(hasNewLineEscaped("\r\n"));
  377. EXPECT_FALSE(hasNewLineEscaped("\n\r"));
  378. EXPECT_FALSE(hasNewLineEscaped("\r\r"));
  379. EXPECT_FALSE(hasNewLineEscaped("\n\n"));
  380. }
  381. TEST_F(LexerTest, GetBeginningOfTokenWithEscapedNewLine) {
  382. // Each line should have the same length for
  383. // further offset calculation to be more straightforward.
  384. const unsigned IdentifierLength = 8;
  385. std::string TextToLex = "rabarbar\n"
  386. "foo\\\nbar\n"
  387. "foo\\\rbar\n"
  388. "fo\\\r\nbar\n"
  389. "foo\\\n\rba\n";
  390. std::vector<tok::TokenKind> ExpectedTokens{5, tok::identifier};
  391. std::vector<Token> LexedTokens = CheckLex(TextToLex, ExpectedTokens);
  392. for (const Token &Tok : LexedTokens) {
  393. std::pair<FileID, unsigned> OriginalLocation =
  394. SourceMgr.getDecomposedLoc(Tok.getLocation());
  395. for (unsigned Offset = 0; Offset < IdentifierLength; ++Offset) {
  396. SourceLocation LookupLocation =
  397. Tok.getLocation().getLocWithOffset(Offset);
  398. std::pair<FileID, unsigned> FoundLocation =
  399. SourceMgr.getDecomposedExpansionLoc(
  400. Lexer::GetBeginningOfToken(LookupLocation, SourceMgr, LangOpts));
  401. // Check that location returned by the GetBeginningOfToken
  402. // is the same as original token location reported by Lexer.
  403. EXPECT_EQ(FoundLocation.second, OriginalLocation.second);
  404. }
  405. }
  406. }
  407. TEST_F(LexerTest, AvoidPastEndOfStringDereference) {
  408. EXPECT_TRUE(Lex(" // \\\n").empty());
  409. EXPECT_TRUE(Lex("#include <\\\\").empty());
  410. EXPECT_TRUE(Lex("#include <\\\\\n").empty());
  411. }
  412. TEST_F(LexerTest, StringizingRasString) {
  413. // For "std::string Lexer::Stringify(StringRef Str, bool Charify)".
  414. std::string String1 = R"(foo
  415. {"bar":[]}
  416. baz)";
  417. // For "void Lexer::Stringify(SmallVectorImpl<char> &Str)".
  418. SmallString<128> String2;
  419. String2 += String1.c_str();
  420. // Corner cases.
  421. std::string String3 = R"(\
  422. \n
  423. \\n
  424. \\)";
  425. SmallString<128> String4;
  426. String4 += String3.c_str();
  427. std::string String5 = R"(a\
  428. \\b)";
  429. SmallString<128> String6;
  430. String6 += String5.c_str();
  431. String1 = Lexer::Stringify(StringRef(String1));
  432. Lexer::Stringify(String2);
  433. String3 = Lexer::Stringify(StringRef(String3));
  434. Lexer::Stringify(String4);
  435. String5 = Lexer::Stringify(StringRef(String5));
  436. Lexer::Stringify(String6);
  437. EXPECT_EQ(String1, R"(foo\n {\"bar\":[]}\n baz)");
  438. EXPECT_EQ(String2, R"(foo\n {\"bar\":[]}\n baz)");
  439. EXPECT_EQ(String3, R"(\\\n \\n\n \\\\n\n \\\\)");
  440. EXPECT_EQ(String4, R"(\\\n \\n\n \\\\n\n \\\\)");
  441. EXPECT_EQ(String5, R"(a\\\n\n\n \\\\b)");
  442. EXPECT_EQ(String6, R"(a\\\n\n\n \\\\b)");
  443. }
  444. } // anonymous namespace