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