Enhance clang_getCXTUResourceUsage() to return the amount of memory used by the Preprocessor's bump allocator as well as those from the PreprocessingRecord.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@130823 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
index 2f2876e..011999b 100644
--- a/include/clang-c/Index.h
+++ b/include/clang-c/Index.h
@@ -1035,12 +1035,14 @@
   CXTUResourceUsage_SourceManager_Membuffer_MMap = 8,
   CXTUResourceUsage_ExternalASTSource_Membuffer_Malloc = 9, 
   CXTUResourceUsage_ExternalASTSource_Membuffer_MMap = 10, 
+  CXTUResourceUsage_Preprocessor = 11,
+  CXTUResourceUsage_PreprocessingRecord = 12,
   CXTUResourceUsage_MEMORY_IN_BYTES_BEGIN = CXTUResourceUsage_AST,
   CXTUResourceUsage_MEMORY_IN_BYTES_END =
-    CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
+    CXTUResourceUsage_PreprocessingRecord,
 
   CXTUResourceUsage_First = CXTUResourceUsage_AST,
-  CXTUResourceUsage_Last = CXTUResourceUsage_ExternalASTSource_Membuffer_MMap
+  CXTUResourceUsage_Last = CXTUResourceUsage_PreprocessingRecord
 };
 
 /**
diff --git a/include/clang/Lex/PreprocessingRecord.h b/include/clang/Lex/PreprocessingRecord.h
index 7be8455..d153449 100644
--- a/include/clang/Lex/PreprocessingRecord.h
+++ b/include/clang/Lex/PreprocessingRecord.h
@@ -291,6 +291,10 @@
     /// \brief Deallocate memory in the preprocessing record.
     void Deallocate(void *Ptr) { }
     
+    size_t getTotalMemory() const {
+      return BumpAlloc.getTotalMemory();
+    }
+    
     // Iteration over the preprocessed entities.
     typedef std::vector<PreprocessedEntity *>::iterator iterator;
     typedef std::vector<PreprocessedEntity *>::const_iterator const_iterator;
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp
index e6774dc..7e697a7 100644
--- a/tools/libclang/CIndex.cpp
+++ b/tools/libclang/CIndex.cpp
@@ -5246,6 +5246,12 @@
     case CXTUResourceUsage_ExternalASTSource_Membuffer_MMap:
       str = "ExternalASTSource: mmap'ed memory buffers";
       break;
+    case CXTUResourceUsage_Preprocessor:
+      str = "Preprocessor: malloc'ed memory";
+      break;
+    case CXTUResourceUsage_PreprocessingRecord:
+      str = "Preprocessor: PreprocessingRecord";
+      break;
   }
   return str;
 }
@@ -5280,7 +5286,7 @@
   unsigned long completionBytes = 0;
   if (GlobalCodeCompletionAllocator *completionAllocator =
       astUnit->getCachedCompletionAllocator().getPtr()) {
-    completionBytes = completionAllocator-> getTotalMemory();
+    completionBytes = completionAllocator->getTotalMemory();
   }
   createCXTUResourceUsageEntry(*entries,
                                CXTUResourceUsage_GlobalCompletionResults,
@@ -5314,7 +5320,21 @@
       CXTUResourceUsage_ExternalASTSource_Membuffer_MMap,
                                  (unsigned long) sizes.mmap_bytes);
   }
-
+  
+  // How much memory is being used by the Preprocessor?
+  Preprocessor &pp = astUnit->getPreprocessor();
+  const llvm::BumpPtrAllocator &ppAlloc = pp.getPreprocessorAllocator();
+  createCXTUResourceUsageEntry(*entries,
+                               CXTUResourceUsage_Preprocessor,
+                               ppAlloc.getTotalMemory());
+  
+  if (PreprocessingRecord *pRec = pp.getPreprocessingRecord()) {
+    createCXTUResourceUsageEntry(*entries,
+                                 CXTUResourceUsage_PreprocessingRecord,
+                                 pRec->getTotalMemory());    
+  }
+  
+  
   CXTUResourceUsage usage = { (void*) entries.get(),
                             (unsigned) entries->size(),
                             entries->size() ? &(*entries)[0] : 0 };