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), 33 DiagID(new DiagnosticIDs()), 34 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()), 35 SourceMgr(Diags, FileMgr), 36 TargetOpts(new TargetOptions) 37 { 38 TargetOpts->Triple = "x86_64-apple-darwin11.1.0"; 39 Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts); 40 } 41 42 FileSystemOptions FileMgrOpts; 43 FileManager FileMgr; 44 IntrusiveRefCntPtr<DiagnosticIDs> DiagID; 45 DiagnosticsEngine Diags; 46 SourceManager SourceMgr; 47 LangOptions LangOpts; 48 std::shared_ptr<TargetOptions> TargetOpts; 49 IntrusiveRefCntPtr<TargetInfo> Target; 50 }; 51 52 TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) { 53 const char *source = 54 "0 1\n" 55 "#if 1\n" 56 "2\n" 57 "#ifndef BB\n" 58 "3 4\n" 59 "#else\n" 60 "#endif\n" 61 "5\n" 62 "#endif\n" 63 "6\n" 64 "#if 1\n" 65 "7\n" 66 "#if 1\n" 67 "#endif\n" 68 "8\n" 69 "#endif\n" 70 "9\n"; 71 72 std::unique_ptr<llvm::MemoryBuffer> Buf = 73 llvm::MemoryBuffer::getMemBuffer(source); 74 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); 75 76 TrivialModuleLoader ModLoader; 77 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr, 78 Diags, LangOpts, Target.get()); 79 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts, 80 SourceMgr, HeaderInfo, ModLoader, 81 /*IILookup =*/nullptr, 82 /*OwnsHeaderSearch =*/false); 83 PP.Initialize(*Target); 84 PPConditionalDirectiveRecord * 85 PPRec = new PPConditionalDirectiveRecord(SourceMgr); 86 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec)); 87 PP.EnterMainSourceFile(); 88 89 std::vector<Token> toks; 90 while (1) { 91 Token tok; 92 PP.Lex(tok); 93 if (tok.is(tok::eof)) 94 break; 95 toks.push_back(tok); 96 } 97 98 // Make sure we got the tokens that we expected. 99 ASSERT_EQ(10U, toks.size()); 100 101 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( 102 SourceRange(toks[0].getLocation(), toks[1].getLocation()))); 103 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( 104 SourceRange(toks[0].getLocation(), toks[2].getLocation()))); 105 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( 106 SourceRange(toks[3].getLocation(), toks[4].getLocation()))); 107 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( 108 SourceRange(toks[1].getLocation(), toks[5].getLocation()))); 109 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( 110 SourceRange(toks[2].getLocation(), toks[6].getLocation()))); 111 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( 112 SourceRange(toks[2].getLocation(), toks[5].getLocation()))); 113 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( 114 SourceRange(toks[0].getLocation(), toks[6].getLocation()))); 115 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective( 116 SourceRange(toks[2].getLocation(), toks[8].getLocation()))); 117 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective( 118 SourceRange(toks[0].getLocation(), toks[9].getLocation()))); 119 120 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( 121 toks[0].getLocation(), toks[2].getLocation())); 122 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion( 123 toks[3].getLocation(), toks[4].getLocation())); 124 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( 125 toks[1].getLocation(), toks[5].getLocation())); 126 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( 127 toks[2].getLocation(), toks[0].getLocation())); 128 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion( 129 toks[4].getLocation(), toks[3].getLocation())); 130 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion( 131 toks[5].getLocation(), toks[1].getLocation())); 132 } 133 134 } // anonymous namespace 135