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 64 TEST_F(LexerTest, LexAPI) { 65 const char *source = 66 "#define M(x) [x]\n" 67 "#define N(x) x\n" 68 "#define INN(x) x\n" 69 "#define NOF1 INN(val)\n" 70 "#define NOF2 val\n" 71 "M(foo) N([bar])\n" 72 "N(INN(val)) N(NOF1) N(NOF2) N(val)"; 73 74 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source); 75 (void)SourceMgr.createMainFileIDForMemBuffer(buf); 76 77 VoidModuleLoader ModLoader; 78 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts, 79 Target.getPtr()); 80 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, Target.getPtr(), 81 SourceMgr, HeaderInfo, ModLoader, 82 /*IILookup =*/ 0, 83 /*OwnsHeaderSearch =*/false, 84 /*DelayInitialization =*/ false); 85 PP.EnterMainSourceFile(); 86 87 std::vector<Token> toks; 88 while (1) { 89 Token tok; 90 PP.Lex(tok); 91 if (tok.is(tok::eof)) 92 break; 93 toks.push_back(tok); 94 } 95 96 // Make sure we got the tokens that we expected. 97 ASSERT_EQ(10U, toks.size()); 98 ASSERT_EQ(tok::l_square, toks[0].getKind()); 99 ASSERT_EQ(tok::identifier, toks[1].getKind()); 100 ASSERT_EQ(tok::r_square, toks[2].getKind()); 101 ASSERT_EQ(tok::l_square, toks[3].getKind()); 102 ASSERT_EQ(tok::identifier, toks[4].getKind()); 103 ASSERT_EQ(tok::r_square, toks[5].getKind()); 104 ASSERT_EQ(tok::identifier, toks[6].getKind()); 105 ASSERT_EQ(tok::identifier, toks[7].getKind()); 106 ASSERT_EQ(tok::identifier, toks[8].getKind()); 107 ASSERT_EQ(tok::identifier, toks[9].getKind()); 108 109 SourceLocation lsqrLoc = toks[0].getLocation(); 110 SourceLocation idLoc = toks[1].getLocation(); 111 SourceLocation rsqrLoc = toks[2].getLocation(); 112 std::pair<SourceLocation,SourceLocation> 113 macroPair = SourceMgr.getExpansionRange(lsqrLoc); 114 SourceRange macroRange = SourceRange(macroPair.first, macroPair.second); 115 116 SourceLocation Loc; 117 EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc)); 118 EXPECT_EQ(Loc, macroRange.getBegin()); 119 EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts)); 120 EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts)); 121 EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc)); 122 EXPECT_EQ(Loc, macroRange.getEnd()); 123 124 CharSourceRange range = Lexer::makeFileCharRange( 125 CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts); 126 EXPECT_TRUE(range.isInvalid()); 127 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc), 128 SourceMgr, LangOpts); 129 EXPECT_TRUE(range.isInvalid()); 130 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), 131 SourceMgr, LangOpts); 132 EXPECT_TRUE(!range.isTokenRange()); 133 EXPECT_EQ(range.getAsRange(), 134 SourceRange(macroRange.getBegin(), 135 macroRange.getEnd().getLocWithOffset(1))); 136 137 StringRef text = Lexer::getSourceText( 138 CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc), 139 SourceMgr, LangOpts); 140 EXPECT_EQ(text, "M(foo)"); 141 142 SourceLocation macroLsqrLoc = toks[3].getLocation(); 143 SourceLocation macroIdLoc = toks[4].getLocation(); 144 SourceLocation macroRsqrLoc = toks[5].getLocation(); 145 SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc); 146 SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc); 147 SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc); 148 149 range = Lexer::makeFileCharRange( 150 CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc), 151 SourceMgr, LangOpts); 152 EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)), 153 range.getAsRange()); 154 155 range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc), 156 SourceMgr, LangOpts); 157 EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)), 158 range.getAsRange()); 159 160 macroPair = SourceMgr.getExpansionRange(macroLsqrLoc); 161 range = Lexer::makeFileCharRange( 162 CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc), 163 SourceMgr, LangOpts); 164 EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)), 165 range.getAsRange()); 166 167 text = Lexer::getSourceText( 168 CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)), 169 SourceMgr, LangOpts); 170 EXPECT_EQ(text, "[bar"); 171 172 173 SourceLocation idLoc1 = toks[6].getLocation(); 174 SourceLocation idLoc2 = toks[7].getLocation(); 175 SourceLocation idLoc3 = toks[8].getLocation(); 176 SourceLocation idLoc4 = toks[9].getLocation(); 177 EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts)); 178 EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts)); 179 EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts)); 180 EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts)); 181 } 182 183 } // anonymous namespace 184