LexerTest.cpp 14 KB

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