[cfe-commits] r149780 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/Basic/Diagnostic.h include/clang/Basic/PartialDiagnostic.h lib/AST/ASTContext.cpp lib/Basic/Diagnostic.cpp

Benjamin Kramer benny.kra at googlemail.com
Sat Feb 4 04:30:46 PST 2012


Author: d0k
Date: Sat Feb  4 06:30:46 2012
New Revision: 149780

URL: http://llvm.org/viewvc/llvm-project?rev=149780&view=rev
Log:
Move Storage and StorageAllocator out of the PartialDiagnostic class so we can forward declare them.

Let ASTContext allocate the storage in its BumpPtrAllocator.
This will help us remove ASTContext's depedency on PartialDiagnostic.h soon.

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

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=149780&r1=149779&r2=149780&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Sat Feb  4 06:30:46 2012
@@ -49,6 +49,7 @@
   class ExternalASTSource;
   class ASTMutationListener;
   class IdentifierTable;
+  class PartialDiagnosticStorageAllocator;
   class SelectorTable;
   class SourceManager;
   class TargetInfo;
@@ -346,7 +347,7 @@
   mutable llvm::BumpPtrAllocator BumpAlloc;
 
   /// \brief Allocator for partial diagnostics.
-  PartialDiagnostic::StorageAllocator DiagAllocator;
+  PartialDiagnosticStorageAllocator *DiagAllocator;
 
   /// \brief The current C++ ABI.
   llvm::OwningPtr<CXXABI> ABI;
@@ -391,8 +392,8 @@
   /// Return the total memory used for various side tables.
   size_t getSideTableAllocatedMemory() const;
   
-  PartialDiagnostic::StorageAllocator &getDiagAllocator() {
-    return DiagAllocator;
+  PartialDiagnosticStorageAllocator &getDiagAllocator() {
+    return *DiagAllocator;
   }
 
   const TargetInfo &getTargetInfo() const { return *Target; }

Modified: cfe/trunk/include/clang/Basic/Diagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Diagnostic.h?rev=149780&r1=149779&r2=149780&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/Diagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/Diagnostic.h Sat Feb  4 06:30:46 2012
@@ -593,6 +593,7 @@
   friend class DiagnosticBuilder;
   friend class Diagnostic;
   friend class PartialDiagnostic;
+  friend struct PartialDiagnosticStorage;
   friend class DiagnosticErrorTrap;
   
   /// CurDiagLoc - This is the location of the current diagnostic that is in

Modified: cfe/trunk/include/clang/Basic/PartialDiagnostic.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/PartialDiagnostic.h?rev=149780&r1=149779&r2=149780&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/PartialDiagnostic.h (original)
+++ cfe/trunk/include/clang/Basic/PartialDiagnostic.h Sat Feb  4 06:30:46 2012
@@ -23,91 +23,87 @@
 
 namespace clang {
 
-class PartialDiagnostic {
-public:
+struct PartialDiagnosticStorage {
+  PartialDiagnosticStorage() : NumDiagArgs(0), NumDiagRanges(0) { }
+
   enum {
-      // The MaxArguments and MaxFixItHints member enum values from
-      // DiagnosticsEngine are private but DiagnosticsEngine declares
-      // PartialDiagnostic a friend.  These enum values are redeclared
-      // here so that the nested Storage class below can access them.
+      /// 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 = DiagnosticsEngine::MaxArguments
   };
 
-  struct Storage {
-    Storage() : NumDiagArgs(0), NumDiagRanges(0) { }
+  /// NumDiagArgs - This contains the number of entries in Arguments.
+  unsigned char NumDiagArgs;
 
-    enum {
-        /// 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 = PartialDiagnostic::MaxArguments
-    };
-
-    /// NumDiagArgs - This contains the number of entries in Arguments.
-    unsigned char NumDiagArgs;
-
-    /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
-    unsigned char NumDiagRanges;
-
-    /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
-    /// values, with one for each argument.  This specifies whether the argument
-    /// is in DiagArgumentsStr or in DiagArguments.
-    unsigned char DiagArgumentsKind[MaxArguments];
-
-    /// DiagArgumentsVal - The values for the various substitution positions.
-    /// This is used when the argument is not an std::string. The specific value
-    /// is mangled into an intptr_t and the interpretation depends on exactly
-    /// what sort of argument kind it is.
-    intptr_t DiagArgumentsVal[MaxArguments];
-
-    /// \brief The values for the various substitution positions that have
-    /// string arguments.
-    std::string DiagArgumentsStr[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;
-  };
+  /// NumDiagRanges - This is the number of ranges in the DiagRanges array.
+  unsigned char NumDiagRanges;
 
-  /// \brief An allocator for Storage objects, which uses a small cache to
-  /// objects, used to reduce malloc()/free() traffic for partial diagnostics.
-  class StorageAllocator {
-    static const unsigned NumCached = 16;
-    Storage Cached[NumCached];
-    Storage *FreeList[NumCached];
-    unsigned NumFreeListEntries;
-
-  public:
-    StorageAllocator();
-    ~StorageAllocator();
-
-    /// \brief Allocate new storage.
-    Storage *Allocate() {
-      if (NumFreeListEntries == 0)
-        return new Storage;
-
-      Storage *Result = FreeList[--NumFreeListEntries];
-      Result->NumDiagArgs = 0;
-      Result->NumDiagRanges = 0;
-      Result->FixItHints.clear();
-      return Result;
-    }
+  /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
+  /// values, with one for each argument.  This specifies whether the argument
+  /// is in DiagArgumentsStr or in DiagArguments.
+  unsigned char DiagArgumentsKind[MaxArguments];
+
+  /// DiagArgumentsVal - The values for the various substitution positions.
+  /// This is used when the argument is not an std::string. The specific value
+  /// is mangled into an intptr_t and the interpretation depends on exactly
+  /// what sort of argument kind it is.
+  intptr_t DiagArgumentsVal[MaxArguments];
+
+  /// \brief The values for the various substitution positions that have
+  /// string arguments.
+  std::string DiagArgumentsStr[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;
+};
 
-    /// \brief Free the given storage object.
-    void Deallocate(Storage *S) {
-      if (S >= Cached && S <= Cached + NumCached) {
-        FreeList[NumFreeListEntries++] = S;
-        return;
-      }
+/// \brief An allocator for Storage objects, which uses a small cache to
+/// objects, used to reduce malloc()/free() traffic for partial diagnostics.
+class PartialDiagnosticStorageAllocator {
+  static const unsigned NumCached = 16;
+  typedef PartialDiagnosticStorage Storage;
+  Storage Cached[NumCached];
+  Storage *FreeList[NumCached];
+  unsigned NumFreeListEntries;
+
+public:
+  PartialDiagnosticStorageAllocator();
+  ~PartialDiagnosticStorageAllocator();
 
-      delete S;
+  /// \brief Allocate new storage.
+  Storage *Allocate() {
+    if (NumFreeListEntries == 0)
+      return new Storage;
+
+    Storage *Result = FreeList[--NumFreeListEntries];
+    Result->NumDiagArgs = 0;
+    Result->NumDiagRanges = 0;
+    Result->FixItHints.clear();
+    return Result;
+  }
+
+  /// \brief Free the given storage object.
+  void Deallocate(Storage *S) {
+    if (S >= Cached && S <= Cached + NumCached) {
+      FreeList[NumFreeListEntries++] = S;
+      return;
     }
-  };
+
+    delete S;
+  }
+};
+
+class PartialDiagnostic {
+public:
+  typedef PartialDiagnosticStorage Storage;
+  typedef PartialDiagnosticStorageAllocator StorageAllocator;
 
 private:
   // NOTE: Sema assumes that PartialDiagnostic is location-invariant

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=149780&r1=149779&r2=149780&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Sat Feb  4 06:30:46 2012
@@ -241,6 +241,9 @@
     LastSDM(0, 0),
     UniqueBlockByRefTypeID(0) 
 {
+  // Create a new allocator for partial diagnostics.
+  DiagAllocator = new (BumpAlloc) PartialDiagnosticStorageAllocator;
+
   if (size_reserve > 0) Types.reserve(size_reserve);
   TUDecl = TranslationUnitDecl::Create(*this);
   
@@ -285,6 +288,9 @@
                                                     AEnd = DeclAttrs.end();
        A != AEnd; ++A)
     A->second->~AttrVec();
+
+  // Destroy the partial diagnostic allocator.
+  DiagAllocator->~PartialDiagnosticStorageAllocator();
 }
 
 void ASTContext::AddDeallocation(void (*Callback)(void*), void *Data) {

Modified: cfe/trunk/lib/Basic/Diagnostic.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Basic/Diagnostic.cpp?rev=149780&r1=149779&r2=149780&view=diff
==============================================================================
--- cfe/trunk/lib/Basic/Diagnostic.cpp (original)
+++ cfe/trunk/lib/Basic/Diagnostic.cpp Sat Feb  4 06:30:46 2012
@@ -842,13 +842,13 @@
 
 void IgnoringDiagConsumer::anchor() { }
 
-PartialDiagnostic::StorageAllocator::StorageAllocator() {
+PartialDiagnosticStorageAllocator::PartialDiagnosticStorageAllocator() {
   for (unsigned I = 0; I != NumCached; ++I)
     FreeList[I] = Cached + I;
   NumFreeListEntries = NumCached;
 }
 
-PartialDiagnostic::StorageAllocator::~StorageAllocator() {
+PartialDiagnosticStorageAllocator::~PartialDiagnosticStorageAllocator() {
   // Don't assert if we are in a CrashRecovery context, as this
   // invariant may be invalidated during a crash.
   assert((NumFreeListEntries == NumCached || llvm::CrashRecoveryContext::isRecoveringFromCrash()) && "A partial is on the lamb");





More information about the cfe-commits mailing list