SourceManagerTest.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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/MemoryBufferCache.h"
  15. #include "clang/Basic/TargetInfo.h"
  16. #include "clang/Basic/TargetOptions.h"
  17. #include "clang/Lex/HeaderSearch.h"
  18. #include "clang/Lex/HeaderSearchOptions.h"
  19. #include "clang/Lex/ModuleLoader.h"
  20. #include "clang/Lex/Preprocessor.h"
  21. #include "clang/Lex/PreprocessorOptions.h"
  22. #include "llvm/ADT/SmallString.h"
  23. #include "llvm/Config/llvm-config.h"
  24. #include "gtest/gtest.h"
  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. TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
  49. const char *source =
  50. "#define M(x) [x]\n"
  51. "M(foo)";
  52. std::unique_ptr<llvm::MemoryBuffer> Buf =
  53. llvm::MemoryBuffer::getMemBuffer(source);
  54. FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
  55. SourceMgr.setMainFileID(mainFileID);
  56. TrivialModuleLoader ModLoader;
  57. MemoryBufferCache PCMCache;
  58. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  59. Diags, LangOpts, &*Target);
  60. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  61. SourceMgr, PCMCache, HeaderInfo, ModLoader,
  62. /*IILookup =*/nullptr,
  63. /*OwnsHeaderSearch =*/false);
  64. PP.Initialize(*Target);
  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. TEST_F(SourceManagerTest, getColumnNumber) {
  95. const char *Source =
  96. "int x;\n"
  97. "int y;";
  98. std::unique_ptr<llvm::MemoryBuffer> Buf =
  99. llvm::MemoryBuffer::getMemBuffer(Source);
  100. FileID MainFileID = SourceMgr.createFileID(std::move(Buf));
  101. SourceMgr.setMainFileID(MainFileID);
  102. bool Invalid;
  103. Invalid = false;
  104. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, &Invalid));
  105. EXPECT_TRUE(!Invalid);
  106. Invalid = false;
  107. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 4, &Invalid));
  108. EXPECT_TRUE(!Invalid);
  109. Invalid = false;
  110. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 7, &Invalid));
  111. EXPECT_TRUE(!Invalid);
  112. Invalid = false;
  113. EXPECT_EQ(5U, SourceMgr.getColumnNumber(MainFileID, 11, &Invalid));
  114. EXPECT_TRUE(!Invalid);
  115. Invalid = false;
  116. EXPECT_EQ(7U, SourceMgr.getColumnNumber(MainFileID, strlen(Source),
  117. &Invalid));
  118. EXPECT_TRUE(!Invalid);
  119. Invalid = false;
  120. SourceMgr.getColumnNumber(MainFileID, strlen(Source)+1, &Invalid);
  121. EXPECT_TRUE(Invalid);
  122. // Test invalid files
  123. Invalid = false;
  124. SourceMgr.getColumnNumber(FileID(), 0, &Invalid);
  125. EXPECT_TRUE(Invalid);
  126. Invalid = false;
  127. SourceMgr.getColumnNumber(FileID(), 1, &Invalid);
  128. EXPECT_TRUE(Invalid);
  129. // Test with no invalid flag.
  130. EXPECT_EQ(1U, SourceMgr.getColumnNumber(MainFileID, 0, nullptr));
  131. }
  132. #if defined(LLVM_ON_UNIX)
  133. TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
  134. const char *header =
  135. "#define FM(x,y) x\n";
  136. const char *main =
  137. "#include \"/test-header.h\"\n"
  138. "#define VAL 0\n"
  139. "FM(VAL,0)\n"
  140. "FM(0,VAL)\n"
  141. "FM(FM(0,VAL),0)\n"
  142. "#define CONCAT(X, Y) X##Y\n"
  143. "CONCAT(1,1)\n";
  144. std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
  145. llvm::MemoryBuffer::getMemBuffer(header);
  146. std::unique_ptr<llvm::MemoryBuffer> MainBuf =
  147. llvm::MemoryBuffer::getMemBuffer(main);
  148. FileID mainFileID = SourceMgr.createFileID(std::move(MainBuf));
  149. SourceMgr.setMainFileID(mainFileID);
  150. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  151. HeaderBuf->getBufferSize(), 0);
  152. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  153. TrivialModuleLoader ModLoader;
  154. MemoryBufferCache PCMCache;
  155. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  156. Diags, LangOpts, &*Target);
  157. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  158. SourceMgr, PCMCache, HeaderInfo, ModLoader,
  159. /*IILookup =*/nullptr,
  160. /*OwnsHeaderSearch =*/false);
  161. PP.Initialize(*Target);
  162. PP.EnterMainSourceFile();
  163. std::vector<Token> toks;
  164. while (1) {
  165. Token tok;
  166. PP.Lex(tok);
  167. if (tok.is(tok::eof))
  168. break;
  169. toks.push_back(tok);
  170. }
  171. // Make sure we got the tokens that we expected.
  172. ASSERT_EQ(4U, toks.size());
  173. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  174. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  175. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  176. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  177. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  178. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  179. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  180. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  181. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  182. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  183. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  184. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  185. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  186. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  187. EXPECT_TRUE(defLoc.isFileID());
  188. EXPECT_TRUE(loc1.isFileID());
  189. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  190. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  191. EXPECT_EQ(loc2, toks[1].getLocation());
  192. EXPECT_EQ(loc3, toks[2].getLocation());
  193. EXPECT_TRUE(defLoc2.isFileID());
  194. }
  195. namespace {
  196. struct MacroAction {
  197. enum Kind { kExpansion, kDefinition, kUnDefinition};
  198. SourceLocation Loc;
  199. std::string Name;
  200. unsigned MAKind : 3;
  201. MacroAction(SourceLocation Loc, StringRef Name, unsigned K)
  202. : Loc(Loc), Name(Name), MAKind(K) { }
  203. bool isExpansion() const { return MAKind == kExpansion; }
  204. bool isDefinition() const { return MAKind & kDefinition; }
  205. bool isUnDefinition() const { return MAKind & kUnDefinition; }
  206. };
  207. class MacroTracker : public PPCallbacks {
  208. std::vector<MacroAction> &Macros;
  209. public:
  210. explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
  211. void MacroDefined(const Token &MacroNameTok,
  212. const MacroDirective *MD) override {
  213. Macros.push_back(MacroAction(MD->getLocation(),
  214. MacroNameTok.getIdentifierInfo()->getName(),
  215. MacroAction::kDefinition));
  216. }
  217. void MacroUndefined(const Token &MacroNameTok,
  218. const MacroDefinition &MD,
  219. const MacroDirective *UD) override {
  220. Macros.push_back(
  221. MacroAction(UD ? UD->getLocation() : SourceLocation(),
  222. MacroNameTok.getIdentifierInfo()->getName(),
  223. UD ? MacroAction::kDefinition | MacroAction::kUnDefinition
  224. : MacroAction::kUnDefinition));
  225. }
  226. void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD,
  227. SourceRange Range, const MacroArgs *Args) override {
  228. Macros.push_back(MacroAction(MacroNameTok.getLocation(),
  229. MacroNameTok.getIdentifierInfo()->getName(),
  230. MacroAction::kExpansion));
  231. }
  232. };
  233. }
  234. TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) {
  235. const char *header =
  236. "#define MACRO_IN_INCLUDE 0\n"
  237. "#define MACRO_DEFINED\n"
  238. "#undef MACRO_DEFINED\n"
  239. "#undef MACRO_UNDEFINED\n";
  240. const char *main =
  241. "#define M(x) x\n"
  242. "#define INC \"/test-header.h\"\n"
  243. "#include M(INC)\n"
  244. "#define INC2 </test-header.h>\n"
  245. "#include M(INC2)\n";
  246. std::unique_ptr<llvm::MemoryBuffer> HeaderBuf =
  247. llvm::MemoryBuffer::getMemBuffer(header);
  248. std::unique_ptr<llvm::MemoryBuffer> MainBuf =
  249. llvm::MemoryBuffer::getMemBuffer(main);
  250. SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(MainBuf)));
  251. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  252. HeaderBuf->getBufferSize(), 0);
  253. SourceMgr.overrideFileContents(headerFile, std::move(HeaderBuf));
  254. TrivialModuleLoader ModLoader;
  255. MemoryBufferCache PCMCache;
  256. HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
  257. Diags, LangOpts, &*Target);
  258. Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
  259. SourceMgr, PCMCache, HeaderInfo, ModLoader,
  260. /*IILookup =*/nullptr,
  261. /*OwnsHeaderSearch =*/false);
  262. PP.Initialize(*Target);
  263. std::vector<MacroAction> Macros;
  264. PP.addPPCallbacks(llvm::make_unique<MacroTracker>(Macros));
  265. PP.EnterMainSourceFile();
  266. std::vector<Token> toks;
  267. while (1) {
  268. Token tok;
  269. PP.Lex(tok);
  270. if (tok.is(tok::eof))
  271. break;
  272. toks.push_back(tok);
  273. }
  274. // Make sure we got the tokens that we expected.
  275. ASSERT_EQ(0U, toks.size());
  276. ASSERT_EQ(15U, Macros.size());
  277. // #define M(x) x
  278. ASSERT_TRUE(Macros[0].isDefinition());
  279. ASSERT_EQ("M", Macros[0].Name);
  280. // #define INC "/test-header.h"
  281. ASSERT_TRUE(Macros[1].isDefinition());
  282. ASSERT_EQ("INC", Macros[1].Name);
  283. // M expansion in #include M(INC)
  284. ASSERT_FALSE(Macros[2].isDefinition());
  285. ASSERT_EQ("M", Macros[2].Name);
  286. // INC expansion in #include M(INC)
  287. ASSERT_TRUE(Macros[3].isExpansion());
  288. ASSERT_EQ("INC", Macros[3].Name);
  289. // #define MACRO_IN_INCLUDE 0
  290. ASSERT_TRUE(Macros[4].isDefinition());
  291. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[4].Name);
  292. // #define MACRO_DEFINED
  293. ASSERT_TRUE(Macros[5].isDefinition());
  294. ASSERT_FALSE(Macros[5].isUnDefinition());
  295. ASSERT_EQ("MACRO_DEFINED", Macros[5].Name);
  296. // #undef MACRO_DEFINED
  297. ASSERT_TRUE(Macros[6].isDefinition());
  298. ASSERT_TRUE(Macros[6].isUnDefinition());
  299. ASSERT_EQ("MACRO_DEFINED", Macros[6].Name);
  300. // #undef MACRO_UNDEFINED
  301. ASSERT_FALSE(Macros[7].isDefinition());
  302. ASSERT_TRUE(Macros[7].isUnDefinition());
  303. ASSERT_EQ("MACRO_UNDEFINED", Macros[7].Name);
  304. // #define INC2 </test-header.h>
  305. ASSERT_TRUE(Macros[8].isDefinition());
  306. ASSERT_EQ("INC2", Macros[8].Name);
  307. // M expansion in #include M(INC2)
  308. ASSERT_FALSE(Macros[9].isDefinition());
  309. ASSERT_EQ("M", Macros[9].Name);
  310. // INC2 expansion in #include M(INC2)
  311. ASSERT_TRUE(Macros[10].isExpansion());
  312. ASSERT_EQ("INC2", Macros[10].Name);
  313. // #define MACRO_IN_INCLUDE 0
  314. ASSERT_TRUE(Macros[11].isDefinition());
  315. ASSERT_EQ("MACRO_IN_INCLUDE", Macros[11].Name);
  316. // The INC expansion in #include M(INC) comes before the first
  317. // MACRO_IN_INCLUDE definition of the included file.
  318. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[3].Loc, Macros[4].Loc));
  319. // The INC2 expansion in #include M(INC2) comes before the second
  320. // MACRO_IN_INCLUDE definition of the included file.
  321. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(Macros[10].Loc, Macros[11].Loc));
  322. }
  323. #endif
  324. } // anonymous namespace