Coverage Report

Created: 2020-03-31 06:27

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