SourceManagerTest.cpp 12 KB

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