|
20 | 20 | #include "swift/Basic/Assertions.h"
|
21 | 21 | #include "swift/Basic/Feature.h"
|
22 | 22 | #include "swift/Basic/Platform.h"
|
| 23 | +#include "swift/Basic/Version.h" |
23 | 24 | #include "swift/Option/Options.h"
|
24 | 25 | #include "swift/Option/SanitizerOptions.h"
|
25 | 26 | #include "swift/Parse/Lexer.h"
|
@@ -92,20 +93,6 @@ void CompilerInvocation::setMainExecutablePath(StringRef Path) {
|
92 | 93 | llvm::sys::path::remove_filename(clangPath);
|
93 | 94 | llvm::sys::path::append(clangPath, "clang");
|
94 | 95 | ClangImporterOpts.clangPath = std::string(clangPath);
|
95 |
| - |
96 |
| - llvm::SmallString<128> DiagnosticDocsPath(Path); |
97 |
| - llvm::sys::path::remove_filename(DiagnosticDocsPath); // Remove /swift |
98 |
| - llvm::sys::path::remove_filename(DiagnosticDocsPath); // Remove /bin |
99 |
| - llvm::sys::path::append(DiagnosticDocsPath, "share", "doc", "swift", |
100 |
| - "diagnostics"); |
101 |
| - DiagnosticOpts.DiagnosticDocumentationPath = std::string(DiagnosticDocsPath.str()); |
102 |
| - |
103 |
| - // Compute the path to the diagnostic translations in the toolchain/build. |
104 |
| - llvm::SmallString<128> DiagnosticMessagesDir(Path); |
105 |
| - llvm::sys::path::remove_filename(DiagnosticMessagesDir); // Remove /swift |
106 |
| - llvm::sys::path::remove_filename(DiagnosticMessagesDir); // Remove /bin |
107 |
| - llvm::sys::path::append(DiagnosticMessagesDir, "share", "swift", "diagnostics"); |
108 |
| - DiagnosticOpts.LocalizationPath = std::string(DiagnosticMessagesDir.str()); |
109 | 96 | }
|
110 | 97 |
|
111 | 98 | static std::string
|
@@ -2369,6 +2356,9 @@ static bool ParseSearchPathArgs(SearchPathOptions &Opts, ArgList &Args,
|
2369 | 2356 |
|
2370 | 2357 | static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
|
2371 | 2358 | DiagnosticEngine &Diags) {
|
| 2359 | + // NOTE: This executes at the beginning of parsing the command line and cannot |
| 2360 | + // depend on the results of parsing other options. |
| 2361 | + |
2372 | 2362 | using namespace options;
|
2373 | 2363 |
|
2374 | 2364 | if (Args.hasArg(OPT_verify))
|
@@ -2492,6 +2482,56 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
|
2492 | 2482 | return false;
|
2493 | 2483 | }
|
2494 | 2484 |
|
| 2485 | +static void configureDiagnosticEngine( |
| 2486 | + const DiagnosticOptions &Options, |
| 2487 | + std::optional<version::Version> effectiveLanguageVersion, |
| 2488 | + StringRef mainExecutablePath, DiagnosticEngine &Diagnostics) { |
| 2489 | + if (Options.ShowDiagnosticsAfterFatalError) { |
| 2490 | + Diagnostics.setShowDiagnosticsAfterFatalError(); |
| 2491 | + } |
| 2492 | + if (Options.SuppressWarnings) { |
| 2493 | + Diagnostics.setSuppressWarnings(true); |
| 2494 | + } |
| 2495 | + if (Options.SuppressRemarks) { |
| 2496 | + Diagnostics.setSuppressRemarks(true); |
| 2497 | + } |
| 2498 | + Diagnostics.setWarningsAsErrorsRules(Options.WarningsAsErrorsRules); |
| 2499 | + Diagnostics.setPrintDiagnosticNamesMode(Options.PrintDiagnosticNames); |
| 2500 | + |
| 2501 | + std::string docsPath = Options.DiagnosticDocumentationPath; |
| 2502 | + if (docsPath.empty()) { |
| 2503 | + // Default to a location relative to the compiler. |
| 2504 | + llvm::SmallString<128> docsPathBuffer(mainExecutablePath); |
| 2505 | + llvm::sys::path::remove_filename(docsPathBuffer); // Remove /swift |
| 2506 | + llvm::sys::path::remove_filename(docsPathBuffer); // Remove /bin |
| 2507 | + llvm::sys::path::append(docsPathBuffer, "share", "doc", "swift", |
| 2508 | + "diagnostics"); |
| 2509 | + docsPath = docsPathBuffer.str(); |
| 2510 | + } |
| 2511 | + Diagnostics.setDiagnosticDocumentationPath(docsPath); |
| 2512 | + |
| 2513 | + if (!Options.LocalizationCode.empty()) { |
| 2514 | + std::string locPath = Options.LocalizationPath; |
| 2515 | + if (locPath.empty()) { |
| 2516 | + llvm::SmallString<128> locPathBuffer(mainExecutablePath); |
| 2517 | + llvm::sys::path::remove_filename(locPathBuffer); // Remove /swift |
| 2518 | + llvm::sys::path::remove_filename(locPathBuffer); // Remove /bin |
| 2519 | + llvm::sys::path::append(locPathBuffer, "share", "swift", "diagnostics"); |
| 2520 | + locPath = locPathBuffer.str(); |
| 2521 | + } |
| 2522 | + Diagnostics.setLocalization(Options.LocalizationCode, locPath); |
| 2523 | + } |
| 2524 | + |
| 2525 | + if (effectiveLanguageVersion) |
| 2526 | + Diagnostics.setLanguageVersion(*effectiveLanguageVersion); |
| 2527 | +} |
| 2528 | + |
| 2529 | +/// Configures the diagnostic engine for the invocation's options. |
| 2530 | +void CompilerInvocation::setUpDiagnosticEngine(DiagnosticEngine &diags) { |
| 2531 | + configureDiagnosticEngine(DiagnosticOpts, LangOpts.EffectiveLanguageVersion, |
| 2532 | + FrontendOpts.MainExecutablePath, diags); |
| 2533 | +} |
| 2534 | + |
2495 | 2535 | /// Parse -enforce-exclusivity=... options
|
2496 | 2536 | void parseExclusivityEnforcementOptions(const llvm::opt::Arg *A,
|
2497 | 2537 | SILOptions &Opts,
|
@@ -3742,6 +3782,16 @@ bool CompilerInvocation::parseArgs(
|
3742 | 3782 | return true;
|
3743 | 3783 | }
|
3744 | 3784 |
|
| 3785 | + // Parse options that control diagnostic behavior as early as possible, so |
| 3786 | + // that they can influence the behavior of diagnostics emitted during the |
| 3787 | + // rest of parsing. |
| 3788 | + if (ParseDiagnosticArgs(DiagnosticOpts, ParsedArgs, Diags)) { |
| 3789 | + return true; |
| 3790 | + } |
| 3791 | + configureDiagnosticEngine(DiagnosticOpts, |
| 3792 | + /*effectiveLanguageVersion=*/std::nullopt, |
| 3793 | + mainExecutablePath, Diags); |
| 3794 | + |
3745 | 3795 | ParseAssertionArgs(ParsedArgs);
|
3746 | 3796 |
|
3747 | 3797 | if (ParseFrontendArgs(FrontendOpts, ParsedArgs, Diags,
|
@@ -3796,10 +3846,6 @@ bool CompilerInvocation::parseArgs(
|
3796 | 3846 | return true;
|
3797 | 3847 | }
|
3798 | 3848 |
|
3799 |
| - if (ParseDiagnosticArgs(DiagnosticOpts, ParsedArgs, Diags)) { |
3800 |
| - return true; |
3801 |
| - } |
3802 |
| - |
3803 | 3849 | if (ParseMigratorArgs(MigratorOpts, LangOpts, FrontendOpts,
|
3804 | 3850 | SearchPathOpts.RuntimeResourcePath, ParsedArgs, Diags)) {
|
3805 | 3851 | return true;
|
|
0 commit comments