71 lines · cpp
1//===- unittests/Frontend/UtilsTest.cpp -----------------------------------===//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/Frontend/Utils.h"10#include "clang/Basic/Diagnostic.h"11#include "clang/Basic/TargetOptions.h"12#include "clang/Driver/CreateInvocationFromArgs.h"13#include "clang/Frontend/CompilerInstance.h"14#include "clang/Frontend/CompilerInvocation.h"15#include "clang/Lex/PreprocessorOptions.h"16#include "llvm/ADT/IntrusiveRefCntPtr.h"17#include "llvm/Support/VirtualFileSystem.h"18#include "gmock/gmock.h"19#include "gtest/gtest.h"20 21namespace clang {22namespace {23using testing::ElementsAre;24 25TEST(BuildCompilerInvocationTest, RecoverMultipleJobs) {26 // This generates multiple jobs and we recover by using the first.27 std::vector<const char *> Args = {"clang", "--target=macho", "-arch", "i386",28 "-arch", "x86_64", "foo.cpp"};29 clang::IgnoringDiagConsumer D;30 clang::DiagnosticOptions DiagOpts;31 CreateInvocationOptions Opts;32 Opts.RecoverOnError = true;33 Opts.VFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();34 Opts.Diags = clang::CompilerInstance::createDiagnostics(*Opts.VFS, DiagOpts,35 &D, false);36 std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, Opts);37 ASSERT_TRUE(CI);38 EXPECT_THAT(CI->getTargetOpts().Triple, testing::StartsWith("i386-"));39}40 41// buildInvocationFromCommandLine should not translate -include to -include-pch,42// even if the PCH file exists.43TEST(BuildCompilerInvocationTest, ProbePrecompiled) {44 std::vector<const char *> Args = {"clang", "-include", "foo.h", "foo.cpp"};45 auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();46 FS->addFile("foo.h", 0, llvm::MemoryBuffer::getMemBuffer(""));47 FS->addFile("foo.h.pch", 0, llvm::MemoryBuffer::getMemBuffer(""));48 49 clang::IgnoringDiagConsumer D;50 clang::DiagnosticOptions DiagOpts;51 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =52 clang::CompilerInstance::createDiagnostics(*FS, DiagOpts, &D, false);53 // Default: ProbePrecompiled=false54 CreateInvocationOptions CIOpts;55 CIOpts.Diags = CommandLineDiagsEngine;56 CIOpts.VFS = FS;57 std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, CIOpts);58 ASSERT_TRUE(CI);59 EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre("foo.h"));60 EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "");61 62 CIOpts.ProbePrecompiled = true;63 CI = createInvocation(Args, CIOpts);64 ASSERT_TRUE(CI);65 EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre());66 EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "foo.h.pch");67}68 69} // namespace70} // namespace clang71