From 3048aeae0654b34dcae561494c1b28872c88a5c8 Mon Sep 17 00:00:00 2001 From: Alexander Kornienko Date: Thu, 10 Jan 2013 15:05:09 +0000 Subject: [PATCH] Basic support for diagnostics. Summary: Uses DiagnosticsEngine to output diagnostics. Reviewers: djasper, klimek Reviewed By: djasper CC: cfe-commits Differential Revision: http://llvm-reviews.chandlerc.com/D278 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172071 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 21 ++++++++++++++++----- lib/Format/UnwrappedLineParser.cpp | 15 +++++++++------ lib/Format/UnwrappedLineParser.h | 7 ++++++- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index f64e51f2d7..09874bbd77 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -18,8 +18,10 @@ #include "clang/Format/Format.h" #include "UnwrappedLineParser.h" +#include "clang/Basic/Diagnostic.h" #include "clang/Basic/OperatorPrecedence.h" #include "clang/Basic/SourceManager.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Lex/Lexer.h" #include @@ -1222,10 +1224,11 @@ private: class Formatter : public UnwrappedLineConsumer { public: - Formatter(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, + Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style, + Lexer &Lex, SourceManager &SourceMgr, const std::vector &Ranges) - : Style(Style), Lex(Lex), SourceMgr(SourceMgr), Ranges(Ranges), - StructuralError(false) { + : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr), + Ranges(Ranges), StructuralError(false) { } virtual ~Formatter() { @@ -1233,7 +1236,7 @@ public: tooling::Replacements format() { LexerBasedFormatTokenSource Tokens(Lex, SourceMgr); - UnwrappedLineParser Parser(Style, Tokens, *this); + UnwrappedLineParser Parser(Diag, Style, Tokens, *this); StructuralError = Parser.parse(); unsigned PreviousEndOfLineColumn = 0; for (std::vector::iterator I = UnwrappedLines.begin(), @@ -1284,6 +1287,7 @@ private: 1; } + clang::DiagnosticsEngine &Diag; FormatStyle Style; Lexer &Lex; SourceManager &SourceMgr; @@ -1296,7 +1300,14 @@ private: tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex, SourceManager &SourceMgr, std::vector Ranges) { - Formatter formatter(Style, Lex, SourceMgr, Ranges); + IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); + TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts); + DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP()); + DiagnosticsEngine Diagnostics( + llvm::IntrusiveRefCntPtr(new DiagnosticIDs()), &*DiagOpts, + &DiagnosticPrinter, false); + Diagnostics.setSourceManager(&SourceMgr); + Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges); return formatter.format(); } diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index e9c6211313..a6c5c165ff 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -17,6 +17,7 @@ //===----------------------------------------------------------------------===// #include "UnwrappedLineParser.h" +#include "clang/Basic/Diagnostic.h" #include "llvm/Support/raw_ostream.h" // Uncomment to get debug output from the UnwrappedLineParser. @@ -110,12 +111,12 @@ private: bool PreBlockRootTokenInitialized; }; -UnwrappedLineParser::UnwrappedLineParser(const FormatStyle &Style, - FormatTokenSource &Tokens, - UnwrappedLineConsumer &Callback) +UnwrappedLineParser::UnwrappedLineParser( + clang::DiagnosticsEngine &Diag, const FormatStyle &Style, + FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback) : Line(new UnwrappedLine), RootTokenInitialized(false), - LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Style(Style), - Tokens(&Tokens), Callback(Callback) { + LastInCurrentLine(NULL), MustBreakBeforeNextToken(false), Diag(Diag), + Style(Style), Tokens(&Tokens), Callback(Callback) { } bool UnwrappedLineParser::parse() { @@ -149,7 +150,9 @@ bool UnwrappedLineParser::parseLevel(bool HasOpeningBrace) { if (HasOpeningBrace) { return false; } else { - // Stray '}' is an error. + Diag.Report(FormatTok.Tok.getLocation(), + Diag.getCustomDiagID(clang::DiagnosticsEngine::Error, + "Stray '}' found")); Error = true; nextToken(); addUnwrappedLine(); diff --git a/lib/Format/UnwrappedLineParser.h b/lib/Format/UnwrappedLineParser.h index 017daf587f..837b5391f6 100644 --- a/lib/Format/UnwrappedLineParser.h +++ b/lib/Format/UnwrappedLineParser.h @@ -27,6 +27,9 @@ #include namespace clang { + +class DiagnosticsEngine; + namespace format { /// \brief A wrapper around a \c Token storing information about the @@ -116,7 +119,8 @@ public: class UnwrappedLineParser { public: - UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens, + UnwrappedLineParser(clang::DiagnosticsEngine &Diag, const FormatStyle &Style, + FormatTokenSource &Tokens, UnwrappedLineConsumer &Callback); /// Returns true in case of a structural error. @@ -161,6 +165,7 @@ private: FormatToken FormatTok; bool MustBreakBeforeNextToken; + clang::DiagnosticsEngine &Diag; const FormatStyle &Style; FormatTokenSource *Tokens; UnwrappedLineConsumer &Callback; -- 2.40.0