Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/Compilation.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Compilation.cpp - Compilation Task Implementation ------------------===//
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/Driver/Compilation.h"
10
#include "clang/Basic/LLVM.h"
11
#include "clang/Driver/Action.h"
12
#include "clang/Driver/Driver.h"
13
#include "clang/Driver/DriverDiagnostic.h"
14
#include "clang/Driver/Job.h"
15
#include "clang/Driver/Options.h"
16
#include "clang/Driver/ToolChain.h"
17
#include "clang/Driver/Util.h"
18
#include "llvm/ADT/STLExtras.h"
19
#include "llvm/ADT/SmallVector.h"
20
#include "llvm/Option/ArgList.h"
21
#include "llvm/Option/OptSpecifier.h"
22
#include "llvm/Option/Option.h"
23
#include "llvm/Support/FileSystem.h"
24
#include "llvm/Support/raw_ostream.h"
25
#include "llvm/TargetParser/Triple.h"
26
#include <cassert>
27
#include <string>
28
#include <system_error>
29
#include <utility>
30
31
using namespace clang;
32
using namespace driver;
33
using namespace llvm::opt;
34
35
Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
36
                         InputArgList *_Args, DerivedArgList *_TranslatedArgs,
37
                         bool ContainsError)
38
52.1k
    : TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
39
52.1k
      TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
40
  // The offloading host toolchain is the default toolchain.
41
52.1k
  OrderedOffloadingToolchains.insert(
42
52.1k
      std::make_pair(Action::OFK_Host, &DefaultToolChain));
43
52.1k
}
44
45
52.1k
Compilation::~Compilation() {
46
  // Remove temporary files. This must be done before arguments are freed, as
47
  // the file names might be derived from the input arguments.
48
52.1k
  if (!TheDriver.isSaveTempsEnabled() && 
!ForceKeepTempFiles52.0k
)
49
51.9k
    CleanupFileList(TempFiles);
50
51
52.1k
  delete TranslatedArgs;
52
52.1k
  delete Args;
53
54
  // Free any derived arg lists.
55
52.1k
  for (auto Arg : TCArgs)
56
51.5k
    if (Arg.second != TranslatedArgs)
57
30.4k
      delete Arg.second;
58
52.1k
}
59
60
const DerivedArgList &
61
Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
62
115k
                                 Action::OffloadKind DeviceOffloadKind) {
63
115k
  if (!TC)
64
509
    TC = &DefaultToolChain;
65
66
115k
  DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];
67
115k
  if (!Entry) {
68
51.6k
    SmallVector<Arg *, 4> AllocatedArgs;
69
51.6k
    DerivedArgList *OpenMPArgs = nullptr;
70
    // Translate OpenMP toolchain arguments provided via the -Xopenmp-target flags.
71
51.6k
    if (DeviceOffloadKind == Action::OFK_OpenMP) {
72
25
      const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>();
73
25
      bool SameTripleAsHost = (TC->getTriple() == HostTC->getTriple());
74
25
      OpenMPArgs = TC->TranslateOpenMPTargetArgs(
75
25
          *TranslatedArgs, SameTripleAsHost, AllocatedArgs);
76
25
    }
77
78
51.6k
    DerivedArgList *NewDAL = nullptr;
79
51.6k
    if (!OpenMPArgs) {
80
51.6k
      NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
81
51.6k
                                      DeviceOffloadKind, &AllocatedArgs);
82
51.6k
    } else {
83
13
      NewDAL = TC->TranslateXarchArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind,
84
13
                                      &AllocatedArgs);
85
13
      if (!NewDAL)
86
13
        NewDAL = OpenMPArgs;
87
0
      else
88
0
        delete OpenMPArgs;
89
13
    }
90
91
51.6k
    if (!NewDAL) {
92
51.5k
      Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
93
51.5k
      if (!Entry)
94
21.0k
        Entry = TranslatedArgs;
95
51.5k
    } else {
96
25
      Entry = TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind);
97
25
      if (!Entry)
98
0
        Entry = NewDAL;
99
25
      else
100
25
        delete NewDAL;
101
25
    }
102
103
    // Add allocated arguments to the final DAL.
104
51.6k
    for (auto *ArgPtr : AllocatedArgs)
105
17
      Entry->AddSynthesizedArg(ArgPtr);
106
51.6k
  }
107
108
115k
  return *Entry;
109
115k
}
110
111
6.51k
bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
112
  // FIXME: Why are we trying to remove files that we have not created? For
113
  // example we should only try to remove a temporary assembly file if
114
  // "clang -cc1" succeed in writing it. Was this a workaround for when
115
  // clang was writing directly to a .s file and sometimes leaving it behind
116
  // during a failure?
117
118
  // FIXME: If this is necessary, we can still try to split
119
  // llvm::sys::fs::remove into a removeFile and a removeDir and avoid the
120
  // duplicated stat from is_regular_file.
121
122
  // Don't try to remove files which we don't have write access to (but may be
123
  // able to remove), or non-regular files. Underlying tools may have
124
  // intentionally not overwritten them.
125
6.51k
  if (!llvm::sys::fs::can_write(File) || 
!llvm::sys::fs::is_regular_file(File)6.23k
)
126
313
    return true;
127
128
6.20k
  if (std::error_code EC = llvm::sys::fs::remove(File)) {
129
    // Failure is only failure if the file exists and is "regular". We checked
130
    // for it being regular before, and llvm::sys::fs::remove ignores ENOENT,
131
    // so we don't need to check again.
132
133
0
    if (IssueErrors)
134
0
      getDriver().Diag(diag::err_drv_unable_to_remove_file)
135
0
        << EC.message();
136
0
    return false;
137
0
  }
138
6.20k
  return true;
139
6.20k
}
140
141
bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files,
142
52.0k
                                  bool IssueErrors) const {
143
52.0k
  bool Success = true;
144
52.0k
  for (const auto &File: Files)
145
6.35k
    Success &= CleanupFile(File, IssueErrors);
146
52.0k
  return Success;
147
52.0k
}
148
149
bool Compilation::CleanupFileMap(const ArgStringMap &Files,
150
                                 const JobAction *JA,
151
312
                                 bool IssueErrors) const {
152
312
  bool Success = true;
153
312
  for (const auto &File : Files) {
154
    // If specified, only delete the files associated with the JobAction.
155
    // Otherwise, delete all files in the map.
156
229
    if (JA && File.first != JA)
157
73
      continue;
158
156
    Success &= CleanupFile(File.second, IssueErrors);
159
156
  }
160
312
  return Success;
161
312
}
162
163
int Compilation::ExecuteCommand(const Command &C,
164
                                const Command *&FailingCommand,
165
8.78k
                                bool LogOnly) const {
166
8.78k
  if ((getDriver().CCPrintOptions ||
167
8.78k
       
getArgs().hasArg(options::OPT_v)8.78k
) &&
!getDriver().CCGenDiagnostics42
) {
168
42
    raw_ostream *OS = &llvm::errs();
169
42
    std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
170
171
    // Follow gcc implementation of CC_PRINT_OPTIONS; we could also cache the
172
    // output stream.
173
42
    if (getDriver().CCPrintOptions &&
174
42
        
!getDriver().CCPrintOptionsFilename.empty()3
) {
175
3
      std::error_code EC;
176
3
      OwnedStream.reset(new llvm::raw_fd_ostream(
177
3
          getDriver().CCPrintOptionsFilename, EC,
178
3
          llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF));
179
3
      if (EC) {
180
0
        getDriver().Diag(diag::err_drv_cc_print_options_failure)
181
0
            << EC.message();
182
0
        FailingCommand = &C;
183
0
        return 1;
184
0
      }
185
3
      OS = OwnedStream.get();
186
3
    }
187
188
42
    if (getDriver().CCPrintOptions)
189
3
      *OS << "[Logging clang options]\n";
190
191
42
    C.Print(*OS, "\n", /*Quote=*/getDriver().CCPrintOptions);
192
42
  }
193
194
8.78k
  if (LogOnly)
195
34
    return 0;
196
197
8.75k
  std::string Error;
198
8.75k
  bool ExecutionFailed;
199
8.75k
  int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
200
8.75k
  if (PostCallback)
201
5
    PostCallback(C, Res);
202
8.75k
  if (!Error.empty()) {
203
1
    assert(Res && "Error string set with 0 result code!");
204
1
    getDriver().Diag(diag::err_drv_command_failure) << Error;
205
1
  }
206
207
8.75k
  if (Res)
208
315
    FailingCommand = &C;
209
210
8.75k
  return ExecutionFailed ? 
11
:
Res8.75k
;
211
8.75k
}
212
213
using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>;
214
215
static bool ActionFailed(const Action *A,
216
8.90k
                         const FailingCommandList &FailingCommands) {
217
8.90k
  if (FailingCommands.empty())
218
8.76k
    return false;
219
220
  // CUDA/HIP can have the same input source code compiled multiple times so do
221
  // not compiled again if there are already failures. It is OK to abort the
222
  // CUDA pipeline on errors.
223
142
  if (A->isOffloading(Action::OFK_Cuda) || 
A->isOffloading(Action::OFK_HIP)139
)
224
6
    return true;
225
226
136
  for (const auto &CI : FailingCommands)
227
156
    if (A == &(CI.second->getSource()))
228
40
      return true;
229
230
96
  for (const auto *AI : A->inputs())
231
76
    if (ActionFailed(AI, FailingCommands))
232
41
      return true;
233
234
55
  return false;
235
96
}
236
237
static bool InputsOk(const Command &C,
238
8.83k
                     const FailingCommandList &FailingCommands) {
239
8.83k
  return !ActionFailed(&C.getSource(), FailingCommands);
240
8.83k
}
241
242
void Compilation::ExecuteJobs(const JobList &Jobs,
243
                              FailingCommandList &FailingCommands,
244
10.3k
                              bool LogOnly) const {
245
  // According to UNIX standard, driver need to continue compiling all the
246
  // inputs on the command line even one of them failed.
247
  // In all but CLMode, execute all the jobs unless the necessary inputs for the
248
  // job is missing due to previous failures.
249
10.3k
  for (const auto &Job : Jobs) {
250
8.83k
    if (!InputsOk(Job, FailingCommands))
251
46
      continue;
252
8.78k
    const Command *FailingCommand = nullptr;
253
8.78k
    if (int Res = ExecuteCommand(Job, FailingCommand, LogOnly)) {
254
314
      FailingCommands.push_back(std::make_pair(Res, FailingCommand));
255
      // Bail as soon as one command fails in cl driver mode.
256
314
      if (TheDriver.IsCLMode())
257
4
        return;
258
314
    }
259
8.78k
  }
260
10.3k
}
261
262
45
void Compilation::initCompilationForDiagnostics() {
263
45
  ForDiagnostics = true;
264
265
  // Free actions and jobs.
266
45
  Actions.clear();
267
45
  AllActions.clear();
268
45
  Jobs.clear();
269
270
  // Remove temporary files.
271
45
  if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
272
45
    CleanupFileList(TempFiles);
273
274
  // Clear temporary/results file lists.
275
45
  TempFiles.clear();
276
45
  ResultFiles.clear();
277
45
  FailureResultFiles.clear();
278
279
  // Remove any user specified output.  Claim any unclaimed arguments, so as
280
  // to avoid emitting warnings about unused args.
281
45
  OptSpecifier OutputOpts[] = {
282
45
      options::OPT_o,  options::OPT_MD, options::OPT_MMD, options::OPT_M,
283
45
      options::OPT_MM, options::OPT_MF, options::OPT_MG,  options::OPT_MJ,
284
45
      options::OPT_MQ, options::OPT_MT, options::OPT_MV};
285
495
  for (const auto &Opt : OutputOpts) {
286
495
    if (TranslatedArgs->hasArg(Opt))
287
9
      TranslatedArgs->eraseArg(Opt);
288
495
  }
289
45
  TranslatedArgs->ClaimAllArgs();
290
291
  // Force re-creation of the toolchain Args, otherwise our modifications just
292
  // above will have no effect.
293
45
  for (auto Arg : TCArgs)
294
45
    if (Arg.second != TranslatedArgs)
295
39
      delete Arg.second;
296
45
  TCArgs.clear();
297
298
  // Redirect stdout/stderr to /dev/null.
299
45
  Redirects = {std::nullopt, {""}, {""}};
300
301
  // Temporary files added by diagnostics should be kept.
302
45
  ForceKeepTempFiles = true;
303
45
}
304
305
51.0k
StringRef Compilation::getSysRoot() const {
306
51.0k
  return getDriver().SysRoot;
307
51.0k
}
308
309
0
void Compilation::Redirect(ArrayRef<std::optional<StringRef>> Redirects) {
310
0
  this->Redirects = Redirects;
311
0
}