LexerTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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) override { }
  36. GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
  37. { return nullptr; }
  38. bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
  39. { return 0; };
  40. };
  41. // The test fixture.
  42. class LexerTest : public ::testing::Test {
  43. protected:
  44. LexerTest()
  45. : FileMgr(FileMgrOpts),
  46. DiagID(new DiagnosticIDs()),
  47. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  48. SourceMgr(Diags, FileMgr),
  49. TargetOpts(new TargetOptions)
  50. {
  51. TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
  52. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  53. }
  54. std::vector<Token> CheckLex(StringRef Source,
  55. ArrayRef<tok::TokenKind> ExpectedTokens) {
  56. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
  57. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
  58. VoidModuleLoader ModLoader;
  59. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  60. Target.get());
  61. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  62. HeaderInfo, ModLoader, /*IILookup =*/nullptr,
  63. /*OwnsHeaderSearch =*/false);
  64. PP.Initialize(*Target);
  65. PP.EnterMainSourceFile();
  66. std::vector<Token> toks;
  67. while (1) {
  68. Token tok;
  69. PP.Lex(tok);
  70. if (tok.is(tok::eof))
  71. break;
  72. toks.push_back(tok);
  73. }
  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. std::pair<SourceLocation,SourceLocation>
  246. macroPair = SourceMgr.getExpansionRange(lsqrLoc);
  247. SourceRange macroRange = SourceRange(macroPair.first, macroPair.second);
  248. SourceLocation Loc;
  249. EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
  250. EXPECT_EQ(Loc, macroRange.getBegin());
  251. EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  252. EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
  253. EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
  254. EXPECT_EQ(Loc, macroRange.getEnd());
  255. CharSourceRange range = Lexer::makeFileCharRange(
  256. CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts);
  257. EXPECT_TRUE(range.isInvalid());
  258. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc),
  259. SourceMgr, LangOpts);
  260. EXPECT_TRUE(range.isInvalid());
  261. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  262. SourceMgr, LangOpts);
  263. EXPECT_TRUE(!range.isTokenRange());
  264. EXPECT_EQ(range.getAsRange(),
  265. SourceRange(macroRange.getBegin(),
  266. macroRange.getEnd().getLocWithOffset(1)));
  267. StringRef text = Lexer::getSourceText(
  268. CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
  269. SourceMgr, LangOpts);
  270. EXPECT_EQ(text, "M(foo)");
  271. SourceLocation macroLsqrLoc = toks[3].getLocation();
  272. SourceLocation macroIdLoc = toks[4].getLocation();
  273. SourceLocation macroRsqrLoc = toks[5].getLocation();
  274. SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
  275. SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
  276. SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
  277. range = Lexer::makeFileCharRange(
  278. CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc),
  279. SourceMgr, LangOpts);
  280. EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
  281. range.getAsRange());
  282. range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc),
  283. SourceMgr, LangOpts);
  284. EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
  285. range.getAsRange());
  286. macroPair = SourceMgr.getExpansionRange(macroLsqrLoc);
  287. range = Lexer::makeFileCharRange(
  288. CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc),
  289. SourceMgr, LangOpts);
  290. EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)),
  291. range.getAsRange());
  292. text = Lexer::getSourceText(
  293. CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
  294. SourceMgr, LangOpts);
  295. EXPECT_EQ(text, "[bar");
  296. SourceLocation idLoc1 = toks[6].getLocation();
  297. SourceLocation idLoc2 = toks[7].getLocation();
  298. SourceLocation idLoc3 = toks[8].getLocation();
  299. SourceLocation idLoc4 = toks[9].getLocation();
  300. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts));
  301. EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts));
  302. EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts));
  303. EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
  304. }
  305. } // anonymous namespace