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
24using namespace clang;
25
26namespace {
27
28// The test fixture.
29class PPConditionalDirectiveRecordTest : public ::testing::Test {
30protected:
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, Opts: 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
52TEST_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(InputData: source);
74 SourceMgr.setMainFileID(SourceMgr.createFileID(Buffer: 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: *Target);
84 PPConditionalDirectiveRecord *
85 PPRec = new PPConditionalDirectiveRecord(SourceMgr);
86 PP.addPPCallbacks(C: std::unique_ptr<PPCallbacks>(PPRec));
87 PP.EnterMainSourceFile();
88
89 std::vector<Token> toks;
90 PP.LexTokensUntilEOF(Tokens: &toks);
91
92 // Make sure we got the tokens that we expected.
93 ASSERT_EQ(10U, toks.size());
94
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())));
113
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()));
126}
127
128} // anonymous namespace
129

source code of clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp