SourceManagerTest.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/LangOptions.h"
  13. #include "clang/Basic/TargetOptions.h"
  14. #include "clang/Basic/TargetInfo.h"
  15. #include "clang/Lex/ModuleLoader.h"
  16. #include "clang/Lex/HeaderSearch.h"
  17. #include "clang/Lex/Preprocessor.h"
  18. #include "llvm/ADT/SmallString.h"
  19. #include "llvm/Config/config.h"
  20. #include "gtest/gtest.h"
  21. using namespace llvm;
  22. using namespace clang;
  23. namespace {
  24. // The test fixture.
  25. class SourceManagerTest : public ::testing::Test {
  26. protected:
  27. SourceManagerTest()
  28. : FileMgr(FileMgrOpts),
  29. DiagID(new DiagnosticIDs()),
  30. Diags(DiagID, new IgnoringDiagConsumer()),
  31. SourceMgr(Diags, FileMgr) {
  32. TargetOpts.Triple = "x86_64-apple-darwin11.1.0";
  33. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  34. }
  35. FileSystemOptions FileMgrOpts;
  36. FileManager FileMgr;
  37. IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  38. DiagnosticsEngine Diags;
  39. SourceManager SourceMgr;
  40. LangOptions LangOpts;
  41. TargetOptions TargetOpts;
  42. IntrusiveRefCntPtr<TargetInfo> Target;
  43. };
  44. class VoidModuleLoader : public ModuleLoader {
  45. virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
  46. Module::NameVisibilityKind Visibility,
  47. bool IsInclusionDirective) {
  48. return 0;
  49. }
  50. };
  51. TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
  52. const char *source =
  53. "#define M(x) [x]\n"
  54. "M(foo)";
  55. MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
  56. FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf);
  57. VoidModuleLoader ModLoader;
  58. HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target);
  59. Preprocessor PP(Diags, LangOpts,
  60. Target.getPtr(),
  61. SourceMgr, HeaderInfo, ModLoader,
  62. /*IILookup =*/ 0,
  63. /*OwnsHeaderSearch =*/false,
  64. /*DelayInitialization =*/ false);
  65. PP.EnterMainSourceFile();
  66. std::vector<Token> toks;
  67. while (1) {
  68. Token tok;
  69. PP.Lex(tok);
  70. if (tok.is(tok::eof))
  71. break;
  72. toks.push_back(tok);
  73. }
  74. // Make sure we got the tokens that we expected.
  75. ASSERT_EQ(3U, toks.size());
  76. ASSERT_EQ(tok::l_square, toks[0].getKind());
  77. ASSERT_EQ(tok::identifier, toks[1].getKind());
  78. ASSERT_EQ(tok::r_square, toks[2].getKind());
  79. SourceLocation lsqrLoc = toks[0].getLocation();
  80. SourceLocation idLoc = toks[1].getLocation();
  81. SourceLocation rsqrLoc = toks[2].getLocation();
  82. SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
  83. SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
  84. ASSERT_TRUE(macroExpStartLoc.isFileID());
  85. ASSERT_TRUE(macroExpEndLoc.isFileID());
  86. SmallString<32> str;
  87. ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
  88. ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
  89. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
  90. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
  91. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
  92. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
  93. }
  94. #if defined(LLVM_ON_UNIX)
  95. TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
  96. const char *header =
  97. "#define FM(x,y) x\n";
  98. const char *main =
  99. "#include \"/test-header.h\"\n"
  100. "#define VAL 0\n"
  101. "FM(VAL,0)\n"
  102. "FM(0,VAL)\n"
  103. "FM(FM(0,VAL),0)\n"
  104. "#define CONCAT(X, Y) X##Y\n"
  105. "CONCAT(1,1)\n";
  106. MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
  107. MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
  108. FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf);
  109. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  110. headerBuf->getBufferSize(), 0);
  111. SourceMgr.overrideFileContents(headerFile, headerBuf);
  112. VoidModuleLoader ModLoader;
  113. HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target);
  114. Preprocessor PP(Diags, LangOpts,
  115. Target.getPtr(),
  116. SourceMgr, HeaderInfo, ModLoader,
  117. /*IILookup =*/ 0,
  118. /*OwnsHeaderSearch =*/false,
  119. /*DelayInitialization =*/ false);
  120. PP.EnterMainSourceFile();
  121. std::vector<Token> toks;
  122. while (1) {
  123. Token tok;
  124. PP.Lex(tok);
  125. if (tok.is(tok::eof))
  126. break;
  127. toks.push_back(tok);
  128. }
  129. // Make sure we got the tokens that we expected.
  130. ASSERT_EQ(4U, toks.size());
  131. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  132. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  133. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  134. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  135. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  136. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  137. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  138. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  139. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  140. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  141. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  142. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  143. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  144. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  145. EXPECT_TRUE(defLoc.isFileID());
  146. EXPECT_TRUE(loc1.isFileID());
  147. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  148. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  149. EXPECT_EQ(loc2, toks[1].getLocation());
  150. EXPECT_EQ(loc3, toks[2].getLocation());
  151. EXPECT_TRUE(defLoc2.isFileID());
  152. }
  153. namespace {
  154. struct MacroAction {
  155. SourceLocation Loc;
  156. std::string Name;
  157. bool isDefinition; // if false, it is expansion.
  158. MacroAction(SourceLocation Loc, StringRef Name, bool isDefinition)
  159. : Loc(Loc), Name(Name), isDefinition(isDefinition) { }
  160. };
  161. class MacroTracker : public PPCallbacks {
  162. std::vector<MacroAction> &Macros;
  163. public:
  164. explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
  165. virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
  166. Macros.push_back(MacroAction(MI->getDefinitionLoc(),
  167. MacroNameTok.getIdentifierInfo()->getName(),
  168. true));
  169. }
  170. virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
  171. SourceRange Range) {
  172. Macros.push_back(MacroAction(MacroNameTok.getLocation(),
  173. MacroNameTok.getIdentifierInfo()->getName(),
  174. false));
  175. }
  176. };
  177. }
  178. TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
  179. const char *header =
  180. "#define MACRO_IN_INCLUDE 0\n";
  181. const char *main =
  182. "#define M(x) x\n"
  183. "#define INC \"/test-header.h\"\n"
  184. "#include M(INC)\n"
  185. "#define INC2 </test-header.h>\n"
  186. "#include M(INC2)\n";
  187. MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
  188. MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
  189. SourceMgr.createMainFileIDForMemBuffer(mainBuf);
  190. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  191. headerBuf->getBufferSize(), 0);
  192. SourceMgr.overrideFileContents(headerFile, headerBuf);
  193. VoidModuleLoader ModLoader;
  194. HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, &*Target);
  195. Preprocessor PP(Diags, LangOpts,
  196. Target.getPtr(),
  197. SourceMgr, HeaderInfo, ModLoader,
  198. /*IILookup =*/ 0,
  199. /*OwnsHeaderSearch =*/false,
  200. /*DelayInitialization =*/ false);
  201. std::vector<MacroAction> Macros;
  202. PP.addPPCallbacks(new MacroTracker(Macros));
  203. PP.EnterMainSourceFile();
  204. std::vector<Token> toks;
  205. while (1) {
  206. Token tok;
  207. PP.Lex(tok);
  208. if (tok.is(tok::eof))
  209. break;
  210. toks.push_back(tok);
  211. }
  212. // Make sure we got the tokens that we expected.
  213. ASSERT_EQ(0U, toks.size());
  214. ASSERT_EQ(9U, Macros.size());
  215. // #define M(x) x
  216. ASSERT_TRUE(Macros[0].isDefinition);
  217. ASSERT_EQ("M", Macros[0].Name);
  218. // #define INC "/test-header.h"
  219. ASSERT_TRUE(Macros[1].isDefinition);
  220. ASSERT_EQ("INC", Macros[1].Name);
  221. // M expansion in #include M(INC)
  222. ASSERT_FALSE(Macros[2].isDefinition);
  223. ASSERT_EQ("M", Macros[2].Name);
  224. // INC expansion in #include M(INC)
  225. ASSERT_FALSE(Macros[3].isDefinition);
  226. ASSERT_EQ("INC", Macros[3].Name);
  227. // #define MACRO_IN_INCLUDE 0
  228. ASSERT_TRUE(Macros[4].isDefinition);
  229. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
  230. // #define INC2 </test-header.h>
  231. ASSERT_TRUE(Macros[5].isDefinition);
  232. ASSERT_EQ("INC2", Macros[5].Name);
  233. // M expansion in #include M(INC2)
  234. ASSERT_FALSE(Macros[6].isDefinition);
  235. ASSERT_EQ("M", Macros[6].Name);
  236. // INC2 expansion in #include M(INC2)
  237. ASSERT_FALSE(Macros[7].isDefinition);
  238. ASSERT_EQ("INC2", Macros[7].Name);
  239. // #define MACRO_IN_INCLUDE 0
  240. ASSERT_TRUE(Macros[8].isDefinition);
  241. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[8].Name);
  242. // The INC expansion in #include M(INC) comes before the first
  243. // MACRO_IN_INCLUDE definition of the included file.
  244. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
  245. // The INC2 expansion in #include M(INC2) comes before the second
  246. // MACRO_IN_INCLUDE definition of the included file.
  247. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[7].Loc, Macros[8].Loc));
  248. }
  249. #endif
  250. } // anonymous namespace