blob: b635604e0982c5fa51c70a6a244db8871cbd582a [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"
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +000015#include "clang/Basic/MemoryBufferCache.h"
Chandler Carruthfa0b3bb2012-12-04 09:53:37 +000016#include "clang/Basic/SourceManager.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000017#include "clang/Basic/TargetInfo.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000018#include "clang/Basic/TargetOptions.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000019#include "clang/Lex/HeaderSearch.h"
Douglas Gregor40ba1a02012-10-24 16:24:38 +000020#include "clang/Lex/HeaderSearchOptions.h"
Chandler Carruth320d9662012-12-04 09:45:34 +000021#include "clang/Lex/ModuleLoader.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000022#include "clang/Lex/Preprocessor.h"
Douglas Gregor1452ff12012-10-24 17:46:57 +000023#include "clang/Lex/PreprocessorOptions.h"
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000024#include "gtest/gtest.h"
25
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000026using 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";
Alp Toker80758082014-07-06 05:26:44 +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;
Alp Toker80758082014-07-06 05:26:44 +000050 std::shared_ptr<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,
Richard Smitha7e2cc62015-05-01 01:53:09 +000064 SourceLocation ImportLoc) override { }
John Thompson2255f2c2014-04-23 12:57:01 +000065
John Thompson2d94bbb2014-04-23 19:04:32 +000066 GlobalModuleIndex *loadGlobalModuleIndex(SourceLocation TriggerLoc) override
Craig Topper416fa342014-06-08 08:38:12 +000067 { return nullptr; }
John Thompson2d94bbb2014-04-23 19:04:32 +000068 bool lookupMissingImports(StringRef Name, SourceLocation TriggerLoc) override
Hans Wennborg4afe5042015-07-22 20:46:26 +000069 { 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
James Y Knightb214cbc2016-03-04 19:00:41 +000092 std::unique_ptr<llvm::MemoryBuffer> Buf =
93 llvm::MemoryBuffer::getMemBuffer(source);
David Blaikie50a5f972014-08-29 07:59:55 +000094 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));
Argyrios Kyrtzidis647dcd82012-03-05 05:48:17 +000095
96 VoidModuleLoader ModLoader;
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +000097 MemoryBufferCache PCMCache;
David Blaikie9c28cb32017-01-06 01:04:46 +000098 HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
99 Diags, LangOpts, Target.get());
David Blaikiee3041682017-01-05 19:11:36 +0000100 Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
Duncan P. N. Exon Smith079c40e2017-03-17 22:55:13 +0000101 SourceMgr, PCMCache, HeaderInfo, ModLoader,
Craig Topper416fa342014-06-08 08:38:12 +0000102 /*IILookup =*/nullptr,
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);
Craig Topperb8a70532014-09-10 04:53:53 +0000107 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(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