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