[clang][analyzer] Improve the modeling of insert in MismatchedIteratorChecker by flovent · Pull Request #132596 · llvm/llvm-project (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@flovent

Fixes #132010

Associative containers in STL has an unique insert overload member function comparing to un-associative containers(https://en.cppreference.com/w/cpp/container/unordered_set/insert):

template< class InputIt >
void insert( InputIt first, InputIt last );

Add support for this insert overload in MismatchedIteratorChecker, verify if first and last belongs to the same container in this case.

@flovent

@llvmbot

@llvm/pr-subscribers-clang

Author: None (flovent)

Changes

This PR fixs #132010.

Associative containers in STL has an unique insert overload member function comparing to un-associative containers(https://en.cppreference.com/w/cpp/container/unordered_set/insert):

template&lt; class InputIt &gt;
void insert( InputIt first, InputIt last );

Add support for this insert overload in MismatchedIteratorChecker, verify if first and last belongs to the same container in this case.


Full diff: https://github.com/llvm/llvm-project/pull/132596.diff

4 Files Affected:

diff --git a/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp index 82a6228318179..1c101b91f727f 100644 --- a/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp @@ -91,12 +91,18 @@ void MismatchedIteratorChecker::checkPreCall(const CallEvent &Call, InstCall->getCXXThisVal().getAsRegion()); } } else if (isInsertCall(Func)) {

diff --git a/clang/test/Analysis/Inputs/system-header-simulator-cxx.h b/clang/test/Analysis/Inputs/system-header-simulator-cxx.h index a379a47515668..c5aeb0af9d578 100644 --- a/clang/test/Analysis/Inputs/system-header-simulator-cxx.h +++ b/clang/test/Analysis/Inputs/system-header-simulator-cxx.h @@ -1244,6 +1244,7 @@ template< class Alloc = std::allocator

class unordered_set { public:

@@ -1260,6 +1261,9 @@ template< Key *val; iterator begin() const { return iterator(val); } iterator end() const { return iterator(val + 1); } +

};

template diff --git a/clang/test/Analysis/issue-132010.cpp b/clang/test/Analysis/issue-132010.cpp new file mode 100644 index 0000000000000..abdaed57f26b9 --- /dev/null +++ b/clang/test/Analysis/issue-132010.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_analyze_cc1 -analyzer-config aggressive-binary-operation-simplification=true -analyzer-checker=alpha.cplusplus.MismatchedIterator -analyzer-output text -verify %s + +// expected-no-diagnostics + +#include "Inputs/system-header-simulator-cxx.h" + +void f() +{

+} diff --git a/clang/test/Analysis/mismatched-iterator.cpp b/clang/test/Analysis/mismatched-iterator.cpp index 570e742751ead..325e7764ad7fa 100644 --- a/clang/test/Analysis/mismatched-iterator.cpp +++ b/clang/test/Analysis/mismatched-iterator.cpp @@ -19,6 +19,11 @@ void good_insert4(std::vector &V, int len, int n) { V.insert(V.cbegin(), {n-1, n, n+1}); // no-warning }

+void good_insert5(std::vector &V, int len, int n) {

+void bad_insert5(std::vector &V1, std::vector &V2, int len, int n) {

@llvmbot

@llvm/pr-subscribers-clang-static-analyzer-1

Author: None (flovent)

Changes

This PR fixs #132010.

Associative containers in STL has an unique insert overload member function comparing to un-associative containers(https://en.cppreference.com/w/cpp/container/unordered_set/insert):

template&lt; class InputIt &gt;
void insert( InputIt first, InputIt last );

Add support for this insert overload in MismatchedIteratorChecker, verify if first and last belongs to the same container in this case.


Full diff: https://github.com/llvm/llvm-project/pull/132596.diff

4 Files Affected:

diff --git a/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp index 82a6228318179..1c101b91f727f 100644 --- a/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/MismatchedIteratorChecker.cpp @@ -91,12 +91,18 @@ void MismatchedIteratorChecker::checkPreCall(const CallEvent &Call, InstCall->getCXXThisVal().getAsRegion()); } } else if (isInsertCall(Func)) {

diff --git a/clang/test/Analysis/Inputs/system-header-simulator-cxx.h b/clang/test/Analysis/Inputs/system-header-simulator-cxx.h index a379a47515668..c5aeb0af9d578 100644 --- a/clang/test/Analysis/Inputs/system-header-simulator-cxx.h +++ b/clang/test/Analysis/Inputs/system-header-simulator-cxx.h @@ -1244,6 +1244,7 @@ template< class Alloc = std::allocator

class unordered_set { public:

@@ -1260,6 +1261,9 @@ template< Key *val; iterator begin() const { return iterator(val); } iterator end() const { return iterator(val + 1); } +

};

template diff --git a/clang/test/Analysis/issue-132010.cpp b/clang/test/Analysis/issue-132010.cpp new file mode 100644 index 0000000000000..abdaed57f26b9 --- /dev/null +++ b/clang/test/Analysis/issue-132010.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_analyze_cc1 -analyzer-config aggressive-binary-operation-simplification=true -analyzer-checker=alpha.cplusplus.MismatchedIterator -analyzer-output text -verify %s + +// expected-no-diagnostics + +#include "Inputs/system-header-simulator-cxx.h" + +void f() +{

+} diff --git a/clang/test/Analysis/mismatched-iterator.cpp b/clang/test/Analysis/mismatched-iterator.cpp index 570e742751ead..325e7764ad7fa 100644 --- a/clang/test/Analysis/mismatched-iterator.cpp +++ b/clang/test/Analysis/mismatched-iterator.cpp @@ -19,6 +19,11 @@ void good_insert4(std::vector &V, int len, int n) { V.insert(V.cbegin(), {n-1, n, n+1}); // no-warning }

+void good_insert5(std::vector &V, int len, int n) {

+void bad_insert5(std::vector &V1, std::vector &V2, int len, int n) {

steakhal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. It looks good with nits.

@flovent

steakhal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thabk you!

Labels