Skip to content

[analyzer] Treat bitwise_cast, std::addressof, and new as trivial in WebKit checkers. #91830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,11 @@ class TrivialFunctionAnalysisVisitor
const auto &Name = safeGetName(Callee);

if (Name == "WTFCrashWithInfo" || Name == "WTFBreakpointTrap" ||
Name == "WTFReportAssertionFailure" ||
Name == "compilerFenceForCrash" || Name.find("__builtin") == 0)
Name == "WTFReportAssertionFailure" || Name == "isMainThread" ||
Name == "isMainThreadOrGCThread" || Name == "isMainRunLoop" ||
Name == "isWebThread" || Name == "isUIThread" ||
Name == "compilerFenceForCrash" || Name == "bitwise_cast" ||
Name == "addressof" || Name.find("__builtin") == 0)
return true;

return TrivialFunctionAnalysis::isTrivialImpl(Callee, Cache);
Expand Down Expand Up @@ -428,6 +431,8 @@ class TrivialFunctionAnalysisVisitor
return TrivialFunctionAnalysis::isTrivialImpl(CE->getConstructor(), Cache);
}

bool VisitCXXNewExpr(const CXXNewExpr *NE) { return VisitChildren(NE); }

bool VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
return Visit(ICE->getSubExpr());
}
Expand Down
38 changes: 38 additions & 0 deletions clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ void WTFCrashWithInfo(int line, const char* file, const char* function, int coun
WTFCrashWithInfoImpl(line, file, function, counter, wtfCrashArg(reason));
}

template<typename ToType, typename FromType>
ToType bitwise_cast(FromType from);

template<typename T>
T* addressof(T& arg);

bool isMainThread();
bool isMainThreadOrGCThread();
bool isMainRunLoop();
bool isWebThread();
bool isUIThread();

enum class Flags : unsigned short {
Flag1 = 1 << 0,
Flag2 = 1 << 1,
Expand Down Expand Up @@ -234,6 +246,16 @@ class RefCounted {
void trivial38() { v++; if (__builtin_expect(!!(number), 1)) (*number)++; }
int trivial39() { return -v; }
int trivial40() { return v << 2; }
unsigned trivial41() { v = ++s_v; return v; }
unsigned trivial42() { return bitwise_cast<unsigned long>(nullptr); }
Number* trivial43() { return addressof(*number); }
Number* trivial44() { return new Number(1); }
ComplexNumber* trivial45() { return new ComplexNumber(); }
void trivial46() { ASSERT(isMainThread()); }
void trivial47() { ASSERT(isMainThreadOrGCThread()); }
void trivial48() { ASSERT(isMainRunLoop()); }
void trivial49() { ASSERT(isWebThread()); }
void trivial50() { ASSERT(isUIThread()); }

static RefCounted& singleton() {
static RefCounted s_RefCounted;
Expand Down Expand Up @@ -312,13 +334,17 @@ class RefCounted {
void nonTrivial16() { complex++; }
ComplexNumber nonTrivial17() { return complex << 2; }
ComplexNumber nonTrivial18() { return +complex; }
ComplexNumber* nonTrivial19() { return new ComplexNumber(complex); }

static unsigned s_v;
unsigned v { 0 };
Number* number { nullptr };
ComplexNumber complex;
Enum enumValue { Enum::Value1 };
};

unsigned RefCounted::s_v = 0;

RefCounted* refCountedObj();

void test()
Expand Down Expand Up @@ -377,6 +403,16 @@ class UnrelatedClass {
getFieldTrivial().trivial38(); // no-warning
getFieldTrivial().trivial39(); // no-warning
getFieldTrivial().trivial40(); // no-warning
getFieldTrivial().trivial41(); // no-warning
getFieldTrivial().trivial42(); // no-warning
getFieldTrivial().trivial43(); // no-warning
getFieldTrivial().trivial44(); // no-warning
getFieldTrivial().trivial45(); // no-warning
getFieldTrivial().trivial46(); // no-warning
getFieldTrivial().trivial47(); // no-warning
getFieldTrivial().trivial48(); // no-warning
getFieldTrivial().trivial49(); // no-warning
getFieldTrivial().trivial50(); // no-warning

RefCounted::singleton().trivial18(); // no-warning
RefCounted::singleton().someFunction(); // no-warning
Expand Down Expand Up @@ -419,6 +455,8 @@ class UnrelatedClass {
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial18();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
getFieldTrivial().nonTrivial19();
// expected-warning@-1{{Call argument for 'this' parameter is uncounted and unsafe}}
}
};

Expand Down
Loading