Skip to content

Break Driver's dependency on Frontend #19012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- OutputFileMap.h - Driver output file map ---------------*- C++ -*-===//
//===--- OutputFileMap.h - Map of inputs to multiple outputs ----*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
Expand All @@ -10,8 +10,8 @@
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_DRIVER_OUTPUTFILEMAP_H
#define SWIFT_DRIVER_OUTPUTFILEMAP_H
#ifndef SWIFT_BASIC_OUTPUTFILEMAP_H
#define SWIFT_BASIC_OUTPUTFILEMAP_H

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_FRONTENDTOOL_REFERENCEDEPENDENCYKEYS_H
#define SWIFT_FRONTENDTOOL_REFERENCEDEPENDENCYKEYS_H
#ifndef SWIFT_BASIC_REFERENCEDEPENDENCYKEYS_H
#define SWIFT_BASIC_REFERENCEDEPENDENCYKEYS_H

#include "swift/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Compilation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

#include "swift/Basic/ArrayRefView.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Basic/Statistic.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/OutputFileMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Chrono.h"
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Basic/Sanitizers.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/OutputFileMap.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
Expand Down
28 changes: 15 additions & 13 deletions include/swift/Driver/FrontendUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,33 @@
#define SWIFT_DRIVER_FRONTENDUTIL_H

#include "swift/Basic/LLVM.h"
#include "llvm/ADT/STLExtras.h"

#include <memory>

namespace swift {

class CompilerInvocation;
class DiagnosticEngine;

namespace driver {

/// \brief Creates a CompilerInvocation from the given driver arguments.
/// Generates the list of arguments that would be passed to the compiler
/// frontend from the given driver arguments.
///
/// \param ArgList The driver arguments for which a CompilerInvocation
/// should be created.
/// \param Diags The DiagnosticEngine which should be used for parsing arguments
/// \param ArgList The driver arguments (i.e. normal arguments for \c swiftc).
/// \param Diags The DiagnosticEngine used to report any errors parsing the
/// arguments.
/// \param Action Called with the list of frontend arguments if there were no
/// errors in processing \p ArgList. This is a callback rather than a return
/// value to avoid copying the arguments more than necessary.
///
/// \returns A fully-formed CompilerInvocation, or nullptr if one couldn't be
/// created.
/// \returns True on error, or if \p Action returns true.
///
/// \note This function is not intended to create CompilerInvocation instances
/// which are suitable for use in REPL or immediate modes, since it will have
/// the effect of overriding the frontend's requested action to
/// FrontendOptions::ActionType::Parse.
std::unique_ptr<CompilerInvocation> createCompilerInvocation(
ArrayRef<const char *> ArgList, DiagnosticEngine &Diags);
/// \note This function is not intended to create invocations which are
/// suitable for use in REPL or immediate modes.
bool getSingleFrontendInvocationFromDriverArguments(
ArrayRef<const char *> ArgList, DiagnosticEngine &Diags,
llvm::function_ref<bool(ArrayRef<const char *> FrontendArgs)> Action);

} // end namespace driver
} // end namespace swift
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Driver/Job.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

#include "swift/Basic/FileTypes.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Driver/Action.h"
#include "swift/Driver/Util.h"
#include "swift/Frontend/OutputFileMap.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerIntPair.h"
Expand Down
1 change: 1 addition & 0 deletions lib/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ add_swift_library(swiftBasic STATIC
LangOptions.cpp
LLVMContext.cpp
Mangler.cpp
OutputFileMap.cpp
Platform.cpp
PrefixMap.cpp
PrettyStackTrace.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- OutputFileMap.cpp - Driver output file map -----------------------===//
//===--- OutputFileMap.h - Map of inputs to multiple outputs --------------===//
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, I just merged before spotting the typo.

//
// This source file is part of the Swift.org open source project
//
Expand All @@ -10,7 +10,7 @@
//
//===----------------------------------------------------------------------===//

#include "swift/Frontend/OutputFileMap.h"
#include "swift/Basic/OutputFileMap.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Path.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set(swiftDriver_targetDefines)
add_swift_library(swiftDriver STATIC
${swiftDriver_sources}
DEPENDS SwiftOptions
LINK_LIBRARIES swiftAST swiftBasic swiftFrontend swiftOption)
LINK_LIBRARIES swiftAST swiftBasic swiftOption)

# Generate the static-stdlib-args.lnk file used by -static-stdlib option
# for 'GenericUnix' (eg linux)
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Basic/Program.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/Statistic.h"
Expand All @@ -26,7 +27,6 @@
#include "swift/Driver/Job.h"
#include "swift/Driver/ParseableOutput.h"
#include "swift/Driver/ToolChain.h"
#include "swift/Frontend/OutputFileMap.h"
#include "swift/Option/Options.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/DarwinToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/TaskQueue.h"
#include "swift/Config.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/DependencyGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
//
//===----------------------------------------------------------------------===//

#include "swift/Basic/ReferenceDependencyKeys.h"
#include "swift/Basic/Statistic.h"
#include "swift/Driver/DependencyGraph.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Frontend/ReferenceDependencyKeys.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringSwitch.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swift/AST/DiagnosticsDriver.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/TaskQueue.h"
Expand All @@ -31,7 +32,6 @@
#include "swift/Driver/Job.h"
#include "swift/Driver/PrettyStackTrace.h"
#include "swift/Driver/ToolChain.h"
#include "swift/Frontend/OutputFileMap.h"
#include "swift/Option/Options.h"
#include "swift/Option/SanitizerOptions.h"
#include "swift/Parse/Lexer.h"
Expand Down
27 changes: 10 additions & 17 deletions lib/Driver/FrontendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Driver/ToolChain.h"
#include "swift/Frontend/Frontend.h"
#include "llvm/Option/ArgList.h"

using namespace swift;
using namespace swift::driver;

std::unique_ptr<CompilerInvocation>
swift::driver::createCompilerInvocation(ArrayRef<const char *> Argv,
DiagnosticEngine &Diags) {
bool swift::driver::getSingleFrontendInvocationFromDriverArguments(
ArrayRef<const char *> Argv, DiagnosticEngine &Diags,
llvm::function_ref<bool(ArrayRef<const char *> FrontendArgs)> Action) {
SmallVector<const char *, 16> Args;
Args.push_back("<swiftc>"); // FIXME: Remove dummy argument.
Args.insert(Args.end(), Argv.begin(), Argv.end());
Expand Down Expand Up @@ -55,16 +55,16 @@ swift::driver::createCompilerInvocation(ArrayRef<const char *> Argv,
std::unique_ptr<llvm::opt::InputArgList> ArgList =
TheDriver.parseArgStrings(ArrayRef<const char *>(Args).slice(1));
if (Diags.hadAnyError())
return nullptr;
return true;

std::unique_ptr<ToolChain> TC = TheDriver.buildToolChain(*ArgList);
if (Diags.hadAnyError())
return nullptr;
return true;

std::unique_ptr<Compilation> C =
TheDriver.buildCompilation(*TC, std::move(ArgList));
if (!C || C->getJobs().empty())
return nullptr; // Don't emit an error; one should already have been emitted
return true; // Don't emit an error; one should already have been emitted

SmallPtrSet<const Job *, 4> CompileCommands;
for (const Job *Cmd : C->getJobs())
Expand All @@ -74,22 +74,15 @@ swift::driver::createCompilerInvocation(ArrayRef<const char *> Argv,
if (CompileCommands.size() != 1) {
// TODO: include Jobs in the diagnostic.
Diags.diagnose(SourceLoc(), diag::error_expected_one_frontend_job);
return nullptr;
return true;
}

const Job *Cmd = *CompileCommands.begin();
if (StringRef("-frontend") != Cmd->getArguments().front()) {
Diags.diagnose(SourceLoc(), diag::error_expected_frontend_command);
return nullptr;
return true;
}

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;
return Action(llvm::makeArrayRef(BaseFrontendArgs).drop_front());
}
2 changes: 1 addition & 1 deletion lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Platform.h"
#include "swift/Basic/Range.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Basic/TaskQueue.h"
#include "swift/Config.h"
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
Expand Down
1 change: 0 additions & 1 deletion lib/Driver/UnixToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
Expand Down
1 change: 0 additions & 1 deletion lib/Driver/WindowsToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "swift/Driver/Compilation.h"
#include "swift/Driver/Driver.h"
#include "swift/Driver/Job.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Option/Options.h"
#include "clang/Basic/Version.h"
#include "clang/Driver/Util.h"
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
#include "ArgsToFrontendInputsConverter.h"
#include "ArgsToFrontendOptionsConverter.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/OutputFileMap.h"
#include "swift/Basic/Platform.h"
#include "swift/Frontend/Frontend.h"
#include "swift/Frontend/OutputFileMap.h"
#include "swift/Option/Options.h"
#include "swift/Option/SanitizerOptions.h"
#include "swift/Strings.h"
Expand Down
1 change: 0 additions & 1 deletion lib/Frontend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ add_swift_library(swiftFrontend STATIC
Frontend.cpp
FrontendInputsAndOutputs.cpp
FrontendOptions.cpp
OutputFileMap.cpp
PrintingDiagnosticConsumer.cpp
SerializedDiagnosticConsumer.cpp
DEPENDS
Expand Down
2 changes: 1 addition & 1 deletion lib/FrontendTool/ReferenceDependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include "swift/AST/ReferencedNameTracker.h"
#include "swift/AST/Types.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/ReferenceDependencyKeys.h"
#include "swift/Frontend/FrontendOptions.h"
#include "swift/Frontend/ReferenceDependencyKeys.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
Expand Down
9 changes: 6 additions & 3 deletions tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,12 @@ bool SwiftASTManager::initCompilerInvocation(CompilerInvocation &Invocation,
Args.push_back("-resource-dir");
Args.push_back(Impl.RuntimeResourcePath.c_str());

if (auto driverInvocation = driver::createCompilerInvocation(Args, Diags)) {
Invocation = *driverInvocation;
} else {
bool HadError = driver::getSingleFrontendInvocationFromDriverArguments(
Args, Diags, [&](ArrayRef<const char *> FrontendArgs) {
return Invocation.parseArgs(FrontendArgs, Diags);
});

if (HadError) {
// FIXME: Get the actual diagnostic.
Error = "error when parsing the compiler arguments";
return true;
Expand Down
17 changes: 10 additions & 7 deletions tools/swift-ide-test/ModuleAPIDiff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,27 +907,30 @@ int swift::doGenerateModuleAPIDescription(StringRef MainExecutablePath,
DiagnosticEngine Diags(SM);
Diags.addConsumer(PDC);

std::unique_ptr<CompilerInvocation> Invocation =
driver::createCompilerInvocation(CStringArgs, Diags);
CompilerInvocation Invocation;
bool HadError = driver::getSingleFrontendInvocationFromDriverArguments(
CStringArgs, Diags, [&](ArrayRef<const char *> FrontendArgs) {
return Invocation.parseArgs(FrontendArgs, Diags);
});

if (!Invocation) {
if (HadError) {
llvm::errs() << "error: unable to create a CompilerInvocation\n";
return 1;
}

Invocation->setMainExecutablePath(MainExecutablePath);
Invocation.setMainExecutablePath(MainExecutablePath);

CompilerInstance CI;
CI.addDiagnosticConsumer(&PDC);
if (CI.setup(*Invocation))
if (CI.setup(Invocation))
return 1;
CI.performSema();

PrintOptions Options = PrintOptions::printEverything();

ModuleDecl *M = CI.getMainModule();
M->getMainSourceFile(Invocation->getSourceFileKind()).print(llvm::outs(),
Options);
M->getMainSourceFile(Invocation.getSourceFileKind()).print(llvm::outs(),
Options);

auto SMAModel = createSMAModel(M);
llvm::yaml::Output YOut(llvm::outs());
Expand Down
Loading