LexerTest.cpp 15 KB

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