SourceManagerTest.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
  38. DiagnosticsEngine Diags;
  39. SourceManager SourceMgr;
  40. LangOptions LangOpts;
  41. TargetOptions TargetOpts;
  42. llvm::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. llvm::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. #endif
  154. } // anonymous namespace