clang  8.0.0svn
Job.cpp
Go to the documentation of this file.
1 //===- Job.cpp - Command to Execute ---------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "clang/Driver/Job.h"
11 #include "InputInfo.h"
12 #include "clang/Basic/LLVM.h"
13 #include "clang/Driver/Driver.h"
15 #include "clang/Driver/Tool.h"
16 #include "clang/Driver/ToolChain.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/StringSet.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Support/FileSystem.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/Program.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <algorithm>
28 #include <cassert>
29 #include <cstddef>
30 #include <string>
31 #include <system_error>
32 #include <utility>
33 
34 using namespace clang;
35 using namespace driver;
36 
37 Command::Command(const Action &Source, const Tool &Creator,
38  const char *Executable,
39  const llvm::opt::ArgStringList &Arguments,
40  ArrayRef<InputInfo> Inputs)
41  : Source(Source), Creator(Creator), Executable(Executable),
42  Arguments(Arguments) {
43  for (const auto &II : Inputs)
44  if (II.isFilename())
45  InputFilenames.push_back(II.getFilename());
46 }
47 
48 /// Check if the compiler flag in question should be skipped when
49 /// emitting a reproducer. Also track how many arguments it has and if the
50 /// option is some kind of include path.
51 static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum,
52  bool &IsInclude) {
53  SkipNum = 2;
54  // These flags are all of the form -Flag <Arg> and are treated as two
55  // arguments. Therefore, we need to skip the flag and the next argument.
56  bool ShouldSkip = llvm::StringSwitch<bool>(Flag)
57  .Cases("-MF", "-MT", "-MQ", "-serialize-diagnostic-file", true)
58  .Cases("-o", "-dependency-file", true)
59  .Cases("-fdebug-compilation-dir", "-diagnostic-log-file", true)
60  .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
61  .Default(false);
62  if (ShouldSkip)
63  return true;
64 
65  // Some include flags shouldn't be skipped if we have a crash VFS
66  IsInclude = llvm::StringSwitch<bool>(Flag)
67  .Cases("-include", "-header-include-file", true)
68  .Cases("-idirafter", "-internal-isystem", "-iwithprefix", true)
69  .Cases("-internal-externc-isystem", "-iprefix", true)
70  .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
71  .Cases("-isysroot", "-I", "-F", "-resource-dir", true)
72  .Cases("-iframework", "-include-pch", true)
73  .Default(false);
74  if (IsInclude)
75  return !HaveCrashVFS;
76 
77  // The remaining flags are treated as a single argument.
78 
79  // These flags are all of the form -Flag and have no second argument.
80  ShouldSkip = llvm::StringSwitch<bool>(Flag)
81  .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
82  .Case("-MMD", true)
83  .Default(false);
84 
85  // Match found.
86  SkipNum = 1;
87  if (ShouldSkip)
88  return true;
89 
90  // These flags are treated as a single argument (e.g., -F<Dir>).
91  StringRef FlagRef(Flag);
92  IsInclude = FlagRef.startswith("-F") || FlagRef.startswith("-I");
93  if (IsInclude)
94  return !HaveCrashVFS;
95  if (FlagRef.startswith("-fmodules-cache-path="))
96  return true;
97 
98  SkipNum = 0;
99  return false;
100 }
101 
102 void Command::printArg(raw_ostream &OS, StringRef Arg, bool Quote) {
103  const bool Escape = Arg.find_first_of("\"\\$") != StringRef::npos;
104 
105  if (!Quote && !Escape) {
106  OS << Arg;
107  return;
108  }
109 
110  // Quote and escape. This isn't really complete, but good enough.
111  OS << '"';
112  for (const auto c : Arg) {
113  if (c == '"' || c == '\\' || c == '$')
114  OS << '\\';
115  OS << c;
116  }
117  OS << '"';
118 }
119 
120 void Command::writeResponseFile(raw_ostream &OS) const {
121  // In a file list, we only write the set of inputs to the response file
122  if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
123  for (const auto *Arg : InputFileList) {
124  OS << Arg << '\n';
125  }
126  return;
127  }
128 
129  // In regular response files, we send all arguments to the response file.
130  // Wrapping all arguments in double quotes ensures that both Unix tools and
131  // Windows tools understand the response file.
132  for (const auto *Arg : Arguments) {
133  OS << '"';
134 
135  for (; *Arg != '\0'; Arg++) {
136  if (*Arg == '\"' || *Arg == '\\') {
137  OS << '\\';
138  }
139  OS << *Arg;
140  }
141 
142  OS << "\" ";
143  }
144 }
145 
146 void Command::buildArgvForResponseFile(
148  // When not a file list, all arguments are sent to the response file.
149  // This leaves us to set the argv to a single parameter, requesting the tool
150  // to read the response file.
151  if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
152  Out.push_back(Executable);
153  Out.push_back(ResponseFileFlag.c_str());
154  return;
155  }
156 
157  llvm::StringSet<> Inputs;
158  for (const auto *InputName : InputFileList)
159  Inputs.insert(InputName);
160  Out.push_back(Executable);
161  // In a file list, build args vector ignoring parameters that will go in the
162  // response file (elements of the InputFileList vector)
163  bool FirstInput = true;
164  for (const auto *Arg : Arguments) {
165  if (Inputs.count(Arg) == 0) {
166  Out.push_back(Arg);
167  } else if (FirstInput) {
168  FirstInput = false;
169  Out.push_back(Creator.getResponseFileFlag());
170  Out.push_back(ResponseFile);
171  }
172  }
173 }
174 
175 /// Rewrite relative include-like flag paths to absolute ones.
176 static void
178  size_t NumArgs,
180  using namespace llvm;
181  using namespace sys;
182 
183  auto getAbsPath = [](StringRef InInc, SmallVectorImpl<char> &OutInc) -> bool {
184  if (path::is_absolute(InInc)) // Nothing to do here...
185  return false;
186  std::error_code EC = fs::current_path(OutInc);
187  if (EC)
188  return false;
189  path::append(OutInc, InInc);
190  return true;
191  };
192 
193  SmallString<128> NewInc;
194  if (NumArgs == 1) {
195  StringRef FlagRef(Args[Idx + NumArgs - 1]);
196  assert((FlagRef.startswith("-F") || FlagRef.startswith("-I")) &&
197  "Expecting -I or -F");
198  StringRef Inc = FlagRef.slice(2, StringRef::npos);
199  if (getAbsPath(Inc, NewInc)) {
200  SmallString<128> NewArg(FlagRef.slice(0, 2));
201  NewArg += NewInc;
202  IncFlags.push_back(std::move(NewArg));
203  }
204  return;
205  }
206 
207  assert(NumArgs == 2 && "Not expecting more than two arguments");
208  StringRef Inc(Args[Idx + NumArgs - 1]);
209  if (!getAbsPath(Inc, NewInc))
210  return;
211  IncFlags.push_back(SmallString<128>(Args[Idx]));
212  IncFlags.push_back(std::move(NewInc));
213 }
214 
215 void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
216  CrashReportInfo *CrashInfo) const {
217  // Always quote the exe.
218  OS << ' ';
219  printArg(OS, Executable, /*Quote=*/true);
220 
221  ArrayRef<const char *> Args = Arguments;
222  SmallVector<const char *, 128> ArgsRespFile;
223  if (ResponseFile != nullptr) {
224  buildArgvForResponseFile(ArgsRespFile);
225  Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
226  }
227 
228  bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
229  for (size_t i = 0, e = Args.size(); i < e; ++i) {
230  const char *const Arg = Args[i];
231 
232  if (CrashInfo) {
233  int NumArgs = 0;
234  bool IsInclude = false;
235  if (skipArgs(Arg, HaveCrashVFS, NumArgs, IsInclude)) {
236  i += NumArgs - 1;
237  continue;
238  }
239 
240  // Relative includes need to be expanded to absolute paths.
241  if (HaveCrashVFS && IsInclude) {
242  SmallVector<SmallString<128>, 2> NewIncFlags;
243  rewriteIncludes(Args, i, NumArgs, NewIncFlags);
244  if (!NewIncFlags.empty()) {
245  for (auto &F : NewIncFlags) {
246  OS << ' ';
247  printArg(OS, F.c_str(), Quote);
248  }
249  i += NumArgs - 1;
250  continue;
251  }
252  }
253 
254  auto Found = std::find_if(InputFilenames.begin(), InputFilenames.end(),
255  [&Arg](StringRef IF) { return IF == Arg; });
256  if (Found != InputFilenames.end() &&
257  (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
258  // Replace the input file name with the crashinfo's file name.
259  OS << ' ';
260  StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
261  printArg(OS, ShortName.str(), Quote);
262  continue;
263  }
264  }
265 
266  OS << ' ';
267  printArg(OS, Arg, Quote);
268  }
269 
270  if (CrashInfo && HaveCrashVFS) {
271  OS << ' ';
272  printArg(OS, "-ivfsoverlay", Quote);
273  OS << ' ';
274  printArg(OS, CrashInfo->VFSPath.str(), Quote);
275 
276  // The leftover modules from the crash are stored in
277  // <name>.cache/vfs/modules
278  // Leave it untouched for pcm inspection and provide a clean/empty dir
279  // path to contain the future generated module cache:
280  // <name>.cache/vfs/repro-modules
281  SmallString<128> RelModCacheDir = llvm::sys::path::parent_path(
282  llvm::sys::path::parent_path(CrashInfo->VFSPath));
283  llvm::sys::path::append(RelModCacheDir, "repro-modules");
284 
285  std::string ModCachePath = "-fmodules-cache-path=";
286  ModCachePath.append(RelModCacheDir.c_str());
287 
288  OS << ' ';
289  printArg(OS, ModCachePath, Quote);
290  }
291 
292  if (ResponseFile != nullptr) {
293  OS << "\n Arguments passed via response file:\n";
294  writeResponseFile(OS);
295  // Avoiding duplicated newline terminator, since FileLists are
296  // newline-separated.
297  if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
298  OS << "\n";
299  OS << " (end of response file)";
300  }
301 
302  OS << Terminator;
303 }
304 
305 void Command::setResponseFile(const char *FileName) {
306  ResponseFile = FileName;
307  ResponseFileFlag = Creator.getResponseFileFlag();
308  ResponseFileFlag += FileName;
309 }
310 
312  Environment.reserve(NewEnvironment.size() + 1);
313  Environment.assign(NewEnvironment.begin(), NewEnvironment.end());
314  Environment.push_back(nullptr);
315 }
316 
318  std::string *ErrMsg, bool *ExecutionFailed) const {
319  if (PrintInputFilenames) {
320  for (const char *Arg : InputFilenames)
321  llvm::outs() << llvm::sys::path::filename(Arg) << "\n";
322  llvm::outs().flush();
323  }
324 
326 
328  std::vector<StringRef> ArgvVectorStorage;
329  if (!Environment.empty()) {
330  assert(Environment.back() == nullptr &&
331  "Environment vector should be null-terminated by now");
332  ArgvVectorStorage = llvm::toStringRefArray(Environment.data());
333  Env = makeArrayRef(ArgvVectorStorage);
334  }
335 
336  if (ResponseFile == nullptr) {
337  Argv.push_back(Executable);
338  Argv.append(Arguments.begin(), Arguments.end());
339  Argv.push_back(nullptr);
340 
341  auto Args = llvm::toStringRefArray(Argv.data());
342  return llvm::sys::ExecuteAndWait(
343  Executable, Args, Env, Redirects, /*secondsToWait*/ 0,
344  /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
345  }
346 
347  // We need to put arguments in a response file (command is too large)
348  // Open stream to store the response file contents
349  std::string RespContents;
350  llvm::raw_string_ostream SS(RespContents);
351 
352  // Write file contents and build the Argv vector
353  writeResponseFile(SS);
354  buildArgvForResponseFile(Argv);
355  Argv.push_back(nullptr);
356  SS.flush();
357 
358  // Save the response file in the appropriate encoding
359  if (std::error_code EC = writeFileWithEncoding(
360  ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
361  if (ErrMsg)
362  *ErrMsg = EC.message();
363  if (ExecutionFailed)
364  *ExecutionFailed = true;
365  return -1;
366  }
367 
368  auto Args = llvm::toStringRefArray(Argv.data());
369  return llvm::sys::ExecuteAndWait(Executable, Args, Env, Redirects,
370  /*secondsToWait*/ 0,
371  /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
372 }
373 
374 FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
375  const char *Executable_,
376  const llvm::opt::ArgStringList &Arguments_,
377  ArrayRef<InputInfo> Inputs,
378  std::unique_ptr<Command> Fallback_)
379  : Command(Source_, Creator_, Executable_, Arguments_, Inputs),
380  Fallback(std::move(Fallback_)) {}
381 
382 void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
383  bool Quote, CrashReportInfo *CrashInfo) const {
384  Command::Print(OS, "", Quote, CrashInfo);
385  OS << " ||";
386  Fallback->Print(OS, Terminator, Quote, CrashInfo);
387 }
388 
389 static bool ShouldFallback(int ExitCode) {
390  // FIXME: We really just want to fall back for internal errors, such
391  // as when some symbol cannot be mangled, when we should be able to
392  // parse something but can't, etc.
393  return ExitCode != 0;
394 }
395 
397  std::string *ErrMsg, bool *ExecutionFailed) const {
398  int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
399  if (!ShouldFallback(PrimaryStatus))
400  return PrimaryStatus;
401 
402  // Clear ExecutionFailed and ErrMsg before falling back.
403  if (ErrMsg)
404  ErrMsg->clear();
405  if (ExecutionFailed)
406  *ExecutionFailed = false;
407 
408  const Driver &D = getCreator().getToolChain().getDriver();
409  D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
410 
411  int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
412  return SecondaryStatus;
413 }
414 
416  const Action &Source_, const Tool &Creator_, const char *Executable_,
417  const llvm::opt::ArgStringList &Arguments_, ArrayRef<InputInfo> Inputs)
418  : Command(Source_, Creator_, Executable_, Arguments_, Inputs) {}
419 
420 void ForceSuccessCommand::Print(raw_ostream &OS, const char *Terminator,
421  bool Quote, CrashReportInfo *CrashInfo) const {
422  Command::Print(OS, "", Quote, CrashInfo);
423  OS << " || (exit 0)" << Terminator;
424 }
425 
427  std::string *ErrMsg,
428  bool *ExecutionFailed) const {
429  int Status = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
430  (void)Status;
431  if (ExecutionFailed)
432  *ExecutionFailed = false;
433  return 0;
434 }
435 
436 void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
437  CrashReportInfo *CrashInfo) const {
438  for (const auto &Job : *this)
439  Job.Print(OS, Terminator, Quote, CrashInfo);
440 }
441 
442 void JobList::clear() { Jobs.clear(); }
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
Definition: Dominators.h:30
const char * getResponseFileFlag() const
Returns which prefix to use when passing the name of a response file as a parameter to this tool...
Definition: Tool.h:113
void setResponseFile(const char *FileName)
Set to pass arguments via a response file when launching the command.
Definition: Job.cpp:305
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:109
static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum, bool &IsInclude)
Check if the compiler flag in question should be skipped when emitting a reproducer.
Definition: Job.cpp:51
ResponseFileSupport getResponseFilesSupport() const
Returns the level of support for response files of this tool, whether it accepts arguments to be pass...
Definition: Tool.h:93
Command(const Action &Source, const Tool &Creator, const char *Executable, const llvm::opt::ArgStringList &Arguments, ArrayRef< InputInfo > Inputs)
Definition: Job.cpp:37
Definition: Format.h:2072
Action - Represent an abstract compilation step to perform.
Definition: Action.h:48
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:58
FallbackCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const llvm::opt::ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs, std::unique_ptr< Command > Fallback_)
Definition: Job.cpp:374
int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
Definition: Job.cpp:426
const Tool & getCreator() const
getCreator - Return the Tool which caused the creation of this job.
Definition: Job.h:108
void setEnvironment(llvm::ArrayRef< const char *> NewEnvironment)
Sets the environment to be used by the new process.
Definition: Job.cpp:311
static void rewriteIncludes(const llvm::ArrayRef< const char *> &Args, size_t Idx, size_t NumArgs, llvm::SmallVectorImpl< llvm::SmallString< 128 >> &IncFlags)
Rewrite relative include-like flag paths to absolute ones.
Definition: Job.cpp:177
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:436
static bool ShouldFallback(int ExitCode)
Definition: Job.cpp:389
static void printArg(llvm::raw_ostream &OS, StringRef Arg, bool Quote)
Print a command argument, and optionally quote it.
Definition: Job.cpp:102
void clear()
Clear the job list.
Definition: Job.cpp:442
Command - An executable path/name and argument vector to execute.
Definition: Job.h:42
const Driver & getDriver() const
Definition: ToolChain.h:185
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
Definition: Job.cpp:382
Dataflow Directional Tag Classes.
void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const override
Definition: Job.cpp:420
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
const ToolChain & getToolChain() const
Definition: Tool.h:84
int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const override
Definition: Job.cpp:396
ForceSuccessCommand(const Action &Source_, const Tool &Creator_, const char *Executable_, const llvm::opt::ArgStringList &Arguments_, ArrayRef< InputInfo > Inputs)
Definition: Job.cpp:415
virtual void Print(llvm::raw_ostream &OS, const char *Terminator, bool Quote, CrashReportInfo *CrashInfo=nullptr) const
Definition: Job.cpp:215
virtual int Execute(ArrayRef< Optional< StringRef >> Redirects, std::string *ErrMsg, bool *ExecutionFailed) const
Definition: Job.cpp:317