blob: 116fbd0541f58398041bcdb554c44ada0997b335 [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"
14#include "llvm/Support/raw_ostream.h"
Daniel Dunbar789e2202009-03-13 23:36:33 +000015#include <cassert>
16using namespace clang::driver;
Hans Wennborgfc338972013-09-12 18:23:34 +000017using llvm::raw_ostream;
18using llvm::StringRef;
Daniel Dunbar789e2202009-03-13 23:36:33 +000019
20Job::~Job() {}
21
Daniel Dunbardaab7b12009-12-02 03:23:25 +000022Command::Command(const Action &_Source, const Tool &_Creator,
Reid Klecknerb1e25a12013-06-14 17:17:23 +000023 const char *_Executable,
24 const llvm::opt::ArgStringList &_Arguments)
25 : 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
Daniel Dunbar789e2202009-03-13 23:36:33 +0000115JobList::JobList() : Job(JobListClass) {}
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000116
Daniel Dunbar9d440232010-03-11 18:04:49 +0000117JobList::~JobList() {
118 for (iterator it = begin(), ie = end(); it != ie; ++it)
119 delete *it;
120}
121
Hans Wennborgfc338972013-09-12 18:23:34 +0000122void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
123 bool CrashReport) const {
124 for (const_iterator it = begin(), ie = end(); it != ie; ++it)
125 (*it)->Print(OS, Terminator, Quote, CrashReport);
126}
127
Chad Rosier2b819102011-08-02 17:58:04 +0000128void JobList::clear() {
129 DeleteContainerPointers(Jobs);
130}