blob: 3028f60b57f76372aa8936e06e6977938fa67a46 [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 {
John Thompson2d94bbb2014-04-23 19:04:32 +000056 ModuleLoadResult loadModule(SourceLocation ImportLoc,
57 ModuleIdPath Path,
58 Module::NameVisibilityKind Visibility,
59 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000060 return ModuleLoadResult();
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000061 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000062
John Thompson2d94bbb2014-04-23 19:04:32 +000063 void makeModuleVisible(Module *Mod,
64 Module::NameVisibilityKind Visibility,
65 SourceLocation ImportLoc,
66 bool Complain) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000067
John Thompson2d94bbb2014-04-23 19:04:32 +000068 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
John Thompson2255f2c2014-04-23 12:57:01 +000069 { return 0; }
John Thompson2d94bbb2014-04-23 19:04:32 +000070 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
71 { return 0; };
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000072};
73
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000074TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000075 const char *source =
76 "0 1\n"
77 "#if 1\n"
78 "2\n"
79 "#ifndef BB\n"
80 "3 4\n"
81 "#else\n"
82 "#endif\n"
83 "5\n"
84 "#endif\n"
85 "6\n"
86 "#if 1\n"
87 "7\n"
88 "#if 1\n"
89 "#endif\n"
90 "8\n"
91 "#endif\n"
92 "9\n";
93
94 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
95 SourceMgr.createMainFileIDForMemBuffer(buf);
96
97 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000098 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Douglas Gregor40ba1a02012-10-24 16:24:38 +000099 Target.getPtr());
Alp Toker1ae02f62014-05-02 03:43:30 +0000100 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts,
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000101 SourceMgr, HeaderInfo, ModLoader,
102 /*IILookup =*/ 0,
Alp Toker1ae02f62014-05-02 03:43:30 +0000103 /*OwnsHeaderSearch =*/false);
104 PP.Initialize(*Target);
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000105 PPConditionalDirectiveRecord *
106 PPRec = new PPConditionalDirectiveRecord(SourceMgr);
107 PP.addPPCallbacks(PPRec);
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000108 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
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000122 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000123 SourceRange(toks[0].getLocation(), toks[1].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000124 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000125 SourceRange(toks[0].getLocation(), toks[2].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000126 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000127 SourceRange(toks[3].getLocation(), toks[4].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000128 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000129 SourceRange(toks[1].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000130 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000131 SourceRange(toks[2].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000132 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000133 SourceRange(toks[2].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000134 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000135 SourceRange(toks[0].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000136 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000137 SourceRange(toks[2].getLocation(), toks[8].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000138 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000139 SourceRange(toks[0].getLocation(), toks[9].getLocation())));
140
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000141 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000142 toks[0].getLocation(), toks[2].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000143 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000144 toks[3].getLocation(), toks[4].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000145 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000146 toks[1].getLocation(), toks[5].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000147 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000148 toks[2].getLocation(), toks[0].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000149 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000150 toks[4].getLocation(), toks[3].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000151 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000152 toks[5].getLocation(), toks[1].getLocation()));
153}
154
155} // anonymous namespace