Since multiple diagnostics can share one diagnostic client, have the client keeping track
of the total number of warnings/errors reported.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@119731 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h
index 3eab3c0..c3eaea1 100644
--- a/include/clang/Basic/Diagnostic.h
+++ b/include/clang/Basic/Diagnostic.h
@@ -894,7 +894,15 @@
 /// DiagnosticClient - This is an abstract interface implemented by clients of
 /// the front-end, which formats and prints fully processed diagnostics.
 class DiagnosticClient {
+  unsigned NumWarnings;       // Number of warnings reported
+  unsigned NumErrors;         // Number of errors reported
+  
 public:
+  DiagnosticClient() : NumWarnings(0), NumErrors(0) { }
+
+  unsigned getNumErrors() const { return NumErrors; }
+  unsigned getNumWarnings() const { return NumWarnings; }
+
   virtual ~DiagnosticClient();
 
   /// BeginSourceFile - Callback to inform the diagnostic client that processing
@@ -924,8 +932,11 @@
 
   /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
   /// capturing it to a log as needed.
+  ///
+  /// Default implementation just keeps track of the total number of warnings
+  /// and errors.
   virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
-                                const DiagnosticInfo &Info) = 0;
+                                const DiagnosticInfo &Info);
 };
 
 }  // end namespace clang
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index a26f497..858b70a 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -145,6 +145,16 @@
 
 DiagnosticClient::~DiagnosticClient() {}
 
+void DiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
+                                        const DiagnosticInfo &Info) {
+  if (!IncludeInDiagnosticCounts())
+    return;
+
+  if (DiagLevel == Diagnostic::Warning)
+    ++NumWarnings;
+  else if (DiagLevel >= Diagnostic::Error)
+    ++NumErrors;
+}
 
 /// ModifierIs - Return true if the specified modifier matches specified string.
 template <std::size_t StrLen>
diff --git a/lib/Checker/PathDiagnostic.cpp b/lib/Checker/PathDiagnostic.cpp
index 1ddc08e..0f0dddc 100644
--- a/lib/Checker/PathDiagnostic.cpp
+++ b/lib/Checker/PathDiagnostic.cpp
@@ -84,6 +84,8 @@
 
 void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
                                             const DiagnosticInfo &Info) {
+  // Default implementation (Warnings/errors count).
+  DiagnosticClient::HandleDiagnostic(DiagLevel, Info);
 
   // Create a PathDiagnostic with a single piece.
 
diff --git a/lib/Frontend/ASTMerge.cpp b/lib/Frontend/ASTMerge.cpp
index 7c8763d..d4ed8d3 100644
--- a/lib/Frontend/ASTMerge.cpp
+++ b/lib/Frontend/ASTMerge.cpp
@@ -69,15 +69,6 @@
       Importer.Import(*D);
     }
 
-    // Aggregate the number of warnings/errors from all diagnostics so
-    // that at CompilerInstance::ExecuteAction we can report the total numbers.
-    // FIXME: This is hacky, maybe keep track of total number of warnings/errors
-    // in DiagnosticClient and have CompilerInstance query that ?
-    CI.getDiagnostics().setNumWarnings(CI.getDiagnostics().getNumWarnings() +
-                                       Diags->getNumWarnings());
-    CI.getDiagnostics().setNumErrors(CI.getDiagnostics().getNumErrors() +
-                                     Diags->getNumErrors());
-
     delete Unit;
   }
 
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index fb93fca..3956cc2 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -440,6 +440,9 @@
 
 void StoredDiagnosticClient::HandleDiagnostic(Diagnostic::Level Level,
                                               const DiagnosticInfo &Info) {
+  // Default implementation (Warnings/errors count).
+  DiagnosticClient::HandleDiagnostic(Level, Info);
+
   StoredDiags.push_back(StoredDiagnostic(Level, Info));
 }
 
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index dafbef1..152dd6d 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/lib/Frontend/CompilerInstance.cpp
@@ -553,9 +553,10 @@
   }
 
   if (getDiagnosticOpts().ShowCarets) {
-    unsigned NumWarnings = getDiagnostics().getNumWarnings();
-    unsigned NumErrors = getDiagnostics().getNumErrors() - 
-                               getDiagnostics().getNumErrorsSuppressed();
+    // We can have multiple diagnostics sharing one diagnostic client.
+    // Get the total number of warnings/errors from the client.
+    unsigned NumWarnings = getDiagnostics().getClient()->getNumWarnings();
+    unsigned NumErrors = getDiagnostics().getClient()->getNumErrors();
     
     if (NumWarnings)
       OS << NumWarnings << " warning" << (NumWarnings == 1 ? "" : "s");
diff --git a/lib/Frontend/TextDiagnosticBuffer.cpp b/lib/Frontend/TextDiagnosticBuffer.cpp
index fdf2ec8..069c86d 100644
--- a/lib/Frontend/TextDiagnosticBuffer.cpp
+++ b/lib/Frontend/TextDiagnosticBuffer.cpp
@@ -20,6 +20,9 @@
 ///
 void TextDiagnosticBuffer::HandleDiagnostic(Diagnostic::Level Level,
                                             const DiagnosticInfo &Info) {
+  // Default implementation (Warnings/errors count).
+  DiagnosticClient::HandleDiagnostic(Level, Info);
+
   llvm::SmallString<100> Buf;
   Info.FormatDiagnostic(Buf);
   switch (Level) {
diff --git a/lib/Frontend/TextDiagnosticPrinter.cpp b/lib/Frontend/TextDiagnosticPrinter.cpp
index 2528777..200054a 100644
--- a/lib/Frontend/TextDiagnosticPrinter.cpp
+++ b/lib/Frontend/TextDiagnosticPrinter.cpp
@@ -764,6 +764,9 @@
 
 void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
                                              const DiagnosticInfo &Info) {
+  // Default implementation (Warnings/errors count).
+  DiagnosticClient::HandleDiagnostic(Level, Info);
+
   // Keeps track of the the starting position of the location
   // information (e.g., "foo.c:10:4:") that precedes the error
   // message. We use this information to determine how long the