xref: /llvm-project/clang/unittests/Lex/LexerTest.cpp (revision c0e275df3d5df8b8f400322d12d011f11eba39b9)
1 //===- unittests/Lex/LexerTest.cpp ------ Lexer tests ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "clang/Lex/Lexer.h"
10 #include "clang/Basic/Diagnostic.h"
11 #include "clang/Basic/DiagnosticOptions.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/LangOptions.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Basic/TargetInfo.h"
16 #include "clang/Basic/TargetOptions.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/HeaderSearchOptions.h"
19 #include "clang/Lex/MacroArgs.h"
20 #include "clang/Lex/MacroInfo.h"
21 #include "clang/Lex/ModuleLoader.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Lex/PreprocessorOptions.h"
24 #include "gtest/gtest.h"
25 
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   std::unique_ptr<Preprocessor> CreatePP(StringRef Source,
45                                          TrivialModuleLoader &ModLoader) {
46     std::unique_ptr<llvm::MemoryBuffer> Buf =
47         llvm::MemoryBuffer::getMemBuffer(Source);
48     SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
49 
50     HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
51                             Diags, LangOpts, Target.get());
52     std::unique_ptr<Preprocessor> PP = llvm::make_unique<Preprocessor>(
53         std::make_shared<PreprocessorOptions>(), Diags, LangOpts, SourceMgr,
54         HeaderInfo, ModLoader,
55         /*IILookup =*/nullptr,
56         /*OwnsHeaderSearch =*/false);
57     PP->Initialize(*Target);
58     PP->EnterMainSourceFile();
59     return PP;
60   }
61 
62   std::vector<Token> Lex(StringRef Source) {
63     TrivialModuleLoader ModLoader;
64     auto PP = CreatePP(Source, ModLoader);
65 
66     std::vector<Token> toks;
67     while (1) {
68       Token tok;
69       PP->Lex(tok);
70       if (tok.is(tok::eof))
71         break;
72       toks.push_back(tok);
73     }
74 
75     return toks;
76   }
77 
78   std::vector<Token> CheckLex(StringRef Source,
79                               ArrayRef<tok::TokenKind> ExpectedTokens) {
80     auto toks = Lex(Source);
81     EXPECT_EQ(ExpectedTokens.size(), toks.size());
82     for (unsigned i = 0, e = ExpectedTokens.size(); i != e; ++i) {
83       EXPECT_EQ(ExpectedTokens[i], toks[i].getKind());
84     }
85 
86     return toks;
87   }
88 
89   std::string getSourceText(Token Begin, Token End) {
90     bool Invalid;
91     StringRef Str =
92         Lexer::getSourceText(CharSourceRange::getTokenRange(SourceRange(
93                                     Begin.getLocation(), End.getLocation())),
94                              SourceMgr, LangOpts, &Invalid);
95     if (Invalid)
96       return "<INVALID>";
97     return Str;
98   }
99 
100   FileSystemOptions FileMgrOpts;
101   FileManager FileMgr;
102   IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
103   DiagnosticsEngine Diags;
104   SourceManager SourceMgr;
105   LangOptions LangOpts;
106   std::shared_ptr<TargetOptions> TargetOpts;
107   IntrusiveRefCntPtr<TargetInfo> Target;
108 };
109 
110 TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgument) {
111   std::vector<tok::TokenKind> ExpectedTokens;
112   ExpectedTokens.push_back(tok::identifier);
113   ExpectedTokens.push_back(tok::l_paren);
114   ExpectedTokens.push_back(tok::identifier);
115   ExpectedTokens.push_back(tok::r_paren);
116 
117   std::vector<Token> toks = CheckLex("#define M(x) x\n"
118                                      "M(f(M(i)))",
119                                      ExpectedTokens);
120 
121   EXPECT_EQ("M(i)", getSourceText(toks[2], toks[2]));
122 }
123 
124 TEST_F(LexerTest, GetSourceTextExpandsToMaximumInMacroArgumentForEndOfMacro) {
125   std::vector<tok::TokenKind> ExpectedTokens;
126   ExpectedTokens.push_back(tok::identifier);
127   ExpectedTokens.push_back(tok::identifier);
128 
129   std::vector<Token> toks = CheckLex("#define M(x) x\n"
130                                      "M(M(i) c)",
131                                      ExpectedTokens);
132 
133   EXPECT_EQ("M(i)", getSourceText(toks[0], toks[0]));
134 }
135 
136 TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForBeginOfMacro) {
137   std::vector<tok::TokenKind> ExpectedTokens;
138   ExpectedTokens.push_back(tok::identifier);
139   ExpectedTokens.push_back(tok::identifier);
140   ExpectedTokens.push_back(tok::identifier);
141 
142   std::vector<Token> toks = CheckLex("#define M(x) x\n"
143                                      "M(c c M(i))",
144                                      ExpectedTokens);
145 
146   EXPECT_EQ("c M(i)", getSourceText(toks[1], toks[2]));
147 }
148 
149 TEST_F(LexerTest, GetSourceTextExpandsInMacroArgumentForEndOfMacro) {
150   std::vector<tok::TokenKind> ExpectedTokens;
151   ExpectedTokens.push_back(tok::identifier);
152   ExpectedTokens.push_back(tok::identifier);
153   ExpectedTokens.push_back(tok::identifier);
154 
155   std::vector<Token> toks = CheckLex("#define M(x) x\n"
156                                      "M(M(i) c c)",
157                                      ExpectedTokens);
158 
159   EXPECT_EQ("M(i) c", getSourceText(toks[0], toks[1]));
160 }
161 
162 TEST_F(LexerTest, GetSourceTextInSeparateFnMacros) {
163   std::vector<tok::TokenKind> ExpectedTokens;
164   ExpectedTokens.push_back(tok::identifier);
165   ExpectedTokens.push_back(tok::identifier);
166   ExpectedTokens.push_back(tok::identifier);
167   ExpectedTokens.push_back(tok::identifier);
168 
169   std::vector<Token> toks = CheckLex("#define M(x) x\n"
170                                      "M(c M(i)) M(M(i) c)",
171                                      ExpectedTokens);
172 
173   EXPECT_EQ("<INVALID>", getSourceText(toks[1], toks[2]));
174 }
175 
176 TEST_F(LexerTest, GetSourceTextWorksAcrossTokenPastes) {
177   std::vector<tok::TokenKind> ExpectedTokens;
178   ExpectedTokens.push_back(tok::identifier);
179   ExpectedTokens.push_back(tok::l_paren);
180   ExpectedTokens.push_back(tok::identifier);
181   ExpectedTokens.push_back(tok::r_paren);
182 
183   std::vector<Token> toks = CheckLex("#define M(x) x\n"
184                                      "#define C(x) M(x##c)\n"
185                                      "M(f(C(i)))",
186                                      ExpectedTokens);
187 
188   EXPECT_EQ("C(i)", getSourceText(toks[2], toks[2]));
189 }
190 
191 TEST_F(LexerTest, GetSourceTextExpandsAcrossMultipleMacroCalls) {
192   std::vector<tok::TokenKind> ExpectedTokens;
193   ExpectedTokens.push_back(tok::identifier);
194   ExpectedTokens.push_back(tok::l_paren);
195   ExpectedTokens.push_back(tok::identifier);
196   ExpectedTokens.push_back(tok::r_paren);
197 
198   std::vector<Token> toks = CheckLex("#define M(x) x\n"
199                                      "f(M(M(i)))",
200                                      ExpectedTokens);
201   EXPECT_EQ("M(M(i))", getSourceText(toks[2], toks[2]));
202 }
203 
204 TEST_F(LexerTest, GetSourceTextInMiddleOfMacroArgument) {
205   std::vector<tok::TokenKind> ExpectedTokens;
206   ExpectedTokens.push_back(tok::identifier);
207   ExpectedTokens.push_back(tok::l_paren);
208   ExpectedTokens.push_back(tok::identifier);
209   ExpectedTokens.push_back(tok::r_paren);
210 
211   std::vector<Token> toks = CheckLex("#define M(x) x\n"
212                                      "M(f(i))",
213                                      ExpectedTokens);
214   EXPECT_EQ("i", getSourceText(toks[2], toks[2]));
215 }
216 
217 TEST_F(LexerTest, GetSourceTextExpandsAroundDifferentMacroCalls) {
218   std::vector<tok::TokenKind> ExpectedTokens;
219   ExpectedTokens.push_back(tok::identifier);
220   ExpectedTokens.push_back(tok::l_paren);
221   ExpectedTokens.push_back(tok::identifier);
222   ExpectedTokens.push_back(tok::r_paren);
223 
224   std::vector<Token> toks = CheckLex("#define M(x) x\n"
225                                      "#define C(x) x\n"
226                                      "f(C(M(i)))",
227                                      ExpectedTokens);
228   EXPECT_EQ("C(M(i))", getSourceText(toks[2], toks[2]));
229 }
230 
231 TEST_F(LexerTest, GetSourceTextOnlyExpandsIfFirstTokenInMacro) {
232   std::vector<tok::TokenKind> ExpectedTokens;
233   ExpectedTokens.push_back(tok::identifier);
234   ExpectedTokens.push_back(tok::l_paren);
235   ExpectedTokens.push_back(tok::identifier);
236   ExpectedTokens.push_back(tok::identifier);
237   ExpectedTokens.push_back(tok::r_paren);
238 
239   std::vector<Token> toks = CheckLex("#define M(x) x\n"
240                                      "#define C(x) c x\n"
241                                      "f(C(M(i)))",
242                                      ExpectedTokens);
243   EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
244 }
245 
246 TEST_F(LexerTest, GetSourceTextExpandsRecursively) {
247   std::vector<tok::TokenKind> ExpectedTokens;
248   ExpectedTokens.push_back(tok::identifier);
249   ExpectedTokens.push_back(tok::identifier);
250   ExpectedTokens.push_back(tok::l_paren);
251   ExpectedTokens.push_back(tok::identifier);
252   ExpectedTokens.push_back(tok::r_paren);
253 
254   std::vector<Token> toks = CheckLex("#define M(x) x\n"
255                                      "#define C(x) c M(x)\n"
256                                      "C(f(M(i)))",
257                                      ExpectedTokens);
258   EXPECT_EQ("M(i)", getSourceText(toks[3], toks[3]));
259 }
260 
261 TEST_F(LexerTest, LexAPI) {
262   std::vector<tok::TokenKind> ExpectedTokens;
263   ExpectedTokens.push_back(tok::l_square);
264   ExpectedTokens.push_back(tok::identifier);
265   ExpectedTokens.push_back(tok::r_square);
266   ExpectedTokens.push_back(tok::l_square);
267   ExpectedTokens.push_back(tok::identifier);
268   ExpectedTokens.push_back(tok::r_square);
269   ExpectedTokens.push_back(tok::identifier);
270   ExpectedTokens.push_back(tok::identifier);
271   ExpectedTokens.push_back(tok::identifier);
272   ExpectedTokens.push_back(tok::identifier);
273 
274   std::vector<Token> toks = CheckLex("#define M(x) [x]\n"
275                                      "#define N(x) x\n"
276                                      "#define INN(x) x\n"
277                                      "#define NOF1 INN(val)\n"
278                                      "#define NOF2 val\n"
279                                      "M(foo) N([bar])\n"
280                                      "N(INN(val)) N(NOF1) N(NOF2) N(val)",
281                                      ExpectedTokens);
282 
283   SourceLocation lsqrLoc = toks[0].getLocation();
284   SourceLocation idLoc = toks[1].getLocation();
285   SourceLocation rsqrLoc = toks[2].getLocation();
286   CharSourceRange macroRange = SourceMgr.getExpansionRange(lsqrLoc);
287 
288   SourceLocation Loc;
289   EXPECT_TRUE(Lexer::isAtStartOfMacroExpansion(lsqrLoc, SourceMgr, LangOpts, &Loc));
290   EXPECT_EQ(Loc, macroRange.getBegin());
291   EXPECT_FALSE(Lexer::isAtStartOfMacroExpansion(idLoc, SourceMgr, LangOpts));
292   EXPECT_FALSE(Lexer::isAtEndOfMacroExpansion(idLoc, SourceMgr, LangOpts));
293   EXPECT_TRUE(Lexer::isAtEndOfMacroExpansion(rsqrLoc, SourceMgr, LangOpts, &Loc));
294   EXPECT_EQ(Loc, macroRange.getEnd());
295   EXPECT_TRUE(macroRange.isTokenRange());
296 
297   CharSourceRange range = Lexer::makeFileCharRange(
298            CharSourceRange::getTokenRange(lsqrLoc, idLoc), SourceMgr, LangOpts);
299   EXPECT_TRUE(range.isInvalid());
300   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(idLoc, rsqrLoc),
301                                    SourceMgr, LangOpts);
302   EXPECT_TRUE(range.isInvalid());
303   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
304                                    SourceMgr, LangOpts);
305   EXPECT_TRUE(!range.isTokenRange());
306   EXPECT_EQ(range.getAsRange(),
307             SourceRange(macroRange.getBegin(),
308                         macroRange.getEnd().getLocWithOffset(1)));
309 
310   StringRef text = Lexer::getSourceText(
311                                CharSourceRange::getTokenRange(lsqrLoc, rsqrLoc),
312                                SourceMgr, LangOpts);
313   EXPECT_EQ(text, "M(foo)");
314 
315   SourceLocation macroLsqrLoc = toks[3].getLocation();
316   SourceLocation macroIdLoc = toks[4].getLocation();
317   SourceLocation macroRsqrLoc = toks[5].getLocation();
318   SourceLocation fileLsqrLoc = SourceMgr.getSpellingLoc(macroLsqrLoc);
319   SourceLocation fileIdLoc = SourceMgr.getSpellingLoc(macroIdLoc);
320   SourceLocation fileRsqrLoc = SourceMgr.getSpellingLoc(macroRsqrLoc);
321 
322   range = Lexer::makeFileCharRange(
323       CharSourceRange::getTokenRange(macroLsqrLoc, macroIdLoc),
324       SourceMgr, LangOpts);
325   EXPECT_EQ(SourceRange(fileLsqrLoc, fileIdLoc.getLocWithOffset(3)),
326             range.getAsRange());
327 
328   range = Lexer::makeFileCharRange(CharSourceRange::getTokenRange(macroIdLoc, macroRsqrLoc),
329                                    SourceMgr, LangOpts);
330   EXPECT_EQ(SourceRange(fileIdLoc, fileRsqrLoc.getLocWithOffset(1)),
331             range.getAsRange());
332 
333   macroRange = SourceMgr.getExpansionRange(macroLsqrLoc);
334   range = Lexer::makeFileCharRange(
335                      CharSourceRange::getTokenRange(macroLsqrLoc, macroRsqrLoc),
336                      SourceMgr, LangOpts);
337   EXPECT_EQ(SourceRange(macroRange.getBegin(), macroRange.getEnd().getLocWithOffset(1)),
338             range.getAsRange());
339 
340   text = Lexer::getSourceText(
341           CharSourceRange::getTokenRange(SourceRange(macroLsqrLoc, macroIdLoc)),
342           SourceMgr, LangOpts);
343   EXPECT_EQ(text, "[bar");
344 
345 
346   SourceLocation idLoc1 = toks[6].getLocation();
347   SourceLocation idLoc2 = toks[7].getLocation();
348   SourceLocation idLoc3 = toks[8].getLocation();
349   SourceLocation idLoc4 = toks[9].getLocation();
350   EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc1, SourceMgr, LangOpts));
351   EXPECT_EQ("INN", Lexer::getImmediateMacroName(idLoc2, SourceMgr, LangOpts));
352   EXPECT_EQ("NOF2", Lexer::getImmediateMacroName(idLoc3, SourceMgr, LangOpts));
353   EXPECT_EQ("N", Lexer::getImmediateMacroName(idLoc4, SourceMgr, LangOpts));
354 }
355 
356 TEST_F(LexerTest, DontMergeMacroArgsFromDifferentMacroFiles) {
357   std::vector<Token> toks =
358       Lex("#define helper1 0\n"
359           "void helper2(const char *, ...);\n"
360           "#define M1(a, ...) helper2(a, ##__VA_ARGS__)\n"
361           "#define M2(a, ...) M1(a, helper1, ##__VA_ARGS__)\n"
362           "void f1() { M2(\"a\", \"b\"); }");
363 
364   // Check the file corresponding to the "helper1" macro arg in M2.
365   //
366   // The lexer used to report its size as 31, meaning that the end of the
367   // expansion would be on the *next line* (just past `M2("a", "b")`). Make
368   // sure that we get the correct end location (the comma after "helper1").
369   SourceLocation helper1ArgLoc = toks[20].getLocation();
370   EXPECT_EQ(SourceMgr.getFileIDSize(SourceMgr.getFileID(helper1ArgLoc)), 8U);
371 }
372 
373 TEST_F(LexerTest, DontOverallocateStringifyArgs) {
374   TrivialModuleLoader ModLoader;
375   auto PP = CreatePP("\"StrArg\", 5, 'C'", ModLoader);
376 
377   llvm::BumpPtrAllocator Allocator;
378   std::array<IdentifierInfo *, 3> ParamList;
379   MacroInfo *MI = PP->AllocateMacroInfo({});
380   MI->setIsFunctionLike();
381   MI->setParameterList(ParamList, Allocator);
382   EXPECT_EQ(3u, MI->getNumParams());
383   EXPECT_TRUE(MI->isFunctionLike());
384 
385   Token Eof;
386   Eof.setKind(tok::eof);
387   std::vector<Token> ArgTokens;
388   while (1) {
389     Token tok;
390     PP->Lex(tok);
391     if (tok.is(tok::eof)) {
392       ArgTokens.push_back(Eof);
393       break;
394     }
395     if (tok.is(tok::comma))
396       ArgTokens.push_back(Eof);
397     else
398       ArgTokens.push_back(tok);
399   }
400 
401   auto MacroArgsDeleter = [&PP](MacroArgs *M) { M->destroy(*PP); };
402   std::unique_ptr<MacroArgs, decltype(MacroArgsDeleter)> MA(
403       MacroArgs::create(MI, ArgTokens, false, *PP), MacroArgsDeleter);
404   auto StringifyArg = [&](int ArgNo) {
405     return MA->StringifyArgument(MA->getUnexpArgument(ArgNo), *PP,
406                                  /*Charify=*/false, {}, {});
407   };
408   Token Result = StringifyArg(0);
409   EXPECT_EQ(tok::string_literal, Result.getKind());
410   EXPECT_STREQ("\"\\\"StrArg\\\"\"", Result.getLiteralData());
411   Result = StringifyArg(1);
412   EXPECT_EQ(tok::string_literal, Result.getKind());
413   EXPECT_STREQ("\"5\"", Result.getLiteralData());
414   Result = StringifyArg(2);
415   EXPECT_EQ(tok::string_literal, Result.getKind());
416   EXPECT_STREQ("\"'C'\"", Result.getLiteralData());
417 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
418   EXPECT_DEATH(StringifyArg(3), "Invalid arg #");
419 #endif
420 }
421 
422 TEST_F(LexerTest, IsNewLineEscapedValid) {
423   auto hasNewLineEscaped = [](const char *S) {
424     return Lexer::isNewLineEscaped(S, S + strlen(S) - 1);
425   };
426 
427   EXPECT_TRUE(hasNewLineEscaped("\\\r"));
428   EXPECT_TRUE(hasNewLineEscaped("\\\n"));
429   EXPECT_TRUE(hasNewLineEscaped("\\\r\n"));
430   EXPECT_TRUE(hasNewLineEscaped("\\\n\r"));
431   EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r"));
432   EXPECT_TRUE(hasNewLineEscaped("\\ \t\v\f\r\n"));
433 
434   EXPECT_FALSE(hasNewLineEscaped("\\\r\r"));
435   EXPECT_FALSE(hasNewLineEscaped("\\\r\r\n"));
436   EXPECT_FALSE(hasNewLineEscaped("\\\n\n"));
437   EXPECT_FALSE(hasNewLineEscaped("\r"));
438   EXPECT_FALSE(hasNewLineEscaped("\n"));
439   EXPECT_FALSE(hasNewLineEscaped("\r\n"));
440   EXPECT_FALSE(hasNewLineEscaped("\n\r"));
441   EXPECT_FALSE(hasNewLineEscaped("\r\r"));
442   EXPECT_FALSE(hasNewLineEscaped("\n\n"));
443 }
444 
445 TEST_F(LexerTest, GetBeginningOfTokenWithEscapedNewLine) {
446   // Each line should have the same length for
447   // further offset calculation to be more straightforward.
448   const unsigned IdentifierLength = 8;
449   std::string TextToLex = "rabarbar\n"
450                           "foo\\\nbar\n"
451                           "foo\\\rbar\n"
452                           "fo\\\r\nbar\n"
453                           "foo\\\n\rba\n";
454   std::vector<tok::TokenKind> ExpectedTokens{5, tok::identifier};
455   std::vector<Token> LexedTokens = CheckLex(TextToLex, ExpectedTokens);
456 
457   for (const Token &Tok : LexedTokens) {
458     std::pair<FileID, unsigned> OriginalLocation =
459         SourceMgr.getDecomposedLoc(Tok.getLocation());
460     for (unsigned Offset = 0; Offset < IdentifierLength; ++Offset) {
461       SourceLocation LookupLocation =
462           Tok.getLocation().getLocWithOffset(Offset);
463 
464       std::pair<FileID, unsigned> FoundLocation =
465           SourceMgr.getDecomposedExpansionLoc(
466               Lexer::GetBeginningOfToken(LookupLocation, SourceMgr, LangOpts));
467 
468       // Check that location returned by the GetBeginningOfToken
469       // is the same as original token location reported by Lexer.
470       EXPECT_EQ(FoundLocation.second, OriginalLocation.second);
471     }
472   }
473 }
474 
475 TEST_F(LexerTest, AvoidPastEndOfStringDereference) {
476   EXPECT_TRUE(Lex("  //  \\\n").empty());
477   EXPECT_TRUE(Lex("#include <\\\\").empty());
478   EXPECT_TRUE(Lex("#include <\\\\\n").empty());
479 }
480 
481 TEST_F(LexerTest, StringizingRasString) {
482   // For "std::string Lexer::Stringify(StringRef Str, bool Charify)".
483   std::string String1 = R"(foo
484     {"bar":[]}
485     baz)";
486   // For "void Lexer::Stringify(SmallVectorImpl<char> &Str)".
487   SmallString<128> String2;
488   String2 += String1.c_str();
489 
490   // Corner cases.
491   std::string String3 = R"(\
492     \n
493     \\n
494     \\)";
495   SmallString<128> String4;
496   String4 += String3.c_str();
497   std::string String5 = R"(a\
498 
499 
500     \\b)";
501   SmallString<128> String6;
502   String6 += String5.c_str();
503 
504   String1 = Lexer::Stringify(StringRef(String1));
505   Lexer::Stringify(String2);
506   String3 = Lexer::Stringify(StringRef(String3));
507   Lexer::Stringify(String4);
508   String5 = Lexer::Stringify(StringRef(String5));
509   Lexer::Stringify(String6);
510 
511   EXPECT_EQ(String1, R"(foo\n    {\"bar\":[]}\n    baz)");
512   EXPECT_EQ(String2, R"(foo\n    {\"bar\":[]}\n    baz)");
513   EXPECT_EQ(String3, R"(\\\n    \\n\n    \\\\n\n    \\\\)");
514   EXPECT_EQ(String4, R"(\\\n    \\n\n    \\\\n\n    \\\\)");
515   EXPECT_EQ(String5, R"(a\\\n\n\n    \\\\b)");
516   EXPECT_EQ(String6, R"(a\\\n\n\n    \\\\b)");
517 }
518 
519 TEST_F(LexerTest, CharRangeOffByOne) {
520   std::vector<Token> toks = Lex(R"(#define MOO 1
521     void foo() { MOO; })");
522   const Token &moo = toks[5];
523 
524   EXPECT_EQ(getSourceText(moo, moo), "MOO");
525 
526   SourceRange R{moo.getLocation(), moo.getLocation()};
527 
528   EXPECT_TRUE(
529       Lexer::isAtStartOfMacroExpansion(R.getBegin(), SourceMgr, LangOpts));
530   EXPECT_TRUE(
531       Lexer::isAtEndOfMacroExpansion(R.getEnd(), SourceMgr, LangOpts));
532 
533   CharSourceRange CR = Lexer::getAsCharRange(R, SourceMgr, LangOpts);
534 
535   EXPECT_EQ(Lexer::getSourceText(CR, SourceMgr, LangOpts), "MOO"); // Was "MO".
536 }
537 
538 } // anonymous namespace
539