LexerTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/ModuleLoader.h"
  21. #include "clang/Lex/Preprocessor.h"
  22. #include "clang/Lex/PreprocessorOptions.h"
  23. #include "gtest/gtest.h"
  24. using namespace clang;
  25. namespace {
  26. // The test fixture.
  27. class LexerTest : public ::testing::Test {
  28. protected:
  29. LexerTest()
  30. : FileMgr(FileMgrOpts),
  31. DiagID(new DiagnosticIDs()),
  32. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  33. SourceMgr(Diags, FileMgr),
  34. TargetOpts(new TargetOptions)
  35. {
  36. TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
  37. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  38. }
  39. std::vector<Token> Lex(StringRef Source) {
  40. std::unique_ptr<llvm::MemoryBuffer> Buf =
  41. llvm::MemoryBuffer::getMemBuffer(Source);
  42. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
  43. TrivialModuleLoader ModLoader;
  44. MemoryBufferCache PCMCache;
  45. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  46. Diags, LangOpts, Target.get());
  47. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  48. SourceMgr, PCMCache, HeaderInfo, ModLoader,
  49. /*IILookup =*/nullptr,
  50. /*OwnsHeaderSearch =*/false);
  51. PP.Initialize(*Target);
  52. PP.EnterMainSourceFile();
  53. std::vector<Token> toks;
  54. while (1) {
  55. Token tok;
  56. PP.Lex(tok);
  57. if (tok.is(tok::eof))
  58. break;
  59. toks.push_back(tok);
  60. }
  61. return toks;
  62. }
  63. std::vector<Token> CheckLex(StringRef Source,
  64. ArrayRef<tok::TokenKind> ExpectedTokens) {
  65. auto toks = Lex(Source);
  66. EXPECT_EQ(ExpectedTokens.size(), toks.size());
  67. for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
  68. EXPECT_EQ(ExpectedTokens[i], toks[i].getKind());
  69. }
  70. return toks;
  71. }
  72. std::string getSourceText(Token Begin, Token End) {
  73. bool Invalid;
  74. StringRef Str =
  75. Lexer::getSourceText(CharSourceRange::getTokenRange(SourceRange(
  76. Begin.getLocation(), End.getLocation())),
  77. SourceMgr, LangOpts, &Invalid);
  78. if (Invalid)
  79. return "<INVALID>";
  80. return Str;
  81. }
  82. FileSystemOptions FileMgrOpts;
  83. FileManager FileMgr;
  84. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  85. DiagnosticsEngine Diags;
  86. SourceManager SourceMgr;
  87. LangOptions LangOpts;
  88. std::shared_ptr<TargetOptions> TargetOpts;
  89. IntrusiveRefCntPtr<TargetInfo> Target;
  90. };
  91. TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgument) {
  92. std::vector<tok::TokenKind> ExpectedTokens;
  93. ExpectedTokens.push_back(tok::identifier);
  94. ExpectedTokens.push_back(tok::l_paren);
  95. ExpectedTokens.push_back(tok::identifier);
  96. ExpectedTokens.push_back(tok::r_paren);
  97. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  98. "M(f(M(i)))",
  99. ExpectedTokens);
  100. EXPECT_EQ("M(i)", getSourceText(toks[2], toks[2]));
  101. }
  102. TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro) {
  103. std::vector<tok::TokenKind> ExpectedTokens;
  104. ExpectedTokens.push_back(tok::identifier);
  105. ExpectedTokens.push_back(tok::identifier);
  106. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  107. "M(M(i) c)",
  108. ExpectedTokens);
  109. EXPECT_EQ("M(i)", getSourceText(toks[0], toks[0]));
  110. }
  111. TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForBeginOfMacro) {
  112. std::vector<tok::TokenKind> ExpectedTokens;
  113. ExpectedTokens.push_back(tok::identifier);
  114. ExpectedTokens.push_back(tok::identifier);
  115. ExpectedTokens.push_back(tok::identifier);
  116. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  117. "M(c c M(i))",
  118. ExpectedTokens);
  119. EXPECT_EQ("c M(i)", getSourceText(toks[1], toks[2]));
  120. }
  121. TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForEndOfMacro) {
  122. std::vector<tok::TokenKind> ExpectedTokens;
  123. ExpectedTokens.push_back(tok::identifier);
  124. ExpectedTokens.push_back(tok::identifier);
  125. ExpectedTokens.push_back(tok::identifier);
  126. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  127. "M(M(i) c c)",
  128. ExpectedTokens);
  129. EXPECT_EQ("M(i) c", getSourceText(toks[0], toks[1]));
  130. }
  131. TEST_F(LexerTest, GetSourceTextInSeparateFnMacros) {
  132. std::vector<tok::TokenKind> ExpectedTokens;
  133. ExpectedTokens.push_back(tok::identifier);
  134. ExpectedTokens.push_back(tok::identifier);
  135. ExpectedTokens.push_back(tok::identifier);
  136. ExpectedTokens.push_back(tok::identifier);
  137. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  138. "M(c M(i)) M(M(i) c)",
  139. ExpectedTokens);
  140. EXPECT_EQ("<INVALID>", getSourceText(toks[1], toks[2]));
  141. }
  142. TEST_F(LexerTest, GetSourceTextWorksAcrossTokenPastes) {
  143. std::vector<tok::TokenKind> ExpectedTokens;
  144. ExpectedTokens.push_back(tok::identifier);
  145. ExpectedTokens.push_back(tok::l_paren);
  146. ExpectedTokens.push_back(tok::identifier);
  147. ExpectedTokens.push_back(tok::r_paren);
  148. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  149. "#define C(x) M(x##c)\n"
  150. "M(f(C(i)))",
  151. ExpectedTokens);
  152. EXPECT_EQ("C(i)", getSourceText(toks[2], toks[2]));
  153. }
  154. TEST_F(LexerTest, GetSourceTextExpandsAcrossMultipleMacroCalls) {
  155. std::vector<tok::TokenKind> ExpectedTokens;
  156. ExpectedTokens.push_back(tok::identifier);
  157. ExpectedTokens.push_back(tok::l_paren);
  158. ExpectedTokens.push_back(tok::identifier);
  159. ExpectedTokens.push_back(tok::r_paren);
  160. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  161. "f(M(M(i)))",
  162. ExpectedTokens);
  163. EXPECT_EQ("M(M(i))", getSourceText(toks[2], toks[2]));
  164. }
  165. TEST_F(LexerTest, GetSourceTextInMiddleOfMacroArgument) {
  166. std::vector<tok::TokenKind> ExpectedTokens;
  167. ExpectedTokens.push_back(tok::identifier);
  168. ExpectedTokens.push_back(tok::l_paren);
  169. ExpectedTokens.push_back(tok::identifier);
  170. ExpectedTokens.push_back(tok::r_paren);
  171. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  172. "M(f(i))",
  173. ExpectedTokens);
  174. EXPECT_EQ("i", getSourceText(toks[2], toks[2]));
  175. }
  176. TEST_F(LexerTest, GetSourceTextExpandsAroundDifferentMacroCalls) {
  177. std::vector<tok::TokenKind> ExpectedTokens;
  178. ExpectedTokens.push_back(tok::identifier);
  179. ExpectedTokens.push_back(tok::l_paren);
  180. ExpectedTokens.push_back(tok::identifier);
  181. ExpectedTokens.push_back(tok::r_paren);
  182. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  183. "#define C(x) x\n"
  184. "f(C(M(i)))",
  185. ExpectedTokens);
  186. EXPECT_EQ("C(M(i))", getSourceText(toks[2], toks[2]));
  187. }
  188. TEST_F(LexerTest, GetSourceTextOnlyExpandsIfFirstTokenInMacro) {
  189. std::vector<tok::TokenKind> ExpectedTokens;
  190. ExpectedTokens.push_back(tok::identifier);
  191. ExpectedTokens.push_back(tok::l_paren);
  192. ExpectedTokens.push_back(tok::identifier);
  193. ExpectedTokens.push_back(tok::identifier);
  194. ExpectedTokens.push_back(tok::r_paren);
  195. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  196. "#define C(x) c x\n"
  197. "f(C(M(i)))",
  198. ExpectedTokens);
  199. EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
  200. }
  201. TEST_F(LexerTest, GetSourceTextExpandsRecursively) {
  202. std::vector<tok::TokenKind> ExpectedTokens;
  203. ExpectedTokens.push_back(tok::identifier);
  204. ExpectedTokens.push_back(tok::identifier);
  205. ExpectedTokens.push_back(tok::l_paren);
  206. ExpectedTokens.push_back(tok::identifier);
  207. ExpectedTokens.push_back(tok::r_paren);
  208. std::vector<Token> toks = CheckLex("#define M(x) x\n"
  209. "#define C(x) c M(x)\n"
  210. "C(f(M(i)))",
  211. ExpectedTokens);
  212. EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
  213. }
  214. TEST_F(LexerTest, LexAPI) {
  215. std::vector<tok::TokenKind> ExpectedTokens;
  216. ExpectedTokens.push_back(tok::l_square);
  217. ExpectedTokens.push_back(tok::identifier);
  218. ExpectedTokens.push_back(tok::r_square);
  219. ExpectedTokens.push_back(tok::l_square);
  220. ExpectedTokens.push_back(tok::identifier);
  221. ExpectedTokens.push_back(tok::r_square);
  222. ExpectedTokens.push_back(tok::identifier);
  223. ExpectedTokens.push_back(tok::identifier);
  224. ExpectedTokens.push_back(tok::identifier);
  225. ExpectedTokens.push_back(tok::identifier);
  226. std::vector<Token> toks = CheckLex("#define M(x) [x]\n"
  227. "#define N(x) x\n"
  228. "#define INN(x) x\n"
  229. "#define NOF1 INN(val)\n"
  230. "#define NOF2 val\n"
  231. "M(foo) N([bar])\n"
  232. "N(INN(val)) N(NOF1) N(NOF2) N(val)",
  233. ExpectedTokens);
  234. SourceLocation lsqrLoc = toks[0].getLocation();
  235. SourceLocation idLoc = toks[1].getLocation();
  236. SourceLocation rsqrLoc = toks[2].getLocation();
  237. std::pair<SourceLocation,SourceLocation>
  238. macroPair = SourceMgr.getExpansionRange(lsqrLoc);
  239. SourceRange macroRange = SourceRange(macroPair.first, macroPair.second);
  240. SourceLocation Loc;
  241. EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
  242. EXPECT_EQ(Loc, macroRange.getBegin());
  243. EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  244. EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  245. EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
  246. EXPECT_EQ(Loc, macroRange.getEnd());
  247. CharSourceRange range = Lexer::makeFileCharRange(
  248. CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts);
  249. EXPECT_TRUE(range.isInvalid());
  250. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc),
  251. SourceMgr, LangOpts);
  252. EXPECT_TRUE(range.isInvalid());
  253. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  254. SourceMgr, LangOpts);
  255. EXPECT_TRUE(!range.isTokenRange());
  256. EXPECT_EQ(range.getAsRange(),
  257. SourceRange(macroRange.getBegin(),
  258. macroRange.getEnd().getLocWithOffset(1)));
  259. StringRef text = Lexer::getSourceText(
  260. CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  261. SourceMgr, LangOpts);
  262. EXPECT_EQ(text, "M(foo)");
  263. SourceLocation macroLsqrLoc = toks[3].getLocation();
  264. SourceLocation macroIdLoc = toks[4].getLocation();
  265. SourceLocation macroRsqrLoc = toks[5].getLocation();
  266. SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
  267. SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
  268. SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
  269. range = Lexer::makeFileCharRange(
  270. CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc),
  271. SourceMgr, LangOpts);
  272. EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
  273. range.getAsRange());
  274. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc),
  275. SourceMgr, LangOpts);
  276. EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
  277. range.getAsRange());
  278. macroPair = SourceMgr.getExpansionRange(macroLsqrLoc);
  279. range = Lexer::makeFileCharRange(
  280. CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc),
  281. SourceMgr, LangOpts);
  282. EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)),
  283. range.getAsRange());
  284. text = Lexer::getSourceText(
  285. CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
  286. SourceMgr, LangOpts);
  287. EXPECT_EQ(text, "[bar");
  288. SourceLocation idLoc1 = toks[6].getLocation();
  289. SourceLocation idLoc2 = toks[7].getLocation();
  290. SourceLocation idLoc3 = toks[8].getLocation();
  291. SourceLocation idLoc4 = toks[9].getLocation();
  292. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts));
  293. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts));
  294. EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts));
  295. EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
  296. }
  297. TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) {
  298. std::vector<Token> toks =
  299. Lex("#define helper1 0\n"
  300. "void helper2(const char *, ...);\n"
  301. "#define M1(a, ...) helper2(a, ##__VA_ARGS__)\n"
  302. "#define M2(a, ...) M1(a, helper1, ##__VA_ARGS__)\n"
  303. "void f1() { M2(\"a\", \"b\"); }");
  304. // Check the file corresponding to the "helper1" macro arg in M2.
  305. //
  306. // The lexer used to report its size as 31, meaning that the end of the
  307. // expansion would be on the *next line* (just past `M2("a", "b")`). Make
  308. // sure that we get the correct end location (the comma after "helper1").
  309. SourceLocation helper1ArgLoc = toks[20].getLocation();
  310. EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U);
  311. }
  312. } // anonymous namespace