xref: /llvm-project/clang/unittests/Frontend/UtilsTest.cpp (revision df9a14d7bbf1180e4f1474254c9d7ed6bcb4ce55)
12ff7ca98SSam McCall //===- unittests/Frontend/UtilsTest.cpp -----------------------------------===//
22ff7ca98SSam McCall //
32ff7ca98SSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42ff7ca98SSam McCall // See https://llvm.org/LICENSE.txt for license information.
52ff7ca98SSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62ff7ca98SSam McCall //
72ff7ca98SSam McCall //===----------------------------------------------------------------------===//
82ff7ca98SSam McCall 
92ff7ca98SSam McCall #include "clang/Frontend/Utils.h"
102ff7ca98SSam McCall #include "clang/Basic/Diagnostic.h"
112ff7ca98SSam McCall #include "clang/Basic/TargetOptions.h"
122ff7ca98SSam McCall #include "clang/Frontend/CompilerInstance.h"
132ff7ca98SSam McCall #include "clang/Frontend/CompilerInvocation.h"
1404b41904SSam McCall #include "clang/Lex/PreprocessorOptions.h"
1504b41904SSam McCall #include "llvm/ADT/IntrusiveRefCntPtr.h"
162ff7ca98SSam McCall #include "llvm/Support/VirtualFileSystem.h"
172ff7ca98SSam McCall #include "gmock/gmock.h"
182ff7ca98SSam McCall #include "gtest/gtest.h"
192ff7ca98SSam McCall 
202ff7ca98SSam McCall namespace clang {
212ff7ca98SSam McCall namespace {
2204b41904SSam McCall using testing::ElementsAre;
232ff7ca98SSam McCall 
242ff7ca98SSam McCall TEST(BuildCompilerInvocationTest, RecoverMultipleJobs) {
252ff7ca98SSam McCall   // This generates multiple jobs and we recover by using the first.
262ff7ca98SSam McCall   std::vector<const char *> Args = {"clang", "--target=macho", "-arch",  "i386",
272ff7ca98SSam McCall                                     "-arch", "x86_64",         "foo.cpp"};
282ff7ca98SSam McCall   clang::IgnoringDiagConsumer D;
2940c13720SSam McCall   CreateInvocationOptions Opts;
3040c13720SSam McCall   Opts.RecoverOnError = true;
3140c13720SSam McCall   Opts.VFS = new llvm::vfs::InMemoryFileSystem();
32*df9a14d7SKadir Cetinkaya   Opts.Diags = clang::CompilerInstance::createDiagnostics(
33*df9a14d7SKadir Cetinkaya       *Opts.VFS, new DiagnosticOptions, &D, false);
3440c13720SSam McCall   std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, Opts);
352ff7ca98SSam McCall   ASSERT_TRUE(CI);
362ff7ca98SSam McCall   EXPECT_THAT(CI->TargetOpts->Triple, testing::StartsWith("i386-"));
372ff7ca98SSam McCall }
382ff7ca98SSam McCall 
3904b41904SSam McCall // buildInvocationFromCommandLine should not translate -include to -include-pch,
4004b41904SSam McCall // even if the PCH file exists.
4104b41904SSam McCall TEST(BuildCompilerInvocationTest, ProbePrecompiled) {
4204b41904SSam McCall   std::vector<const char *> Args = {"clang", "-include", "foo.h", "foo.cpp"};
4304b41904SSam McCall   auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
4404b41904SSam McCall   FS->addFile("foo.h", 0, llvm::MemoryBuffer::getMemBuffer(""));
4504b41904SSam McCall   FS->addFile("foo.h.pch", 0, llvm::MemoryBuffer::getMemBuffer(""));
4604b41904SSam McCall 
4704b41904SSam McCall   clang::IgnoringDiagConsumer D;
4804b41904SSam McCall   llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
49*df9a14d7SKadir Cetinkaya       clang::CompilerInstance::createDiagnostics(*FS, new DiagnosticOptions, &D,
5004b41904SSam McCall                                                  false);
5100a3c9f2SSam McCall   // Default: ProbePrecompiled=false
52f44552abSSam McCall   CreateInvocationOptions CIOpts;
53f44552abSSam McCall   CIOpts.Diags = CommandLineDiagsEngine;
54f44552abSSam McCall   CIOpts.VFS = FS;
55f44552abSSam McCall   std::unique_ptr<CompilerInvocation> CI = createInvocation(Args, CIOpts);
5604b41904SSam McCall   ASSERT_TRUE(CI);
5704b41904SSam McCall   EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre("foo.h"));
5804b41904SSam McCall   EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "");
5900a3c9f2SSam McCall 
6000a3c9f2SSam McCall   CIOpts.ProbePrecompiled = true;
6100a3c9f2SSam McCall   CI = createInvocation(Args, CIOpts);
6200a3c9f2SSam McCall   ASSERT_TRUE(CI);
6300a3c9f2SSam McCall   EXPECT_THAT(CI->getPreprocessorOpts().Includes, ElementsAre());
6400a3c9f2SSam McCall   EXPECT_EQ(CI->getPreprocessorOpts().ImplicitPCHInclude, "foo.h.pch");
6504b41904SSam McCall }
6604b41904SSam McCall 
672ff7ca98SSam McCall } // namespace
682ff7ca98SSam McCall } // namespace clang
69