SourceManagerTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 llvm;
  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,
  58. bool Complain) override { }
  59. GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
  60. { return nullptr; }
  61. bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
  62. { return 0; };
  63. };
  64. TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
  65. const char *source =
  66. "#define M(x) [x]\n"
  67. "M(foo)";
  68. std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(source);
  69. FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
  70. SourceMgr.setMainFileID(mainFileID);
  71. VoidModuleLoader ModLoader;
  72. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  73. &*Target);
  74. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  75. HeaderInfo, ModLoader,
  76. /*IILookup =*/nullptr,
  77. /*OwnsHeaderSearch =*/false);
  78. PP.Initialize(*Target);
  79. PP.EnterMainSourceFile();
  80. std::vector<Token> toks;
  81. while (1) {
  82. Token tok;
  83. PP.Lex(tok);
  84. if (tok.is(tok::eof))
  85. break;
  86. toks.push_back(tok);
  87. }
  88. // Make sure we got the tokens that we expected.
  89. ASSERT_EQ(3U, toks.size());
  90. ASSERT_EQ(tok::l_square, toks[0].getKind());
  91. ASSERT_EQ(tok::identifier, toks[1].getKind());
  92. ASSERT_EQ(tok::r_square, toks[2].getKind());
  93. SourceLocation lsqrLoc = toks[0].getLocation();
  94. SourceLocation idLoc = toks[1].getLocation();
  95. SourceLocation rsqrLoc = toks[2].getLocation();
  96. SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
  97. SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
  98. ASSERT_TRUE(macroExpStartLoc.isFileID());
  99. ASSERT_TRUE(macroExpEndLoc.isFileID());
  100. SmallString<32> str;
  101. ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
  102. ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
  103. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
  104. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
  105. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
  106. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
  107. }
  108. TEST_F(SourceManagerTest, getColumnNumber) {
  109. const char *Source =
  110. "int x;\n"
  111. "int y;";
  112. std::unique_ptr<MemoryBuffer> Buf = 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<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
  158. std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
  159. FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
  160. SourceMgr.setMainFileID(mainFileID);
  161. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  162. HeaderBuf->getBufferSize(), 0);
  163. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  164. VoidModuleLoader ModLoader;
  165. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  166. &*Target);
  167. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  168. HeaderInfo, ModLoader,
  169. /*IILookup =*/nullptr,
  170. /*OwnsHeaderSearch =*/false);
  171. PP.Initialize(*Target);
  172. PP.EnterMainSourceFile();
  173. std::vector<Token> toks;
  174. while (1) {
  175. Token tok;
  176. PP.Lex(tok);
  177. if (tok.is(tok::eof))
  178. break;
  179. toks.push_back(tok);
  180. }
  181. // Make sure we got the tokens that we expected.
  182. ASSERT_EQ(4U, toks.size());
  183. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  184. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  185. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  186. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  187. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  188. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  189. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  190. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  191. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  192. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  193. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  194. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  195. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  196. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  197. EXPECT_TRUE(defLoc.isFileID());
  198. EXPECT_TRUE(loc1.isFileID());
  199. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  200. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  201. EXPECT_EQ(loc2, toks[1].getLocation());
  202. EXPECT_EQ(loc3, toks[2].getLocation());
  203. EXPECT_TRUE(defLoc2.isFileID());
  204. }
  205. namespace {
  206. struct MacroAction {
  207. SourceLocation Loc;
  208. std::string Name;
  209. bool isDefinition; // if false, it is expansion.
  210. MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
  211. : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
  212. };
  213. class MacroTracker : public PPCallbacks {
  214. std::vector<MacroAction> &Macros;
  215. public:
  216. explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
  217. virtual void MacroDefined(const Token &MacroNameTok,
  218. const MacroDirective *MD) {
  219. Macros.push_back(MacroAction(MD->getLocation(),
  220. MacroNameTok.getIdentifierInfo()->getName(),
  221. true));
  222. }
  223. virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
  224. SourceRange Range, const MacroArgs *Args) {
  225. Macros.push_back(MacroAction(MacroNameTok.getLocation(),
  226. MacroNameTok.getIdentifierInfo()->getName(),
  227. false));
  228. }
  229. };
  230. }
  231. TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
  232. const char *header =
  233. "#define MACRO_IN_INCLUDE 0\n";
  234. const char *main =
  235. "#define M(x) x\n"
  236. "#define INC \"/test-header.h\"\n"
  237. "#include M(INC)\n"
  238. "#define INC2 </test-header.h>\n"
  239. "#include M(INC2)\n";
  240. std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
  241. std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
  242. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
  243. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  244. HeaderBuf->getBufferSize(), 0);
  245. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  246. VoidModuleLoader ModLoader;
  247. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  248. &*Target);
  249. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  250. HeaderInfo, ModLoader,
  251. /*IILookup =*/nullptr,
  252. /*OwnsHeaderSearch =*/false);
  253. PP.Initialize(*Target);
  254. std::vector<MacroAction> Macros;
  255. PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
  256. PP.EnterMainSourceFile();
  257. std::vector<Token> toks;
  258. while (1) {
  259. Token tok;
  260. PP.Lex(tok);
  261. if (tok.is(tok::eof))
  262. break;
  263. toks.push_back(tok);
  264. }
  265. // Make sure we got the tokens that we expected.
  266. ASSERT_EQ(0U, toks.size());
  267. ASSERT_EQ(9U, Macros.size());
  268. // #define M(x) x
  269. ASSERT_TRUE(Macros[0].isDefinition);
  270. ASSERT_EQ("M", Macros[0].Name);
  271. // #define INC "/test-header.h"
  272. ASSERT_TRUE(Macros[1].isDefinition);
  273. ASSERT_EQ("INC", Macros[1].Name);
  274. // M expansion in #include M(INC)
  275. ASSERT_FALSE(Macros[2].isDefinition);
  276. ASSERT_EQ("M", Macros[2].Name);
  277. // INC expansion in #include M(INC)
  278. ASSERT_FALSE(Macros[3].isDefinition);
  279. ASSERT_EQ("INC", Macros[3].Name);
  280. // #define MACRO_IN_INCLUDE 0
  281. ASSERT_TRUE(Macros[4].isDefinition);
  282. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
  283. // #define INC2 </test-header.h>
  284. ASSERT_TRUE(Macros[5].isDefinition);
  285. ASSERT_EQ("INC2", Macros[5].Name);
  286. // M expansion in #include M(INC2)
  287. ASSERT_FALSE(Macros[6].isDefinition);
  288. ASSERT_EQ("M", Macros[6].Name);
  289. // INC2 expansion in #include M(INC2)
  290. ASSERT_FALSE(Macros[7].isDefinition);
  291. ASSERT_EQ("INC2", Macros[7].Name);
  292. // #define MACRO_IN_INCLUDE 0
  293. ASSERT_TRUE(Macros[8].isDefinition);
  294. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
  295. // The INC expansion in #include M(INC) comes before the first
  296. // MACRO_IN_INCLUDE definition of the included file.
  297. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
  298. // The INC2 expansion in #include M(INC2) comes before the second
  299. // MACRO_IN_INCLUDE definition of the included file.
  300. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
  301. }
  302. #endif
  303. } // anonymous namespace