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

source code of clang/unittests/Frontend/UtilsTest.cpp