From e72cdc5ba1e65ecd8632663b6604eb9be8d1a162 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Tue, 22 Dec 2020 15:05:16 +0100 Subject: [clang][cli] NFC: Ensure non-null DiagnosticsEngine in ParseDiagnosticArgs Before this patch, ParseDiagnosticArgs can be called with a nullptr DiagnosticsEngine *. This happens early on in the compilation process, where no proper DiagnosticEngine exists, because the diagnostic options (passed through command line) are not known yet. This patch ensures nullptr is replaced by an ignoring DiagnosticEngine in ParseDiagnosticArgs, which allows to switch from pointer to a reference in some utility functions. Besides simplifying the code, this patch enables a future patch (D84673) that ports diagnostic options to the new marshalling infrastructure. Reviewed By: dexonsmith Differential Revision: https://reviews.llvm.org/D93701 --- clang/lib/Frontend/CompilerInvocation.cpp | 50 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'clang/lib/Frontend/CompilerInvocation.cpp') diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 7fb7ec8..7f3e39e 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -830,7 +830,7 @@ GenerateOptimizationRemarkRegex(DiagnosticsEngine &Diags, ArgList &Args, static bool parseDiagnosticLevelMask(StringRef FlagName, const std::vector &Levels, - DiagnosticsEngine *Diags, + DiagnosticsEngine &Diags, DiagnosticLevelMask &M) { bool Success = true; for (const auto &Level : Levels) { @@ -843,8 +843,7 @@ static bool parseDiagnosticLevelMask(StringRef FlagName, .Default(DiagnosticLevelMask::None); if (PM == DiagnosticLevelMask::None) { Success = false; - if (Diags) - Diags->Report(diag::err_drv_invalid_value) << FlagName << Level; + Diags.Report(diag::err_drv_invalid_value) << FlagName << Level; } M = M | PM; } @@ -1383,7 +1382,7 @@ static bool parseShowColorsArgs(const ArgList &Args, bool DefaultColor) { } static bool checkVerifyPrefixes(const std::vector &VerifyPrefixes, - DiagnosticsEngine *Diags) { + DiagnosticsEngine &Diags) { bool Success = true; for (const auto &Prefix : VerifyPrefixes) { // Every prefix must start with a letter and contain only alphanumeric @@ -1393,10 +1392,8 @@ static bool checkVerifyPrefixes(const std::vector &VerifyPrefixes, }); if (BadChar != Prefix.end() || !isLetter(Prefix[0])) { Success = false; - if (Diags) { - Diags->Report(diag::err_drv_invalid_value) << "-verify=" << Prefix; - Diags->Report(diag::note_drv_verify_prefix_spelling); - } + Diags.Report(diag::err_drv_invalid_value) << "-verify=" << Prefix; + Diags.Report(diag::note_drv_verify_prefix_spelling); } } return Success; @@ -1405,6 +1402,13 @@ static bool checkVerifyPrefixes(const std::vector &VerifyPrefixes, bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, DiagnosticsEngine *Diags, bool DefaultDiagColor) { + Optional IgnoringDiags; + if (!Diags) { + IgnoringDiags.emplace(new DiagnosticIDs(), new DiagnosticOptions(), + new IgnoringDiagConsumer()); + Diags = &*IgnoringDiags; + } + bool Success = true; Opts.DiagnosticLogFile = @@ -1439,10 +1443,9 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.setShowOverloads(Ovl_All); else { Success = false; - if (Diags) - Diags->Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args) - << ShowOverloads; + Diags->Report(diag::err_drv_invalid_value) + << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args) + << ShowOverloads; } StringRef ShowCategory = @@ -1455,10 +1458,9 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.ShowCategories = 2; else { Success = false; - if (Diags) - Diags->Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_fdiagnostics_show_category)->getAsString(Args) - << ShowCategory; + Diags->Report(diag::err_drv_invalid_value) + << Args.getLastArg(OPT_fdiagnostics_show_category)->getAsString(Args) + << ShowCategory; } StringRef Format = @@ -1474,10 +1476,9 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.setFormat(DiagnosticOptions::Vi); else { Success = false; - if (Diags) - Diags->Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_fdiagnostics_format)->getAsString(Args) - << Format; + Diags->Report(diag::err_drv_invalid_value) + << Args.getLastArg(OPT_fdiagnostics_format)->getAsString(Args) + << Format; } Opts.ShowSourceRanges = Args.hasArg(OPT_fdiagnostics_print_source_range_info); @@ -1488,7 +1489,7 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.VerifyPrefixes.push_back("expected"); // Keep VerifyPrefixes in its original order for the sake of diagnostics, and // then sort it to prepare for fast lookup using std::binary_search. - if (!checkVerifyPrefixes(Opts.VerifyPrefixes, Diags)) { + if (!checkVerifyPrefixes(Opts.VerifyPrefixes, *Diags)) { Opts.VerifyDiagnostics = false; Success = false; } @@ -1497,7 +1498,7 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, DiagnosticLevelMask DiagMask = DiagnosticLevelMask::None; Success &= parseDiagnosticLevelMask("-verify-ignore-unexpected=", Args.getAllArgValues(OPT_verify_ignore_unexpected_EQ), - Diags, DiagMask); + *Diags, DiagMask); if (Args.hasArg(OPT_verify_ignore_unexpected)) DiagMask = DiagnosticLevelMask::All; Opts.setVerifyIgnoreUnexpected(DiagMask); @@ -1523,9 +1524,8 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, DiagnosticOptions::DefaultTabStop, Diags); if (Opts.TabStop == 0 || Opts.TabStop > DiagnosticOptions::MaxTabStop) { Opts.TabStop = DiagnosticOptions::DefaultTabStop; - if (Diags) - Diags->Report(diag::warn_ignoring_ftabstop_value) - << Opts.TabStop << DiagnosticOptions::DefaultTabStop; + Diags->Report(diag::warn_ignoring_ftabstop_value) + << Opts.TabStop << DiagnosticOptions::DefaultTabStop; } Opts.MessageLength = getLastArgIntValue(Args, OPT_fmessage_length_EQ, 0, Diags); -- cgit v1.1