Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
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
// Construct a compiler invocation object for command line driver arguments
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Frontend/Utils.h"
14
#include "clang/Basic/DiagnosticOptions.h"
15
#include "clang/Driver/Compilation.h"
16
#include "clang/Driver/Driver.h"
17
#include "clang/Driver/Action.h"
18
#include "clang/Driver/Options.h"
19
#include "clang/Driver/Tool.h"
20
#include "clang/Frontend/CompilerInstance.h"
21
#include "clang/Frontend/FrontendDiagnostic.h"
22
#include "llvm/Option/ArgList.h"
23
#include "llvm/Support/Host.h"
24
using namespace clang;
25
using namespace llvm::opt;
26
27
std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
28
    ArrayRef<const char *> ArgList, IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
29
    IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, bool ShouldRecoverOnErorrs,
30
4.52k
    std::vector<std::string> *CC1Args) {
31
4.52k
  if (!Diags.get()) {
32
    // No diagnostics engine was provided, so create our own diagnostics object
33
    // with the default options.
34
0
    Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions);
35
0
  }
36
37
4.52k
  SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());
38
39
  // FIXME: Find a cleaner way to force the driver into restricted modes.
40
4.52k
  Args.push_back("-fsyntax-only");
41
42
  // FIXME: We shouldn't have to pass in the path info.
43
4.52k
  driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
44
4.52k
                           "clang LLVM compiler", VFS);
45
46
  // Don't check that inputs exist, they may have been remapped.
47
4.52k
  TheDriver.setCheckInputsExist(false);
48
49
4.52k
  std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
50
4.52k
  if (!C)
51
0
    return nullptr;
52
53
  // Just print the cc1 options if -### was present.
54
4.52k
  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
55
0
    C->getJobs().Print(llvm::errs(), "\n", true);
56
0
    return nullptr;
57
0
  }
58
59
  // We expect to get back exactly one command job, if we didn't something
60
  // failed. Offload compilation is an exception as it creates multiple jobs. If
61
  // that's the case, we proceed with the first job. If caller needs a
62
  // particular job, it should be controlled via options (e.g.
63
  // --cuda-{host|device}-only for CUDA) passed to the driver.
64
4.52k
  const driver::JobList &Jobs = C->getJobs();
65
4.52k
  bool OffloadCompilation = false;
66
4.52k
  if (Jobs.size() > 1) {
67
4
    for (auto &A : C->getActions()){
68
      // On MacOSX real actions may end up being wrapped in BindArchAction
69
4
      if (isa<driver::BindArchAction>(A))
70
4
        A = *A->input_begin();
71
4
      if (isa<driver::OffloadAction>(A)) {
72
2
        OffloadCompilation = true;
73
2
        break;
74
2
      }
75
4.52k
    }
76
2
  }
77
4.52k
  if (Jobs.size() == 0 || !isa<driver::Command>(*Jobs.begin()) ||
78
4.52k
      (Jobs.size() > 1 && 
!OffloadCompilation2
)) {
79
0
    SmallString<256> Msg;
80
0
    llvm::raw_svector_ostream OS(Msg);
81
0
    Jobs.Print(OS, "; ", true);
82
0
    Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
83
0
    return nullptr;
84
0
  }
85
86
4.52k
  const driver::Command &Cmd = cast<driver::Command>(*Jobs.begin());
87
4.52k
  if (StringRef(Cmd.getCreator().getName()) != "clang") {
88
0
    Diags->Report(diag::err_fe_expected_clang_command);
89
0
    return nullptr;
90
0
  }
91
92
4.52k
  const ArgStringList &CCArgs = Cmd.getArguments();
93
4.52k
  if (CC1Args)
94
0
    *CC1Args = {CCArgs.begin(), CCArgs.end()};
95
4.52k
  auto CI = std::make_unique<CompilerInvocation>();
96
4.52k
  if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
97
0
      !ShouldRecoverOnErorrs)
98
0
    return nullptr;
99
4.52k
  return CI;
100
4.52k
}