@@ -64,8 +64,9 @@ bool compileCommandsAreEqual(const tooling::CompileCommand &LHS,
64
64
65
65
class CppFilePreambleCallbacks : public PreambleCallbacks {
66
66
public:
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) {}
69
70
70
71
IncludeStructure takeIncludes () { return std::move (Includes); }
71
72
@@ -88,6 +89,25 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
88
89
IsMainFileIncludeGuarded =
89
90
CI.getPreprocessor ().getHeaderSearchInfo ().isFileMultipleIncludeGuarded (
90
91
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
+ }
91
111
}
92
112
93
113
void BeforeExecute (CompilerInstance &CI) override {
@@ -135,6 +155,7 @@ class CppFilePreambleCallbacks : public PreambleCallbacks {
135
155
std::unique_ptr<CommentHandler> IWYUHandler = nullptr ;
136
156
const clang::LangOptions *LangOpts = nullptr ;
137
157
const SourceManager *SourceMgr = nullptr ;
158
+ PreambleBuildStats *Stats;
138
159
};
139
160
140
161
// Represents directives other than includes, where basic textual information is
@@ -456,7 +477,7 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
456
477
// to read back. We rely on dynamic index for the comments instead.
457
478
CI.getPreprocessorOpts ().WriteCommentListToPCH = false ;
458
479
459
- CppFilePreambleCallbacks CapturedInfo (FileName, PreambleCallback);
480
+ CppFilePreambleCallbacks CapturedInfo (FileName, PreambleCallback, Stats );
460
481
auto VFS = Inputs.TFS ->view (Inputs.CompileCommand .Directory );
461
482
llvm::SmallString<32 > AbsFileName (FileName);
462
483
VFS->makeAbsolute (AbsFileName);
@@ -476,6 +497,12 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
476
497
// bodies.
477
498
CI.getFrontendOpts ().SkipFunctionBodies = false ;
478
499
500
+ if (Stats != nullptr ) {
501
+ Stats->TotalBuildTime = PreambleTimer.getTime ();
502
+ Stats->FileSystemTime = TimedFS->getTime ();
503
+ Stats->SerializedSize = BuiltPreamble ? BuiltPreamble->getSize () : 0 ;
504
+ }
505
+
479
506
if (BuiltPreamble) {
480
507
vlog (" Built preamble of size {0} for file {1} version {2} in {3} seconds" ,
481
508
BuiltPreamble->getSize (), FileName, Inputs.Version ,
@@ -491,10 +518,6 @@ buildPreamble(PathRef FileName, CompilerInvocation CI,
491
518
Result->CanonIncludes = CapturedInfo.takeCanonicalIncludes ();
492
519
Result->StatCache = std::move (StatCache);
493
520
Result->MainIsIncludeGuarded = CapturedInfo.isMainFileIncludeGuarded ();
494
- if (Stats != nullptr ) {
495
- Stats->TotalBuildTime = PreambleTimer.getTime ();
496
- Stats->FileSystemTime = TimedFS->getTime ();
497
- }
498
521
return Result;
499
522
}
500
523
0 commit comments