/// Emits a Make-style dependencies file. static bool emitMakeDependencies(DiagnosticEngine &diags, DependencyTracker &depTracker, const FrontendOptions &opts) { std::error_code EC; llvm::raw_fd_ostream out(opts.DependenciesFilePath, EC, llvm::sys::fs::F_None); if (out.has_error() || EC) { diags.diagnose(SourceLoc(), diag::error_opening_output, opts.DependenciesFilePath, EC.message()); out.clear_error(); return true; } // Declare a helper for escaping file names for use in Makefiles. llvm::SmallString<256> pathBuf; auto escape = [&](StringRef raw) -> StringRef { pathBuf.clear(); static const char badChars[] = " $#:\n"; size_t prev = 0; for (auto index = raw.find_first_of(badChars); index != StringRef::npos; index = raw.find_first_of(badChars, index+1)) { pathBuf.append(raw.slice(prev, index)); if (raw[index] == '$') pathBuf.push_back('$'); else pathBuf.push_back('\\'); prev = index; } pathBuf.append(raw.substr(prev)); return pathBuf; }; // FIXME: Xcode can't currently handle multiple targets in a single // dependency line. opts.forAllOutputPaths([&](StringRef targetName) { out << escape(targetName) << " :"; // First include all other files in the module. Make-style dependencies // need to be conservative! for (StringRef path : opts.InputFilenames) out << ' ' << escape(path); // Then print dependencies we've picked up during compilation. for (StringRef path : depTracker.getDependencies()) out << ' ' << escape(path); out << '\n'; }); return false; }
static bool ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, DiagnosticEngine &Diags) { using namespace options; if (const Arg *A = Args.getLastArg(OPT_debug_crash_Group)) { Option Opt = A->getOption(); if (Opt.matches(OPT_debug_assert_immediately)) { debugFailWithAssertion(); } else if (Opt.matches(OPT_debug_crash_immediately)) { debugFailWithCrash(); } else if (Opt.matches(OPT_debug_assert_after_parse)) { // Set in FrontendOptions Opts.CrashMode = FrontendOptions::DebugCrashMode::AssertAfterParse; } else if (Opt.matches(OPT_debug_crash_after_parse)) { // Set in FrontendOptions Opts.CrashMode = FrontendOptions::DebugCrashMode::CrashAfterParse; } else { llvm_unreachable("Unknown debug_crash_Group option!"); } } if (const Arg *A = Args.getLastArg(OPT_dump_api_path)) { Opts.DumpAPIPath = A->getValue(); } Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil); Opts.EmitSortedSIL |= Args.hasArg(OPT_emit_sorted_sil); Opts.DelayedFunctionBodyParsing |= Args.hasArg(OPT_delayed_function_body_parsing); Opts.EnableTesting |= Args.hasArg(OPT_enable_testing); Opts.EnableResilience |= Args.hasArg(OPT_enable_resilience); Opts.PrintStats |= Args.hasArg(OPT_print_stats); Opts.PrintClangStats |= Args.hasArg(OPT_print_clang_stats); Opts.DebugTimeFunctionBodies |= Args.hasArg(OPT_debug_time_function_bodies); Opts.DebugTimeCompilation |= Args.hasArg(OPT_debug_time_compilation); Opts.PlaygroundTransform |= Args.hasArg(OPT_playground); if (Args.hasArg(OPT_disable_playground_transform)) Opts.PlaygroundTransform = false; Opts.PlaygroundHighPerformance |= Args.hasArg(OPT_playground_high_performance); if (const Arg *A = Args.getLastArg(OPT_help, OPT_help_hidden)) { if (A->getOption().matches(OPT_help)) { Opts.PrintHelp = true; } else if (A->getOption().matches(OPT_help_hidden)) { Opts.PrintHelpHidden = true; } else { llvm_unreachable("Unknown help option parsed"); } } if (const Arg *A = Args.getLastArg(OPT_filelist)) { const Arg *primaryFileArg = Args.getLastArg(OPT_primary_file); auto primaryFileIndex = readFileList(Opts.InputFilenames, A, primaryFileArg); if (primaryFileArg) Opts.PrimaryInput = SelectedInput(primaryFileIndex); assert(!Args.hasArg(OPT_INPUT) && "mixing -filelist with inputs"); } else { for (const Arg *A : make_range(Args.filtered_begin(OPT_INPUT, OPT_primary_file), Args.filtered_end())) { if (A->getOption().matches(OPT_INPUT)) { Opts.InputFilenames.push_back(A->getValue()); } else if (A->getOption().matches(OPT_primary_file)) { Opts.PrimaryInput = SelectedInput(Opts.InputFilenames.size()); Opts.InputFilenames.push_back(A->getValue()); } else { llvm_unreachable("Unknown input-related argument!"); } } } Opts.ParseStdlib |= Args.hasArg(OPT_parse_stdlib); // Determine what the user has asked the frontend to do. FrontendOptions::ActionType &Action = Opts.RequestedAction; if (const Arg *A = Args.getLastArg(OPT_modes_Group)) { Option Opt = A->getOption(); if (Opt.matches(OPT_emit_object)) { Action = FrontendOptions::EmitObject; } else if (Opt.matches(OPT_emit_assembly)) { Action = FrontendOptions::EmitAssembly; } else if (Opt.matches(OPT_emit_ir)) { Action = FrontendOptions::EmitIR; } else if (Opt.matches(OPT_emit_bc)) { Action = FrontendOptions::EmitBC; } else if (Opt.matches(OPT_emit_sil)) { Action = FrontendOptions::EmitSIL; } else if (Opt.matches(OPT_emit_silgen)) { Action = FrontendOptions::EmitSILGen; } else if (Opt.matches(OPT_emit_sib)) { Action = FrontendOptions::EmitSIB; } else if (Opt.matches(OPT_emit_sibgen)) { Action = FrontendOptions::EmitSIBGen; } else if (Opt.matches(OPT_parse)) { Action = FrontendOptions::Parse; } else if (Opt.matches(OPT_dump_parse)) { Action = FrontendOptions::DumpParse; } else if (Opt.matches(OPT_dump_ast)) { Action = FrontendOptions::DumpAST; } else if (Opt.matches(OPT_dump_type_refinement_contexts)) { Action = FrontendOptions::DumpTypeRefinementContexts; } else if (Opt.matches(OPT_dump_interface_hash)) { Action = FrontendOptions::DumpInterfaceHash; } else if (Opt.matches(OPT_print_ast)) { Action = FrontendOptions::PrintAST; } else if (Opt.matches(OPT_repl) || Opt.matches(OPT_deprecated_integrated_repl)) { Action = FrontendOptions::REPL; } else if (Opt.matches(OPT_interpret)) { Action = FrontendOptions::Immediate; } else { llvm_unreachable("Unhandled mode option"); } } else { // We don't have a mode, so determine a default. if (Args.hasArg(OPT_emit_module, OPT_emit_module_path)) { // We've been told to emit a module, but have no other mode indicators. // As a result, put the frontend into EmitModuleOnly mode. // (Setting up module output will be handled below.) Action = FrontendOptions::EmitModuleOnly; } } if (Opts.RequestedAction == FrontendOptions::Immediate && Opts.PrimaryInput.hasValue()) { Diags.diagnose(SourceLoc(), diag::error_immediate_mode_primary_file); return true; } bool TreatAsSIL = Args.hasArg(OPT_parse_sil); if (!TreatAsSIL && Opts.InputFilenames.size() == 1) { // If we have exactly one input filename, and its extension is "sil", // treat the input as SIL. StringRef Input(Opts.InputFilenames[0]); TreatAsSIL = llvm::sys::path::extension(Input).endswith(SIL_EXTENSION); } else if (Opts.PrimaryInput.hasValue() && Opts.PrimaryInput->isFilename()) { // If we have a primary input and it's a filename with extension "sil", // treat the input as SIL. StringRef Input(Opts.InputFilenames[Opts.PrimaryInput->Index]); TreatAsSIL = llvm::sys::path::extension(Input).endswith(SIL_EXTENSION); } // If we have exactly one input filename, and its extension is "bc" or "ll", // treat the input as LLVM_IR. bool TreatAsLLVM = false; if (Opts.InputFilenames.size() == 1) { StringRef Input(Opts.InputFilenames[0]); TreatAsLLVM = llvm::sys::path::extension(Input).endswith(LLVM_BC_EXTENSION) || llvm::sys::path::extension(Input).endswith(LLVM_IR_EXTENSION); } if (Opts.RequestedAction == FrontendOptions::REPL) { if (!Opts.InputFilenames.empty()) { Diags.diagnose(SourceLoc(), diag::error_repl_requires_no_input_files); return true; } } else if (TreatAsSIL && Opts.PrimaryInput.hasValue()) { // If we have the SIL as our primary input, we can waive the one file // requirement as long as all the other inputs are SIBs. if (Opts.PrimaryInput.hasValue()) { for (unsigned i = 0, e = Opts.InputFilenames.size(); i != e; ++i) { if (i == Opts.PrimaryInput->Index) continue; StringRef File(Opts.InputFilenames[i]); if (!llvm::sys::path::extension(File).endswith(SIB_EXTENSION)) { Diags.diagnose(SourceLoc(), diag::error_mode_requires_one_sil_multi_sib); return true; } } } } else if (TreatAsSIL) { if (Opts.InputFilenames.size() != 1) { Diags.diagnose(SourceLoc(), diag::error_mode_requires_one_input_file); return true; } } else if (Opts.RequestedAction != FrontendOptions::NoneAction) { if (Opts.InputFilenames.empty()) { Diags.diagnose(SourceLoc(), diag::error_mode_requires_an_input_file); return true; } } if (Opts.RequestedAction == FrontendOptions::Immediate) { assert(!Opts.InputFilenames.empty()); Opts.ImmediateArgv.push_back(Opts.InputFilenames[0]); // argv[0] if (const Arg *A = Args.getLastArg(OPT__DASH_DASH)) { for (unsigned i = 0, e = A->getNumValues(); i != e; ++i) { Opts.ImmediateArgv.push_back(A->getValue(i)); } } } if (TreatAsSIL) Opts.InputKind = InputFileKind::IFK_SIL; else if (TreatAsLLVM) Opts.InputKind = InputFileKind::IFK_LLVM_IR; else if (Args.hasArg(OPT_parse_as_library)) Opts.InputKind = InputFileKind::IFK_Swift_Library; else if (Action == FrontendOptions::REPL) Opts.InputKind = InputFileKind::IFK_Swift_REPL; else Opts.InputKind = InputFileKind::IFK_Swift; if (const Arg *A = Args.getLastArg(OPT_output_filelist)) { readFileList(Opts.OutputFilenames, A); assert(!Args.hasArg(OPT_o) && "don't use -o with -output-filelist"); } else { Opts.OutputFilenames = Args.getAllArgValues(OPT_o); } bool UserSpecifiedModuleName = false; { const Arg *A = Args.getLastArg(OPT_module_name); StringRef ModuleName = Opts.ModuleName; if (A) { ModuleName = A->getValue(); UserSpecifiedModuleName = true; } else if (ModuleName.empty()) { // The user did not specify a module name, so determine a default fallback // based on other options. // Note: this code path will only be taken when running the frontend // directly; the driver should always pass -module-name when invoking the // frontend. if (Opts.RequestedAction == FrontendOptions::REPL) { // Default to a module named "REPL" if we're in REPL mode. ModuleName = "REPL"; } else if (!Opts.InputFilenames.empty()) { StringRef OutputFilename = Opts.getSingleOutputFilename(); if (OutputFilename.empty() || OutputFilename == "-" || llvm::sys::fs::is_directory(OutputFilename)) { ModuleName = Opts.InputFilenames[0]; } else { ModuleName = OutputFilename; } ModuleName = llvm::sys::path::stem(ModuleName); } } if (!Lexer::isIdentifier(ModuleName) || (ModuleName == STDLIB_NAME && !Opts.ParseStdlib)) { if (!Opts.actionHasOutput() || (Opts.InputKind == InputFileKind::IFK_Swift && Opts.InputFilenames.size() == 1)) { ModuleName = "main"; } else { auto DID = (ModuleName == STDLIB_NAME) ? diag::error_stdlib_module_name : diag::error_bad_module_name; Diags.diagnose(SourceLoc(), DID, ModuleName, A == nullptr); ModuleName = "__bad__"; } } Opts.ModuleName = ModuleName; } if (Opts.OutputFilenames.empty() || llvm::sys::fs::is_directory(Opts.getSingleOutputFilename())) { // No output filename was specified, or an output directory was specified. // Determine the correct output filename. // Note: this should typically only be used when invoking the frontend // directly, as the driver will always pass -o with an appropriate filename // if output is required for the requested action. StringRef Suffix; switch (Opts.RequestedAction) { case FrontendOptions::NoneAction: break; case FrontendOptions::Parse: case FrontendOptions::DumpParse: case FrontendOptions::DumpInterfaceHash: case FrontendOptions::DumpAST: case FrontendOptions::PrintAST: case FrontendOptions::DumpTypeRefinementContexts: // Textual modes. Opts.setSingleOutputFilename("-"); break; case FrontendOptions::EmitSILGen: case FrontendOptions::EmitSIL: { if (Opts.OutputFilenames.empty()) Opts.setSingleOutputFilename("-"); else Suffix = SIL_EXTENSION; break; } case FrontendOptions::EmitSIBGen: case FrontendOptions::EmitSIB: Suffix = SIB_EXTENSION; break; case FrontendOptions::EmitModuleOnly: Suffix = SERIALIZED_MODULE_EXTENSION; break; case FrontendOptions::Immediate: case FrontendOptions::REPL: // These modes have no frontend-generated output. Opts.OutputFilenames.clear(); break; case FrontendOptions::EmitAssembly: { if (Opts.OutputFilenames.empty()) Opts.setSingleOutputFilename("-"); else Suffix = "s"; break; } case FrontendOptions::EmitIR: { if (Opts.OutputFilenames.empty()) Opts.setSingleOutputFilename("-"); else Suffix = "ll"; break; } case FrontendOptions::EmitBC: { Suffix = "bc"; break; } case FrontendOptions::EmitObject: Suffix = "o"; break; } if (!Suffix.empty()) { // We need to deduce a file name. // First, if we're reading from stdin and we don't have a directory, // output to stdout. if (Opts.InputFilenames.size() == 1 && Opts.InputFilenames[0] == "-" && Opts.OutputFilenames.empty()) Opts.setSingleOutputFilename("-"); else { // We have a suffix, so determine an appropriate name. llvm::SmallString<128> Path(Opts.getSingleOutputFilename()); StringRef BaseName; if (Opts.PrimaryInput.hasValue() && Opts.PrimaryInput->isFilename()) { unsigned Index = Opts.PrimaryInput->Index; BaseName = llvm::sys::path::stem(Opts.InputFilenames[Index]); } else if (!UserSpecifiedModuleName && Opts.InputFilenames.size() == 1) { BaseName = llvm::sys::path::stem(Opts.InputFilenames[0]); } else { BaseName = Opts.ModuleName; } llvm::sys::path::append(Path, BaseName); llvm::sys::path::replace_extension(Path, Suffix); Opts.setSingleOutputFilename(Path.str()); } } if (Opts.OutputFilenames.empty()) { if (Opts.RequestedAction != FrontendOptions::REPL && Opts.RequestedAction != FrontendOptions::Immediate && Opts.RequestedAction != FrontendOptions::NoneAction) { Diags.diagnose(SourceLoc(), diag::error_no_output_filename_specified); return true; } } else if (Opts.getSingleOutputFilename() != "-" && llvm::sys::fs::is_directory(Opts.getSingleOutputFilename())) { Diags.diagnose(SourceLoc(), diag::error_implicit_output_file_is_directory, Opts.getSingleOutputFilename()); return true; } } auto determineOutputFilename = [&](std::string &output, OptSpecifier optWithoutPath, OptSpecifier optWithPath, const char *extension, bool useMainOutput) { if (const Arg *A = Args.getLastArg(optWithPath)) { Args.ClaimAllArgs(optWithoutPath); output = A->getValue(); return; } if (!Args.hasArg(optWithoutPath)) return; if (useMainOutput && !Opts.OutputFilenames.empty()) { output = Opts.getSingleOutputFilename(); return; } if (!output.empty()) return; StringRef OriginalPath; if (!Opts.OutputFilenames.empty() && Opts.getSingleOutputFilename() != "-") // Put the serialized diagnostics file next to the output file. OriginalPath = Opts.getSingleOutputFilename(); else if (Opts.PrimaryInput.hasValue() && Opts.PrimaryInput->isFilename()) // We have a primary input, so use that as the basis for the name of the // serialized diagnostics file. OriginalPath = llvm::sys::path::filename( Opts.InputFilenames[Opts.PrimaryInput->Index]); else // We don't have any better indication of name, so fall back on the // module name. OriginalPath = Opts.ModuleName; llvm::SmallString<128> Path(OriginalPath); llvm::sys::path::replace_extension(Path, extension); output = Path.str(); }; determineOutputFilename(Opts.DependenciesFilePath, OPT_emit_dependencies, OPT_emit_dependencies_path, "d", false); determineOutputFilename(Opts.ReferenceDependenciesFilePath, OPT_emit_reference_dependencies, OPT_emit_reference_dependencies_path, "swiftdeps", false); determineOutputFilename(Opts.SerializedDiagnosticsPath, OPT_serialize_diagnostics, OPT_serialize_diagnostics_path, "dia", false); determineOutputFilename(Opts.ObjCHeaderOutputPath, OPT_emit_objc_header, OPT_emit_objc_header_path, "h", false); if (const Arg *A = Args.getLastArg(OPT_emit_fixits_path)) { Opts.FixitsOutputPath = A->getValue(); } bool IsSIB = Opts.RequestedAction == FrontendOptions::EmitSIB || Opts.RequestedAction == FrontendOptions::EmitSIBGen; bool canUseMainOutputForModule = Opts.RequestedAction == FrontendOptions::EmitModuleOnly || IsSIB; auto ext = IsSIB ? SIB_EXTENSION : SERIALIZED_MODULE_EXTENSION; auto sibOpt = Opts.RequestedAction == FrontendOptions::EmitSIB ? OPT_emit_sib : OPT_emit_sibgen; determineOutputFilename(Opts.ModuleOutputPath, IsSIB ? sibOpt : OPT_emit_module, OPT_emit_module_path, ext, canUseMainOutputForModule); determineOutputFilename(Opts.ModuleDocOutputPath, OPT_emit_module_doc, OPT_emit_module_doc_path, SERIALIZED_MODULE_DOC_EXTENSION, false); if (!Opts.DependenciesFilePath.empty()) { switch (Opts.RequestedAction) { case FrontendOptions::NoneAction: case FrontendOptions::DumpParse: case FrontendOptions::DumpInterfaceHash: case FrontendOptions::DumpAST: case FrontendOptions::PrintAST: case FrontendOptions::DumpTypeRefinementContexts: case FrontendOptions::Immediate: case FrontendOptions::REPL: Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_dependencies); return true; case FrontendOptions::Parse: case FrontendOptions::EmitModuleOnly: case FrontendOptions::EmitSILGen: case FrontendOptions::EmitSIL: case FrontendOptions::EmitSIBGen: case FrontendOptions::EmitSIB: case FrontendOptions::EmitIR: case FrontendOptions::EmitBC: case FrontendOptions::EmitAssembly: case FrontendOptions::EmitObject: break; } } if (!Opts.ObjCHeaderOutputPath.empty()) { switch (Opts.RequestedAction) { case FrontendOptions::NoneAction: case FrontendOptions::DumpParse: case FrontendOptions::DumpInterfaceHash: case FrontendOptions::DumpAST: case FrontendOptions::PrintAST: case FrontendOptions::DumpTypeRefinementContexts: case FrontendOptions::Immediate: case FrontendOptions::REPL: Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_header); return true; case FrontendOptions::Parse: case FrontendOptions::EmitModuleOnly: case FrontendOptions::EmitSILGen: case FrontendOptions::EmitSIL: case FrontendOptions::EmitSIBGen: case FrontendOptions::EmitSIB: case FrontendOptions::EmitIR: case FrontendOptions::EmitBC: case FrontendOptions::EmitAssembly: case FrontendOptions::EmitObject: break; } } if (!Opts.ModuleOutputPath.empty() || !Opts.ModuleDocOutputPath.empty()) { switch (Opts.RequestedAction) { case FrontendOptions::NoneAction: case FrontendOptions::Parse: case FrontendOptions::DumpParse: case FrontendOptions::DumpInterfaceHash: case FrontendOptions::DumpAST: case FrontendOptions::PrintAST: case FrontendOptions::DumpTypeRefinementContexts: case FrontendOptions::EmitSILGen: case FrontendOptions::Immediate: case FrontendOptions::REPL: if (!Opts.ModuleOutputPath.empty()) Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module); else Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_doc); return true; case FrontendOptions::EmitModuleOnly: case FrontendOptions::EmitSIL: case FrontendOptions::EmitSIBGen: case FrontendOptions::EmitSIB: case FrontendOptions::EmitIR: case FrontendOptions::EmitBC: case FrontendOptions::EmitAssembly: case FrontendOptions::EmitObject: break; } } if (const Arg *A = Args.getLastArg(OPT_module_link_name)) { Opts.ModuleLinkName = A->getValue(); } Opts.AlwaysSerializeDebuggingOptions |= Args.hasArg(OPT_serialize_debugging_options); Opts.EnableSourceImport |= Args.hasArg(OPT_enable_source_import); Opts.ImportUnderlyingModule |= Args.hasArg(OPT_import_underlying_module); Opts.SILSerializeAll |= Args.hasArg(OPT_sil_serialize_all); if (const Arg *A = Args.getLastArg(OPT_import_objc_header)) { Opts.ImplicitObjCHeaderPath = A->getValue(); Opts.SerializeBridgingHeader |= !Opts.PrimaryInput && !Opts.ModuleOutputPath.empty(); } for (const Arg *A : make_range(Args.filtered_begin(OPT_import_module), Args.filtered_end())) { Opts.ImplicitImportModuleNames.push_back(A->getValue()); } for (const Arg *A : make_range(Args.filtered_begin(OPT_Xllvm), Args.filtered_end())) { Opts.LLVMArgs.push_back(A->getValue()); } return false; }
static bool ParseSILArgs(SILOptions &Opts, ArgList &Args, IRGenOptions &IRGenOpts, FrontendOptions &FEOpts, DiagnosticEngine &Diags) { using namespace options; if (const Arg *A = Args.getLastArg(OPT_sil_inline_threshold)) { if (StringRef(A->getValue()).getAsInteger(10, Opts.InlineThreshold)) { Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, A->getAsString(Args), A->getValue()); return true; } } if (const Arg *A = Args.getLastArg(OPT_num_threads)) { if (StringRef(A->getValue()).getAsInteger(10, Opts.NumThreads)) { Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, A->getAsString(Args), A->getValue()); return true; } } if (const Arg *A = Args.getLastArg(OPT_disable_sil_linking, OPT_sil_link_all)) { if (A->getOption().matches(OPT_disable_sil_linking)) Opts.LinkMode = SILOptions::LinkNone; else if (A->getOption().matches(OPT_sil_link_all)) Opts.LinkMode = SILOptions::LinkAll; else llvm_unreachable("Unknown SIL linking option!"); } // Parse the optimization level. if (const Arg *A = Args.getLastArg(OPT_O_Group)) { if (A->getOption().matches(OPT_Onone)) { IRGenOpts.Optimize = false; Opts.Optimization = SILOptions::SILOptMode::None; } else if (A->getOption().matches(OPT_Ounchecked)) { // Turn on optimizations and remove all runtime checks. IRGenOpts.Optimize = true; Opts.Optimization = SILOptions::SILOptMode::OptimizeUnchecked; // Removal of cond_fail (overflow on binary operations). Opts.RemoveRuntimeAsserts = true; Opts.AssertConfig = SILOptions::Unchecked; } else if (A->getOption().matches(OPT_Oplayground)) { // For now -Oplayground is equivalent to -Onone. IRGenOpts.Optimize = false; Opts.Optimization = SILOptions::SILOptMode::None; } else { assert(A->getOption().matches(OPT_O)); IRGenOpts.Optimize = true; Opts.Optimization = SILOptions::SILOptMode::Optimize; } } // Parse the assert configuration identifier. if (const Arg *A = Args.getLastArg(OPT_AssertConfig)) { StringRef Configuration = A->getValue(); if (Configuration == "DisableReplacement") { Opts.AssertConfig = SILOptions::DisableReplacement; } else if (Configuration == "Debug") { Opts.AssertConfig = SILOptions::Debug; } else if (Configuration == "Release") { Opts.AssertConfig = SILOptions::Release; } else if (Configuration == "Unchecked") { Opts.AssertConfig = SILOptions::Unchecked; } else { Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, A->getAsString(Args), A->getValue()); return true; } } else if (FEOpts.ParseStdlib) { // Disable assertion configuration replacement when we build the standard // library. Opts.AssertConfig = SILOptions::DisableReplacement; } else if (Opts.AssertConfig == SILOptions::Debug) { // Set the assert configuration according to the optimization level if it // has not been set by the -Ounchecked flag. Opts.AssertConfig = IRGenOpts.Optimize ? SILOptions::Release : SILOptions::Debug; } // -Ounchecked might also set removal of runtime asserts (cond_fail). Opts.RemoveRuntimeAsserts |= Args.hasArg(OPT_remove_runtime_asserts); Opts.EnableARCOptimizations |= !Args.hasArg(OPT_disable_arc_opts); Opts.DisableSILPerfOptimizations |= Args.hasArg(OPT_disable_sil_perf_optzns); Opts.VerifyAll |= Args.hasArg(OPT_sil_verify_all); Opts.DebugSerialization |= Args.hasArg(OPT_sil_debug_serialization); Opts.EmitVerboseSIL |= Args.hasArg(OPT_emit_verbose_sil); Opts.PrintInstCounts |= Args.hasArg(OPT_print_inst_counts); if (const Arg *A = Args.getLastArg(OPT_external_pass_pipeline_filename)) Opts.ExternalPassPipelineFilename = A->getValue(); Opts.GenerateProfile |= Args.hasArg(OPT_profile_generate); Opts.EmitProfileCoverageMapping |= Args.hasArg(OPT_profile_coverage_mapping); Opts.EnableGuaranteedClosureContexts |= Args.hasArg(OPT_enable_guaranteed_closure_contexts); if (Args.hasArg(OPT_debug_on_sil)) { // Derive the name of the SIL file for debugging from // the regular outputfile. StringRef BaseName = FEOpts.getSingleOutputFilename(); // If there are no or multiple outputfiles, derive the name // from the module name. if (BaseName.empty()) BaseName = FEOpts.ModuleName; Opts.SILOutputFileNameForDebugging = BaseName.str(); } return false; }
/// Performs the compile requested by the user. /// \returns true on error static bool performCompile(CompilerInstance &Instance, CompilerInvocation &Invocation, ArrayRef<const char *> Args, int &ReturnValue) { FrontendOptions opts = Invocation.getFrontendOptions(); FrontendOptions::ActionType Action = opts.RequestedAction; IRGenOptions &IRGenOpts = Invocation.getIRGenOptions(); bool inputIsLLVMIr = Invocation.getInputKind() == InputFileKind::IFK_LLVM_IR; if (inputIsLLVMIr) { auto &LLVMContext = llvm::getGlobalContext(); // Load in bitcode file. assert(Invocation.getInputFilenames().size() == 1 && "We expect a single input for bitcode input!"); llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(Invocation.getInputFilenames()[0]); if (!FileBufOrErr) { Instance.getASTContext().Diags.diagnose(SourceLoc(), diag::error_open_input_file, Invocation.getInputFilenames()[0], FileBufOrErr.getError().message()); return true; } llvm::MemoryBuffer *MainFile = FileBufOrErr.get().get(); llvm::SMDiagnostic Err; std::unique_ptr<llvm::Module> Module = llvm::parseIR( MainFile->getMemBufferRef(), Err, LLVMContext); if (!Module) { // TODO: Translate from the diagnostic info to the SourceManager location // if available. Instance.getASTContext().Diags.diagnose(SourceLoc(), diag::error_parse_input_file, Invocation.getInputFilenames()[0], Err.getMessage()); return true; } // TODO: remove once the frontend understands what action it should perform IRGenOpts.OutputKind = getOutputKind(Action); return performLLVM(IRGenOpts, Instance.getASTContext(), Module.get()); } ReferencedNameTracker nameTracker; bool shouldTrackReferences = !opts.ReferenceDependenciesFilePath.empty(); if (shouldTrackReferences) Instance.setReferencedNameTracker(&nameTracker); if (Action == FrontendOptions::DumpParse || Action == FrontendOptions::DumpInterfaceHash) Instance.performParseOnly(); else Instance.performSema(); FrontendOptions::DebugCrashMode CrashMode = opts.CrashMode; if (CrashMode == FrontendOptions::DebugCrashMode::AssertAfterParse) debugFailWithAssertion(); else if (CrashMode == FrontendOptions::DebugCrashMode::CrashAfterParse) debugFailWithCrash(); ASTContext &Context = Instance.getASTContext(); if (Action == FrontendOptions::REPL) { runREPL(Instance, ProcessCmdLine(Args.begin(), Args.end()), Invocation.getParseStdlib()); return false; } SourceFile *PrimarySourceFile = Instance.getPrimarySourceFile(); // We've been told to dump the AST (either after parsing or type-checking, // which is already differentiated in CompilerInstance::performSema()), // so dump or print the main source file and return. if (Action == FrontendOptions::DumpParse || Action == FrontendOptions::DumpAST || Action == FrontendOptions::PrintAST || Action == FrontendOptions::DumpTypeRefinementContexts || Action == FrontendOptions::DumpInterfaceHash) { SourceFile *SF = PrimarySourceFile; if (!SF) { SourceFileKind Kind = Invocation.getSourceFileKind(); SF = &Instance.getMainModule()->getMainSourceFile(Kind); } if (Action == FrontendOptions::PrintAST) SF->print(llvm::outs(), PrintOptions::printEverything()); else if (Action == FrontendOptions::DumpTypeRefinementContexts) SF->getTypeRefinementContext()->dump(llvm::errs(), Context.SourceMgr); else if (Action == FrontendOptions::DumpInterfaceHash) SF->dumpInterfaceHash(llvm::errs()); else SF->dump(); return false; } // If we were asked to print Clang stats, do so. if (opts.PrintClangStats && Context.getClangModuleLoader()) Context.getClangModuleLoader()->printStatistics(); if (!opts.DependenciesFilePath.empty()) (void)emitMakeDependencies(Context.Diags, *Instance.getDependencyTracker(), opts); if (shouldTrackReferences) emitReferenceDependencies(Context.Diags, Instance.getPrimarySourceFile(), *Instance.getDependencyTracker(), opts); if (Context.hadError()) return true; // FIXME: This is still a lousy approximation of whether the module file will // be externally consumed. bool moduleIsPublic = !Instance.getMainModule()->hasEntryPoint() && opts.ImplicitObjCHeaderPath.empty() && !Context.LangOpts.EnableAppExtensionRestrictions; // We've just been told to perform a parse, so we can return now. if (Action == FrontendOptions::Parse) { if (!opts.ObjCHeaderOutputPath.empty()) return printAsObjC(opts.ObjCHeaderOutputPath, Instance.getMainModule(), opts.ImplicitObjCHeaderPath, moduleIsPublic); return false; } assert(Action >= FrontendOptions::EmitSILGen && "All actions not requiring SILGen must have been handled!"); std::unique_ptr<SILModule> SM = Instance.takeSILModule(); if (!SM) { if (opts.PrimaryInput.hasValue() && opts.PrimaryInput.getValue().isFilename()) { FileUnit *PrimaryFile = PrimarySourceFile; if (!PrimaryFile) { auto Index = opts.PrimaryInput.getValue().Index; PrimaryFile = Instance.getMainModule()->getFiles()[Index]; } SM = performSILGeneration(*PrimaryFile, Invocation.getSILOptions(), None, opts.SILSerializeAll); } else { SM = performSILGeneration(Instance.getMainModule(), Invocation.getSILOptions(), opts.SILSerializeAll, true); } } // We've been told to emit SIL after SILGen, so write it now. if (Action == FrontendOptions::EmitSILGen) { // If we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); return writeSIL(*SM, Instance.getMainModule(), opts.EmitVerboseSIL, opts.getSingleOutputFilename(), opts.EmitSortedSIL); } if (Action == FrontendOptions::EmitSIBGen) { // If we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance.getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.SerializeAllSIL = true; serializationOpts.IsSIB = true; serialize(DC, serializationOpts, SM.get()); } return false; } // Perform "stable" optimizations that are invariant across compiler versions. if (!Invocation.getDiagnosticOptions().SkipDiagnosticPasses && runSILDiagnosticPasses(*SM)) return true; // Now if we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); SM->verify(); // Perform SIL optimization passes if optimizations haven't been disabled. // These may change across compiler versions. if (IRGenOpts.Optimize) { StringRef CustomPipelinePath = Invocation.getSILOptions().ExternalPassPipelineFilename; if (!CustomPipelinePath.empty()) { runSILOptimizationPassesWithFileSpecification(*SM, CustomPipelinePath); } else { runSILOptimizationPasses(*SM); } } else { runSILPassesForOnone(*SM); } SM->verify(); // Gather instruction counts if we are asked to do so. if (SM->getOptions().PrintInstCounts) { performSILInstCount(&*SM); } // Get the main source file's private discriminator and attach it to // the compile unit's flags. if (PrimarySourceFile) { Identifier PD = PrimarySourceFile->getPrivateDiscriminator(); if (!PD.empty()) IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str(); } if (!opts.ObjCHeaderOutputPath.empty()) { (void)printAsObjC(opts.ObjCHeaderOutputPath, Instance.getMainModule(), opts.ImplicitObjCHeaderPath, moduleIsPublic); } if (Action == FrontendOptions::EmitSIB) { auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance.getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.SerializeAllSIL = true; serializationOpts.IsSIB = true; serialize(DC, serializationOpts, SM.get()); } return false; } if (!opts.ModuleOutputPath.empty() || !opts.ModuleDocOutputPath.empty()) { auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance.getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.DocOutputPath = opts.ModuleDocOutputPath.c_str(); serializationOpts.SerializeAllSIL = opts.SILSerializeAll; if (opts.SerializeBridgingHeader) serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath; serializationOpts.ModuleLinkName = opts.ModuleLinkName; serializationOpts.ExtraClangOptions = Invocation.getClangImporterOptions().ExtraArgs; if (!IRGenOpts.ForceLoadSymbolName.empty()) serializationOpts.AutolinkForceLoad = true; // Options contain information about the developer's computer, // so only serialize them if the module isn't going to be shipped to // the public. serializationOpts.SerializeOptionsForDebugging = !moduleIsPublic || opts.AlwaysSerializeDebuggingOptions; serialize(DC, serializationOpts, SM.get()); } if (Action == FrontendOptions::EmitModuleOnly) return false; } assert(Action >= FrontendOptions::EmitSIL && "All actions not requiring SILPasses must have been handled!"); // We've been told to write canonical SIL, so write it now. if (Action == FrontendOptions::EmitSIL) { return writeSIL(*SM, Instance.getMainModule(), opts.EmitVerboseSIL, opts.getSingleOutputFilename(), opts.EmitSortedSIL); } assert(Action >= FrontendOptions::Immediate && "All actions not requiring IRGen must have been handled!"); assert(Action != FrontendOptions::REPL && "REPL mode must be handled immediately after Instance.performSema()"); // Check if we had any errors; if we did, don't proceed to IRGen. if (Context.hadError()) return true; // Cleanup instructions/builtin calls not suitable for IRGen. performSILCleanup(SM.get()); // TODO: remove once the frontend understands what action it should perform IRGenOpts.OutputKind = getOutputKind(Action); if (Action == FrontendOptions::Immediate) { assert(!PrimarySourceFile && "-i doesn't work in -primary-file mode"); IRGenOpts.UseJIT = true; IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal; const ProcessCmdLine &CmdLine = ProcessCmdLine(opts.ImmediateArgv.begin(), opts.ImmediateArgv.end()); Instance.setSILModule(std::move(SM)); ReturnValue = RunImmediately(Instance, CmdLine, IRGenOpts, Invocation.getSILOptions()); return false; } // FIXME: We shouldn't need to use the global context here, but // something is persisting across calls to performIRGeneration. auto &LLVMContext = llvm::getGlobalContext(); if (PrimarySourceFile) { performIRGeneration(IRGenOpts, *PrimarySourceFile, SM.get(), opts.getSingleOutputFilename(), LLVMContext); } else { performIRGeneration(IRGenOpts, Instance.getMainModule(), SM.get(), opts.getSingleOutputFilename(), LLVMContext); } return false; }
/// Performs the compile requested by the user. /// \param Instance Will be reset after performIRGeneration when the verifier /// mode is NoVerify and there were no errors. /// \returns true on error static bool performCompile(std::unique_ptr<CompilerInstance> &Instance, CompilerInvocation &Invocation, ArrayRef<const char *> Args, int &ReturnValue, FrontendObserver *observer) { FrontendOptions opts = Invocation.getFrontendOptions(); FrontendOptions::ActionType Action = opts.RequestedAction; IRGenOptions &IRGenOpts = Invocation.getIRGenOptions(); bool inputIsLLVMIr = Invocation.getInputKind() == InputFileKind::IFK_LLVM_IR; if (inputIsLLVMIr) { auto &LLVMContext = getGlobalLLVMContext(); // Load in bitcode file. assert(Invocation.getInputFilenames().size() == 1 && "We expect a single input for bitcode input!"); llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr = llvm::MemoryBuffer::getFileOrSTDIN(Invocation.getInputFilenames()[0]); if (!FileBufOrErr) { Instance->getASTContext().Diags.diagnose(SourceLoc(), diag::error_open_input_file, Invocation.getInputFilenames()[0], FileBufOrErr.getError().message()); return true; } llvm::MemoryBuffer *MainFile = FileBufOrErr.get().get(); llvm::SMDiagnostic Err; std::unique_ptr<llvm::Module> Module = llvm::parseIR( MainFile->getMemBufferRef(), Err, LLVMContext); if (!Module) { // TODO: Translate from the diagnostic info to the SourceManager location // if available. Instance->getASTContext().Diags.diagnose(SourceLoc(), diag::error_parse_input_file, Invocation.getInputFilenames()[0], Err.getMessage()); return true; } // TODO: remove once the frontend understands what action it should perform IRGenOpts.OutputKind = getOutputKind(Action); return performLLVM(IRGenOpts, Instance->getASTContext(), Module.get()); } ReferencedNameTracker nameTracker; bool shouldTrackReferences = !opts.ReferenceDependenciesFilePath.empty(); if (shouldTrackReferences) Instance->setReferencedNameTracker(&nameTracker); if (Action == FrontendOptions::Parse || Action == FrontendOptions::DumpParse || Action == FrontendOptions::DumpInterfaceHash) Instance->performParseOnly(); else Instance->performSema(); if (Action == FrontendOptions::Parse) return Instance->getASTContext().hadError(); if (observer) { observer->performedSemanticAnalysis(*Instance); } FrontendOptions::DebugCrashMode CrashMode = opts.CrashMode; if (CrashMode == FrontendOptions::DebugCrashMode::AssertAfterParse) debugFailWithAssertion(); else if (CrashMode == FrontendOptions::DebugCrashMode::CrashAfterParse) debugFailWithCrash(); ASTContext &Context = Instance->getASTContext(); if (Action == FrontendOptions::REPL) { runREPL(*Instance, ProcessCmdLine(Args.begin(), Args.end()), Invocation.getParseStdlib()); return Context.hadError(); } SourceFile *PrimarySourceFile = Instance->getPrimarySourceFile(); // We've been told to dump the AST (either after parsing or type-checking, // which is already differentiated in CompilerInstance::performSema()), // so dump or print the main source file and return. if (Action == FrontendOptions::DumpParse || Action == FrontendOptions::DumpAST || Action == FrontendOptions::PrintAST || Action == FrontendOptions::DumpScopeMaps || Action == FrontendOptions::DumpTypeRefinementContexts || Action == FrontendOptions::DumpInterfaceHash) { SourceFile *SF = PrimarySourceFile; if (!SF) { SourceFileKind Kind = Invocation.getSourceFileKind(); SF = &Instance->getMainModule()->getMainSourceFile(Kind); } if (Action == FrontendOptions::PrintAST) SF->print(llvm::outs(), PrintOptions::printEverything()); else if (Action == FrontendOptions::DumpScopeMaps) { ASTScope &scope = SF->getScope(); if (opts.DumpScopeMapLocations.empty()) { scope.expandAll(); } else if (auto bufferID = SF->getBufferID()) { SourceManager &sourceMgr = Instance->getSourceMgr(); // Probe each of the locations, and dump what we find. for (auto lineColumn : opts.DumpScopeMapLocations) { SourceLoc loc = sourceMgr.getLocForLineCol(*bufferID, lineColumn.first, lineColumn.second); if (loc.isInvalid()) continue; llvm::errs() << "***Scope at " << lineColumn.first << ":" << lineColumn.second << "***\n"; auto locScope = scope.findInnermostEnclosingScope(loc); locScope->print(llvm::errs(), 0, false, false); // Dump the AST context, too. if (auto dc = locScope->getDeclContext()) { dc->printContext(llvm::errs()); } // Grab the local bindings introduced by this scope. auto localBindings = locScope->getLocalBindings(); if (!localBindings.empty()) { llvm::errs() << "Local bindings: "; interleave(localBindings.begin(), localBindings.end(), [&](ValueDecl *value) { llvm::errs() << value->getFullName(); }, [&]() { llvm::errs() << " "; }); llvm::errs() << "\n"; } } llvm::errs() << "***Complete scope map***\n"; } // Print the resulting map. scope.print(llvm::errs()); } else if (Action == FrontendOptions::DumpTypeRefinementContexts) SF->getTypeRefinementContext()->dump(llvm::errs(), Context.SourceMgr); else if (Action == FrontendOptions::DumpInterfaceHash) SF->dumpInterfaceHash(llvm::errs()); else SF->dump(); return Context.hadError(); } // If we were asked to print Clang stats, do so. if (opts.PrintClangStats && Context.getClangModuleLoader()) Context.getClangModuleLoader()->printStatistics(); if (!opts.DependenciesFilePath.empty()) (void)emitMakeDependencies(Context.Diags, *Instance->getDependencyTracker(), opts); if (shouldTrackReferences) emitReferenceDependencies(Context.Diags, Instance->getPrimarySourceFile(), *Instance->getDependencyTracker(), opts); if (Context.hadError()) return true; // FIXME: This is still a lousy approximation of whether the module file will // be externally consumed. bool moduleIsPublic = !Instance->getMainModule()->hasEntryPoint() && opts.ImplicitObjCHeaderPath.empty() && !Context.LangOpts.EnableAppExtensionRestrictions; // We've just been told to perform a typecheck, so we can return now. if (Action == FrontendOptions::Typecheck) { if (!opts.ObjCHeaderOutputPath.empty()) return printAsObjC(opts.ObjCHeaderOutputPath, Instance->getMainModule(), opts.ImplicitObjCHeaderPath, moduleIsPublic); return Context.hadError(); } assert(Action >= FrontendOptions::EmitSILGen && "All actions not requiring SILGen must have been handled!"); std::unique_ptr<SILModule> SM = Instance->takeSILModule(); if (!SM) { if (opts.PrimaryInput.hasValue() && opts.PrimaryInput.getValue().isFilename()) { FileUnit *PrimaryFile = PrimarySourceFile; if (!PrimaryFile) { auto Index = opts.PrimaryInput.getValue().Index; PrimaryFile = Instance->getMainModule()->getFiles()[Index]; } SM = performSILGeneration(*PrimaryFile, Invocation.getSILOptions(), None, opts.SILSerializeAll); } else { SM = performSILGeneration(Instance->getMainModule(), Invocation.getSILOptions(), opts.SILSerializeAll, true); } } if (observer) { observer->performedSILGeneration(*SM); } // We've been told to emit SIL after SILGen, so write it now. if (Action == FrontendOptions::EmitSILGen) { // If we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); return writeSIL(*SM, Instance->getMainModule(), opts.EmitVerboseSIL, opts.getSingleOutputFilename(), opts.EmitSortedSIL); } if (Action == FrontendOptions::EmitSIBGen) { // If we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance->getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.SerializeAllSIL = true; serializationOpts.IsSIB = true; serialize(DC, serializationOpts, SM.get()); } return Context.hadError(); } // Perform "stable" optimizations that are invariant across compiler versions. if (!Invocation.getDiagnosticOptions().SkipDiagnosticPasses) { if (runSILDiagnosticPasses(*SM)) return true; if (observer) { observer->performedSILDiagnostics(*SM); } } else { // Even if we are not supposed to run the diagnostic passes, we still need // to run the ownership evaluator. if (runSILOwnershipEliminatorPass(*SM)) return true; } // Now if we are asked to link all, link all. if (Invocation.getSILOptions().LinkMode == SILOptions::LinkAll) performSILLinking(SM.get(), true); { SharedTimer timer("SIL verification (pre-optimization)"); SM->verify(); } // Perform SIL optimization passes if optimizations haven't been disabled. // These may change across compiler versions. { SharedTimer timer("SIL optimization"); if (Invocation.getSILOptions().Optimization > SILOptions::SILOptMode::None) { StringRef CustomPipelinePath = Invocation.getSILOptions().ExternalPassPipelineFilename; if (!CustomPipelinePath.empty()) { runSILOptimizationPassesWithFileSpecification(*SM, CustomPipelinePath); } else { runSILOptimizationPasses(*SM); } } else { runSILPassesForOnone(*SM); } } if (observer) { observer->performedSILOptimization(*SM); } { SharedTimer timer("SIL verification (post-optimization)"); SM->verify(); } // Gather instruction counts if we are asked to do so. if (SM->getOptions().PrintInstCounts) { performSILInstCount(&*SM); } // Get the main source file's private discriminator and attach it to // the compile unit's flags. if (PrimarySourceFile) { Identifier PD = PrimarySourceFile->getPrivateDiscriminator(); if (!PD.empty()) IRGenOpts.DWARFDebugFlags += (" -private-discriminator "+PD.str()).str(); } if (!opts.ObjCHeaderOutputPath.empty()) { (void)printAsObjC(opts.ObjCHeaderOutputPath, Instance->getMainModule(), opts.ImplicitObjCHeaderPath, moduleIsPublic); } if (Action == FrontendOptions::EmitSIB) { auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance->getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.SerializeAllSIL = true; serializationOpts.IsSIB = true; serialize(DC, serializationOpts, SM.get()); } return Context.hadError(); } if (!opts.ModuleOutputPath.empty() || !opts.ModuleDocOutputPath.empty()) { auto DC = PrimarySourceFile ? ModuleOrSourceFile(PrimarySourceFile) : Instance->getMainModule(); if (!opts.ModuleOutputPath.empty()) { SerializationOptions serializationOpts; serializationOpts.OutputPath = opts.ModuleOutputPath.c_str(); serializationOpts.DocOutputPath = opts.ModuleDocOutputPath.c_str(); serializationOpts.GroupInfoPath = opts.GroupInfoPath.c_str(); serializationOpts.SerializeAllSIL = opts.SILSerializeAll; if (opts.SerializeBridgingHeader) serializationOpts.ImportedHeader = opts.ImplicitObjCHeaderPath; serializationOpts.ModuleLinkName = opts.ModuleLinkName; serializationOpts.ExtraClangOptions = Invocation.getClangImporterOptions().ExtraArgs; if (!IRGenOpts.ForceLoadSymbolName.empty()) serializationOpts.AutolinkForceLoad = true; // Options contain information about the developer's computer, // so only serialize them if the module isn't going to be shipped to // the public. serializationOpts.SerializeOptionsForDebugging = !moduleIsPublic || opts.AlwaysSerializeDebuggingOptions; serialize(DC, serializationOpts, SM.get()); } if (Action == FrontendOptions::EmitModuleOnly) return Context.hadError(); } assert(Action >= FrontendOptions::EmitSIL && "All actions not requiring SILPasses must have been handled!"); // We've been told to write canonical SIL, so write it now. if (Action == FrontendOptions::EmitSIL) { return writeSIL(*SM, Instance->getMainModule(), opts.EmitVerboseSIL, opts.getSingleOutputFilename(), opts.EmitSortedSIL); } assert(Action >= FrontendOptions::Immediate && "All actions not requiring IRGen must have been handled!"); assert(Action != FrontendOptions::REPL && "REPL mode must be handled immediately after Instance->performSema()"); // Check if we had any errors; if we did, don't proceed to IRGen. if (Context.hadError()) return true; // Cleanup instructions/builtin calls not suitable for IRGen. performSILCleanup(SM.get()); // TODO: remove once the frontend understands what action it should perform IRGenOpts.OutputKind = getOutputKind(Action); if (Action == FrontendOptions::Immediate) { assert(!PrimarySourceFile && "-i doesn't work in -primary-file mode"); IRGenOpts.UseJIT = true; IRGenOpts.DebugInfoKind = IRGenDebugInfoKind::Normal; const ProcessCmdLine &CmdLine = ProcessCmdLine(opts.ImmediateArgv.begin(), opts.ImmediateArgv.end()); Instance->setSILModule(std::move(SM)); if (observer) { observer->aboutToRunImmediately(*Instance); } ReturnValue = RunImmediately(*Instance, CmdLine, IRGenOpts, Invocation.getSILOptions()); return Context.hadError(); } // FIXME: We shouldn't need to use the global context here, but // something is persisting across calls to performIRGeneration. auto &LLVMContext = getGlobalLLVMContext(); std::unique_ptr<llvm::Module> IRModule; llvm::GlobalVariable *HashGlobal; if (PrimarySourceFile) { IRModule = performIRGeneration(IRGenOpts, *PrimarySourceFile, std::move(SM), opts.getSingleOutputFilename(), LLVMContext, 0, &HashGlobal); } else { IRModule = performIRGeneration(IRGenOpts, Instance->getMainModule(), std::move(SM), opts.getSingleOutputFilename(), LLVMContext, &HashGlobal); } // Just because we had an AST error it doesn't mean we can't performLLVM. bool HadError = Instance->getASTContext().hadError(); // If the AST Context has no errors but no IRModule is available, // parallelIRGen happened correctly, since parallel IRGen produces multiple // modules. if (!IRModule) { return HadError; } std::unique_ptr<llvm::TargetMachine> TargetMachine = createTargetMachine(IRGenOpts, Context); version::Version EffectiveLanguageVersion = Context.LangOpts.EffectiveLanguageVersion; DiagnosticEngine &Diags = Context.Diags; const DiagnosticOptions &DiagOpts = Invocation.getDiagnosticOptions(); // Delete the compiler instance now that we have an IRModule. if (DiagOpts.VerifyMode == DiagnosticOptions::NoVerify) { SM.reset(); Instance.reset(); } // Now that we have a single IR Module, hand it over to performLLVM. return performLLVM(IRGenOpts, &Diags, nullptr, HashGlobal, IRModule.get(), TargetMachine.get(), EffectiveLanguageVersion, opts.getSingleOutputFilename()) || HadError; }