@@ -64,8 +64,9 @@ bool compileCommandsAreEqual(const tooling::CompileCommand &LHS,
6464
6565class CppFilePreambleCallbacks : public PreambleCallbacks {
6666public:
67- CppFilePreambleCallbacks (PathRef File, PreambleParsedCallback ParsedCallback)
68- : File(File), ParsedCallback(ParsedCallback) {}
67+ CppFilePreambleCallbacks (PathRef File, PreambleParsedCallback ParsedCallback,
68+ PreambleBuildStats *Stats)
69+ : File(File), ParsedCallback(ParsedCallback), Stats(Stats) {}
6970
7071 IncludeStructure takeIncludes () { return std::move (Includes); }
7172
@@ -88,6 +89,25 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
8889 IsMainFileIncludeGuarded =
8990 CI.getPreprocessor ().getHeaderSearchInfo ().isFileMultipleIncludeGuarded (
9091 MainFE);
92+
93+ if (Stats) {
94+ const ASTContext &AST = CI.getASTContext ();
95+ Stats->BuildSize = AST.getASTAllocatedMemory ();
96+ Stats->BuildSize += AST.getSideTableAllocatedMemory ();
97+ Stats->BuildSize += AST.Idents .getAllocator ().getTotalMemory ();
98+ Stats->BuildSize += AST.Selectors .getTotalMemory ();
99+
100+ Stats->BuildSize += AST.getSourceManager ().getContentCacheSize ();
101+ Stats->BuildSize += AST.getSourceManager ().getDataStructureSizes ();
102+ Stats->BuildSize +=
103+ AST.getSourceManager ().getMemoryBufferSizes ().malloc_bytes ;
104+
105+ const Preprocessor &PP = CI.getPreprocessor ();
106+ Stats->BuildSize += PP.getTotalMemory ();
107+ if (PreprocessingRecord *PRec = PP.getPreprocessingRecord ())
108+ Stats->BuildSize += PRec->getTotalMemory ();
109+ Stats->BuildSize += PP.getHeaderSearchInfo ().getTotalMemory ();
110+ }
91111 }
92112
93113 void BeforeExecute (CompilerInstance &CI) override {
@@ -135,6 +155,7 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
135155 std::unique_ptr<CommentHandler> IWYUHandler = nullptr ;
136156 const clang::LangOptions *LangOpts = nullptr ;
137157 const SourceManager *SourceMgr = nullptr ;
158+ PreambleBuildStats *Stats;
138159};
139160
140161// Represents directives other than includes, where basic textual information is
@@ -456,7 +477,7 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
456477 // to read back. We rely on dynamic index for the comments instead.
457478 CI.getPreprocessorOpts ().WriteCommentListToPCH = false ;
458479
459- CppFilePreambleCallbacks CapturedInfo (FileName, PreambleCallback);
480+ CppFilePreambleCallbacks CapturedInfo (FileName, PreambleCallback, Stats );
460481 auto VFS = Inputs.TFS ->view (Inputs.CompileCommand .Directory );
461482 llvm::SmallString<32 > AbsFileName (FileName);
462483 VFS->makeAbsolute (AbsFileName);
@@ -476,6 +497,12 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
476497 // bodies.
477498 CI.getFrontendOpts ().SkipFunctionBodies = false ;
478499
500+ if (Stats != nullptr ) {
501+ Stats->TotalBuildTime = PreambleTimer.getTime ();
502+ Stats->FileSystemTime = TimedFS->getTime ();
503+ Stats->SerializedSize = BuiltPreamble ? BuiltPreamble->getSize () : 0 ;
504+ }
505+
479506 if (BuiltPreamble) {
480507 vlog (" Built preamble of size {0} for file {1} version {2} in {3} seconds" ,
481508 BuiltPreamble->getSize (), FileName, Inputs.Version ,
@@ -491,10 +518,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
491518 Result->CanonIncludes = CapturedInfo.takeCanonicalIncludes ();
492519 Result->StatCache = std::move (StatCache);
493520 Result->MainIsIncludeGuarded = CapturedInfo.isMainFileIncludeGuarded ();
494- if (Stats != nullptr ) {
495- Stats->TotalBuildTime = PreambleTimer.getTime ();
496- Stats->FileSystemTime = TimedFS->getTime ();
497- }
498521 return Result;
499522 }
500523
0 commit comments