From: Daniel Dunbar Date: Wed, 4 Nov 2009 06:24:30 +0000 (+0000) Subject: Factor out a diagnostic options class. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eace8743030d2979251a0c5ae247371cfd9056e5;p=clang Factor out a diagnostic options class. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86010 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Frontend/DiagnosticOptions.h b/include/clang/Frontend/DiagnosticOptions.h new file mode 100644 index 0000000000..958a8e9191 --- /dev/null +++ b/include/clang/Frontend/DiagnosticOptions.h @@ -0,0 +1,49 @@ +//===--- DiagnosticOptions.h ------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_FRONTEND_DIAGNOSTICOPTIONS_H +#define LLVM_CLANG_FRONTEND_DIAGNOSTICOPTIONS_H + +#include +#include + +namespace clang { + +/// DiagnosticOptions - Options for controlling the compiler diagnostics +/// engine. +class DiagnosticOptions { +public: + unsigned ShowColumn : 1; /// Show column number on diagnostics. + unsigned ShowLocation : 1; /// Show source location information. + unsigned ShowCarets : 1; /// Show source location information. + unsigned ShowFixits : 1; /// Show fixit information. + unsigned ShowSourceRanges : 1; /// Show source ranges in numeric form. + unsigned ShowOptionNames : 1; /// Show the diagnostic name for mappable + /// diagnostics. + unsigned ShowColors : 1; /// Show diagnostics with ANSI color sequences. + + /// Column limit for formatting message diagnostics, or 0 if unused. + unsigned MessageLength; + +public: + DiagnosticOptions() { + ShowColumn = 1; + ShowLocation = 1; + ShowCarets = 1; + ShowFixits = 1; + ShowSourceRanges = 0; + ShowOptionNames = 0; + ShowColors = 0; + MessageLength = 0; + } +}; + +} // end namespace clang + +#endif diff --git a/include/clang/Frontend/TextDiagnosticPrinter.h b/include/clang/Frontend/TextDiagnosticPrinter.h index 0fd8d44f72..98e5a75c65 100644 --- a/include/clang/Frontend/TextDiagnosticPrinter.h +++ b/include/clang/Frontend/TextDiagnosticPrinter.h @@ -23,42 +23,21 @@ namespace llvm { } namespace clang { -class SourceManager; +class DiagnosticOptions; class LangOptions; +class SourceManager; class TextDiagnosticPrinter : public DiagnosticClient { llvm::raw_ostream &OS; const LangOptions *LangOpts; + const DiagnosticOptions *DiagOpts; + SourceLocation LastWarningLoc; FullSourceLoc LastLoc; bool LastCaretDiagnosticWasNote; - bool ShowColumn; - bool CaretDiagnostics; - bool ShowLocation; - bool PrintRangeInfo; - bool PrintDiagnosticOption; - bool PrintFixItInfo; - unsigned MessageLength; - bool UseColors; - public: - TextDiagnosticPrinter(llvm::raw_ostream &os, - bool showColumn = true, - bool caretDiagnistics = true, bool showLocation = true, - bool printRangeInfo = true, - bool printDiagnosticOption = true, - bool printFixItInfo = true, - unsigned messageLength = 0, - bool useColors = false) - : OS(os), LangOpts(0), - LastCaretDiagnosticWasNote(false), ShowColumn(showColumn), - CaretDiagnostics(caretDiagnistics), ShowLocation(showLocation), - PrintRangeInfo(printRangeInfo), - PrintDiagnosticOption(printDiagnosticOption), - PrintFixItInfo(printFixItInfo), - MessageLength(messageLength), - UseColors(useColors) {} + TextDiagnosticPrinter(llvm::raw_ostream &os, const DiagnosticOptions &diags); void setLangOptions(const LangOptions *LO) { LangOpts = LO; diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp index 14769c1873..b1d8800369 100644 --- a/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/lib/Frontend/TextDiagnosticPrinter.cpp @@ -13,6 +13,7 @@ #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Basic/SourceManager.h" +#include "clang/Frontend/DiagnosticOptions.h" #include "clang/Lex/Lexer.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" @@ -37,6 +38,12 @@ static const enum llvm::raw_ostream::Colors savedColor = /// \brief Number of spaces to indent when word-wrapping. const unsigned WordWrapIndentation = 6; +TextDiagnosticPrinter::TextDiagnosticPrinter(llvm::raw_ostream &os, + const DiagnosticOptions &diags) + : OS(os), LangOpts(0), DiagOpts(&diags), + LastCaretDiagnosticWasNote(false) { +} + void TextDiagnosticPrinter:: PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { if (Loc.isInvalid()) return; @@ -46,7 +53,7 @@ PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { // Print out the other include frames first. PrintIncludeStack(PLoc.getIncludeLoc(), SM); - if (ShowLocation) + if (DiagOpts->ShowLocation) OS << "In file included from " << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; else @@ -281,13 +288,13 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, Ranges[i] = SourceRange(S, E); } - if (ShowLocation) { + if (DiagOpts->ShowLocation) { std::pair IInfo = SM.getDecomposedInstantiationLoc(Loc); // Emit the file/line/column that this expansion came from. OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':' << SM.getLineNumber(IInfo.first, IInfo.second) << ':'; - if (ShowColumn) + if (DiagOpts->ShowColumn) OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':'; OS << ' '; } @@ -370,13 +377,13 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, // produce easily machine parsable output. Add a space before the source line // and the caret to make it trivial to tell the main diagnostic line from what // the user is intended to see. - if (PrintRangeInfo) { + if (DiagOpts->ShowSourceRanges) { SourceLine = ' ' + SourceLine; CaretLine = ' ' + CaretLine; } std::string FixItInsertionLine; - if (NumHints && PrintFixItInfo) { + if (NumHints && DiagOpts->ShowFixits) { for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints; Hint != LastHint; ++Hint) { if (Hint->InsertionLoc.isValid()) { @@ -417,20 +424,20 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, // Emit what we have computed. OS << SourceLine << '\n'; - if (UseColors) + if (DiagOpts->ShowColors) OS.changeColor(caretColor, true); OS << CaretLine << '\n'; - if (UseColors) + if (DiagOpts->ShowColors) OS.resetColor(); if (!FixItInsertionLine.empty()) { - if (UseColors) + if (DiagOpts->ShowColors) // Print fixit line in color OS.changeColor(fixitColor, false); - if (PrintRangeInfo) + if (DiagOpts->ShowSourceRanges) OS << ' '; OS << FixItInsertionLine << '\n'; - if (UseColors) + if (DiagOpts->ShowColors) OS.resetColor(); } } @@ -627,15 +634,15 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, } // Compute the column number. - if (ShowLocation) { - if (UseColors) + if (DiagOpts->ShowLocation) { + if (DiagOpts->ShowColors) OS.changeColor(savedColor, true); OS << PLoc.getFilename() << ':' << LineNo << ':'; - if (ShowColumn) + if (DiagOpts->ShowColumn) if (unsigned ColNo = PLoc.getColumn()) OS << ColNo << ':'; - if (PrintRangeInfo && Info.getNumRanges()) { + if (DiagOpts->ShowSourceRanges && Info.getNumRanges()) { FileID CaretFileID = SM.getFileID(SM.getInstantiationLoc(Info.getLocation())); bool PrintedRange = false; @@ -679,12 +686,12 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, OS << ':'; } OS << ' '; - if (UseColors) + if (DiagOpts->ShowColors) OS.resetColor(); } } - if (UseColors) { + if (DiagOpts->ShowColors) { // Print diagnostic category in bold and color switch (Level) { case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); @@ -703,20 +710,20 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, case Diagnostic::Fatal: OS << "fatal error: "; break; } - if (UseColors) + if (DiagOpts->ShowColors) OS.resetColor(); llvm::SmallString<100> OutStr; Info.FormatDiagnostic(OutStr); - if (PrintDiagnosticOption) + if (DiagOpts->ShowOptionNames) if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) { OutStr += " [-W"; OutStr += Opt; OutStr += ']'; } - if (UseColors) { + if (DiagOpts->ShowColors) { // Print warnings, errors and fatal errors in bold, no color switch (Level) { case Diagnostic::Warning: OS.changeColor(savedColor, true); break; @@ -726,17 +733,17 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, } } - if (MessageLength) { + if (DiagOpts->MessageLength) { // We will be word-wrapping the error message, so compute the // column number where we currently are (after printing the // location information). unsigned Column = OS.tell() - StartOfLocationInfo; - PrintWordWrapped(OS, OutStr, MessageLength, Column); + PrintWordWrapped(OS, OutStr, DiagOpts->MessageLength, Column); } else { OS.write(OutStr.begin(), OutStr.size()); } OS << '\n'; - if (UseColors) + if (DiagOpts->ShowColors) OS.resetColor(); // If caret diagnostics are enabled and we have location, we want to @@ -745,7 +752,7 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, // was part of a different warning or error diagnostic, or if the // diagnostic has ranges. We don't want to emit the same caret // multiple times if one loc has multiple diagnostics. - if (CaretDiagnostics && Info.getLocation().isValid() && + if (DiagOpts->ShowCarets && Info.getLocation().isValid() && ((LastLoc != Info.getLocation()) || Info.getNumRanges() || (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) || Info.getNumCodeModificationHints())) { @@ -772,7 +779,7 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), Info.getCodeModificationHints(), Info.getNumCodeModificationHints(), - MessageLength); + DiagOpts->MessageLength); } OS.flush(); diff --git a/tools/clang-cc/clang-cc.cpp b/tools/clang-cc/clang-cc.cpp index 1a3b830ad5..6b84c73af1 100644 --- a/tools/clang-cc/clang-cc.cpp +++ b/tools/clang-cc/clang-cc.cpp @@ -26,6 +26,7 @@ #include "clang/Frontend/ASTConsumers.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompileOptions.h" +#include "clang/Frontend/DiagnosticOptions.h" #include "clang/Frontend/FixItRewriter.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "clang/Frontend/InitHeaderSearch.h" @@ -238,6 +239,8 @@ TokenCache("token-cache", llvm::cl::value_desc("path"), // Diagnostic Options //===----------------------------------------------------------------------===// +static DiagnosticOptions DiagOpts; + static llvm::cl::opt VerifyDiagnostics("verify", llvm::cl::desc("Verify emitted diagnostics and warnings")); @@ -1566,14 +1569,7 @@ public: // Output diags both where requested... Chain1.reset(Normal); // .. and to our log file. - Chain2.reset(new TextDiagnosticPrinter(*BuildLogFile, - !NoShowColumn, - !NoCaretDiagnostics, - !NoShowLocation, - PrintSourceRangeInfo, - PrintDiagnosticOption, - !NoDiagnosticsFixIt, - MessageLength)); + Chain2.reset(new TextDiagnosticPrinter(*BuildLogFile, DiagOpts)); } virtual void setLangOptions(const LangOptions *LO) { @@ -2157,6 +2153,23 @@ int main(int argc, char **argv) { if (InputFilenames.empty()) InputFilenames.push_back("-"); + // If -fmessage-length=N was not specified, determine whether this + // is a terminal and, if so, implicitly define -fmessage-length + // appropriately. + if (MessageLength.getNumOccurrences() == 0) + MessageLength.setValue(llvm::sys::Process::StandardErrColumns()); + + // Initialize the diagnostic options. + DiagOpts.ShowColumn = !NoShowColumn; + DiagOpts.ShowLocation = !NoShowLocation; + DiagOpts.ShowCarets = !NoCaretDiagnostics; + DiagOpts.ShowFixits = !NoDiagnosticsFixIt; + DiagOpts.ShowSourceRanges = PrintSourceRangeInfo; + DiagOpts.ShowOptionNames = PrintDiagnosticOption; + DiagOpts.ShowColors = (!NoColorDiagnostic && + llvm::sys::Process::StandardErrHasColors()); + DiagOpts.MessageLength = MessageLength; + // Create the diagnostic client for reporting errors or for // implementing -verify. llvm::OwningPtr DiagClient; @@ -2173,26 +2186,7 @@ int main(int argc, char **argv) { } } else if (HTMLDiag.empty()) { // Print diagnostics to stderr by default. - - // If -fmessage-length=N was not specified, determine whether this - // is a terminal and, if so, implicitly define -fmessage-length - // appropriately. - if (MessageLength.getNumOccurrences() == 0) - MessageLength.setValue(llvm::sys::Process::StandardErrColumns()); - - // Disable color diagnostics if not supported on stderr. - if (!NoColorDiagnostic && !llvm::sys::Process::StandardErrHasColors()) - NoColorDiagnostic.setValue(true); - - DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), - !NoShowColumn, - !NoCaretDiagnostics, - !NoShowLocation, - PrintSourceRangeInfo, - PrintDiagnosticOption, - !NoDiagnosticsFixIt, - MessageLength, - !NoColorDiagnostic)); + DiagClient.reset(new TextDiagnosticPrinter(llvm::errs(), DiagOpts)); } else { DiagClient.reset(CreateHTMLDiagnosticClient(HTMLDiag)); }