[cfe-commits] r172071 - in /cfe/trunk/lib/Format: Format.cpp UnwrappedLineParser.cpp UnwrappedLineParser.h

Alexander Kornienko alexfh at google.com
Thu Jan 10 07:05:09 PST 2013


Author: alexfh
Date: Thu Jan 10 09:05:09 2013
New Revision: 172071

URL: http://llvm.org/viewvc/llvm-project?rev=172071&view=rev
Log:
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

Modified:
    cfe/trunk/lib/Format/Format.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/lib/Format/UnwrappedLineParser.h

Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=172071&r1=172070&r2=172071&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Thu Jan 10 09:05:09 2013
@@ -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 <string>
 
@@ -1222,10 +1224,11 @@
 
 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<CharSourceRange> &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 @@
 
   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<UnwrappedLine>::iterator I = UnwrappedLines.begin(),
@@ -1284,6 +1287,7 @@
            1;
   }
 
+  clang::DiagnosticsEngine &Diag;
   FormatStyle Style;
   Lexer &Lex;
   SourceManager &SourceMgr;
@@ -1296,7 +1300,14 @@
 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
                                SourceManager &SourceMgr,
                                std::vector<CharSourceRange> Ranges) {
-  Formatter formatter(Style, Lex, SourceMgr, Ranges);
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
+  DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+  DiagnosticsEngine Diagnostics(
+      llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
+      &DiagnosticPrinter, false);
+  Diagnostics.setSourceManager(&SourceMgr);
+  Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
   return formatter.format();
 }
 

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=172071&r1=172070&r2=172071&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Thu Jan 10 09:05:09 2013
@@ -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 @@
   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 @@
       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();

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.h?rev=172071&r1=172070&r2=172071&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.h (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.h Thu Jan 10 09:05:09 2013
@@ -27,6 +27,9 @@
 #include <vector>
 
 namespace clang {
+
+class DiagnosticsEngine;
+
 namespace format {
 
 /// \brief A wrapper around a \c Token storing information about the
@@ -116,7 +119,8 @@
 
 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 @@
   FormatToken FormatTok;
   bool MustBreakBeforeNextToken;
 
+  clang::DiagnosticsEngine &Diag;
   const FormatStyle &Style;
   FormatTokenSource *Tokens;
   UnwrappedLineConsumer &Callback;





More information about the cfe-commits mailing list