Skip to content

Commit e6a5d73

Browse files
authored
[WebKit checkers] Treat std::bit_cast as a pointer conversion (#137476)
WebKit repalced its use of WTF::bitwise_cast with std::bit_cast. Add the support for recognizing it as a pointer conversion.
1 parent c3715ec commit e6a5d73

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ bool isPtrConversion(const FunctionDecl *F) {
462462
const auto FunctionName = safeGetName(F);
463463
if (FunctionName == "getPtr" || FunctionName == "WeakPtr" ||
464464
FunctionName == "dynamicDowncast" || FunctionName == "downcast" ||
465-
FunctionName == "checkedDowncast" ||
465+
FunctionName == "checkedDowncast" || FunctionName == "bit_cast" ||
466466
FunctionName == "uncheckedDowncast" || FunctionName == "bitwise_cast" ||
467467
FunctionName == "bridge_cast" || FunctionName == "bridge_id_cast" ||
468468
FunctionName == "dynamic_cf_cast" || FunctionName == "checked_cf_cast" ||
@@ -645,6 +645,10 @@ class TrivialFunctionAnalysisVisitor
645645
auto *Callee = CE->getDirectCallee();
646646
if (!Callee)
647647
return false;
648+
649+
if (isPtrConversion(Callee))
650+
return true;
651+
648652
const auto &Name = safeGetName(Callee);
649653

650654
if (Callee->isInStdNamespace() &&
@@ -658,7 +662,7 @@ class TrivialFunctionAnalysisVisitor
658662
Name == "isMainThreadOrGCThread" || Name == "isMainRunLoop" ||
659663
Name == "isWebThread" || Name == "isUIThread" ||
660664
Name == "mayBeGCThread" || Name == "compilerFenceForCrash" ||
661-
Name == "bitwise_cast" || isTrivialBuiltinFunction(Callee))
665+
isTrivialBuiltinFunction(Callee))
662666
return true;
663667

664668
return IsFunctionTrivial(Callee);

clang/test/Analysis/Checkers/WebKit/call-args-checked-ptr.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,14 @@ namespace param_formarding_function {
173173

174174
namespace casts {
175175

176-
CheckedObj* downcast(CheckedObj*) { return nullptr; }
177-
178-
template<class T>
179-
T* bitwise_cast(T*) { return nullptr; }
176+
CheckedObj* downcast(CheckedObj*);
177+
template<class T> T* bitwise_cast(T*);
178+
template<class T> T* bit_cast(T*);
180179

181180
void foo(CheckedObj* param) {
182181
consume_ref_countable_ptr(downcast(param));
183182
consume_ref_countable_ptr(bitwise_cast(param));
183+
consume_ref_countable_ptr(bit_cast(param));
184184
}
185185
}
186186
}

clang/test/Analysis/Checkers/WebKit/call-args.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,15 @@ namespace param_formarding_function {
173173

174174
namespace casts {
175175

176-
RefCountable* downcast(RefCountable*) { return nullptr; }
177-
178-
template<class T>
179-
T* bitwise_cast(T*) { return nullptr; }
180-
181-
void foo(RefCountable* param) {
182-
consume_ref_countable_ptr(downcast(param));
183-
consume_ref_countable_ptr(bitwise_cast(param));
184-
}
176+
RefCountable* downcast(RefCountable*);
177+
template<class T> T* bitwise_cast(T*);
178+
template<class T> T* bit_cast(T*);
179+
180+
void foo(RefCountable* param) {
181+
consume_ref_countable_ptr(downcast(param));
182+
consume_ref_countable_ptr(bitwise_cast(param));
183+
consume_ref_countable_ptr(bit_cast(param));
184+
}
185185
}
186186
}
187187

clang/test/Analysis/Checkers/WebKit/uncounted-local-vars.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ void foo2() {
205205
}
206206
} // namespace guardian_casts
207207

208+
namespace casts {
209+
210+
RefCountable* provide() { return nullptr; }
211+
RefCountable* downcast(RefCountable*);
212+
template<class T> T* bitwise_cast(T*);
213+
template<class T> T* bit_cast(T*);
214+
215+
void foo() {
216+
auto* cast1 = downcast(provide());
217+
auto* cast2 = bitwise_cast(provide());
218+
auto* cast3 = bit_cast(provide());
219+
}
220+
} // namespace casts
221+
208222
namespace guardian_ref_conversion_operator {
209223
void foo() {
210224
Ref<RefCountable> rc;

clang/test/Analysis/Checkers/WebKit/uncounted-obj-arg.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ T&& forward(T& arg);
7777
template<typename T>
7878
T&& move( T&& t );
7979

80+
template<typename ToType, typename FromType>
81+
ToType bit_cast(FromType from);
82+
8083
#define offsetof(t, d) __builtin_offsetof(t, d)
8184

8285
} // namespace std
@@ -386,6 +389,7 @@ class RefCounted {
386389
void trivial68() { point pt = { 1.0 }; }
387390
unsigned trivial69() { return offsetof(OtherObj, children); }
388391
DerivedNumber* trivial70() { [[clang::suppress]] return static_cast<DerivedNumber*>(number); }
392+
unsigned trivial71() { return std::bit_cast<unsigned>(nullptr); }
389393

390394
static RefCounted& singleton() {
391395
static RefCounted s_RefCounted;
@@ -577,6 +581,7 @@ class UnrelatedClass {
577581
getFieldTrivial().trivial68(); // no-warning
578582
getFieldTrivial().trivial69(); // no-warning
579583
getFieldTrivial().trivial70(); // no-warning
584+
getFieldTrivial().trivial71(); // no-warning
580585

581586
RefCounted::singleton().trivial18(); // no-warning
582587
RefCounted::singleton().someFunction(); // no-warning

0 commit comments

Comments
 (0)