From 0b2a92224630f6e177d091b8391cfa943764aba5 Mon Sep 17 00:00:00 2001 From: Artem Dergachev Date: Fri, 17 Jul 2020 19:42:20 -0700 Subject: [PATCH] [analyzer] scan-build: Fix silencing multiple core checkers. It was only silencing one checker because -analyzer-config flags can only carry one value at a time. --- .../null_dereference_and_division_by_zero.c | 8 +++++ .../scan-build/silence-core-checkers.test | 30 +++++++++++++++++++ clang/tools/scan-build/bin/scan-build | 10 ++++--- 3 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 clang/test/Analysis/scan-build/Inputs/null_dereference_and_division_by_zero.c create mode 100644 clang/test/Analysis/scan-build/silence-core-checkers.test diff --git a/clang/test/Analysis/scan-build/Inputs/null_dereference_and_division_by_zero.c b/clang/test/Analysis/scan-build/Inputs/null_dereference_and_division_by_zero.c new file mode 100644 index 000000000000..438af79c90bb --- /dev/null +++ b/clang/test/Analysis/scan-build/Inputs/null_dereference_and_division_by_zero.c @@ -0,0 +1,8 @@ +int test(int x) { + if (x) { + int *p = 0; + return *p; // Null dereference. + } else { + return 1 / x; // Division by zero. + } +} diff --git a/clang/test/Analysis/scan-build/silence-core-checkers.test b/clang/test/Analysis/scan-build/silence-core-checkers.test new file mode 100644 index 000000000000..6d9a3017fcd6 --- /dev/null +++ b/clang/test/Analysis/scan-build/silence-core-checkers.test @@ -0,0 +1,30 @@ +// FIXME: Actually, "perl". +REQUIRES: shell + +RUN: rm -rf %t.output_dir && mkdir %t.output_dir +RUN: %scan-build -o %t.output_dir \ +RUN: %clang -S %S/Inputs/null_dereference_and_division_by_zero.c \ +RUN: | FileCheck %s -check-prefix CHECK-TWO-BUGS + +RUN: rm -rf %t.output_dir && mkdir %t.output_dir +RUN: %scan-build -o %t.output_dir \ +RUN: -disable-checker core.DivideZero \ +RUN: %clang -S %S/Inputs/null_dereference_and_division_by_zero.c \ +RUN: | FileCheck %s -check-prefix CHECK-ONE-BUG + +RUN: rm -rf %t.output_dir && mkdir %t.output_dir +RUN: %scan-build -o %t.output_dir \ +RUN: -disable-checker core.NullDereference \ +RUN: %clang -S %S/Inputs/null_dereference_and_division_by_zero.c \ +RUN: | FileCheck %s -check-prefix CHECK-ONE-BUG + +RUN: rm -rf %t.output_dir && mkdir %t.output_dir +RUN: %scan-build -o %t.output_dir \ +RUN: -disable-checker core.NullDereference \ +RUN: -disable-checker core.DivideZero \ +RUN: %clang -S %S/Inputs/null_dereference_and_division_by_zero.c \ +RUN: | FileCheck %s -check-prefix CHECK-NO-BUGS + +CHECK-NO-BUGS: scan-build: No bugs found. +CHECK-ONE-BUG: scan-build: 1 bug found. +CHECK-TWO-BUGS: scan-build: 2 bugs found. diff --git a/clang/tools/scan-build/bin/scan-build b/clang/tools/scan-build/bin/scan-build index 11334a0b9626..aed8c417b6cc 100755 --- a/clang/tools/scan-build/bin/scan-build +++ b/clang/tools/scan-build/bin/scan-build @@ -1973,11 +1973,13 @@ my $CCC_ANALYZER_ANALYSIS = join ' ', @AnalysesToRun; my $CCC_ANALYZER_PLUGINS = join ' ', map { "-load ".$_ } @{$Options{PluginsToLoad}}; my $CCC_ANALYZER_CONFIG = join ' ', map { "-analyzer-config ".$_ } @{$Options{ConfigOptions}}; -foreach (sort { $Options{SilenceCheckers}{$a} <=> $Options{SilenceCheckers}{$b} } - keys %{$Options{SilenceCheckers}}) { - # Add checkers in order they were silenced. +if (%{$Options{SilenceCheckers}}) { $CCC_ANALYZER_CONFIG = - $CCC_ANALYZER_CONFIG." -analyzer-config silence-checkers=".$_; + $CCC_ANALYZER_CONFIG." -analyzer-config silence-checkers=" + .join(';', sort { + $Options{SilenceCheckers}{$a} <=> + $Options{SilenceCheckers}{$b} + } keys %{$Options{SilenceCheckers}}); } my %EnvVars = (