Skip to content

Commit

Permalink
[analyzer] Check C++ base or member initializer in WebKit checkers. (#…
Browse files Browse the repository at this point in the history
…92220)

Co-authored-by: Ryosuke Niwa <rniwa@apple.com>
  • Loading branch information
rniwa and Ryosuke Niwa authored May 16, 2024
1 parent a9763de commit 72200fc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,19 @@ bool TrivialFunctionAnalysis::isTrivialImpl(
if (!IsNew)
return It->second;

TrivialFunctionAnalysisVisitor V(Cache);

if (auto *CtorDecl = dyn_cast<CXXConstructorDecl>(D)) {
for (auto *CtorInit : CtorDecl->inits()) {
if (!V.Visit(CtorInit->getInit()))
return false;
}
}

const Stmt *Body = D->getBody();
if (!Body)
return false;

TrivialFunctionAnalysisVisitor V(Cache);
bool Result = V.Visit(Body);
if (Result)
Cache[D] = true;
Expand Down
21 changes: 21 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ template<typename E> class OptionSet {
StorageType m_storage { 0 };
};

int atoi(const char* str);

class Number {
public:
Number(int v) : v(v) { }
Number(double);
Number(const char* str) : v(atoi(str)) { }
Number operator+(const Number&);
Number& operator++() { ++v; return *this; }
Number operator++(int) { Number returnValue(v); ++v; return returnValue; }
Expand All @@ -173,9 +176,16 @@ class Number {
int v;
};

class DerivedNumber : public Number {
public:
DerivedNumber(char c) : Number(c - '0') { }
DerivedNumber(const char* str) : Number(atoi(str)) { }
};

class ComplexNumber {
public:
ComplexNumber() : realPart(0), complexPart(0) { }
ComplexNumber(int real, const char* str) : realPart(real), complexPart(str) { }
ComplexNumber(const ComplexNumber&);
ComplexNumber& operator++() { realPart.someMethod(); return *this; }
ComplexNumber operator++(int);
Expand Down Expand Up @@ -311,6 +321,7 @@ class RefCounted {
return;
}
unsigned trivial60() { return ObjectWithNonTrivialDestructor { 5 }.value(); }
unsigned trivial61() { return DerivedNumber('7').value(); }

static RefCounted& singleton() {
static RefCounted s_RefCounted;
Expand Down Expand Up @@ -391,6 +402,9 @@ class RefCounted {
ComplexNumber nonTrivial18() { return +complex; }
ComplexNumber* nonTrivial19() { return new ComplexNumber(complex); }
unsigned nonTrivial20() { return ObjectWithMutatingDestructor { 7 }.value(); }
unsigned nonTrivial21() { return Number("123").value(); }
unsigned nonTrivial22() { return ComplexNumber(123, "456").real().value(); }
unsigned nonTrivial23() { return DerivedNumber("123").value(); }

static unsigned s_v;
unsigned v { 0 };
Expand Down Expand Up @@ -479,6 +493,7 @@ class UnrelatedClass {
getFieldTrivial().trivial58(); // no-warning
getFieldTrivial().trivial59(); // no-warning
getFieldTrivial().trivial60(); // no-warning
getFieldTrivial().trivial61(); // no-warning

RefCounted::singleton().trivial18(); // no-warning
RefCounted::singleton().someFunction(); // no-warning
Expand Down Expand Up @@ -525,6 +540,12 @@ class UnrelatedClass {
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial20();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial21();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial22();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial23();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
}
};

Expand Down

0 comments on commit 72200fc

Please sign in to comment.