mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Added a new API, swift::driver::createCompilerInvocation. This takes an array of driver arguments, constructs a Driver and a Compilation, and then uses the Compilation's frontend arguments to create a CompilerInvocation. This works by forcing Driver to create a Compilation which contains a single compile command. (It achieves this by passing "-force-single-frontend-invocation" after all other arguments.) This approach roughly matches Clang's clang::createInvocationFromCommandLine. As implied by the namespacing, this lives in swiftDriver. As a result, swiftDriver now depends on swiftFrontend. In support of this, added a couple of driver diagnostics for exceptional error cases (where Driver produced something other than a single Command, or if that Command is not a frontend command). This fixes <rdar://problem/16125395>. Swift SVN r20972
88 lines
3.2 KiB
C++
88 lines
3.2 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 llvm::DenseSet<const Command *> CommandSet;
|
|
|
|
static void collectCompileCommands(const JobList &source, CommandSet &outCmds) {
|
|
for (const Job *J : source) {
|
|
if (const Command *Cmd = dyn_cast<Command>(J)) {
|
|
if (isa<CompileJobAction>(Cmd->getSource()))
|
|
outCmds.insert(Cmd);
|
|
|
|
collectCompileCommands(Cmd->getInputs(), outCmds);
|
|
}
|
|
else if (const JobList *JL = dyn_cast<JobList>(J))
|
|
collectCompileCommands(*JL, 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
|
|
|
|
CommandSet 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 Command *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;
|
|
}
|