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