126 lines · cpp
1//===- unittests/Lex/PPConditionalDirectiveRecordTest.cpp-PP directive tests =//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "clang/Lex/PPConditionalDirectiveRecord.h"10#include "clang/Basic/Diagnostic.h"11#include "clang/Basic/DiagnosticOptions.h"12#include "clang/Basic/FileManager.h"13#include "clang/Basic/LangOptions.h"14#include "clang/Basic/SourceManager.h"15#include "clang/Basic/TargetInfo.h"16#include "clang/Basic/TargetOptions.h"17#include "clang/Lex/HeaderSearch.h"18#include "clang/Lex/HeaderSearchOptions.h"19#include "clang/Lex/ModuleLoader.h"20#include "clang/Lex/Preprocessor.h"21#include "clang/Lex/PreprocessorOptions.h"22#include "gtest/gtest.h"23 24using namespace clang;25 26namespace {27 28// The test fixture.29class PPConditionalDirectiveRecordTest : public ::testing::Test {30protected:31 PPConditionalDirectiveRecordTest()32 : FileMgr(FileMgrOpts),33 Diags(DiagnosticIDs::create(), DiagOpts, new IgnoringDiagConsumer()),34 SourceMgr(Diags, FileMgr), TargetOpts(new TargetOptions) {35 TargetOpts->Triple = "x86_64-apple-darwin11.1.0";36 Target = TargetInfo::CreateTargetInfo(Diags, *TargetOpts);37 }38 39 FileSystemOptions FileMgrOpts;40 FileManager FileMgr;41 DiagnosticOptions DiagOpts;42 DiagnosticsEngine Diags;43 SourceManager SourceMgr;44 LangOptions LangOpts;45 std::shared_ptr<TargetOptions> TargetOpts;46 IntrusiveRefCntPtr<TargetInfo> Target;47};48 49TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) {50 const char *source =51 "0 1\n"52 "#if 1\n"53 "2\n"54 "#ifndef BB\n"55 "3 4\n"56 "#else\n"57 "#endif\n"58 "5\n"59 "#endif\n"60 "6\n"61 "#if 1\n"62 "7\n"63 "#if 1\n"64 "#endif\n"65 "8\n"66 "#endif\n"67 "9\n";68 69 std::unique_ptr<llvm::MemoryBuffer> Buf =70 llvm::MemoryBuffer::getMemBuffer(source);71 SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf)));72 73 HeaderSearchOptions HSOpts;74 TrivialModuleLoader ModLoader;75 HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get());76 PreprocessorOptions PPOpts;77 Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader,78 /*IILookup =*/nullptr,79 /*OwnsHeaderSearch =*/false);80 PP.Initialize(*Target);81 PPConditionalDirectiveRecord *82 PPRec = new PPConditionalDirectiveRecord(SourceMgr);83 PP.addPPCallbacks(std::unique_ptr<PPCallbacks>(PPRec));84 PP.EnterMainSourceFile();85 86 std::vector<Token> toks;87 PP.LexTokensUntilEOF(&toks);88 89 // Make sure we got the tokens that we expected.90 ASSERT_EQ(10U, toks.size());91 92 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(93 SourceRange(toks[0].getLocation(), toks[1].getLocation())));94 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(95 SourceRange(toks[0].getLocation(), toks[2].getLocation())));96 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(97 SourceRange(toks[3].getLocation(), toks[4].getLocation())));98 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(99 SourceRange(toks[1].getLocation(), toks[5].getLocation())));100 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(101 SourceRange(toks[2].getLocation(), toks[6].getLocation())));102 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(103 SourceRange(toks[2].getLocation(), toks[5].getLocation())));104 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(105 SourceRange(toks[0].getLocation(), toks[6].getLocation())));106 EXPECT_TRUE(PPRec->rangeIntersectsConditionalDirective(107 SourceRange(toks[2].getLocation(), toks[8].getLocation())));108 EXPECT_FALSE(PPRec->rangeIntersectsConditionalDirective(109 SourceRange(toks[0].getLocation(), toks[9].getLocation())));110 111 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(112 toks[0].getLocation(), toks[2].getLocation()));113 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(114 toks[3].getLocation(), toks[4].getLocation()));115 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(116 toks[1].getLocation(), toks[5].getLocation()));117 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(118 toks[2].getLocation(), toks[0].getLocation()));119 EXPECT_FALSE(PPRec->areInDifferentConditionalDirectiveRegion(120 toks[4].getLocation(), toks[3].getLocation()));121 EXPECT_TRUE(PPRec->areInDifferentConditionalDirectiveRegion(122 toks[5].getLocation(), toks[1].getLocation()));123}124 125} // anonymous namespace126