| 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-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 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" |
| 19 | |
| 20 | namespace clang { |
| 21 | namespace { |
| 22 | using testing::ElementsAre; |
| 23 | |
| 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 | clang::DiagnosticOptions DiagOpts; |
| 30 | CreateInvocationOptions Opts; |
| 31 | Opts.RecoverOnError = true; |
| 32 | Opts.VFS = new llvm::vfs::InMemoryFileSystem(); |
| 33 | Opts.Diags = clang::CompilerInstance::createDiagnostics(VFS&: *Opts.VFS, Opts&: DiagOpts, |
| 34 | Client: &D, ShouldOwnClient: false); |
| 35 | std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, Opts); |
| 36 | ASSERT_TRUE(CI); |
| 37 | EXPECT_THAT(CI->getTargetOpts().Triple, testing::StartsWith("i386-" )); |
| 38 | } |
| 39 | |
| 40 | // buildInvocationFromCommandLine should not translate -include to -include-pch, |
| 41 | // even if the PCH file exists. |
| 42 | TEST(BuildCompilerInvocationTest, ProbePrecompiled) { |
| 43 | std::vector<const char *> Args = {"clang" , "-include" , "foo.h" , "foo.cpp" }; |
| 44 | auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 45 | FS->addFile(Path: "foo.h" , ModificationTime: 0, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: "" )); |
| 46 | FS->addFile(Path: "foo.h.pch" , ModificationTime: 0, Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: "" )); |
| 47 | |
| 48 | clang::IgnoringDiagConsumer D; |
| 49 | clang::DiagnosticOptions DiagOpts; |
| 50 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
| 51 | clang::CompilerInstance::createDiagnostics(VFS&: *FS, Opts&: DiagOpts, Client: &D, ShouldOwnClient: false); |
| 52 | // Default: ProbePrecompiled=false |
| 53 | CreateInvocationOptions CIOpts; |
| 54 | CIOpts.Diags = CommandLineDiagsEngine; |
| 55 | CIOpts.VFS = FS; |
| 56 | std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, Opts: CIOpts); |
| 57 | ASSERT_TRUE(CI); |
| 58 | EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre("foo.h" )); |
| 59 | EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "" ); |
| 60 | |
| 61 | CIOpts.ProbePrecompiled = true; |
| 62 | CI = createInvocation(Args, Opts: CIOpts); |
| 63 | ASSERT_TRUE(CI); |
| 64 | EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre()); |
| 65 | EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "foo.h.pch" ); |
| 66 | } |
| 67 | |
| 68 | } // namespace |
| 69 | } // namespace clang |
| 70 | |