Skip to content

Commit ea99b06

Browse files
committed
[analyzer] Fix -analyze-function debug warning to account for syntax checkers
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 e48fe76 commit ea99b06

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(
@@ -588,10 +591,10 @@ void AnalysisConsumer::runAnalysisOnTranslationUnit(ASTContext &C) {
588591
// If the user wanted to analyze a specific function and the number of basic
589592
// blocks analyzed is zero, than the user might not specified the function
590593
// name correctly.
591-
// FIXME: The user might have analyzed the requested function in Syntax mode,
592-
// but we are unaware of that.
593-
if (!Opts.AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0)
594+
if (!Opts.AnalyzeSpecificFunction.empty() && NumFunctionsAnalyzed == 0 &&
595+
NumFunctionsAnalyzedSyntaxOnly == 0) {
594596
reportAnalyzerFunctionMisuse(Opts, *Ctx);
597+
}
595598
}
596599

597600
void AnalysisConsumer::reportAnalyzerProgress(StringRef S) {
@@ -723,6 +726,7 @@ void AnalysisConsumer::HandleCode(Decl *D, AnalysisMode Mode,
723726
SyntaxCheckTimer->startTimer();
724727
}
725728
checkerMgr->runCheckersOnASTBody(D, *Mgr, BR);
729+
++NumFunctionsAnalyzedSyntaxOnly;
726730
if (SyntaxCheckTimer) {
727731
SyntaxCheckTimer->stopTimer();
728732
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)