Skip to content

Frontend: set up output file .swiftsourceinfo #27277

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 6 commits into from
Sep 25, 2019
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
10 changes: 9 additions & 1 deletion cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,13 @@ function(_compile_swift_files
set(module_base "${module_dir}/${SWIFTFILE_MODULE_NAME}")
if(SWIFTFILE_SDK IN_LIST SWIFT_APPLE_PLATFORMS)
set(specific_module_dir "${module_base}.swiftmodule")
set(specific_module_private_dir "${specific_module_dir}/Private")
set(source_info_file "${specific_module_private_dir}/${SWIFTFILE_ARCHITECTURE}.swiftsourceinfo")
set(module_base "${module_base}.swiftmodule/${SWIFTFILE_ARCHITECTURE}")
else()
set(specific_module_dir)
set(specific_module_private_dir)
set(source_info_file "${module_base}.swiftsourceinfo")
endif()
set(module_file "${module_base}.swiftmodule")
set(module_doc_file "${module_base}.swiftdoc")
Expand All @@ -322,6 +326,8 @@ function(_compile_swift_files
set(sib_file "${module_base}.Onone.sib")
set(sibopt_file "${module_base}.O.sib")
set(sibgen_file "${module_base}.sibgen")
list(APPEND swift_module_flags
"-emit-module-source-info-path" "${source_info_file}")

if(SWIFT_ENABLE_MODULE_INTERFACES)
set(interface_file "${module_base}.swiftinterface")
Expand Down Expand Up @@ -349,7 +355,8 @@ function(_compile_swift_files
swift_install_in_component(DIRECTORY "${specific_module_dir}"
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
COMPONENT "${SWIFTFILE_INSTALL_IN_COMPONENT}"
OPTIONAL)
OPTIONAL
PATTERN "Private" EXCLUDE)
else()
swift_install_in_component(FILES ${module_outputs}
DESTINATION "lib${LLVM_LIBDIR_SUFFIX}/swift/${library_subdir}"
Expand Down Expand Up @@ -490,6 +497,7 @@ function(_compile_swift_files
COMMAND
"${CMAKE_COMMAND}" "-E" "make_directory" ${module_dir}
${specific_module_dir}
${specific_module_private_dir}
COMMAND
"${PYTHON_EXECUTABLE}" "${line_directive_tool}" "@${file_path}" --
"${swift_compiler_tool}" "-emit-module" "-o" "${module_file}"
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ ERROR(error_mode_cannot_emit_module,none,
"this mode does not support emitting modules", ())
ERROR(error_mode_cannot_emit_module_doc,none,
"this mode does not support emitting module documentation files", ())
ERROR(error_mode_cannot_emit_module_source_info,none,
"this mode does not support emitting module source info files", ())
ERROR(error_mode_cannot_emit_interface,none,
"this mode does not support emitting module interface files", ())

Expand Down
1 change: 1 addition & 0 deletions include/swift/Basic/FileTypes.def
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TYPE("autolink", AutolinkFile, "autolink", "")
TYPE("swiftmodule", SwiftModuleFile, "swiftmodule", "")
TYPE("swiftdoc", SwiftModuleDocFile, "swiftdoc", "")
TYPE("swiftinterface", SwiftModuleInterfaceFile, "swiftinterface", "")
TYPE("swiftsourceinfo", SwiftSourceInfoFile, "swiftsourceinfo", "")
TYPE("assembly", Assembly, "s", "")
TYPE("raw-sil", RawSIL, "sil", "")
TYPE("raw-sib", RawSIB, "sib", "")
Expand Down
12 changes: 11 additions & 1 deletion include/swift/Basic/SupplementaryOutputPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ struct SupplementaryOutputPaths {
/// \sa swift::serialize
std::string ModuleOutputPath;

/// The path to which we should emit a module source information file.
/// It is valid whenever there are any inputs.
///
/// This binary format stores source locations and other information about the
/// declarations in a module.
///
/// \sa swift::serialize
std::string ModuleSourceInfoOutputPath;

/// The path to which we should emit a module documentation file.
/// It is valid whenever there are any inputs.
///
Expand Down Expand Up @@ -132,7 +141,8 @@ struct SupplementaryOutputPaths {
ModuleDocOutputPath.empty() && DependenciesFilePath.empty() &&
ReferenceDependenciesFilePath.empty() &&
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
TBDPath.empty() && ModuleInterfaceOutputPath.empty();
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&
ModuleSourceInfoOutputPath.empty();
}
};
} // namespace swift
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ class Driver {
StringRef workingDirectory,
CommandOutput *Output) const;

void chooseSwiftSourceInfoOutputPath(Compilation &C,
const TypeToPathMap *OutputMap,
StringRef workingDirectory,
CommandOutput *Output) const;

void chooseModuleInterfacePath(Compilation &C, const JobAction *JA,
StringRef workingDirectory,
llvm::SmallString<128> &buffer,
Expand Down
1 change: 1 addition & 0 deletions include/swift/Frontend/FrontendInputsAndOutputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ class FrontendInputsAndOutputs {
bool hasLoadedModuleTracePath() const;
bool hasModuleOutputPath() const;
bool hasModuleDocOutputPath() const;
bool hasModuleSourceInfoOutputPath() const;
bool hasModuleInterfaceOutputPath() const;
bool hasTBDPath() const;

Expand Down
3 changes: 3 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def emit_module_doc_path
: Separate<["-"], "emit-module-doc-path">, MetaVarName<"<path>">,
HelpText<"Output module documentation file <path>">;

def emit_module_source_info : Flag<["-"], "emit-module-source-info">,
HelpText<"Output module source info file">;

def merge_modules
: Flag<["-"], "merge-modules">, ModeOpt,
HelpText<"Merge the input modules without otherwise processing them">;
Expand Down
6 changes: 6 additions & 0 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ def emit_module_interface_path :
ArgumentIsPath]>,
MetaVarName<"<path>">, HelpText<"Output module interface file to <path>">;

def emit_module_source_info_path :
Separate<["-"], "emit-module-source-info-path">,
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild,
ArgumentIsPath]>,
MetaVarName<"<path>">, HelpText<"Output module source info file to <path>">;

def emit_parseable_module_interface :
Flag<["-"], "emit-parseable-module-interface">,
Alias<emit_module_interface>,
Expand Down
1 change: 1 addition & 0 deletions include/swift/Serialization/SerializationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace swift {

const char *OutputPath = nullptr;
const char *DocOutputPath = nullptr;
const char *SourceInfoOutputPath = nullptr;

StringRef GroupInfoPath;
StringRef ImportedHeader;
Expand Down
1 change: 1 addition & 0 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ namespace swift {
const SerializationOptions &opts,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
const SILModule *M = nullptr);

/// Get the CPU, subtarget feature options, and triple to use when emitting code.
Expand Down
3 changes: 3 additions & 0 deletions lib/Basic/FileTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ bool file_types::isTextual(ID Id) {
case file_types::TY_RawSIB:
case file_types::TY_SwiftModuleFile:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_LLVM_BC:
case file_types::TY_SerializedDiagnostics:
case file_types::TY_ClangModuleFile:
Expand Down Expand Up @@ -128,6 +129,7 @@ bool file_types::isAfterLLVM(ID Id) {
case file_types::TY_RawSIB:
case file_types::TY_SwiftModuleFile:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_SerializedDiagnostics:
case file_types::TY_ClangModuleFile:
case file_types::TY_SwiftDeps:
Expand Down Expand Up @@ -169,6 +171,7 @@ bool file_types::isPartOfSwiftCompilation(ID Id) {
case file_types::TY_SwiftModuleFile:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SwiftModuleInterfaceFile:
case file_types::TY_SwiftSourceInfoFile:
case file_types::TY_SerializedDiagnostics:
case file_types::TY_ClangModuleFile:
case file_types::TY_SwiftDeps:
Expand Down
78 changes: 56 additions & 22 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
}
case file_types::TY_SwiftModuleFile:
case file_types::TY_SwiftModuleDocFile:
case file_types::TY_SwiftSourceInfoFile:
if (OI.ShouldGenerateModule && !OI.shouldLink()) {
// When generating a .swiftmodule as a top-level output (as opposed
// to, for example, linking an image), treat .swiftmodule files as
Expand Down Expand Up @@ -2541,9 +2542,10 @@ Job *Driver::buildJobsForAction(Compilation &C, const JobAction *JA,
chooseSwiftModuleOutputPath(C, OutputMap, workingDirectory, Output.get());

if (OI.ShouldGenerateModule &&
(isa<CompileJobAction>(JA) || isa<MergeModuleJobAction>(JA)))
chooseSwiftModuleDocOutputPath(C, OutputMap, workingDirectory,
Output.get());
(isa<CompileJobAction>(JA) || isa<MergeModuleJobAction>(JA))) {
chooseSwiftModuleDocOutputPath(C, OutputMap, workingDirectory, Output.get());
chooseSwiftSourceInfoOutputPath(C, OutputMap, workingDirectory, Output.get());
}

if (C.getArgs().hasArg(options::OPT_emit_module_interface,
options::OPT_emit_module_interface_path))
Expand Down Expand Up @@ -2786,37 +2788,69 @@ void Driver::chooseSwiftModuleOutputPath(Compilation &C,
}
}

void Driver::chooseSwiftModuleDocOutputPath(Compilation &C,
const TypeToPathMap *OutputMap,
StringRef workingDirectory,
CommandOutput *Output) const {

if (hasExistingAdditionalOutput(*Output, file_types::TY_SwiftModuleDocFile))
static void chooseModuleAuxiliaryOutputFilePath(Compilation &C,
const TypeToPathMap *OutputMap,
StringRef workingDirectory,
CommandOutput *Output,
file_types::ID fileID,
bool isPrivate,
Optional<options::ID> optId = llvm::None) {
if (hasExistingAdditionalOutput(*Output, fileID))
return;
// Honor driver option for this path if it's given
if (optId.hasValue()) {
if (const Arg *A = C.getArgs().getLastArg(*optId)) {
Output->setAdditionalOutputForType(fileID, StringRef(A->getValue()));
return;
}
}

StringRef OFMModuleDocOutputPath;
StringRef OFMOutputPath;
if (OutputMap) {
auto iter = OutputMap->find(file_types::TY_SwiftModuleDocFile);
auto iter = OutputMap->find(fileID);
if (iter != OutputMap->end())
OFMModuleDocOutputPath = iter->second;
OFMOutputPath = iter->second;
}
if (!OFMModuleDocOutputPath.empty()) {
if (!OFMOutputPath.empty()) {
// Prefer a path from the OutputMap.
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleDocFile,
OFMModuleDocOutputPath);
Output->setAdditionalOutputForType(fileID, OFMOutputPath);
} else if (Output->getPrimaryOutputType() != file_types::TY_Nothing) {
// Otherwise, put it next to the swiftmodule file.
llvm::SmallString<128> Path(
Output->getAnyOutputForType(file_types::TY_SwiftModuleFile));
bool isTempFile = C.isTemporaryFile(Path);
llvm::sys::path::replace_extension(
Path, file_types::getExtension(file_types::TY_SwiftModuleDocFile));
Output->setAdditionalOutputForType(file_types::TY_SwiftModuleDocFile, Path);
auto ModulePath = Output->getAnyOutputForType(file_types::TY_SwiftModuleFile);
bool isTempFile = C.isTemporaryFile(ModulePath);
auto ModuleName = llvm::sys::path::filename(ModulePath);
llvm::SmallString<128> Path(llvm::sys::path::parent_path(ModulePath));
if (isPrivate) {
llvm::sys::path::append(Path, "Private");
// If the build system has created a Private dir for us to include the file, use it.
if (!llvm::sys::fs::exists(Path)) {
llvm::sys::path::remove_filename(Path);
}
}
llvm::sys::path::append(Path, ModuleName);
llvm::sys::path::replace_extension(Path, file_types::getExtension(fileID));
Output->setAdditionalOutputForType(fileID, Path);
if (isTempFile)
C.addTemporaryFile(Path);
}
}

void Driver::chooseSwiftSourceInfoOutputPath(Compilation &C,
const TypeToPathMap *OutputMap,
StringRef workingDirectory,
CommandOutput *Output) const {
chooseModuleAuxiliaryOutputFilePath(C, OutputMap, workingDirectory, Output,
file_types::TY_SwiftSourceInfoFile,
/*isPrivate*/true, options::OPT_emit_module_source_info_path);
}

void Driver::chooseSwiftModuleDocOutputPath(Compilation &C,
const TypeToPathMap *OutputMap,
StringRef workingDirectory,
CommandOutput *Output) const {
chooseModuleAuxiliaryOutputFilePath(C, OutputMap, workingDirectory, Output,
file_types::TY_SwiftModuleDocFile, /*isPrivate*/false);
}

void Driver::chooseRemappingOutputPath(Compilation &C,
const TypeToPathMap *OutputMap,
CommandOutput *Output) const {
Expand Down
8 changes: 8 additions & 0 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ const char *ToolChain::JobContext::computeFrontendModeForCompile() const {
case file_types::TY_TBD:
case file_types::TY_OptRecord:
case file_types::TY_SwiftModuleInterfaceFile:
case file_types::TY_SwiftSourceInfoFile:
llvm_unreachable("Output type can never be primary output.");
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID");
Expand Down Expand Up @@ -639,6 +640,9 @@ void ToolChain::JobContext::addFrontendSupplementaryOutputArguments(
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftModuleDocFile,
"-emit-module-doc-path");

addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftSourceInfoFile,
"-emit-module-source-info-path");

addOutputsOfType(arguments, Output, Args,
file_types::ID::TY_SwiftModuleInterfaceFile,
"-emit-module-interface-path");
Expand Down Expand Up @@ -770,6 +774,7 @@ ToolChain::constructInvocation(const BackendJobAction &job,
case file_types::TY_ModuleTrace:
case file_types::TY_OptRecord:
case file_types::TY_SwiftModuleInterfaceFile:
case file_types::TY_SwiftSourceInfoFile:
llvm_unreachable("Output type can never be primary output.");
case file_types::TY_INVALID:
llvm_unreachable("Invalid type ID");
Expand Down Expand Up @@ -907,6 +912,9 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,

addOutputsOfType(Arguments, context.Output, context.Args,
file_types::TY_SwiftModuleDocFile, "-emit-module-doc-path");
addOutputsOfType(Arguments, context.Output, context.Args,
file_types::TY_SwiftSourceInfoFile,
"-emit-module-source-info-path");
addOutputsOfType(Arguments, context.Output, context.Args,
file_types::ID::TY_SwiftModuleInterfaceFile,
"-emit-module-interface-path");
Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_doc);
return true;
}
// If we cannot emit module doc, we cannot emit source information file either.
if (!FrontendOptions::canActionEmitModuleDoc(Opts.RequestedAction) &&
Opts.InputsAndOutputs.hasModuleSourceInfoOutputPath()) {
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_source_info);
return true;
}
if (!FrontendOptions::canActionEmitInterface(Opts.RequestedAction) &&
Opts.InputsAndOutputs.hasModuleInterfaceOutputPath()) {
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_interface);
Expand Down
14 changes: 12 additions & 2 deletions lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,13 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
auto TBD = getSupplementaryFilenamesFromArguments(options::OPT_emit_tbd_path);
auto moduleInterfaceOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_module_interface_path);
auto moduleSourceInfoOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_module_source_info_path);

if (!objCHeaderOutput || !moduleOutput || !moduleDocOutput ||
!dependenciesFile || !referenceDependenciesFile ||
!serializedDiagnostics || !fixItsOutput || !loadedModuleTrace || !TBD ||
!moduleInterfaceOutput) {
!moduleInterfaceOutput || !moduleSourceInfoOutput) {
return None;
}
std::vector<SupplementaryOutputPaths> result;
Expand All @@ -319,7 +321,7 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
sop.LoadedModuleTracePath = (*loadedModuleTrace)[i];
sop.TBDPath = (*TBD)[i];
sop.ModuleInterfaceOutputPath = (*moduleInterfaceOutput)[i];

sop.ModuleSourceInfoOutputPath = (*moduleSourceInfoOutput)[i];
result.push_back(sop);
}
return result;
Expand Down Expand Up @@ -394,6 +396,11 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
file_types::TY_SwiftModuleDocFile, "",
defaultSupplementaryOutputPathExcludingExtension);

auto moduleSourceInfoOutputPath = determineSupplementaryOutputFilename(
OPT_emit_module_source_info, pathsFromArguments.ModuleSourceInfoOutputPath,
file_types::TY_SwiftSourceInfoFile, "",
defaultSupplementaryOutputPathExcludingExtension);

// There is no non-path form of -emit-interface-path
auto ModuleInterfaceOutputPath =
pathsFromArguments.ModuleInterfaceOutputPath;
Expand All @@ -420,6 +427,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
sop.LoadedModuleTracePath = loadedModuleTracePath;
sop.TBDPath = tbdPath;
sop.ModuleInterfaceOutputPath = ModuleInterfaceOutputPath;
sop.ModuleSourceInfoOutputPath = moduleSourceInfoOutputPath;
return sop;
}

Expand Down Expand Up @@ -489,6 +497,7 @@ createFromTypeToPathMap(const TypeToPathMap *map) {
{file_types::TY_ObjCHeader, paths.ObjCHeaderOutputPath},
{file_types::TY_SwiftModuleFile, paths.ModuleOutputPath},
{file_types::TY_SwiftModuleDocFile, paths.ModuleDocOutputPath},
{file_types::TY_SwiftSourceInfoFile, paths.ModuleSourceInfoOutputPath},
{file_types::TY_Dependencies, paths.DependenciesFilePath},
{file_types::TY_SwiftDeps, paths.ReferenceDependenciesFilePath},
{file_types::TY_SerializedDiagnostics, paths.SerializedDiagnosticsPath},
Expand Down Expand Up @@ -516,6 +525,7 @@ SupplementaryOutputPathsComputer::readSupplementaryOutputFileMap() const {
options::OPT_serialize_diagnostics_path,
options::OPT_emit_loaded_module_trace_path,
options::OPT_emit_module_interface_path,
options::OPT_emit_module_source_info_path,
options::OPT_emit_tbd_path)) {
Diags.diagnose(SourceLoc(),
diag::error_cannot_have_supplementary_outputs,
Expand Down
1 change: 1 addition & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
SerializationOptions serializationOpts;
serializationOpts.OutputPath = outs.ModuleOutputPath.c_str();
serializationOpts.DocOutputPath = outs.ModuleDocOutputPath.c_str();
serializationOpts.SourceInfoOutputPath = outs.ModuleSourceInfoOutputPath.c_str();
serializationOpts.GroupInfoPath = opts.GroupInfoPath.c_str();
if (opts.SerializeBridgingHeader && !outs.ModuleOutputPath.empty())
serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath;
Expand Down
Loading