SourceManagerTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/Config/config.h"
  19. #include "gtest/gtest.h"
  20. using namespace llvm;
  21. using namespace clang;
  22. namespace {
  23. // The test fixture.
  24. class SourceManagerTest : public ::testing::Test {
  25. protected:
  26. SourceManagerTest()
  27. : FileMgr(FileMgrOpts),
  28. DiagID(new DiagnosticIDs()),
  29. Diags(DiagID, new IgnoringDiagConsumer()),
  30. SourceMgr(Diags, FileMgr) {
  31. TargetOpts.Triple = "x86_64-apple-darwin11.1.0";
  32. Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
  33. }
  34. FileSystemOptions FileMgrOpts;
  35. FileManager FileMgr;
  36. llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  37. DiagnosticsEngine Diags;
  38. SourceManager SourceMgr;
  39. LangOptions LangOpts;
  40. TargetOptions TargetOpts;
  41. llvm::IntrusiveRefCntPtr<TargetInfo> Target;
  42. };
  43. class VoidModuleLoader : public ModuleLoader {
  44. virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
  45. Module::NameVisibilityKind Visibility,
  46. bool IsInclusionDirective) {
  47. return 0;
  48. }
  49. };
  50. TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
  51. const char *source =
  52. "#define M(x) [x]\n"
  53. "M(foo)";
  54. MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
  55. FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf);
  56. VoidModuleLoader ModLoader;
  57. HeaderSearch HeaderInfo(FileMgr, Diags);
  58. Preprocessor PP(Diags, LangOpts,
  59. Target.getPtr(),
  60. SourceMgr, HeaderInfo, ModLoader,
  61. /*IILookup =*/ 0,
  62. /*OwnsHeaderSearch =*/false,
  63. /*DelayInitialization =*/ false);
  64. PP.EnterMainSourceFile();
  65. std::vector<Token> toks;
  66. while (1) {
  67. Token tok;
  68. PP.Lex(tok);
  69. if (tok.is(tok::eof))
  70. break;
  71. toks.push_back(tok);
  72. }
  73. // Make sure we got the tokens that we expected.
  74. ASSERT_EQ(3U, toks.size());
  75. ASSERT_EQ(tok::l_square, toks[0].getKind());
  76. ASSERT_EQ(tok::identifier, toks[1].getKind());
  77. ASSERT_EQ(tok::r_square, toks[2].getKind());
  78. SourceLocation lsqrLoc = toks[0].getLocation();
  79. SourceLocation idLoc = toks[1].getLocation();
  80. SourceLocation rsqrLoc = toks[2].getLocation();
  81. SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
  82. SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
  83. ASSERT_TRUE(macroExpStartLoc.isFileID());
  84. ASSERT_TRUE(macroExpEndLoc.isFileID());
  85. llvm::SmallString<32> str;
  86. ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str));
  87. ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str));
  88. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc));
  89. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc));
  90. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc));
  91. EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc));
  92. }
  93. #if defined(LLVM_ON_UNIX)
  94. TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
  95. const char *header =
  96. "#define FM(x,y) x\n";
  97. const char *main =
  98. "#include \"/test-header.h\"\n"
  99. "#define VAL 0\n"
  100. "FM(VAL,0)\n"
  101. "FM(0,VAL)\n"
  102. "FM(FM(0,VAL),0)\n"
  103. "#define CONCAT(X, Y) X##Y\n"
  104. "CONCAT(1,1)\n";
  105. MemoryBuffer *headerBuf = MemoryBuffer::getMemBuffer(header);
  106. MemoryBuffer *mainBuf = MemoryBuffer::getMemBuffer(main);
  107. FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(mainBuf);
  108. const FileEntry *headerFile = FileMgr.getVirtualFile("/test-header.h",
  109. headerBuf->getBufferSize(), 0);
  110. SourceMgr.overrideFileContents(headerFile, headerBuf);
  111. VoidModuleLoader ModLoader;
  112. HeaderSearch HeaderInfo(FileMgr, Diags);
  113. Preprocessor PP(Diags, LangOpts,
  114. Target.getPtr(),
  115. SourceMgr, HeaderInfo, ModLoader,
  116. /*IILookup =*/ 0,
  117. /*OwnsHeaderSearch =*/false,
  118. /*DelayInitialization =*/ false);
  119. PP.EnterMainSourceFile();
  120. std::vector<Token> toks;
  121. while (1) {
  122. Token tok;
  123. PP.Lex(tok);
  124. if (tok.is(tok::eof))
  125. break;
  126. toks.push_back(tok);
  127. }
  128. // Make sure we got the tokens that we expected.
  129. ASSERT_EQ(4U, toks.size());
  130. ASSERT_EQ(tok::numeric_constant, toks[0].getKind());
  131. ASSERT_EQ(tok::numeric_constant, toks[1].getKind());
  132. ASSERT_EQ(tok::numeric_constant, toks[2].getKind());
  133. ASSERT_EQ(tok::numeric_constant, toks[3].getKind());
  134. SourceLocation defLoc = SourceMgr.translateLineCol(mainFileID, 2, 13);
  135. SourceLocation loc1 = SourceMgr.translateLineCol(mainFileID, 3, 8);
  136. SourceLocation loc2 = SourceMgr.translateLineCol(mainFileID, 4, 4);
  137. SourceLocation loc3 = SourceMgr.translateLineCol(mainFileID, 5, 7);
  138. SourceLocation defLoc2 = SourceMgr.translateLineCol(mainFileID, 6, 22);
  139. defLoc = SourceMgr.getMacroArgExpandedLocation(defLoc);
  140. loc1 = SourceMgr.getMacroArgExpandedLocation(loc1);
  141. loc2 = SourceMgr.getMacroArgExpandedLocation(loc2);
  142. loc3 = SourceMgr.getMacroArgExpandedLocation(loc3);
  143. defLoc2 = SourceMgr.getMacroArgExpandedLocation(defLoc2);
  144. EXPECT_TRUE(defLoc.isFileID());
  145. EXPECT_TRUE(loc1.isFileID());
  146. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc2));
  147. EXPECT_TRUE(SourceMgr.isMacroArgExpansion(loc3));
  148. EXPECT_EQ(loc2, toks[1].getLocation());
  149. EXPECT_EQ(loc3, toks[2].getLocation());
  150. EXPECT_TRUE(defLoc2.isFileID());
  151. }
  152. #endif
  153. } // anonymous namespace