SourceManagerTest.cpp 12 KB

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