llvm-project/clang/test/Analysis/new-user-defined.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

31 lines
725 B
C++
Raw Permalink Normal View History

[analyzer] Do list initialization for CXXNewExpr with initializer list arg (#127702) Fixes #116444. Closed #127700 because I accidentally updated it in github UI. ### Current vs expected behavior Previously, the result of a `CXXNewExpr` was not always list initialized when using an initializer list. In this example: ``` struct S { int x; }; void F() { S *s = new S{1}; delete s; } ``` there would be a binding of `s` to `compoundVal{1}`, but this isn't used during later field binding lookup. After this PR, there is instead a binding of `s->x` to `1`. This is the cause of #116444 since the field binding lookup returns undefined in some cases currently. ### Changes This PR swaps around the handling of typed value regions (seems to be the usual region type when doing non-CXX-new-expr list initialization) and symbolic regions (the result of the CXX new expr), so that symbolic regions also get list initialized. In the below snippet, it swaps the order of the two conditionals. https://github.com/llvm/llvm-project/blob/8529bd7b964cc9fafe8fece84f7bd12dacb09560/clang/lib/StaticAnalyzer/Core/RegionStore.cpp#L2426-L2448 ### Followup work This PR only makes CSA do list init for `CXXNewExpr`s. After this, I would like to make some changes to `RegionStoreMananger::bind` in how it handles list initialization generally. I've added some straightforward test cases here for the `new` expr with a list initializer. I started adding some more before realizing that the current general (not just `new` expr) list initialization could be changed to handle more cases like list initialization of unions and arrays (like https://github.com/llvm/llvm-project/issues/54910). Lmk if it is preferred to then leave these test cases out for now.
2025-02-28 10:42:26 -06:00
// RUN: %clang_analyze_cc1 -verify %s\
// RUN: -analyzer-checker=core,debug.ExprInspection
void clang_analyzer_eval(bool);
using size_t = decltype(sizeof(int));
template <class FirstT, class... Rest>
void escape(FirstT first, Rest... args);
namespace CustomClassType {
struct S {
int x;
static void* operator new(size_t size) {
return ::operator new(size);
}
};
void F() {
S *s = new S;
clang_analyzer_eval(s->x); // expected-warning{{UNKNOWN}} FIXME: should be an undefined warning
S *s2 = new S{};
clang_analyzer_eval(0 == s2->x); // expected-warning{{TRUE}}
S *s3 = new S{1};
clang_analyzer_eval(1 == s3->x); // expected-warning{{TRUE}}
escape(s, s2, s3);
}
} // namespace CustomClassType