xref: /minix3/external/bsd/llvm/dist/clang/unittests/Lex/LexerTest.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===- unittests/Lex/LexerTest.cpp ------ Lexer tests ---------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Lex/Lexer.h"
11f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
13f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
14f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/SourceManager.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/TargetOptions.h"
18f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
19f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearchOptions.h"
20f4a2713aSLionel Sambuc #include "clang/Lex/ModuleLoader.h"
21f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
22f4a2713aSLionel Sambuc #include "clang/Lex/PreprocessorOptions.h"
23f4a2713aSLionel Sambuc #include "gtest/gtest.h"
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc using namespace llvm;
26f4a2713aSLionel Sambuc using namespace clang;
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc namespace {
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc class VoidModuleLoader : public ModuleLoader {
loadModule(SourceLocation ImportLoc,ModuleIdPath Path,Module::NameVisibilityKind Visibility,bool IsInclusionDirective)31*0a6a1f1dSLionel Sambuc   ModuleLoadResult loadModule(SourceLocation ImportLoc,
32f4a2713aSLionel Sambuc                               ModuleIdPath Path,
33f4a2713aSLionel Sambuc                               Module::NameVisibilityKind Visibility,
34*0a6a1f1dSLionel Sambuc                               bool IsInclusionDirective) override {
35f4a2713aSLionel Sambuc     return ModuleLoadResult();
36f4a2713aSLionel Sambuc   }
37f4a2713aSLionel Sambuc 
makeModuleVisible(Module * Mod,Module::NameVisibilityKind Visibility,SourceLocation ImportLoc,bool Complain)38*0a6a1f1dSLionel Sambuc   void makeModuleVisible(Module *Mod,
39f4a2713aSLionel Sambuc                          Module::NameVisibilityKind Visibility,
40f4a2713aSLionel Sambuc                          SourceLocation ImportLoc,
41*0a6a1f1dSLionel Sambuc                          bool Complain) override { }
42*0a6a1f1dSLionel Sambuc 
loadGlobalModuleIndex(SourceLocation TriggerLoc)43*0a6a1f1dSLionel Sambuc   GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
44*0a6a1f1dSLionel Sambuc     { return nullptr; }
lookupMissingImports(StringRef Name,SourceLocation TriggerLoc)45*0a6a1f1dSLionel Sambuc   bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
46*0a6a1f1dSLionel Sambuc     { return 0; };
47f4a2713aSLionel Sambuc };
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc // The test fixture.
50f4a2713aSLionel Sambuc class LexerTest : public ::testing::Test {
51f4a2713aSLionel Sambuc protected:
LexerTest()52f4a2713aSLionel Sambuc   LexerTest()
53f4a2713aSLionel Sambuc     : FileMgr(FileMgrOpts),
54f4a2713aSLionel Sambuc       DiagID(new DiagnosticIDs()),
55f4a2713aSLionel Sambuc       Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
56f4a2713aSLionel Sambuc       SourceMgr(Diags, FileMgr),
57f4a2713aSLionel Sambuc       TargetOpts(new TargetOptions)
58f4a2713aSLionel Sambuc   {
59f4a2713aSLionel Sambuc     TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
60*0a6a1f1dSLionel Sambuc     Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
61f4a2713aSLionel Sambuc   }
62f4a2713aSLionel Sambuc 
CheckLex(StringRef Source,ArrayRef<tok::TokenKind> ExpectedTokens)63f4a2713aSLionel Sambuc   std::vector<Token> CheckLex(StringRef Source,
64f4a2713aSLionel Sambuc                               ArrayRef<tok::TokenKind> ExpectedTokens) {
65*0a6a1f1dSLionel Sambuc     std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(Source);
66*0a6a1f1dSLionel Sambuc     SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
67f4a2713aSLionel Sambuc 
68f4a2713aSLionel Sambuc     VoidModuleLoader ModLoader;
69f4a2713aSLionel Sambuc     HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
70*0a6a1f1dSLionel Sambuc                             Target.get());
71*0a6a1f1dSLionel Sambuc     Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
72*0a6a1f1dSLionel Sambuc                     HeaderInfo, ModLoader, /*IILookup =*/nullptr,
73*0a6a1f1dSLionel Sambuc                     /*OwnsHeaderSearch =*/false);
74*0a6a1f1dSLionel Sambuc     PP.Initialize(*Target);
75f4a2713aSLionel Sambuc     PP.EnterMainSourceFile();
76f4a2713aSLionel Sambuc 
77f4a2713aSLionel Sambuc     std::vector<Token> toks;
78f4a2713aSLionel Sambuc     while (1) {
79f4a2713aSLionel Sambuc       Token tok;
80f4a2713aSLionel Sambuc       PP.Lex(tok);
81f4a2713aSLionel Sambuc       if (tok.is(tok::eof))
82f4a2713aSLionel Sambuc         break;
83f4a2713aSLionel Sambuc       toks.push_back(tok);
84f4a2713aSLionel Sambuc     }
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc     EXPECT_EQ(ExpectedTokens.size(), toks.size());
87f4a2713aSLionel Sambuc     for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
88f4a2713aSLionel Sambuc       EXPECT_EQ(ExpectedTokens[i], toks[i].getKind());
89f4a2713aSLionel Sambuc     }
90f4a2713aSLionel Sambuc 
91f4a2713aSLionel Sambuc     return toks;
92f4a2713aSLionel Sambuc   }
93f4a2713aSLionel Sambuc 
getSourceText(Token Begin,Token End)94f4a2713aSLionel Sambuc   std::string getSourceText(Token Begin, Token End) {
95f4a2713aSLionel Sambuc     bool Invalid;
96f4a2713aSLionel Sambuc     StringRef Str =
97f4a2713aSLionel Sambuc         Lexer::getSourceText(CharSourceRange::getTokenRange(SourceRange(
98f4a2713aSLionel Sambuc                                     Begin.getLocation(), End.getLocation())),
99f4a2713aSLionel Sambuc                              SourceMgr, LangOpts, &Invalid);
100f4a2713aSLionel Sambuc     if (Invalid)
101f4a2713aSLionel Sambuc       return "<INVALID>";
102f4a2713aSLionel Sambuc     return Str;
103f4a2713aSLionel Sambuc   }
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   FileSystemOptions FileMgrOpts;
106f4a2713aSLionel Sambuc   FileManager FileMgr;
107f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
108f4a2713aSLionel Sambuc   DiagnosticsEngine Diags;
109f4a2713aSLionel Sambuc   SourceManager SourceMgr;
110f4a2713aSLionel Sambuc   LangOptions LangOpts;
111*0a6a1f1dSLionel Sambuc   std::shared_ptr<TargetOptions> TargetOpts;
112f4a2713aSLionel Sambuc   IntrusiveRefCntPtr<TargetInfo> Target;
113f4a2713aSLionel Sambuc };
114f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsToMaximumInMacroArgument)115f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgument) {
116f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
117f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
118f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
119f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
120f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
121f4a2713aSLionel Sambuc 
122f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
123f4a2713aSLionel Sambuc                                      "M(f(M(i)))",
124f4a2713aSLionel Sambuc                                      ExpectedTokens);
125f4a2713aSLionel Sambuc 
126f4a2713aSLionel Sambuc   EXPECT_EQ("M(i)", getSourceText(toks[2], toks[2]));
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro)129f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro) {
130f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
131f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
132f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
133f4a2713aSLionel Sambuc 
134f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
135f4a2713aSLionel Sambuc                                      "M(M(i) c)",
136f4a2713aSLionel Sambuc                                      ExpectedTokens);
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc   EXPECT_EQ("M(i)", getSourceText(toks[0], toks[0]));
139f4a2713aSLionel Sambuc }
140f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsInMacroArgumentForBeginOfMacro)141f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForBeginOfMacro) {
142f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
143f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
144f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
145f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
146f4a2713aSLionel Sambuc 
147f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
148f4a2713aSLionel Sambuc                                      "M(c c M(i))",
149f4a2713aSLionel Sambuc                                      ExpectedTokens);
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc   EXPECT_EQ("c M(i)", getSourceText(toks[1], toks[2]));
152f4a2713aSLionel Sambuc }
153f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsInMacroArgumentForEndOfMacro)154f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForEndOfMacro) {
155f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
156f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
157f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
158f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
159f4a2713aSLionel Sambuc 
160f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
161f4a2713aSLionel Sambuc                                      "M(M(i) c c)",
162f4a2713aSLionel Sambuc                                      ExpectedTokens);
163f4a2713aSLionel Sambuc 
164f4a2713aSLionel Sambuc   EXPECT_EQ("M(i) c", getSourceText(toks[0], toks[1]));
165f4a2713aSLionel Sambuc }
166f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextInSeparateFnMacros)167f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextInSeparateFnMacros) {
168f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
169f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
170f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
171f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
172f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
173f4a2713aSLionel Sambuc 
174f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
175f4a2713aSLionel Sambuc                                      "M(c M(i)) M(M(i) c)",
176f4a2713aSLionel Sambuc                                      ExpectedTokens);
177f4a2713aSLionel Sambuc 
178f4a2713aSLionel Sambuc   EXPECT_EQ("<INVALID>", getSourceText(toks[1], toks[2]));
179f4a2713aSLionel Sambuc }
180f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextWorksAcrossTokenPastes)181f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextWorksAcrossTokenPastes) {
182f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
183f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
184f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
185f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
186f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
189f4a2713aSLionel Sambuc                                      "#define C(x) M(x##c)\n"
190f4a2713aSLionel Sambuc                                      "M(f(C(i)))",
191f4a2713aSLionel Sambuc                                      ExpectedTokens);
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc   EXPECT_EQ("C(i)", getSourceText(toks[2], toks[2]));
194f4a2713aSLionel Sambuc }
195f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsAcrossMultipleMacroCalls)196f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsAcrossMultipleMacroCalls) {
197f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
198f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
199f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
200f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
201f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
204f4a2713aSLionel Sambuc                                      "f(M(M(i)))",
205f4a2713aSLionel Sambuc                                      ExpectedTokens);
206f4a2713aSLionel Sambuc   EXPECT_EQ("M(M(i))", getSourceText(toks[2], toks[2]));
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextInMiddleOfMacroArgument)209f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextInMiddleOfMacroArgument) {
210f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
211f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
212f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
213f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
214f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
215f4a2713aSLionel Sambuc 
216f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
217f4a2713aSLionel Sambuc                                      "M(f(i))",
218f4a2713aSLionel Sambuc                                      ExpectedTokens);
219f4a2713aSLionel Sambuc   EXPECT_EQ("i", getSourceText(toks[2], toks[2]));
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsAroundDifferentMacroCalls)222f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsAroundDifferentMacroCalls) {
223f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
224f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
225f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
226f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
227f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
230f4a2713aSLionel Sambuc                                      "#define C(x) x\n"
231f4a2713aSLionel Sambuc                                      "f(C(M(i)))",
232f4a2713aSLionel Sambuc                                      ExpectedTokens);
233f4a2713aSLionel Sambuc   EXPECT_EQ("C(M(i))", getSourceText(toks[2], toks[2]));
234f4a2713aSLionel Sambuc }
235f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextOnlyExpandsIfFirstTokenInMacro)236f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextOnlyExpandsIfFirstTokenInMacro) {
237f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
238f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
239f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
240f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
241f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
242f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
243f4a2713aSLionel Sambuc 
244f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
245f4a2713aSLionel Sambuc                                      "#define C(x) c x\n"
246f4a2713aSLionel Sambuc                                      "f(C(M(i)))",
247f4a2713aSLionel Sambuc                                      ExpectedTokens);
248f4a2713aSLionel Sambuc   EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
249f4a2713aSLionel Sambuc }
250f4a2713aSLionel Sambuc 
TEST_F(LexerTest,GetSourceTextExpandsRecursively)251f4a2713aSLionel Sambuc TEST_F(LexerTest, GetSourceTextExpandsRecursively) {
252f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
253f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
254f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
255f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_paren);
256f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
257f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_paren);
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) x\n"
260f4a2713aSLionel Sambuc                                      "#define C(x) c M(x)\n"
261f4a2713aSLionel Sambuc                                      "C(f(M(i)))",
262f4a2713aSLionel Sambuc                                      ExpectedTokens);
263f4a2713aSLionel Sambuc   EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc 
TEST_F(LexerTest,LexAPI)266f4a2713aSLionel Sambuc TEST_F(LexerTest, LexAPI) {
267f4a2713aSLionel Sambuc   std::vector<tok::TokenKind> ExpectedTokens;
268f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_square);
269f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
270f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_square);
271f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::l_square);
272f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
273f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::r_square);
274f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
275f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
276f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
277f4a2713aSLionel Sambuc   ExpectedTokens.push_back(tok::identifier);
278f4a2713aSLionel Sambuc 
279f4a2713aSLionel Sambuc   std::vector<Token> toks = CheckLex("#define M(x) [x]\n"
280f4a2713aSLionel Sambuc                                      "#define N(x) x\n"
281f4a2713aSLionel Sambuc                                      "#define INN(x) x\n"
282f4a2713aSLionel Sambuc                                      "#define NOF1 INN(val)\n"
283f4a2713aSLionel Sambuc                                      "#define NOF2 val\n"
284f4a2713aSLionel Sambuc                                      "M(foo) N([bar])\n"
285f4a2713aSLionel Sambuc                                      "N(INN(val)) N(NOF1) N(NOF2) N(val)",
286f4a2713aSLionel Sambuc                                      ExpectedTokens);
287f4a2713aSLionel Sambuc 
288f4a2713aSLionel Sambuc   SourceLocation lsqrLoc = toks[0].getLocation();
289f4a2713aSLionel Sambuc   SourceLocation idLoc = toks[1].getLocation();
290f4a2713aSLionel Sambuc   SourceLocation rsqrLoc = toks[2].getLocation();
291f4a2713aSLionel Sambuc   std::pair<SourceLocation,SourceLocation>
292f4a2713aSLionel Sambuc     macroPair = SourceMgr.getExpansionRange(lsqrLoc);
293f4a2713aSLionel Sambuc   SourceRange macroRange = SourceRange(macroPair.first, macroPair.second);
294f4a2713aSLionel Sambuc 
295f4a2713aSLionel Sambuc   SourceLocation Loc;
296f4a2713aSLionel Sambuc   EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
297f4a2713aSLionel Sambuc   EXPECT_EQ(Loc, macroRange.getBegin());
298f4a2713aSLionel Sambuc   EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
299f4a2713aSLionel Sambuc   EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
300f4a2713aSLionel Sambuc   EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
301f4a2713aSLionel Sambuc   EXPECT_EQ(Loc, macroRange.getEnd());
302f4a2713aSLionel Sambuc 
303f4a2713aSLionel Sambuc   CharSourceRange range = Lexer::makeFileCharRange(
304f4a2713aSLionel Sambuc            CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts);
305f4a2713aSLionel Sambuc   EXPECT_TRUE(range.isInvalid());
306f4a2713aSLionel Sambuc   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc),
307f4a2713aSLionel Sambuc                                    SourceMgr, LangOpts);
308f4a2713aSLionel Sambuc   EXPECT_TRUE(range.isInvalid());
309f4a2713aSLionel Sambuc   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
310f4a2713aSLionel Sambuc                                    SourceMgr, LangOpts);
311f4a2713aSLionel Sambuc   EXPECT_TRUE(!range.isTokenRange());
312f4a2713aSLionel Sambuc   EXPECT_EQ(range.getAsRange(),
313f4a2713aSLionel Sambuc             SourceRange(macroRange.getBegin(),
314f4a2713aSLionel Sambuc                         macroRange.getEnd().getLocWithOffset(1)));
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc   StringRef text = Lexer::getSourceText(
317f4a2713aSLionel Sambuc                                CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
318f4a2713aSLionel Sambuc                                SourceMgr, LangOpts);
319f4a2713aSLionel Sambuc   EXPECT_EQ(text, "M(foo)");
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   SourceLocation macroLsqrLoc = toks[3].getLocation();
322f4a2713aSLionel Sambuc   SourceLocation macroIdLoc = toks[4].getLocation();
323f4a2713aSLionel Sambuc   SourceLocation macroRsqrLoc = toks[5].getLocation();
324f4a2713aSLionel Sambuc   SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
325f4a2713aSLionel Sambuc   SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
326f4a2713aSLionel Sambuc   SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc   range = Lexer::makeFileCharRange(
329f4a2713aSLionel Sambuc       CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc),
330f4a2713aSLionel Sambuc       SourceMgr, LangOpts);
331f4a2713aSLionel Sambuc   EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
332f4a2713aSLionel Sambuc             range.getAsRange());
333f4a2713aSLionel Sambuc 
334f4a2713aSLionel Sambuc   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc),
335f4a2713aSLionel Sambuc                                    SourceMgr, LangOpts);
336f4a2713aSLionel Sambuc   EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
337f4a2713aSLionel Sambuc             range.getAsRange());
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc   macroPair = SourceMgr.getExpansionRange(macroLsqrLoc);
340f4a2713aSLionel Sambuc   range = Lexer::makeFileCharRange(
341f4a2713aSLionel Sambuc                      CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc),
342f4a2713aSLionel Sambuc                      SourceMgr, LangOpts);
343f4a2713aSLionel Sambuc   EXPECT_EQ(SourceRange(macroPair.first, macroPair.second.getLocWithOffset(1)),
344f4a2713aSLionel Sambuc             range.getAsRange());
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   text = Lexer::getSourceText(
347f4a2713aSLionel Sambuc           CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
348f4a2713aSLionel Sambuc           SourceMgr, LangOpts);
349f4a2713aSLionel Sambuc   EXPECT_EQ(text, "[bar");
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc 
352f4a2713aSLionel Sambuc   SourceLocation idLoc1 = toks[6].getLocation();
353f4a2713aSLionel Sambuc   SourceLocation idLoc2 = toks[7].getLocation();
354f4a2713aSLionel Sambuc   SourceLocation idLoc3 = toks[8].getLocation();
355f4a2713aSLionel Sambuc   SourceLocation idLoc4 = toks[9].getLocation();
356f4a2713aSLionel Sambuc   EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts));
357f4a2713aSLionel Sambuc   EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts));
358f4a2713aSLionel Sambuc   EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts));
359f4a2713aSLionel Sambuc   EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
360f4a2713aSLionel Sambuc }
361f4a2713aSLionel Sambuc 
362f4a2713aSLionel Sambuc } // anonymous namespace
363