1 //===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Lex/PPConditionalDirectiveRecord.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/DiagnosticOptions.h"
13 #include "clang/Basic/FileManager.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/MemoryBufferCache.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/Preprocessor.h"
23 #include "clang/Lex/PreprocessorOptions.h"
24 #include "gtest/gtest.h"
25 
26 using namespace clang;
27 
28 namespace {
29 
30 // The test fixture.
31 class PPConditionalDirectiveRecordTest : public ::testing::Test {
32 protected:
33   PPConditionalDirectiveRecordTest()
34     : FileMgr(FileMgrOpts),
35       DiagID(new DiagnosticIDs()),
36       Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
37       SourceMgr(Diags, FileMgr),
38       TargetOpts(new TargetOptions)
39   {
40     TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
41     Target = TargetInfo::CreateTargetInfo(Diags, TargetOpts);
42   }
43 
44   FileSystemOptions FileMgrOpts;
45   FileManager FileMgr;
46   IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
47   DiagnosticsEngine Diags;
48   SourceManager SourceMgr;
49   LangOptions LangOpts;
50   std::shared_ptr<TargetOptions> TargetOpts;
51   IntrusiveRefCntPtr<TargetInfo> Target;
52 };
53 
54 class VoidModuleLoader : public ModuleLoader {
55   ModuleLoadResult loadModule(SourceLocation ImportLoc,
56                               ModuleIdPath Path,
57                               Module::NameVisibilityKind Visibility,
58                               bool IsInclusionDirective) override {
59     return ModuleLoadResult();
60   }
61 
62   void makeModuleVisible(Module *Mod,
63                          Module::NameVisibilityKind Visibility,
64                          SourceLocation ImportLoc) override { }
65 
66   GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
67     { return nullptr; }
68   bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
69     { return 0; }
70 };
71 
72 TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
73   const char *source =
74       "0 1\n"
75       "#if 1\n"
76       "2\n"
77       "#ifndef BB\n"
78       "3 4\n"
79       "#else\n"
80       "#endif\n"
81       "5\n"
82       "#endif\n"
83       "6\n"
84       "#if 1\n"
85       "7\n"
86       "#if 1\n"
87       "#endif\n"
88       "8\n"
89       "#endif\n"
90       "9\n";
91 
92   std::unique_ptr<llvm::MemoryBuffer> Buf =
93       llvm::MemoryBuffer::getMemBuffer(source);
94   SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
95 
96   VoidModuleLoader ModLoader;
97   MemoryBufferCache PCMCache;
98   HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
99                           Diags, LangOpts, Target.get());
100   Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
101                   SourceMgr, PCMCache, HeaderInfo, ModLoader,
102                   /*IILookup =*/nullptr,
103                   /*OwnsHeaderSearch =*/false);
104   PP.Initialize(*Target);
105   PPConditionalDirectiveRecord *
106     PPRec = new PPConditionalDirectiveRecord(SourceMgr);
107   PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));
108   PP.EnterMainSourceFile();
109 
110   std::vector<Token> toks;
111   while (1) {
112     Token tok;
113     PP.Lex(tok);
114     if (tok.is(tok::eof))
115       break;
116     toks.push_back(tok);
117   }
118 
119   // Make sure we got the tokens that we expected.
120   ASSERT_EQ(10U, toks.size());
121 
122   EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
123                     SourceRange(toks[0].getLocation(), toks[1].getLocation())));
124   EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
125                     SourceRange(toks[0].getLocation(), toks[2].getLocation())));
126   EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
127                     SourceRange(toks[3].getLocation(), toks[4].getLocation())));
128   EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
129                     SourceRange(toks[1].getLocation(), toks[5].getLocation())));
130   EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
131                     SourceRange(toks[2].getLocation(), toks[6].getLocation())));
132   EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
133                     SourceRange(toks[2].getLocation(), toks[5].getLocation())));
134   EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
135                     SourceRange(toks[0].getLocation(), toks[6].getLocation())));
136   EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
137                     SourceRange(toks[2].getLocation(), toks[8].getLocation())));
138   EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
139                     SourceRange(toks[0].getLocation(), toks[9].getLocation())));
140 
141   EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
142                     toks[0].getLocation(), toks[2].getLocation()));
143   EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
144                     toks[3].getLocation(), toks[4].getLocation()));
145   EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
146                     toks[1].getLocation(), toks[5].getLocation()));
147   EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
148                     toks[2].getLocation(), toks[0].getLocation()));
149   EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
150                     toks[4].getLocation(), toks[3].getLocation()));
151   EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
152                     toks[5].getLocation(), toks[1].getLocation()));
153 }
154 
155 } // anonymous namespace
156