Skip to content

Commit afcb77c

Browse files
author
Adam Balogh
committed
[Analyzer] Fix for incorrect use of container and iterator checkers
Iterator checkers (and planned container checkers) need the option aggressive-binary-operation-simplification to be enabled. Without this option they may cause assertions. To prevent such misuse, this patch adds a preventive check which issues a warning and denies the registartion of the checker if this option is disabled. Differential Revision: https://reviews.llvm.org/D75171
1 parent c9eaed5 commit afcb77c

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,8 @@ def err_analyzer_checker_option_unknown : Error<
344344
"checker '%0' has no option called '%1'">;
345345
def err_analyzer_checker_option_invalid_input : Error<
346346
"invalid input for checker option '%0', that expects %1">;
347+
def err_analyzer_checker_incompatible_analyzer_option : Error<
348+
"checker cannot be enabled with analyzer option '%0' == %1">;
347349

348350
def err_drv_invalid_hvx_length : Error<
349351
"-mhvx-length is not supported without a -mhvx/-mhvx= flag">;

clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
1414
#include "clang/AST/DeclTemplate.h"
15+
#include "clang/Driver/DriverDiagnostic.h"
1516
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
1617
#include "clang/StaticAnalyzer/Core/Checker.h"
1718
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
@@ -1068,5 +1069,15 @@ void ento::registerContainerModeling(CheckerManager &mgr) {
10681069
}
10691070

10701071
bool ento::shouldRegisterContainerModeling(const CheckerManager &mgr) {
1072+
if (!mgr.getLangOpts().CPlusPlus)
1073+
return false;
1074+
1075+
if (!mgr.getAnalyzerOptions().ShouldAggressivelySimplifyBinaryOperation) {
1076+
mgr.getASTContext().getDiagnostics().Report(
1077+
diag::err_analyzer_checker_incompatible_analyzer_option)
1078+
<< "aggressive-binary-operation-simplification" << "false";
1079+
return false;
1080+
}
1081+
10711082
return true;
10721083
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang_analyze_cc1 -std=c++11\
2+
// RUN: -analyzer-checker=core,cplusplus,alpha.cplusplus.ContainerModeling\
3+
// RUN: %s 2>&1 | FileCheck %s
4+
5+
// XFAIL: *
6+
7+
// CHECK: checker cannot be enabled with analyzer option 'aggressive-binary-operation-simplification' == false
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %clang_analyze_cc1 -std=c++11\
2+
// RUN: -analyzer-checker=core,cplusplus,debug.DebugIteratorModeling,debug.ExprInspection\
3+
// RUN: %s 2>&1 | FileCheck %s
4+
5+
// XFAIL: *
6+
7+
// CHECK: checker cannot be enabled with analyzer option 'aggressive-binary-operation-simplification' == false
8+
9+
#include "Inputs/system-header-simulator-cxx.h"
10+
11+
void clang_analyzer_eval(bool);
12+
13+
void comparison(std::vector<int> &V) {
14+
clang_analyzer_eval(V.begin() == V.end()); // no-crash
15+
}

clang/test/Analysis/loop-widening-notes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha -analyzer-max-loop 2 -analyzer-config widen-loops=true -analyzer-output=text -verify -analyzer-config eagerly-assume=false %s
1+
// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.core -analyzer-max-loop 2 -analyzer-config widen-loops=true -analyzer-output=text -verify -analyzer-config eagerly-assume=false %s
22

33
int *p_a;
44
int bar();

0 commit comments

Comments
 (0)