Skip to content

Commit c75ae01

Browse files
authored
[analyzer] Fix -analyze-function debug warning to account for syntax checkers (#161664)
Previously, when using `-analyze-function` to target a specific function, the analyzer would incorrectly report "Every top-level function was skipped" even when the function was successfully analyzed by syntax-only checkers. This happened because `NumFunctionsAnalyzed` only counted path-sensitive analysis, not syntax-only analysis. The misuse detection logic would see 0 functions analyzed and incorrectly conclude the function wasn't found.
1 parent cf86ef9 commit c75ae01

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ STAT_COUNTER(NumFunctionTopLevel, "The # of functions at top level.");
5151
ALWAYS_ENABLED_STATISTIC(NumFunctionsAnalyzed,
5252
"The # of functions and blocks analyzed (as top level "
5353
"with inlining turned on).");
54+
ALWAYS_ENABLED_STATISTIC(
55+
NumFunctionsAnalyzedSyntaxOnly,
56+
"The # of functions analyzed by syntax checkers only.");
5457
ALWAYS_ENABLED_STATISTIC(NumBlocksInAnalyzedFunctions,
5558
"The # of basic blocks in the analyzed functions.");
5659
ALWAYS_ENABLED_STATISTIC(
@@ -598,10 +601,10 @@ void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
598601
// If the user wanted to analyze a specific function and the number of basic
599602
// blocks analyzed is zero, than the user might not specified the function
600603
// name correctly.
601-
// FIXME: The user might have analyzed the requested function in Syntax mode,
602-
// but we are unaware of that.
603-
if (!Opts.AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0)
604+
if (!Opts.AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0 &&
605+
NumFunctionsAnalyzedSyntaxOnly == 0) {
604606
reportAnalyzerFunctionMisuse(Opts, *Ctx);
607+
}
605608
}
606609

607610
void AnalysisConsumer::reportAnalyzerProgress(StringRef S) {
@@ -736,6 +739,7 @@ void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
736739
SyntaxCheckTimer->startTimer();
737740
}
738741
checkerMgr->runCheckersOnASTBody(D, *Mgr, BR);
742+
++NumFunctionsAnalyzedSyntaxOnly;
739743
if (SyntaxCheckTimer) {
740744
SyntaxCheckTimer->stopTimer();
741745
llvm::TimeRecord CheckerEndTime = SyntaxCheckTimer->getTotalTime();

clang/test/Analysis/analyze-function-guide.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,17 @@ int fizzbuzz(int x, bool y) {
4646
// CHECK-ADVOCATE-DISPLAY-PROGRESS-NEXT: Pass the -analyzer-display-progress for tracking which functions are analyzed.
4747
// CHECK-ADVOCATE-DISPLAY-PROGRESS-NOT: For analyzing
4848

49-
// Same as the previous but syntax mode only.
50-
// FIXME: This should have empty standard output.
49+
// The user only enables syntax-only analysis, like `debug.DumpDominators`.
50+
// `-analyze-function` should only match the given function.
5151
//
52-
// RUN: %clang_analyze_cc1 -analyzer-checker=core -analyzer-config ipa=none \
52+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.DumpDominators -analyzer-config ipa=none \
5353
// RUN: -analyze-function='fizzbuzz(int, _Bool)' -x c++ \
5454
// RUN: -triple x86_64-pc-linux-gnu 2>&1 %s \
55-
// RUN: | FileCheck %s -check-prefix=CHECK-EMPTY3 --allow-empty
56-
//
57-
// FIXME: This should have empty standard output.
58-
// CHECK-EMPTY3: Every top-level function was skipped.
59-
// CHECK-EMPTY3-NEXT: Pass the -analyzer-display-progress for tracking which functions are analyzed.
55+
// RUN: | FileCheck %s -check-prefix=CHECK-SYNTAX-ONLY --allow-empty
56+
//
57+
// With syntax-only analysis, the function is found and analyzed, so no error message.
58+
// CHECK-SYNTAX-ONLY: Immediate dominance tree (Node#,IDom#):
59+
// CHECK-SYNTAX-ONLY-NEXT: (0,1)
60+
// CHECK-SYNTAX-ONLY-NEXT: (1,2)
61+
// CHECK-SYNTAX-ONLY-NEXT: (2,2)
62+
// CHECK-SYNTAX-ONLY-NOT: Every top-level function was skipped.

0 commit comments

Comments
 (0)