From 5e1db6a434d0e3fe0fbde0bca2ec44552818fb22 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 4 May 2011 01:38:46 +0000 Subject: [PATCH] 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 --- include/clang-c/Index.h | 6 ++++-- include/clang/Lex/PreprocessingRecord.h | 4 ++++ tools/libclang/CIndex.cpp | 24 ++++++++++++++++++++++-- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 2f2876e0fa..011999bfe9 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -1035,12 +1035,14 @@ enum CXTUResourceUsageKind { 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 7be845549d..d1534496b3 100644 --- a/include/clang/Lex/PreprocessingRecord.h +++ b/include/clang/Lex/PreprocessingRecord.h @@ -291,6 +291,10 @@ namespace clang { /// \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::iterator iterator; typedef std::vector::const_iterator const_iterator; diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index e6774dc895..7e697a7c07 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -5246,6 +5246,12 @@ const char *clang_getTUResourceUsageName(CXTUResourceUsageKind kind) { 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 @@ CXTUResourceUsage clang_getCXTUResourceUsage(CXTranslationUnit TU) { 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 clang_getCXTUResourceUsage(CXTranslationUnit TU) { 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 }; -- 2.40.0