Coverage Report

Created: 2021-09-21 08:58

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