mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This completes the FileUnit refactoring. A module consists of multiple FileUnits, which provide decls from various file-like sources. I say "file-like" because the Builtin module is implemented with a single BuiltinUnit, and imported Clang modules are just a single FileUnit source within a module. Most modules, therefore, contain a single file unit; only the main module will contain multiple source files (and eventually partial AST files). The term "translation unit" has been scrubbed from the project. To refer to the context of declarations outside of any other declarations, use "top-level" or "module scope". To refer to a .swift file or its DeclContext, use "source file". To refer to a single unit of compilation, use "module", since the model is that an entire module will be compiled with a single driver call. (It will still be possible to compile a single source file through the direct-to-frontend interface, but only in the context of the whole module.) Swift SVN r10837
151 lines
4.2 KiB
C++
151 lines
4.2 KiB
C++
//===-- CompilerInvocation.cpp - CompilerInvocation methods ---------------===//
|
|
//
|
|
// 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/Frontend/Frontend.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
#include "llvm/Option/Arg.h"
|
|
#include "llvm/Option/ArgList.h"
|
|
#include "llvm/Option/Option.h"
|
|
#include "llvm/Support/Path.h"
|
|
|
|
using namespace swift;
|
|
|
|
swift::CompilerInvocation::CompilerInvocation() {
|
|
TargetTriple = llvm::sys::getDefaultTargetTriple();
|
|
}
|
|
|
|
void CompilerInvocation::setMainExecutablePath(StringRef Path) {
|
|
llvm::SmallString<128> LibPath(Path);
|
|
llvm::sys::path::remove_filename(LibPath); // Remove /swift
|
|
llvm::sys::path::remove_filename(LibPath); // Remove /bin
|
|
llvm::sys::path::append(LibPath, "lib", "swift");
|
|
setRuntimeIncludePath(LibPath.str());
|
|
}
|
|
|
|
namespace {
|
|
|
|
// Create enum with OPT_xxx values for each option in FrontendOptions.td.
|
|
enum Opt {
|
|
OPT_INVALID = 0,
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
HELP, META) \
|
|
OPT_##ID,
|
|
#include "FrontendOptions.inc"
|
|
LastOption
|
|
#undef OPTION
|
|
};
|
|
|
|
// Create prefix string literals used in FrontendOptions.td.
|
|
#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
|
|
#include "FrontendOptions.inc"
|
|
#undef PREFIX
|
|
|
|
// Create table mapping all options defined in FrontendOptions.td.
|
|
static const llvm::opt::OptTable::Info InfoTable[] = {
|
|
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
|
|
HELPTEXT, METAVAR) \
|
|
{ PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, llvm::opt::Option::KIND##Class, \
|
|
PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS },
|
|
#include "FrontendOptions.inc"
|
|
#undef OPTION
|
|
};
|
|
|
|
// Create OptTable class for parsing actual command line arguments
|
|
class FrontendOptTable : public llvm::opt::OptTable {
|
|
public:
|
|
FrontendOptTable() : OptTable(InfoTable, llvm::array_lengthof(InfoTable)){}
|
|
};
|
|
|
|
} // namespace anonymous
|
|
|
|
bool CompilerInvocation::parseArgs(ArrayRef<const char *> Args,
|
|
DiagnosticEngine &Diags) {
|
|
if (Args.empty())
|
|
return false;
|
|
|
|
// Parse command line options using FrontendOptions.td
|
|
std::unique_ptr<llvm::opt::InputArgList> ParsedArgs;
|
|
FrontendOptTable Table;
|
|
unsigned MissingIndex;
|
|
unsigned MissingCount;
|
|
ParsedArgs.reset(
|
|
Table.ParseArgs(Args.begin(), Args.end(), MissingIndex, MissingCount));
|
|
if (MissingCount) {
|
|
Diags.diagnose(SourceLoc(), diag::error_missing_arg_value,
|
|
ParsedArgs->getArgString(MissingIndex), MissingCount);
|
|
return true;
|
|
}
|
|
|
|
for (auto InputArg : *ParsedArgs) {
|
|
switch (InputArg->getOption().getID()) {
|
|
case OPT_triple:
|
|
setTargetTriple(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_import_search_path:
|
|
ImportSearchPaths.push_back(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_framework_search_path:
|
|
FrameworkSearchPaths.push_back(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_sdk:
|
|
setSDKPath(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_module_cache_path:
|
|
setClangModuleCachePath(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_parse_as_library:
|
|
setInputKind(SourceFileKind::Library);
|
|
break;
|
|
|
|
case OPT_parse_stdlib:
|
|
setParseStdlib();
|
|
break;
|
|
|
|
case OPT_Xclang:
|
|
ExtraClangArgs.push_back(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_debug_constraints:
|
|
LangOpts.DebugConstraintSolver = true;
|
|
break;
|
|
|
|
case OPT_link_library:
|
|
addLinkLibrary(InputArg->getValue(), LibraryKind::Library);
|
|
break;
|
|
|
|
case OPT_serialized_diagnostics_path:
|
|
setSerializedDiagnosticsPath(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_module_source_list_path:
|
|
setModuleSourceListPath(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_output:
|
|
setOutputFilename(InputArg->getValue());
|
|
break;
|
|
|
|
case OPT_INPUT:
|
|
addInputFilename(InputArg->getValue());
|
|
break;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|