Coverage Report

Created: 2022-05-21 09:15

/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>
30
clang::createInvocation(ArrayRef<const char *> ArgList,
31
4.72k
                        CreateInvocationOptions Opts) {
32
4.72k
  assert(!ArgList.empty());
33
4.72k
  auto Diags = Opts.Diags
34
4.72k
                   ? std::move(Opts.Diags)
35
4.72k
                   : 
CompilerInstance::createDiagnostics(new DiagnosticOptions)0
;
36
37
4.72k
  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.72k
  Args.insert(
41
4.72k
      llvm::find_if(
42
57.5k
          Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
43
4.72k
      "-fsyntax-only");
44
45
  // FIXME: We shouldn't have to pass in the path info.
46
4.72k
  driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
47
4.72k
                           "clang LLVM compiler", Opts.VFS);
48
49
  // Don't check that inputs exist, they may have been remapped.
50
4.72k
  TheDriver.setCheckInputsExist(false);
51
4.72k
  TheDriver.setProbePrecompiled(Opts.ProbePrecompiled);
52
53
4.72k
  std::unique_ptr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
54
4.72k
  if (!C)
55
0
    return nullptr;
56
57
  // Just print the cc1 options if -### was present.
58
4.72k
  if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
59
0
    C->getJobs().Print(llvm::errs(), "\n", true);
60
0
    return nullptr;
61
0
  }
62
63
  // We expect to get back exactly one command job, if we didn't something
64
  // failed. Offload compilation is an exception as it creates multiple jobs. If
65
  // that's the case, we proceed with the first job. If caller needs a
66
  // particular job, it should be controlled via options (e.g.
67
  // --cuda-{host|device}-only for CUDA) passed to the driver.
68
4.72k
  const driver::JobList &Jobs = C->getJobs();
69
4.72k
  bool OffloadCompilation = false;
70
4.72k
  if (Jobs.size() > 1) {
71
6
    for (auto &A : C->getActions()){
72
      // On MacOSX real actions may end up being wrapped in BindArchAction
73
6
      if (isa<driver::BindArchAction>(A))
74
6
        A = *A->input_begin();
75
6
      if (isa<driver::OffloadAction>(A)) {
76
2
        OffloadCompilation = true;
77
2
        break;
78
2
      }
79
6
    }
80
3
  }
81
82
4.72k
  bool PickFirstOfMany = OffloadCompilation || 
Opts.RecoverOnError4.72k
;
83
4.72k
  if (Jobs.size() == 0 || (Jobs.size() > 1 && 
!PickFirstOfMany3
)) {
84
0
    SmallString<256> Msg;
85
0
    llvm::raw_svector_ostream OS(Msg);
86
0
    Jobs.Print(OS, "; ", true);
87
0
    Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
88
0
    return nullptr;
89
0
  }
90
4.72k
  auto Cmd = llvm::find_if(Jobs, [](const driver::Command &Cmd) {
91
4.72k
    return StringRef(Cmd.getCreator().getName()) == "clang";
92
4.72k
  });
93
4.72k
  if (Cmd == Jobs.end()) {
94
0
    Diags->Report(diag::err_fe_expected_clang_command);
95
0
    return nullptr;
96
0
  }
97
98
4.72k
  const ArgStringList &CCArgs = Cmd->getArguments();
99
4.72k
  if (Opts.CC1Args)
100
0
    *Opts.CC1Args = {CCArgs.begin(), CCArgs.end()};
101
4.72k
  auto CI = std::make_unique<CompilerInvocation>();
102
4.72k
  if (!CompilerInvocation::CreateFromArgs(*CI, CCArgs, *Diags, Args[0]) &&
103
4.72k
      
!Opts.RecoverOnError47
)
104
47
    return nullptr;
105
4.67k
  return CI;
106
4.72k
}