From 253d41d74cd449742fd8642753c3076514fb99c5 Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Mon, 26 Sep 2011 01:21:58 +0000 Subject: [PATCH] Rewrite the printing of diagnostic options, categories, etc to actually use the ostream interface and avoid lots of temporary strings. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@140494 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Frontend/TextDiagnosticPrinter.cpp | 64 ++++++++++++-------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp index 153c08eb75..189a93cac5 100644 --- a/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/lib/Frontend/TextDiagnosticPrinter.cpp @@ -1095,54 +1095,49 @@ static void PrintDiagnosticOptions(raw_ostream &OS, DiagnosticsEngine::Level Level, const Diagnostic &Info, const DiagnosticOptions &DiagOpts) { - std::string OptionName; + bool Started = false; if (DiagOpts.ShowOptionNames) { + // Handle special cases for non-warnings early. + if (Info.getID() == diag::fatal_too_many_errors) { + OS << " [-ferror-limit=]"; + return; + } + // Was this a warning mapped to an error using -Werror or pragma? if (Level == DiagnosticsEngine::Error && DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID())) { diag::Mapping mapping = diag::MAP_IGNORE; Info.getDiags()->getDiagnosticLevel(Info.getID(), Info.getLocation(), &mapping); - if (mapping == diag::MAP_WARNING) - OptionName += "-Werror"; + if (mapping == diag::MAP_WARNING) { + OS << " [-Werror"; + Started = true; + } + } + + // If the diagnostic is an extension diagnostic and not enabled by default + // then it must have been turned on with -pedantic. + bool EnabledByDefault; + if (DiagnosticIDs::isBuiltinExtensionDiag(Info.getID(), + EnabledByDefault) && + !EnabledByDefault) { + OS << (Started ? "," : " [") << "-pedantic"; + Started = true; } StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); if (!Opt.empty()) { - if (!OptionName.empty()) - OptionName += ','; - OptionName += "-W"; - OptionName += Opt; - } else if (Info.getID() == diag::fatal_too_many_errors) { - OptionName = "-ferror-limit="; - } else { - // If the diagnostic is an extension diagnostic and not enabled by default - // then it must have been turned on with -pedantic. - bool EnabledByDefault; - if (DiagnosticIDs::isBuiltinExtensionDiag(Info.getID(), - EnabledByDefault) && - !EnabledByDefault) - OptionName = "-pedantic"; + OS << (Started ? "," : " [") << "-W" << Opt; + Started = true; } } - + // If the user wants to see category information, include it too. - unsigned DiagCategory = 0; - if (DiagOpts.ShowCategories) - DiagCategory = DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); - - // If there is any categorization information, include it. - if (!OptionName.empty() || DiagCategory != 0) { - bool NeedsComma = false; - OS << " ["; - - if (!OptionName.empty()) { - OS << OptionName; - NeedsComma = true; - } - + if (DiagOpts.ShowCategories) { + unsigned DiagCategory = + DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); if (DiagCategory) { - if (NeedsComma) OS << ','; + OS << (Started ? "," : " ["); if (DiagOpts.ShowCategories == 1) OS << llvm::utostr(DiagCategory); else { @@ -1150,9 +1145,8 @@ static void PrintDiagnosticOptions(raw_ostream &OS, OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory); } } - - OS << "]"; } + OS << "]"; } void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, -- 2.40.0