Skip to content

Commit ad3a96b

Browse files
nectoLukacma
authored andcommitted
[clang][analyzer] Add SyntaxRunningTime per-entry-point metric (llvm#163341)
Per-entry-point metrics are captured during the path-sensitive analysis time. For that reason, it is not trivial to add the syntax-only analysis time as it runs in a separate stage. Luckily syntax-only analysis is done before path-senstivie analysis. I use the function summary field to keep the syntax-only anlaysis time once syntax analysis is done, and then forward it to the per-EP metrics snapshot during the path-sensitive analysis. Note that some of the entry points that were analyzed by syntax-only rules may be missing in the CSV export if they were never analyzed by path-sensitive rules. Conversely, if a function is analyzed with path-sensitive analysis but not syntax-only analysis, its `SyntaxRunningTime` will be empty. -- CPP-7099
1 parent 83f80d9 commit ad3a96b

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

clang/include/clang/StaticAnalyzer/Core/PathSensitive/FunctionSummary.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class FunctionSummariesTy {
4848
/// The number of times the function has been inlined.
4949
unsigned TimesInlined : 32;
5050

51+
/// Running time for syntax-based AST analysis in milliseconds.
52+
std::optional<unsigned> SyntaxRunningTime = std::nullopt;
53+
5154
FunctionSummary()
5255
: TotalBasicBlocks(0), InlineChecked(0), MayInline(0),
5356
TimesInlined(0) {}
@@ -69,6 +72,11 @@ class FunctionSummariesTy {
6972
return I;
7073
}
7174

75+
FunctionSummary const *findSummary(const Decl *D) const {
76+
auto I = Map.find(D);
77+
return I == Map.end() ? nullptr : &I->second;
78+
}
79+
7280
void markMayInline(const Decl *D) {
7381
MapTy::iterator I = findOrInsertSummary(D);
7482
I->second.InlineChecked = 1;

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@ AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) {
723723
}
724724

725725
static UnsignedEPStat PathRunningTime("PathRunningTime");
726+
static UnsignedEPStat SyntaxRunningTime("SyntaxRunningTime");
726727

727728
void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
728729
ExprEngine::InliningModes IMode,
@@ -761,6 +762,8 @@ void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
761762
SyntaxCheckTimer->stopTimer();
762763
llvm::TimeRecord CheckerEndTime = SyntaxCheckTimer->getTotalTime();
763764
CheckerEndTime -= CheckerStartTime;
765+
FunctionSummaries.findOrInsertSummary(D)->second.SyntaxRunningTime =
766+
std::lround(CheckerEndTime.getWallTime() * 1000);
764767
DisplayTime(CheckerEndTime);
765768
if (AnalyzerTimers && ShouldClearTimersToPreventDisplayingThem) {
766769
AnalyzerTimers->clear();
@@ -792,11 +795,23 @@ void AnalysisConsumer::RunPathSensitiveChecks(Decl *D,
792795
if (!CFG)
793796
return;
794797

798+
CFGSize.set(CFG->size());
799+
800+
auto *DeclContext = Mgr->getAnalysisDeclContext(D);
795801
// See if the LiveVariables analysis scales.
796-
if (!Mgr->getAnalysisDeclContext(D)->getAnalysis<RelaxedLiveVariables>())
802+
if (!DeclContext->getAnalysis<RelaxedLiveVariables>())
797803
return;
798804

799-
CFGSize.set(CFG->size());
805+
// DeclContext declaration is the redeclaration of D that has a body.
806+
const Decl *DefDecl = DeclContext->getDecl();
807+
808+
// Get the SyntaxRunningTime from the function summary, because it is computed
809+
// during the AM_Syntax analysis, which is done at a different point in time
810+
// and in different order, but always before AM_Path.
811+
if (const auto *Summary = FunctionSummaries.findSummary(DefDecl);
812+
Summary && Summary->SyntaxRunningTime.has_value()) {
813+
SyntaxRunningTime.set(*Summary->SyntaxRunningTime);
814+
}
800815

801816
ExprEngine Eng(CTU, *Mgr, VisitedCallees, &FunctionSummaries, IMode);
802817

clang/test/Analysis/analyzer-stats/entry-point-stats.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// CHECK-NEXT: "DebugName": "fib(unsigned int)",
1111
// CHECK-NEXT: "CFGSize": "5",
1212
// CHECK-NEXT: "PathRunningTime": "{{[0-9]+}}",
13+
// CHECK-NEXT: "SyntaxRunningTime": "{{[0-9]+}}",
1314
// CHECK-NEXT: "MaxBugClassSize": "{{[0-9]+}}",
1415
// CHECK-NEXT: "MaxQueueSize": "{{[0-9]+}}",
1516
// CHECK-NEXT: "MaxReachableSize": "{{[0-9]+}}",
@@ -47,6 +48,7 @@
4748
// CHECK-NEXT: "DebugName": "main(int, char **)",
4849
// CHECK-NEXT: "CFGSize": "3",
4950
// CHECK-NEXT: "PathRunningTime": "{{[0-9]+}}",
51+
// CHECK-NEXT: "SyntaxRunningTime": "{{[0-9]+}}",
5052
// CHECK-NEXT: "MaxBugClassSize": "{{[0-9]+}}",
5153
// CHECK-NEXT: "MaxQueueSize": "{{[0-9]+}}",
5254
// CHECK-NEXT: "MaxReachableSize": "{{[0-9]+}}",

0 commit comments

Comments
 (0)