SourceManagerTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. //===- unittests/Basic/SourceManagerTest.cpp ------ SourceManager 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/Basic/SourceManager.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/TargetInfo.h"
  15. #include "clang/Basic/TargetOptions.h"
  16. #include "clang/Lex/HeaderSearch.h"
  17. #include "clang/Lex/HeaderSearchOptions.h"
  18. #include "clang/Lex/ModuleLoader.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Lex/PreprocessorOptions.h"
  21. #include "llvm/ADT/SmallString.h"
  22. #include "llvm/Config/llvm-config.h"
  23. #include "gtest/gtest.h"
  24. using namespace clang;
  25. namespace {
  26. // The test fixture.
  27. class SourceManagerTest : public ::testing::Test {
  28. protected:
  29. SourceManagerTest()
  30. : FileMgr(FileMgrOpts),
  31. DiagID(new DiagnosticIDs()),
  32. Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
  33. SourceMgr(Diags, FileMgr),
  34. TargetOpts(new TargetOptions) {
  35. TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
  36. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  37. }
  38. FileSystemOptions FileMgrOpts;
  39. FileManager FileMgr;
  40. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  41. DiagnosticsEngine Diags;
  42. SourceManager SourceMgr;
  43. LangOptions LangOpts;
  44. std::shared_ptr<TargetOptions> TargetOpts;
  45. IntrusiveRefCntPtr<TargetInfo> Target;
  46. };
  47. class VoidModuleLoader : public ModuleLoader {
  48. ModuleLoadResult loadModule(SourceLocation ImportLoc,
  49. ModuleIdPath Path,
  50. Module::NameVisibilityKind Visibility,
  51. bool IsInclusionDirective) override {
  52. return ModuleLoadResult();
  53. }
  54. void makeModuleVisible(Module *Mod,
  55. Module::NameVisibilityKind Visibility,
  56. SourceLocation ImportLoc) override { }
  57. GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
  58. { return nullptr; }
  59. bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
  60. { return 0; }
  61. };
  62. TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
  63. const char *source =
  64. "#define M(x) [x]\n"
  65. "M(foo)";
  66. std::unique_ptr<llvm::MemoryBuffer> Buf =
  67. llvm::MemoryBuffer::getMemBuffer(source);
  68. FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
  69. SourceMgr.setMainFileID(mainFileID);
  70. VoidModuleLoader ModLoader;
  71. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  72. Diags, LangOpts, &*Target);
  73. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  74. SourceMgr, HeaderInfo, ModLoader,
  75. /*IILookup =*/nullptr,
  76. /*OwnsHeaderSearch =*/false);
  77. PP.Initialize(*Target);
  78. PP.EnterMainSourceFile();
  79. std::vector<Token> toks;
  80. while (1) {
  81. Token tok;
  82. PP.Lex(tok);
  83. if (tok.is(tok::eof))
  84. break;
  85. toks.push_back(tok);
  86. }
  87. // Make sure we got the tokens that we expected.
  88. ASSERT_EQ(3U, toks.size());
  89. ASSERT_EQ(tok::l_square, toks[0].getKind());
  90. ASSERT_EQ(tok::identifier, toks[1].getKind());
  91. ASSERT_EQ(tok::r_square, toks[2].getKind());
  92. SourceLocation lsqrLoc = toks[0].getLocation();
  93. SourceLocation idLoc = toks[1].getLocation();
  94. SourceLocation rsqrLoc = toks[2].getLocation();
  95. SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
  96. SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
  97. ASSERT_TRUE(macroExpStartLoc.isFileID());
  98. ASSERT_TRUE(macroExpEndLoc.isFileID());
  99. SmallString<32> str;
  100. ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
  101. ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
  102. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
  103. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
  104. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
  105. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
  106. }
  107. TEST_F(SourceManagerTest, getColumnNumber) {
  108. const char *Source =
  109. "int x;\n"
  110. "int y;";
  111. std::unique_ptr<llvm::MemoryBuffer> Buf =
  112. llvm::MemoryBuffer::getMemBuffer(Source);
  113. FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
  114. SourceMgr.setMainFileID(MainFileID);
  115. bool Invalid;
  116. Invalid = false;
  117. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
  118. EXPECT_TRUE(!Invalid);
  119. Invalid = false;
  120. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
  121. EXPECT_TRUE(!Invalid);
  122. Invalid = false;
  123. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
  124. EXPECT_TRUE(!Invalid);
  125. Invalid = false;
  126. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
  127. EXPECT_TRUE(!Invalid);
  128. Invalid = false;
  129. EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
  130. &Invalid));
  131. EXPECT_TRUE(!Invalid);
  132. Invalid = false;
  133. SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
  134. EXPECT_TRUE(Invalid);
  135. // Test invalid files
  136. Invalid = false;
  137. SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
  138. EXPECT_TRUE(Invalid);
  139. Invalid = false;
  140. SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
  141. EXPECT_TRUE(Invalid);
  142. // Test with no invalid flag.
  143. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
  144. }
  145. #if defined(LLVM_ON_UNIX)
  146. TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
  147. const char *header =
  148. "#define FM(x,y) x\n";
  149. const char *main =
  150. "#include \"/test-header.h\"\n"
  151. "#define VAL 0\n"
  152. "FM(VAL,0)\n"
  153. "FM(0,VAL)\n"
  154. "FM(FM(0,VAL),0)\n"
  155. "#define CONCAT(X, Y) X##Y\n"
  156. "CONCAT(1,1)\n";
  157. std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
  158. llvm::MemoryBuffer::getMemBuffer(header);
  159. std::unique_ptr<llvm::MemoryBuffer> MainBuf =
  160. llvm::MemoryBuffer::getMemBuffer(main);
  161. FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
  162. SourceMgr.setMainFileID(mainFileID);
  163. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  164. HeaderBuf->getBufferSize(), 0);
  165. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  166. VoidModuleLoader ModLoader;
  167. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  168. Diags, LangOpts, &*Target);
  169. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  170. SourceMgr, HeaderInfo, ModLoader,
  171. /*IILookup =*/nullptr,
  172. /*OwnsHeaderSearch =*/false);
  173. PP.Initialize(*Target);
  174. PP.EnterMainSourceFile();
  175. std::vector<Token> toks;
  176. while (1) {
  177. Token tok;
  178. PP.Lex(tok);
  179. if (tok.is(tok::eof))
  180. break;
  181. toks.push_back(tok);
  182. }
  183. // Make sure we got the tokens that we expected.
  184. ASSERT_EQ(4U, toks.size());
  185. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  186. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  187. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  188. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  189. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  190. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  191. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  192. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  193. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  194. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  195. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  196. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  197. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  198. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  199. EXPECT_TRUE(defLoc.isFileID());
  200. EXPECT_TRUE(loc1.isFileID());
  201. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  202. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  203. EXPECT_EQ(loc2, toks[1].getLocation());
  204. EXPECT_EQ(loc3, toks[2].getLocation());
  205. EXPECT_TRUE(defLoc2.isFileID());
  206. }
  207. namespace {
  208. struct MacroAction {
  209. SourceLocation Loc;
  210. std::string Name;
  211. bool isDefinition; // if false, it is expansion.
  212. MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
  213. : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
  214. };
  215. class MacroTracker : public PPCallbacks {
  216. std::vector<MacroAction> &Macros;
  217. public:
  218. explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
  219. void MacroDefined(const Token &MacroNameTok,
  220. const MacroDirective *MD) override {
  221. Macros.push_back(MacroAction(MD->getLocation(),
  222. MacroNameTok.getIdentifierInfo()->getName(),
  223. true));
  224. }
  225. void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
  226. SourceRange Range, const MacroArgs *Args) override {
  227. Macros.push_back(MacroAction(MacroNameTok.getLocation(),
  228. MacroNameTok.getIdentifierInfo()->getName(),
  229. false));
  230. }
  231. };
  232. }
  233. TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
  234. const char *header =
  235. "#define MACRO_IN_INCLUDE 0\n";
  236. const char *main =
  237. "#define M(x) x\n"
  238. "#define INC \"/test-header.h\"\n"
  239. "#include M(INC)\n"
  240. "#define INC2 </test-header.h>\n"
  241. "#include M(INC2)\n";
  242. std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
  243. llvm::MemoryBuffer::getMemBuffer(header);
  244. std::unique_ptr<llvm::MemoryBuffer> MainBuf =
  245. llvm::MemoryBuffer::getMemBuffer(main);
  246. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
  247. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  248. HeaderBuf->getBufferSize(), 0);
  249. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  250. VoidModuleLoader ModLoader;
  251. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  252. Diags, LangOpts, &*Target);
  253. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  254. SourceMgr, HeaderInfo, ModLoader,
  255. /*IILookup =*/nullptr,
  256. /*OwnsHeaderSearch =*/false);
  257. PP.Initialize(*Target);
  258. std::vector<MacroAction> Macros;
  259. PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
  260. PP.EnterMainSourceFile();
  261. std::vector<Token> toks;
  262. while (1) {
  263. Token tok;
  264. PP.Lex(tok);
  265. if (tok.is(tok::eof))
  266. break;
  267. toks.push_back(tok);
  268. }
  269. // Make sure we got the tokens that we expected.
  270. ASSERT_EQ(0U, toks.size());
  271. ASSERT_EQ(9U, Macros.size());
  272. // #define M(x) x
  273. ASSERT_TRUE(Macros[0].isDefinition);
  274. ASSERT_EQ("M", Macros[0].Name);
  275. // #define INC "/test-header.h"
  276. ASSERT_TRUE(Macros[1].isDefinition);
  277. ASSERT_EQ("INC", Macros[1].Name);
  278. // M expansion in #include M(INC)
  279. ASSERT_FALSE(Macros[2].isDefinition);
  280. ASSERT_EQ("M", Macros[2].Name);
  281. // INC expansion in #include M(INC)
  282. ASSERT_FALSE(Macros[3].isDefinition);
  283. ASSERT_EQ("INC", Macros[3].Name);
  284. // #define MACRO_IN_INCLUDE 0
  285. ASSERT_TRUE(Macros[4].isDefinition);
  286. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
  287. // #define INC2 </test-header.h>
  288. ASSERT_TRUE(Macros[5].isDefinition);
  289. ASSERT_EQ("INC2", Macros[5].Name);
  290. // M expansion in #include M(INC2)
  291. ASSERT_FALSE(Macros[6].isDefinition);
  292. ASSERT_EQ("M", Macros[6].Name);
  293. // INC2 expansion in #include M(INC2)
  294. ASSERT_FALSE(Macros[7].isDefinition);
  295. ASSERT_EQ("INC2", Macros[7].Name);
  296. // #define MACRO_IN_INCLUDE 0
  297. ASSERT_TRUE(Macros[8].isDefinition);
  298. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
  299. // The INC expansion in #include M(INC) comes before the first
  300. // MACRO_IN_INCLUDE definition of the included file.
  301. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
  302. // The INC2 expansion in #include M(INC2) comes before the second
  303. // MACRO_IN_INCLUDE definition of the included file.
  304. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
  305. }
  306. #endif
  307. } // anonymous namespace