1 //===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//
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
7 //===----------------------------------------------------------------------===//
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"
24 using namespace clang
;
29 class PPConditionalDirectiveRecordTest
: public ::testing::Test
{
31 PPConditionalDirectiveRecordTest()
32 : FileMgr(FileMgrOpts
),
33 DiagID(new DiagnosticIDs()),
34 Diags(DiagID
, new DiagnosticOptions
, new IgnoringDiagConsumer()),
35 SourceMgr(Diags
, FileMgr
),
36 TargetOpts(new TargetOptions
)
38 TargetOpts
->Triple
= "x86_64-apple-darwin11.1.0";
39 Target
= TargetInfo::CreateTargetInfo(Diags
, TargetOpts
);
42 FileSystemOptions FileMgrOpts
;
44 IntrusiveRefCntPtr
<DiagnosticIDs
> DiagID
;
45 DiagnosticsEngine Diags
;
46 SourceManager SourceMgr
;
48 std::shared_ptr
<TargetOptions
> TargetOpts
;
49 IntrusiveRefCntPtr
<TargetInfo
> Target
;
52 TEST_F(PPConditionalDirectiveRecordTest
, PPRecAPI
) {
72 std::unique_ptr
<llvm::MemoryBuffer
> Buf
=
73 llvm::MemoryBuffer::getMemBuffer(source
);
74 SourceMgr
.setMainFileID(SourceMgr
.createFileID(std::move(Buf
)));
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();
89 std::vector
<Token
> toks
;
90 PP
.LexTokensUntilEOF(&toks
);
92 // Make sure we got the tokens that we expected.
93 ASSERT_EQ(10U, toks
.size());
95 EXPECT_FALSE(PPRec
->rangeIntersectsConditionalDirective(
96 SourceRange(toks
[0].getLocation(), toks
[1].getLocation())));
97 EXPECT_TRUE(PPRec
->rangeIntersectsConditionalDirective(
98 SourceRange(toks
[0].getLocation(), toks
[2].getLocation())));
99 EXPECT_FALSE(PPRec
->rangeIntersectsConditionalDirective(
100 SourceRange(toks
[3].getLocation(), toks
[4].getLocation())));
101 EXPECT_TRUE(PPRec
->rangeIntersectsConditionalDirective(
102 SourceRange(toks
[1].getLocation(), toks
[5].getLocation())));
103 EXPECT_TRUE(PPRec
->rangeIntersectsConditionalDirective(
104 SourceRange(toks
[2].getLocation(), toks
[6].getLocation())));
105 EXPECT_FALSE(PPRec
->rangeIntersectsConditionalDirective(
106 SourceRange(toks
[2].getLocation(), toks
[5].getLocation())));
107 EXPECT_FALSE(PPRec
->rangeIntersectsConditionalDirective(
108 SourceRange(toks
[0].getLocation(), toks
[6].getLocation())));
109 EXPECT_TRUE(PPRec
->rangeIntersectsConditionalDirective(
110 SourceRange(toks
[2].getLocation(), toks
[8].getLocation())));
111 EXPECT_FALSE(PPRec
->rangeIntersectsConditionalDirective(
112 SourceRange(toks
[0].getLocation(), toks
[9].getLocation())));
114 EXPECT_TRUE(PPRec
->areInDifferentConditionalDirectiveRegion(
115 toks
[0].getLocation(), toks
[2].getLocation()));
116 EXPECT_FALSE(PPRec
->areInDifferentConditionalDirectiveRegion(
117 toks
[3].getLocation(), toks
[4].getLocation()));
118 EXPECT_TRUE(PPRec
->areInDifferentConditionalDirectiveRegion(
119 toks
[1].getLocation(), toks
[5].getLocation()));
120 EXPECT_TRUE(PPRec
->areInDifferentConditionalDirectiveRegion(
121 toks
[2].getLocation(), toks
[0].getLocation()));
122 EXPECT_FALSE(PPRec
->areInDifferentConditionalDirectiveRegion(
123 toks
[4].getLocation(), toks
[3].getLocation()));
124 EXPECT_TRUE(PPRec
->areInDifferentConditionalDirectiveRegion(
125 toks
[5].getLocation(), toks
[1].getLocation()));
128 } // anonymous namespace