blob: 0a783eb93b11f7096eebdc9d6d69a7e77ce0e4ec [file] [log] [blame]
Nick Lewycky3fdcc6f2010-12-31 17:31:54 +00001//===--- Job.cpp - Command to Execute -------------------------------------===//
Daniel Dunbar789e2202009-03-13 23:36:33 +00002//
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"
Chad Rosier2b819102011-08-02 17:58:04 +000011#include "llvm/ADT/STLExtras.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000012#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/StringSwitch.h"
Hans Wennborgaaaa2a12013-09-12 18:35:08 +000014#include "llvm/Support/Program.h"
Hans Wennborgfc338972013-09-12 18:23:34 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000016#include <cassert>
17using namespace clang::driver;
Hans Wennborgfc338972013-09-12 18:23:34 +000018using llvm::raw_ostream;
19using llvm::StringRef;
Daniel Dunbar789e2202009-03-13 23:36:33 +000020
21Job::~Job() {}
22
Daniel Dunbardaab7b12009-12-02 03:23:25 +000023Command::Command(const Action &_Source, const Tool &_Creator,
Hans Wennborg5c6ecf52013-09-18 00:21:51 +000024 const char *_Executable,
25 const llvm::opt::ArgStringList &_Arguments)
Reid Klecknerb1e25a12013-06-14 17:17:23 +000026 : Job(CommandClass), Source(_Source), Creator(_Creator),
27 Executable(_Executable), Arguments(_Arguments) {}
Daniel Dunbar789e2202009-03-13 23:36:33 +000028
Hans Wennborgfc338972013-09-12 18:23:34 +000029static int skipArgs(const char *Flag) {
30 // These flags are all of the form -Flag <Arg> and are treated as two
31 // arguments. Therefore, we need to skip the flag and the next argument.
32 bool Res = llvm::StringSwitch<bool>(Flag)
33 .Cases("-I", "-MF", "-MT", "-MQ", true)
34 .Cases("-o", "-coverage-file", "-dependency-file", true)
35 .Cases("-fdebug-compilation-dir", "-idirafter", true)
36 .Cases("-include", "-include-pch", "-internal-isystem", true)
37 .Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
38 .Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
39 .Cases("-resource-dir", "-serialize-diagnostic-file", true)
40 .Case("-dwarf-debug-flags", true)
41 .Default(false);
42
43 // Match found.
44 if (Res)
45 return 2;
46
47 // The remaining flags are treated as a single argument.
48
49 // These flags are all of the form -Flag and have no second argument.
50 Res = llvm::StringSwitch<bool>(Flag)
51 .Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
52 .Case("-MMD", true)
53 .Default(false);
54
55 // Match found.
56 if (Res)
57 return 1;
58
59 // These flags are treated as a single argument (e.g., -F<Dir>).
60 StringRef FlagRef(Flag);
61 if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
62 return 1;
63
64 return 0;
65}
66
67static bool quoteNextArg(const char *flag) {
68 return llvm::StringSwitch<bool>(flag)
69 .Case("-D", true)
70 .Default(false);
71}
72
73static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
74 const bool Escape = std::strpbrk(Arg, "\"\\$");
75
76 if (!Quote && !Escape) {
77 OS << Arg;
78 return;
79 }
80
81 // Quote and escape. This isn't really complete, but good enough.
82 OS << '"';
83 while (const char c = *Arg++) {
84 if (c == '"' || c == '\\' || c == '$')
85 OS << '\\';
86 OS << c;
87 }
88 OS << '"';
89}
90
91void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
92 bool CrashReport) const {
93 OS << " \"" << Executable << '"';
94
95 for (size_t i = 0, e = Arguments.size(); i < e; ++i) {
96 const char *const Arg = Arguments[i];
97
98 if (CrashReport) {
99 if (int Skip = skipArgs(Arg)) {
100 i += Skip - 1;
101 continue;
102 }
103 }
104
105 OS << ' ';
106 PrintArg(OS, Arg, Quote);
107
108 if (CrashReport && quoteNextArg(Arg) && i + 1 < e) {
109 OS << ' ';
110 PrintArg(OS, Arguments[++i], true);
111 }
112 }
113 OS << Terminator;
114}
115
Hans Wennborg5c6ecf52013-09-18 00:21:51 +0000116int Command::Execute(const llvm::StringRef **Redirects, std::string *ErrMsg,
Hans Wennborgaaaa2a12013-09-12 18:35:08 +0000117 bool *ExecutionFailed) const {
118 SmallVector<const char*, 128> Argv;
119 Argv.push_back(Executable);
120 for (size_t i = 0, e = Arguments.size(); i != e; ++i)
121 Argv.push_back(Arguments[i]);
122 Argv.push_back(0);
123
124 return llvm::sys::ExecuteAndWait(Executable, Argv.data(), /*env*/ 0,
125 Redirects, /*secondsToWait*/ 0,
126 /*memoryLimit*/ 0, ErrMsg, ExecutionFailed);
127}
128
Daniel Dunbar789e2202009-03-13 23:36:33 +0000129JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000130
Daniel Dunbar9d440232010-03-11 18:04:49 +0000131JobList::~JobList() {
132 for (iterator it = begin(), ie = end(); it != ie; ++it)
133 delete *it;
134}
135
Hans Wennborgfc338972013-09-12 18:23:34 +0000136void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
137 bool CrashReport) const {
138 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
139 (*it)->Print(OS, Terminator, Quote, CrashReport);
140}
141
Chad Rosier2b819102011-08-02 17:58:04 +0000142void JobList::clear() {
143 DeleteContainerPointers(Jobs);
144}