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 10 #include "clang/Basic/SourceManager.h" 11 #include "clang/Basic/FileManager.h" 12 #include "clang/Basic/Diagnostic.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/Preprocessor.h" 19 20 #include "gtest/gtest.h" 21 22 using namespace llvm; 23 using namespace clang; 24 25 namespace { 26 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 IgnoringDiagConsumer()), 34 SourceMgr(Diags, FileMgr) { 35 TargetOpts.Triple = "x86_64-apple-darwin11.1.0"; 36 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 37 } 38 39 FileSystemOptions FileMgrOpts; 40 FileManager FileMgr; 41 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID; 42 DiagnosticsEngine Diags; 43 SourceManager SourceMgr; 44 LangOptions LangOpts; 45 TargetOptions TargetOpts; 46 llvm::IntrusiveRefCntPtr<TargetInfo> Target; 47 }; 48 49 class VoidModuleLoader : public ModuleLoader { 50 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path, 51 Module::NameVisibilityKind Visibility, 52 bool IsInclusionDirective) { 53 return 0; 54 } 55 }; 56 57 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { 58 const char *source = 59 "#define M(x) [x]\n" 60 "M(foo)"; 61 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); 62 FileID mainFileID = SourceMgr.createMainFileIDForMemBuffer(buf); 63 64 VoidModuleLoader ModLoader; 65 HeaderSearch HeaderInfo(FileMgr, Diags); 66 Preprocessor PP(Diags, LangOpts, 67 Target.getPtr(), 68 SourceMgr, HeaderInfo, ModLoader, 69 /*IILookup =*/ 0, 70 /*OwnsHeaderSearch =*/false, 71 /*DelayInitialization =*/ false); 72 PP.EnterMainSourceFile(); 73 74 std::vector<Token> toks; 75 while (1) { 76 Token tok; 77 PP.Lex(tok); 78 if (tok.is(tok::eof)) 79 break; 80 toks.push_back(tok); 81 } 82 83 // Make sure we got the tokens that we expected. 84 ASSERT_EQ(3U, toks.size()); 85 ASSERT_EQ(tok::l_square, toks[0].getKind()); 86 ASSERT_EQ(tok::identifier, toks[1].getKind()); 87 ASSERT_EQ(tok::r_square, toks[2].getKind()); 88 89 SourceLocation lsqrLoc = toks[0].getLocation(); 90 SourceLocation idLoc = toks[1].getLocation(); 91 SourceLocation rsqrLoc = toks[2].getLocation(); 92 93 SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1); 94 SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6); 95 ASSERT_TRUE(macroExpStartLoc.isFileID()); 96 ASSERT_TRUE(macroExpEndLoc.isFileID()); 97 98 llvm::SmallString<32> str; 99 ASSERT_EQ("M", PP.getSpelling(macroExpStartLoc, str)); 100 ASSERT_EQ(")", PP.getSpelling(macroExpEndLoc, str)); 101 102 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(lsqrLoc, idLoc)); 103 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, rsqrLoc)); 104 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(macroExpStartLoc, idLoc)); 105 EXPECT_TRUE(SourceMgr.isBeforeInTranslationUnit(idLoc, macroExpEndLoc)); 106 } 107 108 } // anonymous namespace 109