Debian Codegraph @

clang/unittests/Lex/PPCallbacksTest.cpp

raw
1 //===- unittests/Lex/PPCallbacksTest.cpp - PPCallbacks 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/Preprocessor.h"
10 #include "clang/AST/ASTConsumer.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/Basic/Diagnostic.h"
13 #include "clang/Basic/DiagnosticOptions.h"
14 #include "clang/Basic/FileManager.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Basic/TargetInfo.h"
18 #include "clang/Basic/TargetOptions.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/HeaderSearchOptions.h"
21 #include "clang/Lex/ModuleLoader.h"
22 #include "clang/Lex/PreprocessorOptions.h"
23 #include "clang/Parse/Parser.h"
24 #include "clang/Sema/Sema.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/Support/Path.h"
27 #include "gtest/gtest.h"
28
29 using namespace clang;
30
31 namespace {
32
33 // Stub to collect data from InclusionDirective callbacks.
34 class InclusionDirectiveCallbacks : public PPCallbacks {
35 public:
36 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
37 StringRef FileName, bool IsAngled,
38 CharSourceRange FilenameRange,
39 OptionalFileEntryRef File, StringRef SearchPath,
40 StringRef RelativePath, const Module *SuggestedModule,
41 bool ModuleImported,
42 SrcMgr::CharacteristicKind FileType) override {
43 this->HashLoc = HashLoc;
44 this->IncludeTok = IncludeTok;
45 this->FileName = FileName.str();
46 this->IsAngled = IsAngled;
47 this->FilenameRange = FilenameRange;
48 this->File = File;
49 this->SearchPath = SearchPath.str();
50 this->RelativePath = RelativePath.str();
51 this->SuggestedModule = SuggestedModule;
52 this->ModuleImported = ModuleImported;
53 this->FileType = FileType;
54 }
55
56 SourceLocation HashLoc;
57 Token IncludeTok;
58 SmallString<16> FileName;
59 bool IsAngled;
60 CharSourceRange FilenameRange;
61 OptionalFileEntryRef File;
62 SmallString<16> SearchPath;
63 SmallString<16> RelativePath;
64 const Module *SuggestedModule;
65 bool ModuleImported;
66 SrcMgr::CharacteristicKind FileType;
67 };
68
69 class CondDirectiveCallbacks : public PPCallbacks {
70 public:
71 struct Result {
72 SourceRange ConditionRange;
73 ConditionValueKind ConditionValue;
74
75 Result(SourceRange R, ConditionValueKind K)
76 : ConditionRange(R), ConditionValue(K) {}
77 };
78
79 std::vector<Result> Results;
80
81 void If(SourceLocation Loc, SourceRange ConditionRange,
82 ConditionValueKind ConditionValue) override {
83 Results.emplace_back(ConditionRange, ConditionValue);
84 }
85
86 void Elif(SourceLocation Loc, SourceRange ConditionRange,
87 ConditionValueKind ConditionValue, SourceLocation IfLoc) override {
88 Results.emplace_back(ConditionRange, ConditionValue);
89 }
90 };
91
92 // Stub to collect data from PragmaOpenCLExtension callbacks.
93 class PragmaOpenCLExtensionCallbacks : public PPCallbacks {
94 public:
95 typedef struct {
96 SmallString<16> Name;
97 unsigned State;
98 } CallbackParameters;
99
100 PragmaOpenCLExtensionCallbacks() : Name("Not called."), State(99) {}
101
102 void PragmaOpenCLExtension(clang::SourceLocation NameLoc,
103 const clang::IdentifierInfo *Name,
104 clang::SourceLocation StateLoc,
105 unsigned State) override {
106 this->NameLoc = NameLoc;
107 this->Name = Name->getName();
108 this->StateLoc = StateLoc;
109 this->State = State;
110 }
111
112 SourceLocation NameLoc;
113 SmallString<16> Name;
114 SourceLocation StateLoc;
115 unsigned State;
116 };
117
118 class PragmaMarkCallbacks : public PPCallbacks {
119 public:
120 struct Mark {
121 SourceLocation Location;
122 std::string Trivia;
123 };
124
125 std::vector<Mark> Marks;
126
127 void PragmaMark(SourceLocation Loc, StringRef Trivia) override {
128 Marks.emplace_back(Mark{Loc, Trivia.str()});
129 }
130 };
131
132 // PPCallbacks test fixture.
133 class PPCallbacksTest : public ::testing::Test {
134 protected:
135 PPCallbacksTest()
136 : InMemoryFileSystem(
137 llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>()),
138 FileMgr(FileSystemOptions(), InMemoryFileSystem),
139 DiagID(DiagnosticIDs::create()),
140 Diags(DiagID, DiagOpts, new IgnoringDiagConsumer()),
141 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions()) {
142 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
143 Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);
144 }
145
146 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem;
147 FileManager FileMgr;
148 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
149 DiagnosticOptions DiagOpts;
150 DiagnosticsEngine Diags;
151 SourceManager SourceMgr;
152 LangOptions LangOpts;
153 std::shared_ptr<TargetOptions> TargetOpts;
154 IntrusiveRefCntPtr<TargetInfo> Target;
155
156 // Register a header path as a known file and add its location
157 // to search path.
158 void AddFakeHeader(HeaderSearch &HeaderInfo, const char *HeaderPath,
159 bool IsSystemHeader) {
160 // Tell FileMgr about header.
161 InMemoryFileSystem->addFile(HeaderPath, 0,
162 llvm::MemoryBuffer::getMemBuffer("\n"));
163
164 // Add header's parent path to search path.
165 StringRef SearchPath = llvm::sys::path::parent_path(HeaderPath);
166 auto DE = FileMgr.getOptionalDirectoryRef(SearchPath);
167 DirectoryLookup DL(*DE, SrcMgr::C_User, false);
168 HeaderInfo.AddSearchPath(DL, IsSystemHeader);
169 }
170
171 // Get the raw source string of the range.
172 StringRef GetSourceString(CharSourceRange Range) {
173 const char* B = SourceMgr.getCharacterData(Range.getBegin());
174 const char* E = SourceMgr.getCharacterData(Range.getEnd());
175
176 return StringRef(B, E - B);
177 }
178
179 StringRef GetSourceStringToEnd(CharSourceRange Range) {
180 const char *B = SourceMgr.getCharacterData(Range.getBegin());
181 const char *E = SourceMgr.getCharacterData(Range.getEnd());
182
183 return StringRef(
184 B,
185 E - B + Lexer::MeasureTokenLength(Range.getEnd(), SourceMgr, LangOpts));
186 }
187
188 // Run lexer over SourceText and collect FilenameRange from
189 // the InclusionDirective callback.
190 CharSourceRange InclusionDirectiveFilenameRange(const char *SourceText,
191 const char *HeaderPath,
192 bool SystemHeader) {
193 std::unique_ptr<llvm::MemoryBuffer> Buf =
194 llvm::MemoryBuffer::getMemBuffer(SourceText);
195 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
196
197 HeaderSearchOptions HSOpts;
198 TrivialModuleLoader ModLoader;
199
200 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
201 AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
202
203 PreprocessorOptions PPOpts;
204 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
205 /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
206 return InclusionDirectiveCallback(PP)->FilenameRange;
207 }
208
209 SrcMgr::CharacteristicKind InclusionDirectiveCharacteristicKind(
210 const char *SourceText, const char *HeaderPath, bool SystemHeader) {
211 std::unique_ptr<llvm::MemoryBuffer> Buf =
212 llvm::MemoryBuffer::getMemBuffer(SourceText);
213 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
214
215 HeaderSearchOptions HSOpts;
216 TrivialModuleLoader ModLoader;
217
218 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
219 AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader);
220
221 PreprocessorOptions PPOpts;
222 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
223 /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
224 return InclusionDirectiveCallback(PP)->FileType;
225 }
226
227 InclusionDirectiveCallbacks *InclusionDirectiveCallback(Preprocessor &PP) {
228 PP.Initialize(*Target);
229 InclusionDirectiveCallbacks* Callbacks = new InclusionDirectiveCallbacks;
230 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
231
232 // Lex source text.
233 PP.EnterMainSourceFile();
234 PP.LexTokensUntilEOF();
235
236 // Callbacks have been executed at this point -- return filename range.
237 return Callbacks;
238 }
239
240 std::vector<CondDirectiveCallbacks::Result>
241 DirectiveExprRange(StringRef SourceText, PreprocessorOptions PPOpts = {}) {
242 HeaderSearchOptions HSOpts;
243 TrivialModuleLoader ModLoader;
244 std::unique_ptr<llvm::MemoryBuffer> Buf =
245 llvm::MemoryBuffer::getMemBuffer(SourceText);
246 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
247 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
248 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
249 /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
250 PP.Initialize(*Target);
251 auto *Callbacks = new CondDirectiveCallbacks;
252 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
253
254 // Lex source text.
255 PP.EnterMainSourceFile();
256 PP.LexTokensUntilEOF();
257
258 return Callbacks->Results;
259 }
260
261 std::vector<PragmaMarkCallbacks::Mark>
262 PragmaMarkCall(const char *SourceText) {
263 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
264 llvm::MemoryBuffer::getMemBuffer(SourceText, "test.c");
265 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
266
267 HeaderSearchOptions HSOpts;
268 TrivialModuleLoader ModLoader;
269 PreprocessorOptions PPOpts;
270
271 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
272
273 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,
274 /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
275 PP.Initialize(*Target);
276
277 auto *Callbacks = new PragmaMarkCallbacks;
278 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
279
280 // Lex source text.
281 PP.EnterMainSourceFile();
282 PP.LexTokensUntilEOF();
283
284 return Callbacks->Marks;
285 }
286
287 PragmaOpenCLExtensionCallbacks::CallbackParameters
288 PragmaOpenCLExtensionCall(const char *SourceText) {
289 LangOptions OpenCLLangOpts;
290 OpenCLLangOpts.OpenCL = 1;
291
292 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
293 llvm::MemoryBuffer::getMemBuffer(SourceText, "test.cl");
294 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
295
296 HeaderSearchOptions HSOpts;
297 TrivialModuleLoader ModLoader;
298 PreprocessorOptions PPOpts;
299
300 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, OpenCLLangOpts,
301 Target.get());
302
303 Preprocessor PP(PPOpts, Diags, OpenCLLangOpts, SourceMgr, HeaderInfo,
304 ModLoader, /*IILookup=*/nullptr,
305 /*OwnsHeaderSearch=*/false);
306 PP.Initialize(*Target);
307
308 // parser actually sets correct pragma handlers for preprocessor
309 // according to LangOptions, so we init Parser to register opencl
310 // pragma handlers
311 ASTContext Context(OpenCLLangOpts, SourceMgr, PP.getIdentifierTable(),
312 PP.getSelectorTable(), PP.getBuiltinInfo(), PP.TUKind);
313 Context.InitBuiltinTypes(*Target);
314
315 ASTConsumer Consumer;
316 Sema S(PP, Context, Consumer);
317 Parser P(PP, S, false);
318 PragmaOpenCLExtensionCallbacks* Callbacks = new PragmaOpenCLExtensionCallbacks;
319 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(Callbacks));
320
321 // Lex source text.
322 PP.EnterMainSourceFile();
323 PP.LexTokensUntilEOF();
324
325 PragmaOpenCLExtensionCallbacks::CallbackParameters RetVal = {
326 Callbacks->Name,
327 Callbacks->State
328 };
329 return RetVal;
330 }
331 };
332
333 TEST_F(PPCallbacksTest, UserFileCharacteristics) {
334 const char *Source = "#include \"quoted.h\"\n";
335
336 SrcMgr::CharacteristicKind Kind =
337 InclusionDirectiveCharacteristicKind(Source, "/quoted.h", false);
338
339 ASSERT_EQ(SrcMgr::CharacteristicKind::C_User, Kind);
340 }
341
342 TEST_F(PPCallbacksTest, QuotedFilename) {
343 const char* Source =
344 "#include \"quoted.h\"\n";
345
346 CharSourceRange Range =
347 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
348
349 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
350 }
351
352 TEST_F(PPCallbacksTest, AngledFilename) {
353 const char* Source =
354 "#include <angled.h>\n";
355
356 CharSourceRange Range =
357 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
358
359 ASSERT_EQ("<angled.h>", GetSourceString(Range));
360 }
361
362 TEST_F(PPCallbacksTest, QuotedInMacro) {
363 const char* Source =
364 "#define MACRO_QUOTED \"quoted.h\"\n"
365 "#include MACRO_QUOTED\n";
366
367 CharSourceRange Range =
368 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
369
370 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
371 }
372
373 TEST_F(PPCallbacksTest, AngledInMacro) {
374 const char* Source =
375 "#define MACRO_ANGLED <angled.h>\n"
376 "#include MACRO_ANGLED\n";
377
378 CharSourceRange Range =
379 InclusionDirectiveFilenameRange(Source, "/angled.h", true);
380
381 ASSERT_EQ("<angled.h>", GetSourceString(Range));
382 }
383
384 TEST_F(PPCallbacksTest, StringizedMacroArgument) {
385 const char* Source =
386 "#define MACRO_STRINGIZED(x) #x\n"
387 "#include MACRO_STRINGIZED(quoted.h)\n";
388
389 CharSourceRange Range =
390 InclusionDirectiveFilenameRange(Source, "/quoted.h", false);
391
392 ASSERT_EQ("\"quoted.h\"", GetSourceString(Range));
393 }
394
395 TEST_F(PPCallbacksTest, ConcatenatedMacroArgument) {
396 const char* Source =
397 "#define MACRO_ANGLED <angled.h>\n"
398 "#define MACRO_CONCAT(x, y) x ## _ ## y\n"
399 "#include MACRO_CONCAT(MACRO, ANGLED)\n";
400
401 CharSourceRange Range =
402 InclusionDirectiveFilenameRange(Source, "/angled.h", false);
403
404 ASSERT_EQ("<angled.h>", GetSourceString(Range));
405 }
406
407 TEST_F(PPCallbacksTest, TrigraphFilename) {
408 const char* Source =
409 "#include \"tri\?\?-graph.h\"\n";
410
411 CharSourceRange Range =
412 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
413
414 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
415 }
416
417 TEST_F(PPCallbacksTest, TrigraphInMacro) {
418 const char* Source =
419 "#define MACRO_TRIGRAPH \"tri\?\?-graph.h\"\n"
420 "#include MACRO_TRIGRAPH\n";
421
422 CharSourceRange Range =
423 InclusionDirectiveFilenameRange(Source, "/tri~graph.h", false);
424
425 ASSERT_EQ("\"tri\?\?-graph.h\"", GetSourceString(Range));
426 }
427
428 TEST_F(PPCallbacksTest, FileNotFoundSkipped) {
429 const char *SourceText = "#include \"skipped.h\"\n";
430
431 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
432 llvm::MemoryBuffer::getMemBuffer(SourceText);
433 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
434
435 HeaderSearchOptions HSOpts;
436 TrivialModuleLoader ModLoader;
437 PreprocessorOptions PPOpts;
438 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
439
440 unsigned int NumCalls = 0;
441 DiagnosticConsumer *DiagConsumer = new DiagnosticConsumer;
442 DiagnosticsEngine FileNotFoundDiags(DiagID, DiagOpts, DiagConsumer);
443 Preprocessor PP(PPOpts, FileNotFoundDiags, LangOpts, SourceMgr, HeaderInfo,
444 ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false);
445 PP.Initialize(*Target);
446
447 class FileNotFoundCallbacks : public PPCallbacks {
448 public:
449 unsigned int &NumCalls;
450
451 FileNotFoundCallbacks(unsigned int &NumCalls) : NumCalls(NumCalls) {}
452
453 bool FileNotFound(StringRef FileName) override {
454 NumCalls++;
455 return FileName == "skipped.h";
456 }
457 };
458
459 PP.addPPCallbacks(std::make_unique<FileNotFoundCallbacks>(NumCalls));
460
461 // Lex source text.
462 PP.EnterMainSourceFile();
463 PP.LexTokensUntilEOF();
464
465 ASSERT_EQ(1u, NumCalls);
466 ASSERT_EQ(0u, DiagConsumer->getNumErrors());
467 }
468
469 TEST_F(PPCallbacksTest, EmbedFileNotFoundChained) {
470 const char *SourceText = "#embed \"notfound.h\"\n";
471
472 std::unique_ptr<llvm::MemoryBuffer> SourceBuf =
473 llvm::MemoryBuffer::getMemBuffer(SourceText);
474 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(SourceBuf)));
475
476 unsigned int NumCalls = 0;
477 HeaderSearchOptions HSOpts;
478 TrivialModuleLoader ModLoader;
479 PreprocessorOptions PPOpts;
480 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());
481
482 DiagnosticConsumer *DiagConsumer = new DiagnosticConsumer;
483 DiagnosticsEngine EmbedFileNotFoundDiags(DiagID, DiagOpts, DiagConsumer);
484 Preprocessor PP(PPOpts, EmbedFileNotFoundDiags, LangOpts, SourceMgr,
485 HeaderInfo, ModLoader, /*IILookup=*/nullptr,
486 /*OwnsHeaderSearch=*/false);
487 PP.Initialize(*Target);
488
489 class EmbedFileNotFoundCallbacks : public PPCallbacks {
490 public:
491 unsigned int &NumCalls;
492
493 EmbedFileNotFoundCallbacks(unsigned int &NumCalls) : NumCalls(NumCalls) {}
494
495 bool EmbedFileNotFound(StringRef FileName) override {
496 NumCalls++;
497 return true;
498 }
499 };
500
501 // Add two instances of `EmbedFileNotFoundCallbacks` to ensure the
502 // preprocessor is using an instance of `PPChainedCallbaks`.
503 PP.addPPCallbacks(std::make_unique<EmbedFileNotFoundCallbacks>(NumCalls));
504 PP.addPPCallbacks(std::make_unique<EmbedFileNotFoundCallbacks>(NumCalls));
505
506 // Lex source text.
507 PP.EnterMainSourceFile();
508 PP.LexTokensUntilEOF();
509
510 ASSERT_EQ(2u, NumCalls);
511 ASSERT_EQ(0u, DiagConsumer->getNumErrors());
512 }
513
514 TEST_F(PPCallbacksTest, OpenCLExtensionPragmaEnabled) {
515 const char* Source =
516 "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n";
517
518 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
519 PragmaOpenCLExtensionCall(Source);
520
521 ASSERT_EQ("cl_khr_fp64", Parameters.Name);
522 unsigned ExpectedState = 1;
523 ASSERT_EQ(ExpectedState, Parameters.State);
524 }
525
526 TEST_F(PPCallbacksTest, OpenCLExtensionPragmaDisabled) {
527 const char* Source =
528 "#pragma OPENCL EXTENSION cl_khr_fp16 : disable\n";
529
530 PragmaOpenCLExtensionCallbacks::CallbackParameters Parameters =
531 PragmaOpenCLExtensionCall(Source);
532
533 ASSERT_EQ("cl_khr_fp16", Parameters.Name);
534 unsigned ExpectedState = 0;
535 ASSERT_EQ(ExpectedState, Parameters.State);
536 }
537
538 TEST_F(PPCallbacksTest, CollectMarks) {
539 const char *Source =
540 "#pragma mark\n"
541 "#pragma mark\r\n"
542 "#pragma mark - trivia\n"
543 "#pragma mark - trivia\r\n";
544
545 auto Marks = PragmaMarkCall(Source);
546
547 ASSERT_EQ(4u, Marks.size());
548 ASSERT_TRUE(Marks[0].Trivia.empty());
549 ASSERT_TRUE(Marks[1].Trivia.empty());
550 ASSERT_FALSE(Marks[2].Trivia.empty());
551 ASSERT_FALSE(Marks[3].Trivia.empty());
552 ASSERT_EQ(" - trivia", Marks[2].Trivia);
553 ASSERT_EQ(" - trivia", Marks[3].Trivia);
554 }
555
556 TEST_F(PPCallbacksTest, DirectiveExprRanges) {
557 const auto &Results1 = DirectiveExprRange("#if FLUZZY_FLOOF\n#endif\n");
558 EXPECT_EQ(Results1.size(), 1U);
559 EXPECT_EQ(
560 GetSourceStringToEnd(CharSourceRange(Results1[0].ConditionRange, false)),
561 "FLUZZY_FLOOF");
562
563 const auto &Results2 = DirectiveExprRange("#if 1 + 4 < 7\n#endif\n");
564 EXPECT_EQ(Results2.size(), 1U);
565 EXPECT_EQ(
566 GetSourceStringToEnd(CharSourceRange(Results2[0].ConditionRange, false)),
567 "1 + 4 < 7");
568
569 const auto &Results3 = DirectiveExprRange("#if 1 + \\\n 2\n#endif\n");
570 EXPECT_EQ(Results3.size(), 1U);
571 EXPECT_EQ(
572 GetSourceStringToEnd(CharSourceRange(Results3[0].ConditionRange, false)),
573 "1 + \\\n 2");
574
575 const auto &Results4 = DirectiveExprRange("#if 0\n#elif FLOOFY\n#endif\n");
576 EXPECT_EQ(Results4.size(), 2U);
577 EXPECT_EQ(
578 GetSourceStringToEnd(CharSourceRange(Results4[0].ConditionRange, false)),
579 "0");
580 EXPECT_EQ(
581 GetSourceStringToEnd(CharSourceRange(Results4[1].ConditionRange, false)),
582 "FLOOFY");
583
584 const auto &Results5 = DirectiveExprRange("#if 1\n#elif FLOOFY\n#endif\n");
585 EXPECT_EQ(Results5.size(), 2U);
586 EXPECT_EQ(
587 GetSourceStringToEnd(CharSourceRange(Results5[0].ConditionRange, false)),
588 "1");
589 EXPECT_EQ(
590 GetSourceStringToEnd(CharSourceRange(Results5[1].ConditionRange, false)),
591 "FLOOFY");
592
593 const auto &Results6 =
594 DirectiveExprRange("#if defined(FLUZZY_FLOOF)\n#endif\n");
595 EXPECT_EQ(Results6.size(), 1U);
596 EXPECT_EQ(
597 GetSourceStringToEnd(CharSourceRange(Results6[0].ConditionRange, false)),
598 "defined(FLUZZY_FLOOF)");
599
600 const auto &Results7 =
601 DirectiveExprRange("#if 1\n#elif defined(FLOOFY)\n#endif\n");
602 EXPECT_EQ(Results7.size(), 2U);
603 EXPECT_EQ(
604 GetSourceStringToEnd(CharSourceRange(Results7[0].ConditionRange, false)),
605 "1");
606 EXPECT_EQ(
607 GetSourceStringToEnd(CharSourceRange(Results7[1].ConditionRange, false)),
608 "defined(FLOOFY)");
609
610 const auto &Results8 =
611 DirectiveExprRange("#define FLOOFY 0\n#if __FILE__ > FLOOFY\n#endif\n");
612 EXPECT_EQ(Results8.size(), 1U);
613 EXPECT_EQ(
614 GetSourceStringToEnd(CharSourceRange(Results8[0].ConditionRange, false)),
615 "__FILE__ > FLOOFY");
616 EXPECT_EQ(
617 Lexer::getSourceText(CharSourceRange(Results8[0].ConditionRange, false),
618 SourceMgr, LangOpts),
619 "__FILE__ > FLOOFY");
620
621 const char *MultiExprIf = "#if defined(FLOOFY) || defined(FLUZZY)\n#endif\n";
622 const auto &Results9 = DirectiveExprRange(MultiExprIf);
623 EXPECT_EQ(Results9.size(), 1U);
624 EXPECT_EQ(
625 Lexer::getSourceText(CharSourceRange(Results9[0].ConditionRange, false),
626 SourceMgr, LangOpts),
627 "defined(FLOOFY) || defined(FLUZZY)");
628
629 PreprocessorOptions PPOptsSingleFileParse;
630 PPOptsSingleFileParse.SingleFileParseMode = true;
631 const auto &Results10 =
632 DirectiveExprRange(MultiExprIf, PPOptsSingleFileParse);
633 EXPECT_EQ(Results10.size(), 1U);
634 EXPECT_EQ(
635 Lexer::getSourceText(CharSourceRange(Results10[0].ConditionRange, false),
636 SourceMgr, LangOpts),
637 "defined(FLOOFY) || defined(FLUZZY)");
638 }
639
640 } // namespace