llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Callan (relaxcn) <details> <summary>Changes</summary> `handleOverloadedOperator()` passed `Call.getNumArgs()` as the `Postfix` flag for both member and free operators. The count is 0 or 1 for a member `operator++`, so converting it to `bool` happens to work, but a free `operator++(T &)` has one argument while `operator++(T &, int)` has two. Prefix `++` on a free-function iterator was therefore handled as postfix and `handleIncrement()` stored the old position back into the iterator, so the increment had no effect. The free-function calls now compare the argument count against 1. Member calls keep the current interpretation because `CXXInstanceCall` does not count the implicit object argument. The test defines the increment and decrement operators as free functions on its iterator. Prefix `++` and `--` report `$c.begin() + 1` and `$c.end() - 1` after the change; before it they stayed at `$c.begin()` and `$c.end()`. Postfix cases are unchanged. --- Full diff: https://github.com/llvm/llvm-project/pull/240764.diff 2 Files Affected: - (modified) clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp (+2-2) - (added) clang/test/Analysis/iterator-free-operator.cpp (+68) ``````````diff diff --git a/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp b/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp index 0b7a7903e71c0f..d81654cf1fa996 100644 --- a/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp +++ b/clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp @@ -408,7 +408,7 @@ IteratorModeling::handleOverloadedOperator(CheckerContext &C, } handleIncrement(C, Call.getCFGElementRef(), Call.getReturnValue(), - Call.getArgSVal(0), Call.getNumArgs()); + Call.getArgSVal(0), Call.getNumArgs() > 1); return; } else if (isDecrementOperator(Op)) { if (const auto *InstCall = dyn_cast<CXXInstanceCall>(&Call)) { @@ -418,7 +418,7 @@ IteratorModeling::handleOverloadedOperator(CheckerContext &C, } handleDecrement(C, Call.getCFGElementRef(), Call.getReturnValue(), - Call.getArgSVal(0), Call.getNumArgs()); + Call.getArgSVal(0), Call.getNumArgs() > 1); return; } } diff --git a/clang/test/Analysis/iterator-free-operator.cpp b/clang/test/Analysis/iterator-free-operator.cpp new file mode 100644 index 00000000000000..0428a56ef78ffc --- /dev/null +++ b/clang/test/Analysis/iterator-free-operator.cpp @@ -0,0 +1,68 @@ +// RUN: %clang_analyze_cc1 -std=c++11 -analyzer-checker=core,cplusplus,debug.DebugIteratorModeling,debug.ExprInspection -analyzer-config aggressive-binary-operation-simplification=true -analyzer-config display-checker-name=false -verify %s + +template <typename Container> +long clang_analyzer_container_begin(const Container &); +template <typename Container> +long clang_analyzer_container_end(const Container &); +template <typename Iterator> +long clang_analyzer_iterator_position(const Iterator &); +void clang_analyzer_denote(long, const char *); +void clang_analyzer_express(long); + +// The member operators only make IteratorModeling recognize the type as an +// iterator. The test calls the free operators explicitly so that the +// CXXInstanceCall path is not taken. +struct MyIterator { + MyIterator(const MyIterator &) {} + MyIterator &operator=(const MyIterator &) { return *this; } + ~MyIterator() {} + MyIterator &operator++(); + MyIterator operator++(int); + int operator*() const; +}; + +struct MyContainer { + MyIterator begin() const; + MyIterator end() const; +}; + +MyIterator &operator++(MyIterator &I) { return I; } +MyIterator operator++(MyIterator &I, int) { return I; } +MyIterator &operator--(MyIterator &I) { return I; } +MyIterator operator--(MyIterator &I, int) { return I; } + +void prefix_increment(const MyContainer &c) { + auto i = c.begin(); + clang_analyzer_denote(clang_analyzer_container_begin(c), "$c.begin()"); + + operator++(i); + + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning-re {{$c.begin() + 1{{$}}}} +} + +void postfix_increment(const MyContainer &c) { + auto i = c.begin(); + clang_analyzer_denote(clang_analyzer_container_begin(c), "$c.begin()"); + + operator++(i, 0); + + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning-re {{$c.begin() + 1{{$}}}} +} + +void prefix_decrement(const MyContainer &c) { + auto i = c.end(); + clang_analyzer_denote(clang_analyzer_container_end(c), "$c.end()"); + + operator--(i); + + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning-re {{$c.end() - 1{{$}}}} +} + +void postfix_decrement(const MyContainer &c) { + auto i = c.end(); + clang_analyzer_denote(clang_analyzer_container_end(c), "$c.end()"); + + operator--(i, 0); + + clang_analyzer_express(clang_analyzer_iterator_position(i)); // expected-warning-re {{$c.end() - 1{{$}}}} +} `````````` </details> https://github.com/llvm/llvm-project/pull/240764 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
