blob: 8659f0fc93404a4d6a6fb73a59939c5234ac338e [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 "gtest/gtest.h"
24
25using namespace llvm;
26using namespace clang;
27
28namespace {
29
30// The test fixture.
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000031class PPConditionalDirectiveRecordTest : public ::testing::Test {
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000032protected:
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000033 PPConditionalDirectiveRecordTest()
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000034 : FileMgr(FileMgrOpts),
35 DiagID(new DiagnosticIDs()),
Douglas Gregor3f7d5482012-10-23 22:43:37 +000036 Diags(DiagID, new DiagnosticOptions, new IgnoringDiagConsumer()),
Douglas Gregor44d63612012-10-17 00:11:35 +000037 SourceMgr(Diags, FileMgr),
38 TargetOpts(new TargetOptions)
39 {
40 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";
NAKAMURA Takumife40a352012-11-16 04:40:11 +000041 Target = TargetInfo::CreateTargetInfo(Diags, &*TargetOpts);
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000042 }
43
44 FileSystemOptions FileMgrOpts;
45 FileManager FileMgr;
46 IntrusiveRefCntPtr<DiagnosticIDs> DiagID;
47 DiagnosticsEngine Diags;
48 SourceManager SourceMgr;
49 LangOptions LangOpts;
Douglas Gregor44d63612012-10-17 00:11:35 +000050 IntrusiveRefCntPtr<TargetOptions> TargetOpts;
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000051 IntrusiveRefCntPtr<TargetInfo> Target;
52};
53
54class VoidModuleLoader : public ModuleLoader {
John Thompson2d94bbb2014-04-23 19:04:32 +000055 ModuleLoadResult loadModule(SourceLocation ImportLoc,
56 ModuleIdPath Path,
57 Module::NameVisibilityKind Visibility,
58 bool IsInclusionDirective) override {
Douglas Gregor8c058932012-11-30 00:01:57 +000059 return ModuleLoadResult();
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000060 }
NAKAMURA Takumie73d2a92013-01-12 02:16:29 +000061
John Thompson2d94bbb2014-04-23 19:04:32 +000062 void makeModuleVisible(Module *Mod,
63 Module::NameVisibilityKind Visibility,
64 SourceLocation ImportLoc,
65 bool Complain) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000066
John Thompson2d94bbb2014-04-23 19:04:32 +000067 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000068 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000069 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
70 { return 0; };
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000071};
72
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +000073TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000074 const char *source =
75 "0 1\n"
76 "#if 1\n"
77 "2\n"
78 "#ifndef BB\n"
79 "3 4\n"
80 "#else\n"
81 "#endif\n"
82 "5\n"
83 "#endif\n"
84 "6\n"
85 "#if 1\n"
86 "7\n"
87 "#if 1\n"
88 "#endif\n"
89 "8\n"
90 "#endif\n"
91 "9\n";
92
93 MemoryBuffer *buf = MemoryBuffer::getMemBuffer(source);
Alp Tokerb671e342014-05-21 01:12:41 +000094 SourceMgr.setMainFileID(SourceMgr.createFileID(buf));
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000095
96 VoidModuleLoader ModLoader;
Manuel Klimek1f76c4e2013-10-24 07:51:24 +000097 HeaderSearch HeaderInfo(new HeaderSearchOptions, SourceMgr, Diags, LangOpts,
Alp Tokerf994cef2014-07-05 03:08:06 +000098 Target.get());
Alp Toker96637802014-05-02 03:43:38 +000099 Preprocessor PP(new PreprocessorOptions(), Diags, LangOpts, SourceMgr,
100 HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000101 /*IILookup =*/nullptr,
Alp Toker1ae02f62014-05-02 03:43:30 +0000102 /*OwnsHeaderSearch =*/false);
103 PP.Initialize(*Target);
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000104 PPConditionalDirectiveRecord *
105 PPRec = new PPConditionalDirectiveRecord(SourceMgr);
106 PP.addPPCallbacks(PPRec);
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000107 PP.EnterMainSourceFile();
108
109 std::vector<Token> toks;
110 while (1) {
111 Token tok;
112 PP.Lex(tok);
113 if (tok.is(tok::eof))
114 break;
115 toks.push_back(tok);
116 }
117
118 // Make sure we got the tokens that we expected.
119 ASSERT_EQ(10U, toks.size());
120
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000121 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000122 SourceRange(toks[0].getLocation(), toks[1].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000123 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000124 SourceRange(toks[0].getLocation(), toks[2].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000125 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000126 SourceRange(toks[3].getLocation(), toks[4].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000127 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000128 SourceRange(toks[1].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000129 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000130 SourceRange(toks[2].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000131 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000132 SourceRange(toks[2].getLocation(), toks[5].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000133 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000134 SourceRange(toks[0].getLocation(), toks[6].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000135 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000136 SourceRange(toks[2].getLocation(), toks[8].getLocation())));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000137 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000138 SourceRange(toks[0].getLocation(), toks[9].getLocation())));
139
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000140 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000141 toks[0].getLocation(), toks[2].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000142 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000143 toks[3].getLocation(), toks[4].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000144 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000145 toks[1].getLocation(), toks[5].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000146 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000147 toks[2].getLocation(), toks[0].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000148 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000149 toks[4].getLocation(), toks[3].getLocation()));
Argyrios Kyrtzidisf3d587e2012-12-04 07:27:05 +0000150 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +0000151 toks[5].getLocation(), toks[1].getLocation()));
152}
153
154} // anonymous namespace