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