From 3054f097f813d19090bdb23645dcd48df71d1a89 Mon Sep 17 00:00:00 2001
From: Daniel Dunbar <daniel@zuster.org>
Date: Tue, 13 Mar 2012 21:02:14 +0000
Subject: [PATCH] [Basic] Fix up DiagnosticBuilder::{FlushCounts,Emit} to be
 inline.

 - This is much more important than it appears at first glance...

The intended design of DiagnosticBuilder was that it never escape and that all
its members would get lowered to registers by the compiler. By fixing Emit here,
the compiler can completely eliminate the DiagnosticBuilder object and never
need to push those registers back into it.

Unfortunately, Sema has broken DiagnosticBuilder in other ways (by introducing
SemaDiagnosticBuilder), so we don't get the fill impact of this, but it is still
good for 30k reduction in code size. I'll work on fixing the
SemaDiagnosticBuilder problems next.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@152669 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/clang/Basic/Diagnostic.h | 31 +++++++++++++++++++++++++++----
 lib/Basic/Diagnostic.cpp         | 29 ++++++-----------------------
 2 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/include/clang/Basic/Diagnostic.h b/include/clang/Basic/Diagnostic.h
index c23a3f6456f..c68826c38d5 100644
--- a/include/clang/Basic/Diagnostic.h
+++ b/include/clang/Basic/Diagnostic.h
@@ -700,6 +700,9 @@ private:
     return Diags->ProcessDiag(*this);
   }
 
+  /// \brief Emit the current diagnostic and clear the diagnostic state.
+  bool EmitCurrentDiagnostic();
+
   friend class ASTReader;
   friend class ASTWriter;
 };
@@ -764,7 +767,11 @@ class DiagnosticBuilder {
   friend class PartialDiagnostic;
 
 protected:
-  void FlushCounts();
+  void FlushCounts() {
+    DiagObj->NumDiagArgs = NumArgs;
+    DiagObj->NumDiagRanges = NumRanges;
+    DiagObj->NumDiagFixItHints = NumFixits;
+  }
 
   /// \brief Clear out the current diagnostic.
   void Clear() { DiagObj = 0; }
@@ -792,7 +799,24 @@ protected:
   ///
   /// \returns true if a diagnostic was emitted, false if the
   /// diagnostic was suppressed.
-  bool Emit();
+  bool Emit() {
+    // If DiagObj is null, then its soul was stolen by the copy ctor
+    // or the user called Emit().
+    if (DiagObj == 0) return false;
+
+    // When emitting diagnostics, we set the final argument count into
+    // the DiagnosticsEngine object.
+    FlushCounts();
+
+    // Process the diagnostic.
+    bool Result = DiagObj->EmitCurrentDiagnostic();
+
+    // This diagnostic is dead.
+    DiagObj = 0;
+
+    return Result;
+  }
+
   
 public:
   /// Copy constructor.  When copied, this "takes" the diagnostic info from the
@@ -808,8 +832,7 @@ public:
   /// Destructor - The dtor emits the diagnostic if it hasn't already
   /// been emitted.
   ~DiagnosticBuilder() {
-    if (DiagObj)
-      Emit();
+    Emit();
   }
   
   /// Operator bool: conversion of DiagnosticBuilder to bool always returns
diff --git a/lib/Basic/Diagnostic.cpp b/lib/Basic/Diagnostic.cpp
index a36f3ce38a1..f7d5d87b367 100644
--- a/lib/Basic/Diagnostic.cpp
+++ b/lib/Basic/Diagnostic.cpp
@@ -385,35 +385,18 @@ void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
   CurDiagID = ~0U;
 }
 
-void DiagnosticBuilder::FlushCounts() {
-  DiagObj->NumDiagArgs = NumArgs;
-  DiagObj->NumDiagRanges = NumRanges;
-  DiagObj->NumDiagFixItHints = NumFixits;
-}
-
-bool DiagnosticBuilder::Emit() {
-  // If DiagObj is null, then its soul was stolen by the copy ctor
-  // or the user called Emit().
-  if (DiagObj == 0) return false;
-
-  // When emitting diagnostics, we set the final argument count into
-  // the DiagnosticsEngine object.
-  FlushCounts();
-
+bool DiagnosticsEngine::EmitCurrentDiagnostic() {
   // Process the diagnostic, sending the accumulated information to the
   // DiagnosticConsumer.
-  bool Emitted = DiagObj->ProcessDiag();
+  bool Emitted = ProcessDiag();
 
   // Clear out the current diagnostic object.
-  unsigned DiagID = DiagObj->CurDiagID;
-  DiagObj->Clear();
+  unsigned DiagID = CurDiagID;
+  Clear();
 
   // If there was a delayed diagnostic, emit it now.
-  if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID)
-    DiagObj->ReportDelayed();
-
-  // This diagnostic is dead.
-  DiagObj = 0;
+  if (DelayedDiagID && DelayedDiagID != DiagID)
+    ReportDelayed();
 
   return Emitted;
 }
-- 
GitLab