2011-01-01 01:31:54 +08:00
|
|
|
//===--- Job.cpp - Command to Execute -------------------------------------===//
|
2009-03-14 07:36:33 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Driver/Job.h"
|
2011-08-03 01:58:04 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2013-09-13 02:23:34 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/ADT/StringSwitch.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-03-14 07:36:33 +08:00
|
|
|
#include <cassert>
|
|
|
|
using namespace clang::driver;
|
2013-09-13 02:23:34 +08:00
|
|
|
using llvm::raw_ostream;
|
|
|
|
using llvm::StringRef;
|
2009-03-14 07:36:33 +08:00
|
|
|
|
|
|
|
Job::~Job() {}
|
|
|
|
|
2009-12-02 11:23:25 +08:00
|
|
|
Command::Command(const Action &_Source, const Tool &_Creator,
|
2013-06-15 01:17:23 +08:00
|
|
|
const char *_Executable,
|
|
|
|
const llvm::opt::ArgStringList &_Arguments)
|
|
|
|
: Job(CommandClass), Source(_Source), Creator(_Creator),
|
|
|
|
Executable(_Executable), Arguments(_Arguments) {}
|
2009-03-14 07:36:33 +08:00
|
|
|
|
2013-09-13 02:23:34 +08:00
|
|
|
static int skipArgs(const char *Flag) {
|
|
|
|
// These flags are all of the form -Flag <Arg> and are treated as two
|
|
|
|
// arguments. Therefore, we need to skip the flag and the next argument.
|
|
|
|
bool Res = llvm::StringSwitch<bool>(Flag)
|
|
|
|
.Cases("-I", "-MF", "-MT", "-MQ", true)
|
|
|
|
.Cases("-o", "-coverage-file", "-dependency-file", true)
|
|
|
|
.Cases("-fdebug-compilation-dir", "-idirafter", true)
|
|
|
|
.Cases("-include", "-include-pch", "-internal-isystem", true)
|
|
|
|
.Cases("-internal-externc-isystem", "-iprefix", "-iwithprefix", true)
|
|
|
|
.Cases("-iwithprefixbefore", "-isysroot", "-isystem", "-iquote", true)
|
|
|
|
.Cases("-resource-dir", "-serialize-diagnostic-file", true)
|
|
|
|
.Case("-dwarf-debug-flags", true)
|
|
|
|
.Default(false);
|
|
|
|
|
|
|
|
// Match found.
|
|
|
|
if (Res)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
// The remaining flags are treated as a single argument.
|
|
|
|
|
|
|
|
// These flags are all of the form -Flag and have no second argument.
|
|
|
|
Res = llvm::StringSwitch<bool>(Flag)
|
|
|
|
.Cases("-M", "-MM", "-MG", "-MP", "-MD", true)
|
|
|
|
.Case("-MMD", true)
|
|
|
|
.Default(false);
|
|
|
|
|
|
|
|
// Match found.
|
|
|
|
if (Res)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// These flags are treated as a single argument (e.g., -F<Dir>).
|
|
|
|
StringRef FlagRef(Flag);
|
|
|
|
if (FlagRef.startswith("-F") || FlagRef.startswith("-I"))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool quoteNextArg(const char *flag) {
|
|
|
|
return llvm::StringSwitch<bool>(flag)
|
|
|
|
.Case("-D", true)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintArg(raw_ostream &OS, const char *Arg, bool Quote) {
|
|
|
|
const bool Escape = std::strpbrk(Arg, "\"\\$");
|
|
|
|
|
|
|
|
if (!Quote && !Escape) {
|
|
|
|
OS << Arg;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quote and escape. This isn't really complete, but good enough.
|
|
|
|
OS << '"';
|
|
|
|
while (const char c = *Arg++) {
|
|
|
|
if (c == '"' || c == '\\' || c == '$')
|
|
|
|
OS << '\\';
|
|
|
|
OS << c;
|
|
|
|
}
|
|
|
|
OS << '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
void Command::Print(raw_ostream &OS, const char *Terminator, bool Quote,
|
|
|
|
bool CrashReport) const {
|
|
|
|
OS << " \"" << Executable << '"';
|
|
|
|
|
|
|
|
for (size_t i = 0, e = Arguments.size(); i < e; ++i) {
|
|
|
|
const char *const Arg = Arguments[i];
|
|
|
|
|
|
|
|
if (CrashReport) {
|
|
|
|
if (int Skip = skipArgs(Arg)) {
|
|
|
|
i += Skip - 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OS << ' ';
|
|
|
|
PrintArg(OS, Arg, Quote);
|
|
|
|
|
|
|
|
if (CrashReport && quoteNextArg(Arg) && i + 1 < e) {
|
|
|
|
OS << ' ';
|
|
|
|
PrintArg(OS, Arguments[++i], true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OS << Terminator;
|
|
|
|
}
|
|
|
|
|
2009-03-14 07:36:33 +08:00
|
|
|
JobList::JobList() : Job(JobListClass) {}
|
2009-03-18 15:06:02 +08:00
|
|
|
|
2010-03-12 02:04:49 +08:00
|
|
|
JobList::~JobList() {
|
|
|
|
for (iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
delete *it;
|
|
|
|
}
|
|
|
|
|
2013-09-13 02:23:34 +08:00
|
|
|
void JobList::Print(raw_ostream &OS, const char *Terminator, bool Quote,
|
|
|
|
bool CrashReport) const {
|
|
|
|
for (const_iterator it = begin(), ie = end(); it != ie; ++it)
|
|
|
|
(*it)->Print(OS, Terminator, Quote, CrashReport);
|
|
|
|
}
|
|
|
|
|
2011-08-03 01:58:04 +08:00
|
|
|
void JobList::clear() {
|
|
|
|
DeleteContainerPointers(Jobs);
|
|
|
|
}
|