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