Skip to content

Commit b29b369

Browse files
committed
[WebKit checkers] Add an annotation for pointer conversion.
This PR adds the WebKit checker support for [[clang::annotate_type("webkit.pointerconversion")]]. When this attribute is set on the return value of a function, the function is treated as safe to call anywhere and the return value's pointer origin is the argument.`
1 parent 436504c commit b29b369

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ bool isPtrConversion(const FunctionDecl *F) {
468468
FunctionName == "checked_objc_cast")
469469
return true;
470470

471+
auto ReturnType = F->getReturnType();
472+
if (auto *Type = ReturnType.getTypePtrOrNull()) {
473+
if (auto *AttrType = dyn_cast<AttributedType>(Type)) {
474+
if (auto *Attr = AttrType->getAttr()) {
475+
if (auto *AnnotateType = dyn_cast<AnnotateTypeAttr>(Attr)) {
476+
if (AnnotateType->getAnnotation() == "webkit.pointerconversion")
477+
return true;
478+
}
479+
}
480+
}
481+
}
482+
471483
return false;
472484
}
473485

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %clang_analyze_cc1 -analyzer-checker=alpha.webkit.UncountedCallArgsChecker -verify %s
2-
// expected-no-diagnostics
32

43
class Base {
54
public:
@@ -44,6 +43,12 @@ inline Target* uncheckedDowncast(Source* source)
4443
return static_cast<Target*>(source);
4544
}
4645

46+
template<typename Target, typename Source>
47+
Target* [[clang::annotate_type("webkit.pointerconversion")]] newCastFunction(Source*);
48+
49+
template<typename Target, typename Source>
50+
Target* [[clang::annotate_type("unrelated-annotation")]] badCastFunction(Source*);
51+
4752
template<typename... Types>
4853
String toString(const Types&... values);
4954

@@ -52,5 +57,8 @@ void foo(OtherObject* other)
5257
dynamicDowncast<SubDerived>(other->obj());
5358
checkedDowncast<SubDerived>(other->obj());
5459
uncheckedDowncast<SubDerived>(other->obj());
60+
newCastFunction<SubDerived>(other->obj());
61+
badCastFunction<SubDerived>(other->obj());
62+
// expected-warning@-1{{Call argument is uncounted and unsafe}}
5563
toString(other->obj());
5664
}

0 commit comments

Comments
 (0)