SourceManagerTest.cpp 12 KB

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