SourceManagerTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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) 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<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(source);
  68. FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
  69. SourceMgr.setMainFileID(mainFileID);
  70. VoidModuleLoader ModLoader;
  71. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  72. &*Target);
  73. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  74. 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<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
  112. FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
  113. SourceMgr.setMainFileID(MainFileID);
  114. bool Invalid;
  115. Invalid = false;
  116. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
  117. EXPECT_TRUE(!Invalid);
  118. Invalid = false;
  119. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
  120. EXPECT_TRUE(!Invalid);
  121. Invalid = false;
  122. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
  123. EXPECT_TRUE(!Invalid);
  124. Invalid = false;
  125. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
  126. EXPECT_TRUE(!Invalid);
  127. Invalid = false;
  128. EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
  129. &Invalid));
  130. EXPECT_TRUE(!Invalid);
  131. Invalid = false;
  132. SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
  133. EXPECT_TRUE(Invalid);
  134. // Test invalid files
  135. Invalid = false;
  136. SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
  137. EXPECT_TRUE(Invalid);
  138. Invalid = false;
  139. SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
  140. EXPECT_TRUE(Invalid);
  141. // Test with no invalid flag.
  142. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
  143. }
  144. #if defined(LLVM_ON_UNIX)
  145. TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
  146. const char *header =
  147. "#define FM(x,y) x\n";
  148. const char *main =
  149. "#include \"/test-header.h\"\n"
  150. "#define VAL 0\n"
  151. "FM(VAL,0)\n"
  152. "FM(0,VAL)\n"
  153. "FM(FM(0,VAL),0)\n"
  154. "#define CONCAT(X, Y) X##Y\n"
  155. "CONCAT(1,1)\n";
  156. std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
  157. std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
  158. FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
  159. SourceMgr.setMainFileID(mainFileID);
  160. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  161. HeaderBuf->getBufferSize(), 0);
  162. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  163. VoidModuleLoader ModLoader;
  164. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  165. &*Target);
  166. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  167. HeaderInfo, ModLoader,
  168. /*IILookup =*/nullptr,
  169. /*OwnsHeaderSearch =*/false);
  170. PP.Initialize(*Target);
  171. PP.EnterMainSourceFile();
  172. std::vector<Token> toks;
  173. while (1) {
  174. Token tok;
  175. PP.Lex(tok);
  176. if (tok.is(tok::eof))
  177. break;
  178. toks.push_back(tok);
  179. }
  180. // Make sure we got the tokens that we expected.
  181. ASSERT_EQ(4U, toks.size());
  182. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  183. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  184. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  185. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  186. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  187. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  188. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  189. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  190. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  191. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  192. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  193. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  194. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  195. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  196. EXPECT_TRUE(defLoc.isFileID());
  197. EXPECT_TRUE(loc1.isFileID());
  198. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  199. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  200. EXPECT_EQ(loc2, toks[1].getLocation());
  201. EXPECT_EQ(loc3, toks[2].getLocation());
  202. EXPECT_TRUE(defLoc2.isFileID());
  203. }
  204. namespace {
  205. struct MacroAction {
  206. SourceLocation Loc;
  207. std::string Name;
  208. bool isDefinition; // if false, it is expansion.
  209. MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
  210. : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
  211. };
  212. class MacroTracker : public PPCallbacks {
  213. std::vector<MacroAction> &Macros;
  214. public:
  215. explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
  216. void MacroDefined(const Token &MacroNameTok,
  217. const MacroDirective *MD) override {
  218. Macros.push_back(MacroAction(MD->getLocation(),
  219. MacroNameTok.getIdentifierInfo()->getName(),
  220. true));
  221. }
  222. void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
  223. SourceRange Range, const MacroArgs *Args) override {
  224. Macros.push_back(MacroAction(MacroNameTok.getLocation(),
  225. MacroNameTok.getIdentifierInfo()->getName(),
  226. false));
  227. }
  228. };
  229. }
  230. TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
  231. const char *header =
  232. "#define MACRO_IN_INCLUDE 0\n";
  233. const char *main =
  234. "#define M(x) x\n"
  235. "#define INC \"/test-header.h\"\n"
  236. "#include M(INC)\n"
  237. "#define INC2 </test-header.h>\n"
  238. "#include M(INC2)\n";
  239. std::unique_ptr<MemoryBuffer> HeaderBuf = MemoryBuffer::getMemBuffer(header);
  240. std::unique_ptr<MemoryBuffer> MainBuf = MemoryBuffer::getMemBuffer(main);
  241. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
  242. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  243. HeaderBuf->getBufferSize(), 0);
  244. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  245. VoidModuleLoader ModLoader;
  246. HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
  247. &*Target);
  248. Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
  249. HeaderInfo, ModLoader,
  250. /*IILookup =*/nullptr,
  251. /*OwnsHeaderSearch =*/false);
  252. PP.Initialize(*Target);
  253. std::vector<MacroAction> Macros;
  254. PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
  255. PP.EnterMainSourceFile();
  256. std::vector<Token> toks;
  257. while (1) {
  258. Token tok;
  259. PP.Lex(tok);
  260. if (tok.is(tok::eof))
  261. break;
  262. toks.push_back(tok);
  263. }
  264. // Make sure we got the tokens that we expected.
  265. ASSERT_EQ(0U, toks.size());
  266. ASSERT_EQ(9U, Macros.size());
  267. // #define M(x) x
  268. ASSERT_TRUE(Macros[0].isDefinition);
  269. ASSERT_EQ("M", Macros[0].Name);
  270. // #define INC "/test-header.h"
  271. ASSERT_TRUE(Macros[1].isDefinition);
  272. ASSERT_EQ("INC", Macros[1].Name);
  273. // M expansion in #include M(INC)
  274. ASSERT_FALSE(Macros[2].isDefinition);
  275. ASSERT_EQ("M", Macros[2].Name);
  276. // INC expansion in #include M(INC)
  277. ASSERT_FALSE(Macros[3].isDefinition);
  278. ASSERT_EQ("INC", Macros[3].Name);
  279. // #define MACRO_IN_INCLUDE 0
  280. ASSERT_TRUE(Macros[4].isDefinition);
  281. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
  282. // #define INC2 </test-header.h>
  283. ASSERT_TRUE(Macros[5].isDefinition);
  284. ASSERT_EQ("INC2", Macros[5].Name);
  285. // M expansion in #include M(INC2)
  286. ASSERT_FALSE(Macros[6].isDefinition);
  287. ASSERT_EQ("M", Macros[6].Name);
  288. // INC2 expansion in #include M(INC2)
  289. ASSERT_FALSE(Macros[7].isDefinition);
  290. ASSERT_EQ("INC2", Macros[7].Name);
  291. // #define MACRO_IN_INCLUDE 0
  292. ASSERT_TRUE(Macros[8].isDefinition);
  293. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
  294. // The INC expansion in #include M(INC) comes before the first
  295. // MACRO_IN_INCLUDE definition of the included file.
  296. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
  297. // The INC2 expansion in #include M(INC2) comes before the second
  298. // MACRO_IN_INCLUDE definition of the included file.
  299. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
  300. }
  301. #endif
  302. } // anonymous namespace