blob: 4e22589069dd48a0d33eb300970657fbf5c22633 [file] [log] [blame]
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +00001//===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +00002//
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
Chandler Carruth320d9662012-12-04 09:45:34 +000010#include "clang/Lex/PPConditionalDirectiveRecord.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000011#include "clang/Basic/Diagnostic.h"
Douglas Gregor3f7d5482012-10-23 22:43:37 +000012#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000013#include "clang/Basic/FileManager.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000014#include "clang/Basic/LangOptions.h"
Chandler Carruthfa0b3bb2012-12-04 09:53:37 +000015#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000016#include "clang/Basic/TargetInfo.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000017#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000018#include "clang/Lex/HeaderSearch.h"
Douglas Gregor40ba1a02012-10-24 16:24:38 +000019#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000020#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000021#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000022#include "clang/Lex/PreprocessorOptions.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000023#include "llvm/Config/config.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000024#include "gtest/gtest.h"
25
26using namespace llvm;
27using namespace clang;
28
29namespace {
30
31// The test fixture.
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000032class PPConditionalDirectiveRecordTest : public ::testing::Test {
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000033protected:
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000034 PPConditionalDirectiveRecordTest()
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000035 : 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";
NAKAMURA Takumife40a352012-11-16 04:40:11 +000042 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 {
Douglas Gregor8c058932012-11-30 00:01:57 +000056 virtual ModuleLoadResult loadModule(SourceLocation ImportLoc,
57 ModuleIdPath Path,
58 Module::NameVisibilityKind Visibility,
59 bool IsInclusionDirective) {
60 return ModuleLoadResult();
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000061 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000062
63 virtual void makeModuleVisible(Module *Mod,
Argyrios Kyrtzidis125df052013-02-01 16:36:12 +000064 Module::NameVisibilityKind Visibility,
Douglas Gregorfb912652013-03-20 21:10:35 +000065 SourceLocation ImportLoc,
66 bool Complain) { }
John Thompson2255f2c2014-04-23 12:57:01 +000067
68 virtual GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc)
69 { return 0; }
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000070};
71
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000072TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000073 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 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
93 SourceMgr.createMainFileIDForMemBuffer(buf);
94
95 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000096 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +000097 Target.getPtr());
Douglas Gregor1452ff12012-10-24 17:46:57 +000098 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts,Target.getPtr(),
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000099 SourceMgr, HeaderInfo, ModLoader,
100 /*IILookup =*/ 0,
101 /*OwnsHeaderSearch =*/false,
102 /*DelayInitialization =*/ false);
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000103 PPConditionalDirectiveRecord *
104 PPRec = new PPConditionalDirectiveRecord(SourceMgr);
105 PP.addPPCallbacks(PPRec);
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000106 PP.EnterMainSourceFile();
107
108 std::vector<Token> toks;
109 while (1) {
110 Token tok;
111 PP.Lex(tok);
112 if (tok.is(tok::eof))
113 break;
114 toks.push_back(tok);
115 }
116
117 // Make sure we got the tokens that we expected.
118 ASSERT_EQ(10U, toks.size());
119
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000120 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000121 SourceRange(toks[0].getLocation(), toks[1].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000122 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000123 SourceRange(toks[0].getLocation(), toks[2].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000124 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000125 SourceRange(toks[3].getLocation(), toks[4].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000126 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000127 SourceRange(toks[1].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000128 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000129 SourceRange(toks[2].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000130 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000131 SourceRange(toks[2].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000132 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000133 SourceRange(toks[0].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000134 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000135 SourceRange(toks[2].getLocation(), toks[8].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000136 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000137 SourceRange(toks[0].getLocation(), toks[9].getLocation())));
138
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000139 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000140 toks[0].getLocation(), toks[2].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000141 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000142 toks[3].getLocation(), toks[4].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000143 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000144 toks[1].getLocation(), toks[5].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000145 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000146 toks[2].getLocation(), toks[0].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000147 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000148 toks[4].getLocation(), toks[3].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000149 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000150 toks[5].getLocation(), toks[1].getLocation()));
151}
152
153} // anonymous namespace