1 //===- unittests/Basic/LexerTest.cpp ------ Lexer 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 #include "llvm/Config/config.h" 20 21 #include "gtest/gtest.h" 22 23 using namespace llvm; 24 using namespace clang; 25 26 namespace { 27 28 // The test fixture. 29 class LexerTest : public ::testing::Test { 30 protected: 31 LexerTest() 32 : FileMgr(FileMgrOpts), 33 DiagID(new DiagnosticIDs()), 34 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), 35 SourceMgr(Diags, FileMgr), 36 TargetOpts(new TargetOptions) 37 { 38 TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; 39 Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts); 40 } 41 42 FileSystemOptions FileMgrOpts; 43 FileManager FileMgr; 44 IntrusiveRefCntPtr<DiagnosticIDs> DiagID; 45 DiagnosticsEngine Diags; 46 SourceManager SourceMgr; 47 LangOptions LangOpts; 48 IntrusiveRefCntPtr<TargetOptions> TargetOpts; 49 IntrusiveRefCntPtr<TargetInfo> Target; 50 }; 51 52 class VoidModuleLoader : public ModuleLoader { 53 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path, 54 Module::NameVisibilityKind Visibility, 55 bool IsInclusionDirective) { 56 return 0; 57 } 58 }; 59 60 TEST_F(LexerTest, LexAPI) { 61 const char *source = 62 "#define M(x) [x]\n" 63 "#define N(x) x\n" 64 "#define INN(x) x\n" 65 "#define NOF1 INN(val)\n" 66 "#define NOF2 val\n" 67 "M(foo) N([bar])\n" 68 "N(INN(val)) N(NOF1) N(NOF2) N(val)"; 69 70 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); 71 (void)SourceMgr.createMainFileIDForMemBuffer(buf); 72 73 VoidModuleLoader ModLoader; 74 HeaderSearch HeaderInfo(FileMgr, Diags, LangOpts, Target.getPtr()); 75 Preprocessor PP(Diags, LangOpts, 76 Target.getPtr(), 77 SourceMgr, HeaderInfo, ModLoader, 78 /*IILookup =*/ 0, 79 /*OwnsHeaderSearch =*/false, 80 /*DelayInitialization =*/ false); 81 PP.EnterMainSourceFile(); 82 83 std::vector<Token> toks; 84 while (1) { 85 Token tok; 86 PP.Lex(tok); 87 if (tok.is(tok::eof)) 88 break; 89 toks.push_back(tok); 90 } 91 92 // Make sure we got the tokens that we expected. 93 ASSERT_EQ(10U, toks.size()); 94 ASSERT_EQ(tok::l_square, toks[0].getKind()); 95 ASSERT_EQ(tok::identifier, toks[1].getKind()); 96 ASSERT_EQ(tok::r_square, toks[2].getKind()); 97 ASSERT_EQ(tok::l_square, toks[3].getKind()); 98 ASSERT_EQ(tok::identifier, toks[4].getKind()); 99 ASSERT_EQ(tok::r_square, toks[5].getKind()); 100 ASSERT_EQ(tok::identifier, toks[6].getKind()); 101 ASSERT_EQ(tok::identifier, toks[7].getKind()); 102 ASSERT_EQ(tok::identifier, toks[8].getKind()); 103 ASSERT_EQ(tok::identifier, toks[9].getKind()); 104 105 SourceLocation lsqrLoc = toks[0].getLocation(); 106 SourceLocation idLoc = toks[1].getLocation(); 107 SourceLocation rsqrLoc = toks[2].getLocation(); 108 std::pair<SourceLocation,SourceLocation> 109 macroPair = SourceMgr.getExpansionRange(lsqrLoc); 110 SourceRange macroRange = SourceRange(macroPair.first, macroPair.second); 111 112 SourceLocation Loc; 113 EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc)); 114 EXPECT_EQ(Loc, macroRange.getBegin()); 115 EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts)); 116 EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts)); 117 EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc)); 118 EXPECT_EQ(Loc, macroRange.getEnd()); 119 120 CharSourceRange range = Lexer::makeFileCharRange( 121 CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts); 122 EXPECT_TRUE(range.isInvalid()); 123 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc), 124 SourceMgr, LangOpts); 125 EXPECT_TRUE(range.isInvalid()); 126 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), 127 SourceMgr, LangOpts); 128 EXPECT_TRUE(!range.isTokenRange()); 129 EXPECT_EQ(range.getAsRange(), 130 SourceRange(macroRange.getBegin(), 131 macroRange.getEnd().getLocWithOffset(1))); 132 133 StringRef text = Lexer::getSourceText( 134 CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), 135 SourceMgr, LangOpts); 136 EXPECT_EQ(text, "M(foo)"); 137 138 SourceLocation macroLsqrLoc = toks[3].getLocation(); 139 SourceLocation macroIdLoc = toks[4].getLocation(); 140 SourceLocation macroRsqrLoc = toks[5].getLocation(); 141 SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc); 142 SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc); 143 SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc); 144 145 range = Lexer::makeFileCharRange( 146 CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc), 147 SourceMgr, LangOpts); 148 EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)), 149 range.getAsRange()); 150 151 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc), 152 SourceMgr, LangOpts); 153 EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)), 154 range.getAsRange()); 155 156 macroPair = SourceMgr.getExpansionRange(macroLsqrLoc); 157 range = Lexer::makeFileCharRange( 158 CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc), 159 SourceMgr, LangOpts); 160 EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)), 161 range.getAsRange()); 162 163 text = Lexer::getSourceText( 164 CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)), 165 SourceMgr, LangOpts); 166 EXPECT_EQ(text, "[bar"); 167 168 169 SourceLocation idLoc1 = toks[6].getLocation(); 170 SourceLocation idLoc2 = toks[7].getLocation(); 171 SourceLocation idLoc3 = toks[8].getLocation(); 172 SourceLocation idLoc4 = toks[9].getLocation(); 173 EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts)); 174 EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts)); 175 EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts)); 176 EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts)); 177 } 178 179 } // anonymous namespace 180