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