Files
swift-mirror/lib/Driver/FrontendUtil.cpp
Jordan Rose c4fd29eb0b [Driver] Eliminate the common base class of Command and JobList.
...and rename Command to Job (previously the name of the base class).

We never generated job lists directly contained in other job lists, so
let's not even worry about this case. We may some day need to break Job
out into separate subclasses (Clang has Command and FallbackCommand in
addition to JobList), but we should be able to keep the list separate.

No intended functionality change.

Swift SVN r23144
2014-11-07 00:10:18 +00:00

84 lines
3.0 KiB
C++

//===--- FrontendUtil.cpp - Driver Utilities for Frontend -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/Driver/FrontendUtil.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Driver/Action.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/Tool.h"
#include "swift/Frontend/Frontend.h"
using namespace swift;
using namespace swift::driver;
typedef SmallPtrSet<const Job *, 4> SmallCommandSet;
static void collectCompileCommands(const JobList &source, SmallCommandSet &outCmds) {
for (const Job *Cmd : source) {
if (isa<CompileJobAction>(Cmd->getSource()))
outCmds.insert(Cmd);
collectCompileCommands(Cmd->getInputs(), outCmds);
}
}
std::unique_ptr<CompilerInvocation>
swift::driver::createCompilerInvocation(ArrayRef<const char *> ArgList,
DiagnosticEngine &Diags) {
SmallVector<const char *, 16> Args;
Args.push_back("<swiftc>"); // FIXME: Remove dummy argument.
Args.insert(Args.end(), ArgList.begin(), ArgList.end());
// When creating a CompilerInvocation, ensure that the driver creates a single
// frontend command.
Args.push_back("-force-single-frontend-invocation");
// Force the driver into batch mode by specifying "swiftc" as the name.
Driver TheDriver("swiftc", "swiftc", Diags);
// Don't check for the existence of input files, since the user of the
// CompilerInvocation may wish to remap inputs to source buffers.
TheDriver.setCheckInputFilesExist(false);
std::unique_ptr<Compilation> C = TheDriver.buildCompilation(Args);
if (!C || C->getJobs().empty())
return nullptr; // Don't emit an error; one should already have been emitted
SmallCommandSet CompileCommands;
collectCompileCommands(C->getJobs(), CompileCommands);
if (CompileCommands.size() != 1) {
// TODO: include Jobs in the diagnostic.
Diags.diagnose(SourceLoc(), diag::error_expected_one_frontend_job);
return nullptr;
}
const Job *Cmd = *CompileCommands.begin();
if (Cmd->getCreator().getName() != "swift") {
Diags.diagnose(SourceLoc(), diag::error_expected_frontend_command);
return nullptr;
}
std::unique_ptr<CompilerInvocation> Invocation(new CompilerInvocation());
const llvm::opt::ArgStringList &BaseFrontendArgs = Cmd->getArguments();
ArrayRef<const char *> FrontendArgs =
llvm::makeArrayRef(BaseFrontendArgs.data() + 1,
BaseFrontendArgs.data() + BaseFrontendArgs.size());
if (Invocation->parseArgs(FrontendArgs, Diags))
return nullptr; // Don't emit an error; one should already have been emitted
return Invocation;
}