Custom DiagnosticConsumer parameter of reformat() + silence diagnostics in unit tests.

Summary:
Added tests for clang-format diagnostics. Added DiagnosticConsumer
argument to clang::format::reformat().

Reviewers: klimek, djasper

Reviewed By: djasper

CC: cfe-commits, thakis, rafael.espindola

Differential Revision: http://llvm-reviews.chandlerc.com/D290

llvm-svn: 172399
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 67a5b8c..548bdee 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1331,7 +1331,7 @@
 
 class Formatter : public UnwrappedLineConsumer {
 public:
-  Formatter(clang::DiagnosticsEngine &Diag, const FormatStyle &Style,
+  Formatter(DiagnosticsEngine &Diag, const FormatStyle &Style,
             Lexer &Lex, SourceManager &SourceMgr,
             const std::vector<CharSourceRange> &Ranges)
       : Diag(Diag), Style(Style), Lex(Lex), SourceMgr(SourceMgr),
@@ -1517,7 +1517,7 @@
     return Indent;
   }
 
-  clang::DiagnosticsEngine &Diag;
+  DiagnosticsEngine &Diag;
   FormatStyle Style;
   Lexer &Lex;
   SourceManager &SourceMgr;
@@ -1529,13 +1529,18 @@
 
 tooling::Replacements reformat(const FormatStyle &Style, Lexer &Lex,
                                SourceManager &SourceMgr,
-                               std::vector<CharSourceRange> Ranges) {
+                               std::vector<CharSourceRange> Ranges,
+                               DiagnosticConsumer *DiagClient) {
   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
-  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
-  DiagnosticPrinter.BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+  OwningPtr<DiagnosticConsumer> DiagPrinter;
+  if (DiagClient == 0) {
+    DiagPrinter.reset(new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts));
+    DiagPrinter->BeginSourceFile(Lex.getLangOpts(), Lex.getPP());
+    DiagClient = DiagPrinter.get();
+  }
   DiagnosticsEngine Diagnostics(
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
-      &DiagnosticPrinter, false);
+      DiagClient, false);
   Diagnostics.setSourceManager(&SourceMgr);
   Formatter formatter(Diagnostics, Style, Lex, SourceMgr, Ranges);
   return formatter.format();