}
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;
#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"
/// \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;
// 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
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 << ' ';
}
// 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()) {
// 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();
}
}
}
// 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;
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");
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;
}
}
- 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
// 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())) {
EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(),
Info.getCodeModificationHints(),
Info.getNumCodeModificationHints(),
- MessageLength);
+ DiagOpts->MessageLength);
}
OS.flush();
#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"
// Diagnostic Options
//===----------------------------------------------------------------------===//
+static DiagnosticOptions DiagOpts;
+
static llvm::cl::opt<bool>
VerifyDiagnostics("verify",
llvm::cl::desc("Verify emitted diagnostics and warnings"));
// 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) {
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;
}
} 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));
}