[cfe-commits] r86010 - in /cfe/trunk: include/clang/Frontend/DiagnosticOptions.h include/clang/Frontend/TextDiagnosticPrinter.h lib/Frontend/TextDiagnosticPrinter.cpp tools/clang-cc/clang-cc.cpp

Daniel Dunbar daniel at zuster.org
Tue Nov 3 22:24:30 PST 2009


Author: ddunbar
Date: Wed Nov  4 00:24:30 2009
New Revision: 86010

URL: http://llvm.org/viewvc/llvm-project?rev=86010&view=rev
Log:
Factor out a diagnostic options class.

Added:
    cfe/trunk/include/clang/Frontend/DiagnosticOptions.h
Modified:
    cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
    cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
    cfe/trunk/tools/clang-cc/clang-cc.cpp

Added: cfe/trunk/include/clang/Frontend/DiagnosticOptions.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/DiagnosticOptions.h?rev=86010&view=auto

==============================================================================
--- cfe/trunk/include/clang/Frontend/DiagnosticOptions.h (added)
+++ cfe/trunk/include/clang/Frontend/DiagnosticOptions.h Wed Nov  4 00:24:30 2009
@@ -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 <string>
+#include <vector>
+
+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

Modified: cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h?rev=86010&r1=86009&r2=86010&view=diff

==============================================================================
--- cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h (original)
+++ cfe/trunk/include/clang/Frontend/TextDiagnosticPrinter.h Wed Nov  4 00:24:30 2009
@@ -23,42 +23,21 @@
 }
 
 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;

Modified: cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp?rev=86010&r1=86009&r2=86010&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp (original)
+++ cfe/trunk/lib/Frontend/TextDiagnosticPrinter.cpp Wed Nov  4 00:24:30 2009
@@ -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 @@
 /// \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 @@
   // 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 @@
       Ranges[i] = SourceRange(S, E);
     }
 
-    if (ShowLocation) {
+    if (DiagOpts->ShowLocation) {
       std::pair<FileID, unsigned> 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 @@
   // 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 @@
   // 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 @@
     }
 
     // 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 @@
           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 @@
   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 @@
     }
   }
 
-  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 @@
   // 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 @@
     EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
                         Info.getCodeModificationHints(),
                         Info.getNumCodeModificationHints(),
-                        MessageLength);
+                        DiagOpts->MessageLength);
   }
 
   OS.flush();

Modified: cfe/trunk/tools/clang-cc/clang-cc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-cc/clang-cc.cpp?rev=86010&r1=86009&r2=86010&view=diff

==============================================================================
--- cfe/trunk/tools/clang-cc/clang-cc.cpp (original)
+++ cfe/trunk/tools/clang-cc/clang-cc.cpp Wed Nov  4 00:24:30 2009
@@ -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 @@
 // Diagnostic Options
 //===----------------------------------------------------------------------===//
 
+static DiagnosticOptions DiagOpts;
+
 static llvm::cl::opt<bool>
 VerifyDiagnostics("verify",
                   llvm::cl::desc("Verify emitted diagnostics and warnings"));
@@ -1566,14 +1569,7 @@
     // 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 @@
   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<DiagnosticClient> DiagClient;
@@ -2173,26 +2186,7 @@
     }
   } 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));
   }





More information about the cfe-commits mailing list