SourceManagerTest.cpp 11 KB

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