[cfe-commits] r152639 - in /cfe/trunk: include/clang/Basic/Diagnostic.h lib/Basic/Diagnostic.cpp

Daniel Dunbar daniel at zuster.org
Tue Mar 13 11:21:17 PDT 2012


Author: ddunbar
Date: Tue Mar 13 13:21:17 2012
New Revision: 152639

URL: http://llvm.org/viewvc/llvm-project?rev=152639&view=rev
Log:
[Basic] Stop using a SmallVector<> for Diagnostic. This drops Clang binary size
by ~%.3/~100k in my build -- simply by eliminating the horrible code bloat coming
from the .clear() of the SmallVector<FixItHint>, which does a std::~string, etc.
 - My understanding is we don't ever emit arbitrary numbers of fixits, so I just
   moved us to using a statically sized array like we do for arguments and
   ranges.

Modified:
    cfe/trunk/include/clang/Basic/Diagnostic.h
    cfe/trunk/lib/Basic/Diagnostic.cpp

Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=152639&r1=152638&r2=152639&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Tue Mar 13 13:21:17 2012
@@ -636,13 +636,22 @@
     /// MaxArguments - The maximum number of arguments we can hold. We currently
     /// only support up to 10 arguments (%0-%9).  A single diagnostic with more
     /// than that almost certainly has to be simplified anyway.
-    MaxArguments = 10
+    MaxArguments = 10,
+
+    /// MaxRanges - The maximum number of ranges we can hold.
+    MaxRanges = 10,
+
+    /// MaxFixItHints - The maximum number of ranges we can hold.
+    MaxFixItHints = 10
   };
 
   /// NumDiagArgs - This contains the number of entries in Arguments.
   signed char NumDiagArgs;
-  /// NumRanges - This is the number of ranges in the DiagRanges array.
+  /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
   unsigned char NumDiagRanges;
+  /// NumDiagFixItHints - This is the number of hints in the DiagFixItHints
+  /// array.
+  unsigned char NumDiagFixItHints;
 
   /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
   /// values, with one for each argument.  This specifies whether the argument
@@ -660,13 +669,12 @@
   /// sort of argument kind it is.
   intptr_t DiagArgumentsVal[MaxArguments];
 
-  /// DiagRanges - The list of ranges added to this diagnostic.  It currently
-  /// only support 10 ranges, could easily be extended if needed.
-  CharSourceRange DiagRanges[10];
-
-  /// FixItHints - If valid, provides a hint with some code
-  /// to insert, remove, or modify at a particular position.
-  SmallVector<FixItHint, 6> FixItHints;
+  /// DiagRanges - The list of ranges added to this diagnostic.
+  CharSourceRange DiagRanges[MaxRanges];
+
+  /// FixItHints - If valid, provides a hint with some code to insert, remove,
+  /// or modify at a particular position.
+  FixItHint DiagFixItHints[MaxFixItHints];
 
   DiagnosticMappingInfo makeMappingInfo(diag::Mapping Map, SourceLocation L) {
     bool isPragma = L.isValid();
@@ -744,13 +752,13 @@
 /// for example.
 class DiagnosticBuilder {
   mutable DiagnosticsEngine *DiagObj;
-  mutable unsigned NumArgs, NumRanges;
+  mutable unsigned NumArgs, NumRanges, NumFixits;
 
   void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT
   friend class DiagnosticsEngine;
   explicit DiagnosticBuilder(DiagnosticsEngine *diagObj)
-    : DiagObj(diagObj), NumArgs(0), NumRanges(0) {
-    DiagObj->FixItHints.clear();
+    : DiagObj(diagObj), NumArgs(0), NumRanges(0), NumFixits(0) {
+    assert(diagObj && "DiagnosticBuilder requires a valid DiagnosticsEngine!");
   }
 
   friend class PartialDiagnostic;
@@ -766,6 +774,7 @@
     D.DiagObj = 0;
     NumArgs = D.NumArgs;
     NumRanges = D.NumRanges;
+    NumFixits = D.NumFixits;
   }
 
   /// \brief Simple enumeration value used to give a name to the
@@ -775,7 +784,7 @@
   /// \brief Create an empty DiagnosticBuilder object that represents
   /// no actual diagnostic.
   explicit DiagnosticBuilder(SuppressKind)
-    : DiagObj(0), NumArgs(0), NumRanges(0) { }
+    : DiagObj(0), NumArgs(0), NumRanges(0), NumFixits(0) { }
 
   /// \brief Force the diagnostic builder to emit the diagnostic now.
   ///
@@ -833,16 +842,17 @@
   }
 
   void AddSourceRange(const CharSourceRange &R) const {
-    assert(NumRanges <
-           sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) &&
+    assert(NumRanges < DiagnosticsEngine::MaxRanges &&
            "Too many arguments to diagnostic!");
     if (DiagObj)
       DiagObj->DiagRanges[NumRanges++] = R;
   }
 
   void AddFixItHint(const FixItHint &Hint) const {
+    assert(NumFixits < DiagnosticsEngine::MaxFixItHints &&
+           "Too many arguments to diagnostic!");
     if (DiagObj)
-      DiagObj->FixItHints.push_back(Hint);
+      DiagObj->DiagFixItHints[NumFixits++] = Hint;
   }
 };
 
@@ -1018,16 +1028,16 @@
   }
 
   unsigned getNumFixItHints() const {
-    return DiagObj->FixItHints.size();
+    return DiagObj->NumDiagFixItHints;
   }
 
   const FixItHint &getFixItHint(unsigned Idx) const {
-    return DiagObj->FixItHints[Idx];
+    assert(Idx < getNumFixItHints() && "Invalid index!");
+    return DiagObj->DiagFixItHints[Idx];
   }
 
   const FixItHint *getFixItHints() const {
-    return getNumFixItHints()?
-             DiagObj->FixItHints.data() : 0;
+    return getNumFixItHints()? DiagObj->DiagFixItHints : 0;
   }
 
   /// FormatDiagnostic - Format this diagnostic into a string, substituting the

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=152639&r1=152638&r2=152639&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Tue Mar 13 13:21:17 2012
@@ -357,7 +357,7 @@
   NumDiagArgs = 0;
 
   NumDiagRanges = storedDiag.range_size();
-  assert(NumDiagRanges < sizeof(DiagRanges)/sizeof(DiagRanges[0]) &&
+  assert(NumDiagRanges < DiagnosticsEngine::MaxRanges &&
          "Too many arguments to diagnostic!");
   unsigned i = 0;
   for (StoredDiagnostic::range_iterator
@@ -365,11 +365,13 @@
          RE = storedDiag.range_end(); RI != RE; ++RI)
     DiagRanges[i++] = *RI;
 
-  FixItHints.clear();
+  assert(NumDiagRanges < DiagnosticsEngine::MaxFixItHints &&
+         "Too many arguments to diagnostic!");
+  NumDiagFixItHints = 0;
   for (StoredDiagnostic::fixit_iterator
          FI = storedDiag.fixit_begin(),
          FE = storedDiag.fixit_end(); FI != FE; ++FI)
-    FixItHints.push_back(*FI);
+    DiagFixItHints[NumDiagFixItHints++] = *FI;
 
   assert(Client && "DiagnosticConsumer not set!");
   Level DiagLevel = storedDiag.getLevel();
@@ -386,6 +388,7 @@
 void DiagnosticBuilder::FlushCounts() {
   DiagObj->NumDiagArgs = NumArgs;
   DiagObj->NumDiagRanges = NumRanges;
+  DiagObj->NumDiagFixItHints = NumFixits;
 }
 
 bool DiagnosticBuilder::Emit() {





More information about the cfe-commits mailing list