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;
30 Command::Command(const Action &_Source, const Tool &_Creator,
31 const char *_Executable,
32 const ArgStringList &_Arguments)
33 : Job(CommandClass), Source(_Source), Creator(_Creator),
34 Executable(_Executable), Arguments(_Arguments),
35 ResponseFile(nullptr) {}
37 static int skipArgs(const char *Flag, bool HaveCrashVFS) {
38 // These flags are all of the form -Flag <Arg> and are treated as two
39 // arguments. Therefore, we need to skip the flag and the next argument.
40 bool Res = llvm::StringSwitch<bool>(Flag)
41 .Cases("-I", "-MF", "-MT", "-MQ", true)
42 .Cases("-o", "-coverage-file", "-dependency-file", true)
43 .Cases("-fdebug-compilation-dir", "-idirafter", true)
44 .Cases("-include", "-include-pch", "-internal-isystem", true)
45 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
46 .Cases("-iwithprefixbefore", "-isystem", "-iquote", true)
47 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
48 .Cases("-dwarf-debug-flags", "-ivfsoverlay", true)
49 // Some include flags shouldn't be skipped if we have a crash VFS
50 .Case("-isysroot", !HaveCrashVFS)
57 // The remaining flags are treated as a single argument.
59 // These flags are all of the form -Flag and have no second argument.
60 Res = llvm::StringSwitch<bool>(Flag)
61 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
69 // These flags are treated as a single argument (e.g., -F<Dir>).
70 StringRef FlagRef(Flag);
71 if (FlagRef.startswith("-F") || FlagRef.startswith("-I") ||
72 FlagRef.startswith("-fmodules-cache-path="))
78 static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
79 const bool Escape = std::strpbrk(Arg, "\"\\$");
81 if (!Quote && !Escape) {
86 // Quote and escape. This isn't really complete, but good enough.
88 while (const char c = *Arg++) {
89 if (c == '"' || c == '\\' || c == '$')
96 void Command::writeResponseFile(raw_ostream &OS) const {
97 // In a file list, we only write the set of inputs to the response file
98 if (Creator.getResponseFilesSupport() == Tool::RF_FileList) {
99 for (const char *Arg : InputFileList) {
105 // In regular response files, we send all arguments to the response file
106 for (const char *Arg : Arguments) {
109 for (; *Arg != '\0'; Arg++) {
110 if (*Arg == '\"' || *Arg == '\\') {
120 void Command::buildArgvForResponseFile(
121 llvm::SmallVectorImpl<const char *> &Out) const {
122 // When not a file list, all arguments are sent to the response file.
123 // This leaves us to set the argv to a single parameter, requesting the tool
124 // to read the response file.
125 if (Creator.getResponseFilesSupport() != Tool::RF_FileList) {
126 Out.push_back(Executable);
127 Out.push_back(ResponseFileFlag.c_str());
131 llvm::StringSet<> Inputs;
132 for (const char *InputName : InputFileList)
133 Inputs.insert(InputName);
134 Out.push_back(Executable);
135 // In a file list, build args vector ignoring parameters that will go in the
136 // response file (elements of the InputFileList vector)
137 bool FirstInput = true;
138 for (const char *Arg : Arguments) {
139 if (Inputs.count(Arg) == 0) {
141 } else if (FirstInput) {
143 Out.push_back(Creator.getResponseFileFlag());
144 Out.push_back(ResponseFile);
149 void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
150 CrashReportInfo *CrashInfo) const {
151 // Always quote the exe.
153 PrintArg(OS, Executable, /*Quote=*/true);
155 llvm::ArrayRef<const char *> Args = Arguments;
156 llvm::SmallVector<const char *, 128> ArgsRespFile;
157 if (ResponseFile != nullptr) {
158 buildArgvForResponseFile(ArgsRespFile);
159 Args = ArrayRef<const char *>(ArgsRespFile).slice(1); // no executable name
162 StringRef MainFilename;
163 // We'll need the argument to -main-file-name to find the input file name.
165 for (size_t I = 0, E = Args.size(); I + 1 < E; ++I)
166 if (StringRef(Args[I]).equals("-main-file-name"))
167 MainFilename = Args[I + 1];
169 bool HaveCrashVFS = CrashInfo && !CrashInfo->VFSPath.empty();
170 for (size_t i = 0, e = Args.size(); i < e; ++i) {
171 const char *const Arg = Args[i];
174 if (int Skip = skipArgs(Arg, HaveCrashVFS)) {
177 } else if (llvm::sys::path::filename(Arg) == MainFilename &&
178 (i == 0 || StringRef(Args[i - 1]) != "-main-file-name")) {
179 // Replace the input file name with the crashinfo's file name.
181 StringRef ShortName = llvm::sys::path::filename(CrashInfo->Filename);
182 PrintArg(OS, ShortName.str().c_str(), Quote);
188 PrintArg(OS, Arg, Quote);
191 if (CrashInfo && HaveCrashVFS) {
193 PrintArg(OS, "-ivfsoverlay", Quote);
195 PrintArg(OS, CrashInfo->VFSPath.str().c_str(), Quote);
198 if (ResponseFile != nullptr) {
199 OS << "\n Arguments passed via response file:\n";
200 writeResponseFile(OS);
201 // Avoiding duplicated newline terminator, since FileLists are
202 // newline-separated.
203 if (Creator.getResponseFilesSupport() != Tool::RF_FileList)
205 OS << " (end of response file)";
211 void Command::setResponseFile(const char *FileName) {
212 ResponseFile = FileName;
213 ResponseFileFlag = Creator.getResponseFileFlag();
214 ResponseFileFlag += FileName;
217 int Command::Execute(const StringRef **Redirects, std::string *ErrMsg,
218 bool *ExecutionFailed) const {
219 SmallVector<const char*, 128> Argv;
221 if (ResponseFile == nullptr) {
222 Argv.push_back(Executable);
223 Argv.append(Arguments.begin(), Arguments.end());
224 Argv.push_back(nullptr);
226 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
227 Redirects, /*secondsToWait*/ 0,
228 /*memoryLimit*/ 0, ErrMsg,
232 // We need to put arguments in a response file (command is too large)
233 // Open stream to store the response file contents
234 std::string RespContents;
235 llvm::raw_string_ostream SS(RespContents);
237 // Write file contents and build the Argv vector
238 writeResponseFile(SS);
239 buildArgvForResponseFile(Argv);
240 Argv.push_back(nullptr);
243 // Save the response file in the appropriate encoding
244 if (std::error_code EC = writeFileWithEncoding(
245 ResponseFile, RespContents, Creator.getResponseFileEncoding())) {
247 *ErrMsg = EC.message();
249 *ExecutionFailed = true;
253 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ nullptr,
254 Redirects, /*secondsToWait*/ 0,
255 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
258 FallbackCommand::FallbackCommand(const Action &Source_, const Tool &Creator_,
259 const char *Executable_,
260 const ArgStringList &Arguments_,
261 std::unique_ptr<Command> Fallback_)
262 : Command(Source_, Creator_, Executable_, Arguments_),
263 Fallback(std::move(Fallback_)) {}
265 void FallbackCommand::Print(raw_ostream &OS, const char *Terminator,
266 bool Quote, CrashReportInfo *CrashInfo) const {
267 Command::Print(OS, "", Quote, CrashInfo);
269 Fallback->Print(OS, Terminator, Quote, CrashInfo);
272 static bool ShouldFallback(int ExitCode) {
273 // FIXME: We really just want to fall back for internal errors, such
274 // as when some symbol cannot be mangled, when we should be able to
275 // parse something but can't, etc.
276 return ExitCode != 0;
279 int FallbackCommand::Execute(const StringRef **Redirects, std::string *ErrMsg,
280 bool *ExecutionFailed) const {
281 int PrimaryStatus = Command::Execute(Redirects, ErrMsg, ExecutionFailed);
282 if (!ShouldFallback(PrimaryStatus))
283 return PrimaryStatus;
285 // Clear ExecutionFailed and ErrMsg before falling back.
289 *ExecutionFailed = false;
291 const Driver &D = getCreator().getToolChain().getDriver();
292 D.Diag(diag::warn_drv_invoking_fallback) << Fallback->getExecutable();
294 int SecondaryStatus = Fallback->Execute(Redirects, ErrMsg, ExecutionFailed);
295 return SecondaryStatus;
298 JobList::JobList() : Job(JobListClass) {}
300 void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
301 CrashReportInfo *CrashInfo) const {
302 for (const auto &Job : *this)
303 Job.Print(OS, Terminator, Quote, CrashInfo);
306 void JobList::clear() { Jobs.clear(); }