1 //===--- Job.cpp - Command to Execute -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Driver/Driver.h"
11 #include "clang/Driver/DriverDiagnostic.h"
12 #include "clang/Driver/Job.h"
13 #include "clang/Driver/Tool.h"
14 #include "clang/Driver/ToolChain.h"
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/StringSet.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/Support/Program.h"
21 #include "llvm/Support/raw_ostream.h"
23 using namespace clang::driver;
24 using llvm::raw_ostream;
25 using llvm::StringRef;
28 Command::Command(const Action &Source, const Tool &Creator,
29 const char *Executable, const ArgStringList &Arguments)
30 : Source(Source), Creator(Creator), Executable(Executable),
31 Arguments(Arguments), ResponseFile(nullptr) {}
33 static int skipArgs(const char *Flag, bool HaveCrashVFS) {
34 // These flags are all of the form -Flag <Arg> and are treated as two
35 // arguments. Therefore, we need to skip the flag and the next argument.
36 bool Res = llvm::StringSwitch<bool>(Flag)
37 .Cases("-I", "-MF", "-MT", "-MQ", true)
38 .Cases("-o", "-coverage-file", "-dependency-file", true)
39 .Cases("-fdebug-compilation-dir", "-idirafter", true)
40 .Cases("-include", "-include-pch", "-internal-isystem", true)
41 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
42 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
43 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
44 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
45 // Some include flags shouldn't be skipped if we have a crash VFS
46 .Case("-isysroot", !HaveCrashVFS)
53 // The remaining flags are treated as a single argument.
55 // These flags are all of the form -Flag and have no second argument.
56 Res = llvm::StringSwitch<bool>(Flag)
57 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
65 // These flags are treated as a single argument (e.g., -F<Dir>).
66 StringRef FlagRef(Flag);
67 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
68 FlagRef.startswith("-fmodules-cache-path="))
74 void Command::printArg(raw_ostream &OS, const char *Arg, bool Quote) {
75 const bool Escape = std::strpbrk(Arg, "\"\\$");
77 if (!Quote && !Escape) {
82 // Quote and escape. This isn't really complete, but good enough.
84 while (const char c = *Arg++) {
85 if (c == '"' || c == '\\' || c == '$')
92 void Command::writeResponseFile(raw_ostream &OS) const {
93 // In a file list, we only write the set of inputs to the response file
94 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
95 for (const char *Arg : InputFileList) {
101 // In regular response files, we send all arguments to the response file
102 for (const char *Arg : Arguments) {
105 for (; *Arg != '\0'; Arg++) {
106 if (*Arg == '\"' || *Arg == '\\') {
116 void Command::buildArgvForResponseFile(
117 llvm::SmallVectorImpl<const char *> &Out) const {
118 // When not a file list, all arguments are sent to the response file.
119 // This leaves us to set the argv to a single parameter, requesting the tool
120 // to read the response file.
121 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
122 Out.push_back(Executable);
123 Out.push_back(ResponseFileFlag.c_str());
127 llvm::StringSet<> Inputs;
128 for (const char *InputName : InputFileList)
129 Inputs.insert(InputName);
130 Out.push_back(Executable);
131 // In a file list, build args vector ignoring parameters that will go in the
132 // response file (elements of the InputFileList vector)
133 bool FirstInput = true;
134 for (const char *Arg : Arguments) {
135 if (Inputs.count(Arg) == 0) {
137 } else if (FirstInput) {
139 Out.push_back(Creator.getResponseFileFlag());
140 Out.push_back(ResponseFile);
145 void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
146 CrashReportInfo *CrashInfo) const {
147 // Always quote the exe.
149 printArg(OS, Executable, /*Quote=*/true);
151 llvm::ArrayRef<const char *> Args = Arguments;
152 llvm::SmallVector<const char *, 128> ArgsRespFile;
153 if (ResponseFile != nullptr) {
154 buildArgvForResponseFile(ArgsRespFile);
155 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
158 StringRef MainFilename;
159 // We'll need the argument to -main-file-name to find the input file name.
161 for (size_t I = 0, E = Args.size(); I + 1 < E; ++I)
162 if (StringRef(Args[I]).equals("-main-file-name"))
163 MainFilename = Args[I + 1];
165 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
166 for (size_t i = 0, e = Args.size(); i < e; ++i) {
167 const char *const Arg = Args[i];
170 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
173 } else if (llvm::sys::path::filename(Arg) == MainFilename &&
174 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
175 // Replace the input file name with the crashinfo's file name.
177 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
178 printArg(OS, ShortName.str().c_str(), Quote);
184 printArg(OS, Arg, Quote);
187 if (CrashInfo && HaveCrashVFS) {
189 printArg(OS, "-ivfsoverlay", Quote);
191 printArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
194 if (ResponseFile != nullptr) {
195 OS << "\n Arguments passed via response file:\n";
196 writeResponseFile(OS);
197 // Avoiding duplicated newline terminator, since FileLists are
198 // newline-separated.
199 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
201 OS << " (end of response file)";
207 void Command::setResponseFile(const char *FileName) {
208 ResponseFile = FileName;
209 ResponseFileFlag = Creator.getResponseFileFlag();
210 ResponseFileFlag += FileName;
213 int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
214 bool *ExecutionFailed) const {
215 SmallVector<const char*, 128> Argv;
217 if (ResponseFile == nullptr) {
218 Argv.push_back(Executable);
219 Argv.append(Arguments.begin(), Arguments.end());
220 Argv.push_back(nullptr);
222 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
223 Redirects, /*secondsToWait*/ 0,
224 /*memoryLimit*/ 0, ErrMsg,
228 // We need to put arguments in a response file (command is too large)
229 // Open stream to store the response file contents
230 std::string RespContents;
231 llvm::raw_string_ostream SS(RespContents);
233 // Write file contents and build the Argv vector
234 writeResponseFile(SS);
235 buildArgvForResponseFile(Argv);
236 Argv.push_back(nullptr);
239 // Save the response file in the appropriate encoding
240 if (std::error_code EC = writeFileWithEncoding(
241 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
243 *ErrMsg = EC.message();
245 *ExecutionFailed = true;
249 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
250 Redirects, /*secondsToWait*/ 0,
251 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
254 FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
255 const char *Executable_,
256 const ArgStringList &Arguments_,
257 std::unique_ptr<Command> Fallback_)
258 : Command(Source_, Creator_, Executable_, Arguments_),
259 Fallback(std::move(Fallback_)) {}
261 void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
262 bool Quote, CrashReportInfo *CrashInfo) const {
263 Command::Print(OS, "", Quote, CrashInfo);
265 Fallback->Print(OS, Terminator, Quote, CrashInfo);
268 static bool ShouldFallback(int ExitCode) {
269 // FIXME: We really just want to fall back for internal errors, such
270 // as when some symbol cannot be mangled, when we should be able to
271 // parse something but can't, etc.
272 return ExitCode != 0;
275 int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
276 bool *ExecutionFailed) const {
277 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
278 if (!ShouldFallback(PrimaryStatus))
279 return PrimaryStatus;
281 // Clear ExecutionFailed and ErrMsg before falling back.
285 *ExecutionFailed = false;
287 const Driver &D = getCreator().getToolChain().getDriver();
288 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
290 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
291 return SecondaryStatus;
294 void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
295 CrashReportInfo *CrashInfo) const {
296 for (const auto &Job : *this)
297 Job.Print(OS, Terminator, Quote, CrashInfo);
300 void JobList::clear() { Jobs.clear(); }