| 1 | //===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive 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/PPConditionalDirectiveRecord.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/ModuleLoader.h" |
| 20 | #include "clang/Lex/Preprocessor.h" |
| 21 | #include "clang/Lex/PreprocessorOptions.h" |
| 22 | #include "gtest/gtest.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | |
| 28 | // The test fixture. |
| 29 | class PPConditionalDirectiveRecordTest : public ::testing::Test { |
| 30 | protected: |
| 31 | PPConditionalDirectiveRecordTest() |
| 32 | : FileMgr(FileMgrOpts), DiagID(new DiagnosticIDs()), |
| 33 | Diags(DiagID, DiagOpts, new IgnoringDiagConsumer()), |
| 34 | SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) { |
| 35 | TargetOpts->Triple = "x86_64-apple-darwin11.1.0" ; |
| 36 | Target = TargetInfo::CreateTargetInfo(Diags, Opts&: *TargetOpts); |
| 37 | } |
| 38 | |
| 39 | FileSystemOptions FileMgrOpts; |
| 40 | FileManager FileMgr; |
| 41 | IntrusiveRefCntPtr<DiagnosticIDs> DiagID; |
| 42 | DiagnosticOptions DiagOpts; |
| 43 | DiagnosticsEngine Diags; |
| 44 | SourceManager SourceMgr; |
| 45 | LangOptions LangOpts; |
| 46 | std::shared_ptr<TargetOptions> TargetOpts; |
| 47 | IntrusiveRefCntPtr<TargetInfo> Target; |
| 48 | }; |
| 49 | |
| 50 | TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) { |
| 51 | const char *source = |
| 52 | "0 1\n" |
| 53 | "#if 1\n" |
| 54 | "2\n" |
| 55 | "#ifndef BB\n" |
| 56 | "3 4\n" |
| 57 | "#else\n" |
| 58 | "#endif\n" |
| 59 | "5\n" |
| 60 | "#endif\n" |
| 61 | "6\n" |
| 62 | "#if 1\n" |
| 63 | "7\n" |
| 64 | "#if 1\n" |
| 65 | "#endif\n" |
| 66 | "8\n" |
| 67 | "#endif\n" |
| 68 | "9\n" ; |
| 69 | |
| 70 | std::unique_ptr<llvm::MemoryBuffer> Buf = |
| 71 | llvm::MemoryBuffer::getMemBuffer(InputData: source); |
| 72 | SourceMgr.setMainFileID(SourceMgr.createFileID(Buffer: std::move(Buf))); |
| 73 | |
| 74 | HeaderSearchOptions HSOpts; |
| 75 | TrivialModuleLoader ModLoader; |
| 76 | HeaderSearch (HSOpts, SourceMgr, Diags, LangOpts, Target.get()); |
| 77 | PreprocessorOptions PPOpts; |
| 78 | Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, |
| 79 | /*IILookup =*/nullptr, |
| 80 | /*OwnsHeaderSearch =*/false); |
| 81 | PP.Initialize(Target: *Target); |
| 82 | PPConditionalDirectiveRecord * |
| 83 | PPRec = new PPConditionalDirectiveRecord(SourceMgr); |
| 84 | PP.addPPCallbacks(C: std::unique_ptr<PPCallbacks>(PPRec)); |
| 85 | PP.EnterMainSourceFile(); |
| 86 | |
| 87 | std::vector<Token> toks; |
| 88 | PP.LexTokensUntilEOF(Tokens: &toks); |
| 89 | |
| 90 | // Make sure we got the tokens that we expected. |
| 91 | ASSERT_EQ(10U, toks.size()); |
| 92 | |
| 93 | EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( |
| 94 | SourceRange(toks[0].getLocation(), toks[1].getLocation()))); |
| 95 | EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( |
| 96 | SourceRange(toks[0].getLocation(), toks[2].getLocation()))); |
| 97 | EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( |
| 98 | SourceRange(toks[3].getLocation(), toks[4].getLocation()))); |
| 99 | EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( |
| 100 | SourceRange(toks[1].getLocation(), toks[5].getLocation()))); |
| 101 | EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( |
| 102 | SourceRange(toks[2].getLocation(), toks[6].getLocation()))); |
| 103 | EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( |
| 104 | SourceRange(toks[2].getLocation(), toks[5].getLocation()))); |
| 105 | EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( |
| 106 | SourceRange(toks[0].getLocation(), toks[6].getLocation()))); |
| 107 | EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( |
| 108 | SourceRange(toks[2].getLocation(), toks[8].getLocation()))); |
| 109 | EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( |
| 110 | SourceRange(toks[0].getLocation(), toks[9].getLocation()))); |
| 111 | |
| 112 | EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 113 | toks[0].getLocation(), toks[2].getLocation())); |
| 114 | EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 115 | toks[3].getLocation(), toks[4].getLocation())); |
| 116 | EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 117 | toks[1].getLocation(), toks[5].getLocation())); |
| 118 | EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 119 | toks[2].getLocation(), toks[0].getLocation())); |
| 120 | EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 121 | toks[4].getLocation(), toks[3].getLocation())); |
| 122 | EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( |
| 123 | toks[5].getLocation(), toks[1].getLocation())); |
| 124 | } |
| 125 | |
| 126 | } // anonymous namespace |
| 127 | |