blob: 6056d55f9c8ed08b2f6b2f57bbd0793485da2060 [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"
Douglas Gregor1452ff12012-10-24 17:46:57 +000021#include "clang/Lex/PreprocessorOptions.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000022#include "clang/Lex/PreprocessingRecord.h"
23#include "llvm/Config/config.h"
24
25#include "gtest/gtest.h"
26
27using namespace llvm;
28using namespace clang;
29
30namespace {
31
32// The test fixture.
33class PreprocessingRecordTest : public ::testing::Test {
34protected:
35 PreprocessingRecordTest()
36 : FileMgr(FileMgrOpts),
37 DiagID(new DiagnosticIDs()),
Douglas Gregor3f7d5482012-10-23 22:43:37 +000038 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor44d63612012-10-17 00:11:35 +000039 SourceMgr(Diags, FileMgr),
40 TargetOpts(new TargetOptions)
41 {
42 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
NAKAMURA Takumife40a352012-11-16 04:40:11 +000043 Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000044 }
45
46 FileSystemOptions FileMgrOpts;
47 FileManager FileMgr;
48 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
49 DiagnosticsEngine Diags;
50 SourceManager SourceMgr;
51 LangOptions LangOpts;
Douglas Gregor44d63612012-10-17 00:11:35 +000052 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000053 IntrusiveRefCntPtr<TargetInfo> Target;
54};
55
56class VoidModuleLoader : public ModuleLoader {
57 virtual Module *loadModule(SourceLocation ImportLoc, ModuleIdPath Path,
58 Module::NameVisibilityKind Visibility,
59 bool IsInclusionDirective) {
60 return 0;
61 }
62};
63
64TEST_F(PreprocessingRecordTest, PPRecAPI) {
65 const char *source =
66 "0 1\n"
67 "#if 1\n"
68 "2\n"
69 "#ifndef BB\n"
70 "3 4\n"
71 "#else\n"
72 "#endif\n"
73 "5\n"
74 "#endif\n"
75 "6\n"
76 "#if 1\n"
77 "7\n"
78 "#if 1\n"
79 "#endif\n"
80 "8\n"
81 "#endif\n"
82 "9\n";
83
84 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
85 SourceMgr.createMainFileIDForMemBuffer(buf);
86
87 VoidModuleLoader ModLoader;
Douglas Gregor40ba1a02012-10-24 16:24:38 +000088 HeaderSearch HeaderInfo(new HeaderSearchOptions, FileMgr, Diags, LangOpts,
89 Target.getPtr());
Douglas Gregor1452ff12012-10-24 17:46:57 +000090 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts,Target.getPtr(),
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000091 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