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