52 Commits

Author SHA1 Message Date
Ryosuke Niwa
e9e714f43a
[alpha.webkit.RetainPtrCtorAdoptChecker] Don't treat calling (void)copy:(id) as a leak (#179713)
UIResponderStandardEditActions defines (void)copy:(id)sender but this
selector should not be treated as a copy operation since it's a "copy"
in the sense of application triggering copy & paste for the system
pasteboard.

---------

Co-authored-by: Balázs Benics <benicsbalazs@gmail.com>
2026-02-05 02:10:36 -08:00
Ryosuke Niwa
aca48a44d1
Add the support for recognizing WTF::move like std::move (#170820)
This PR adds support for treating WTF::move like std::move in various
WebKit checkers.
2025-12-10 11:04:48 -08:00
Ryosuke Niwa
06f0758282
[alpha.webkit.UnretainedCallArgsChecker] Recognize [allocObj() init] pattern (#161019)
Generalize the check for recognizing [[Obj alloc] init] to also
recognize [allocObj() init]. We do this by utilizing isAllocInit
function in RetainPtrCtorAdoptChecker.
2025-12-09 12:29:11 -08:00
Ryosuke Niwa
65c895dfe0
[alpha.webkit.UnretainedCallArgsChecker] Treat getter on a dependent smart pointer type as safe (#161025)
Add the support for recognizing smart pointer type appearing as the type
of the object pointer in CXXDependentScopeMemberExpr.
2025-10-16 15:15:39 -07:00
Ryosuke Niwa
b6262825bd
[alpha.webkit.UnretainedCallArgsChecker] Treat NSStringFromSelector and alike as trivial and returns a retained value (#161135)
Treat NSStringFromSelector, NSSelectorFromString, NSStringFromClass,
NSClassFromString, NSStringFromProtocol, and NSProtocolFromString as
trivial, and treat their return values as a safe pointer origin since
the return value of these functions don't need to be retained.
2025-10-15 14:21:18 -07:00
Matheus Izvekov
b516dcc998
[clang] NFC: rename TagType::getOriginalDecl back to getDecl (#163271)
This rename was made as part of
https://github.com/llvm/llvm-project/pull/147835 in order to ease
rebasing the PR, and give a nice window for other patches to get rebased
as well.

It has been a while already, so lets go ahead and rename it back.
2025-10-15 16:11:17 -03:00
Ryosuke Niwa
1127dd7754
[WebKit Checkers] Treat a NS/CF global defined in a system header as a safe pointer origin (#161146) 2025-10-14 16:52:19 -07:00
Ryosuke Niwa
0e4fb17971
[WebKit checkers] Add support for ns_returns_autoreleased (#161236)
Recognize ns_returns_autoreleased on a function and treat its return
value as a safe pointer origin.
2025-10-14 13:34:08 -07:00
Ryosuke Niwa
872c4319df
[WebKit Checkers] Recognize NSApp as a safe global variable (#160990)
Treat accessing NSApp without retaining it as safe
2025-10-07 12:09:29 -07:00
Ryosuke Niwa
e166816af0
[WebKit Checkers] Treat a boxed value as a safe pointer origin (#161133) 2025-10-07 12:09:01 -07:00
Ryosuke Niwa
9d933c794a
[WebKit checkers] Add the support for OSObjectPtr (#159484)
Add the support for OSObjectPtr, which behaves like RetainPtr.
2025-09-18 21:46:22 -07:00
Ryosuke Niwa
8ee31ab00b
[WebKit checkers] Treat function pointers with "Singleton" suffix as singleton. (#158012) 2025-09-12 15:08:23 -07:00
Ryosuke Niwa
50da22a9f4
[alpha.webkit.UncountedCallArgsChecker] A return value can be erroneously treated as unsafe if it's a template parameter (#157993)
When a template class takes Ref<T> as a template parameter and this
template parameter is used as the return value of a member function, the
return value can be treated as unsafe (i.e. emits a false positive). The
issue was caused by getCanonicalType sometimes converting Ref<T> to T.
Workaround this problem by avoid emitting a warning when the original,
non-canonical type is a safe pointer type.
2025-09-11 15:51:02 -07:00
Ryosuke Niwa
b64ed9d79e
[WebKit checkers] Recognize NS_RETURNS_RETAINED and CF_RETURNS_RETAINED. (#157629)
This PR adds the support for treating a function return value to be safe
if the function is annotated with NS_RETURNS_RETAINED or
CF_RETURNS_RETAINED.
2025-09-11 13:32:51 -07:00
Ryosuke Niwa
db74eae1dc
[WebKit checkers] Treat NULL, 0, and nil like nullptr (#157700)
This PR makes WebKit checkers treat NULL, 0, and nil like nullptr in
various places.
2025-09-10 16:04:03 -07:00
Ryosuke Niwa
49f1751522
[alpha.webkit.UncheckedCallArgsChecker] Don't emit a warning for passing a temporary object as an argument. (#155033)
Since a temporary object lives until the end of the statement, it's safe
to pass such an object as a function argument without explicitly
creating a CheckedRef/CheckedPtr in stack.
2025-09-08 11:13:55 -07:00
Ryosuke Niwa
ae3bba4d15
WebKit checkers: recgonize @YES / @NO as safe constants (#148721) 2025-07-15 15:12:15 -07:00
Ryosuke Niwa
e5fa38b02b
[WebKit checkers] Treat passing of a member variable which is capable of CheckedPtr as safe. (#142485)
It's safe for a member function of a class or struct to call a function
or allocate a local variable with a pointer or a reference to a member
variable since "this" pointer, and therefore all its members, will be
kept alive by its caller so recognize as such.
2025-06-09 07:32:15 -07:00
Ryosuke Niwa
7809b147fa
[alpha.webkit.UncheckedCallArgsChecker] Forwarding r-value reference should not result in a warning (#142471)
This PR fixes the bug that the checker emits a warning when a function
takes T&& and passes it to another function using std::move. We should
treat std::move like any other pointer conversion and the origin of the
pointer to be that of the argument.
2025-06-06 07:49:38 -06:00
Kazu Hirata
8d49c64fa2
[StaticAnalyzer] Remove unused includes (NFC) (#141525)
These are identified by misc-include-cleaner.  I've filtered out those
that break builds.  Also, I'm staying away from llvm-config.h,
config.h, and Compiler.h, which likely cause platform- or
compiler-specific build failures.
2025-05-26 14:57:13 -07:00
Ryosuke Niwa
8ca8c404d3
[WebKit checkers] Treat global const variables as safe (#136170)
This PR makes WebKit checkers treat a variable with global storage as
safe instead of constraining to ones that start with k or _k.
2025-04-22 16:15:26 -07:00
Ryosuke Niwa
27d83184c4
[alpha.webkit.UncountedCallArgsChecker] Treat an explicit construction of Ref from a Ref return value safe. (#130911)
Fix a bug that an explicit construction of Ref out of a Ref return value
would not be treated as safe. It is definitely safe albit redundant.
2025-03-12 18:33:17 -07:00
Ryosuke Niwa
caf301891a
Add unretained call args checker (#130901)
Reland https://github.com/llvm/llvm-project/pull/130729
2025-03-12 10:37:13 -07:00
Ryosuke Niwa
7573ee1781
Revert "[alpha.webkit.UnretainedCallArgsChecker] Add a checker for NS or CF type call arguments." (#130828)
Reverts llvm/llvm-project#130729
2025-03-11 13:42:31 -07:00
Ryosuke Niwa
9e64fc6fb7
[alpha.webkit.UnretainedCallArgsChecker] Add a checker for NS or CF type call arguments. (#130729)
This PR adds alpha.webkit.UnretainedCallArgsChecker by generalizing
RawPtrRefCallArgsChecker. It checks call arguments of NS or CF types are
backed by a RetainPtr or not. The new checker emits warnings for CF
types in Objective-C++ code regardless of ARC and it emits warning for
NS types when ARC is disabled.

Hard-code a few cases as safe: class and superclass properties on class
interfaces and constant dictionary or array literals as well as the
return value of NSClassFromString.

Finally, add the support for variadic function calls and selector
invocations.
2025-03-11 12:47:13 -07:00
Ryosuke Niwa
3ce43c8b16
[WebKit checkers] Don't treat virtual functions as safe. (#129632)
Prior to this PR, WebKit checkers erroneously treated functions to be
safe if it has a trivial body even if it was marked as virtual. In the
case of a virtual function, it can have an override which does not pass
the triviality check so we must not make such an assumption.

This PR also restricts the allowed operator overloading while finding
the pointer origin to just operators on smart pointer types: Ref,
RefPtr, CheckedRef, CheckedPtr, RetainPtr, WeakPtr, WeakRef, unique_ptr,
and UniqueRef.
2025-03-10 21:01:39 -07:00
Ryosuke Niwa
c419acdf82
[alpha.webkit.UncountedCallArgsChecker] Recognize CXXUnresolvedConstructExpr as a safe origin. (#130258)
Handle CXXUnresolvedConstructExpr in tryToFindPtrOrigin so that
constructing Ref, RefPtr, CheckedRef, CheckedPtr, ... constructed in
such a way that its type is unresolved at AST level will be still
treated as a safe pointer origin.

Also fix a bug in isPtrOfType that it was not recognizing
DeducedTemplateSpecializationType.
2025-03-07 14:40:33 -08:00
Oliver Hunt
e6aae2a490
[analyzer] Handle structured bindings in alpha.webkit.UncountedCallArgsChecker (#129424)
Simply add awareness of BindingDecl to the logic for identifying local
assignments.
2025-03-02 20:30:06 -08:00
Ryosuke Niwa
cec3507702
[alpha.webkit.UnretainedLocalVarsChecker] Add a checker for local variables to NS and CF types. (#127554)
This PR adds alpha.webkit.UnretainedLocalVarsChecker by generalizing
RawPtrRefLocalVarsChecker. It checks local variables to NS or CF types
are guarded with a RetainPtr or not. The new checker is effective for NS
and CF types in Objective-C++ code without ARC, and it's effective for
CF types in code with ARC.
2025-02-24 12:14:14 -08:00
Florian Mayer
ad905f133c [NFC] [clang] Use isa instead of dyn_cast 2025-02-11 13:42:31 -08:00
Florian Mayer
5c7071e996
[NFC] [clang] fix unused variable warning (#126796) 2025-02-11 13:22:22 -08:00
Ryosuke Niwa
8c67f14f62
[WebKit Checkers] Allow operator T&() in a const member function (#126470)
Allow operator T&() in a member function which returns a const member
variable.

In particular, this will allow UniqueRef::operator T&() and
Ref::operator T&() to be treated as a safe pointer origin when they're
called on a const member.
2025-02-11 11:07:52 -08:00
Ryosuke Niwa
71478ecdb4
[WebKit Checkers] Treat const Objective-C ivar as a safe origin (#126353)
Like const C++ member variables, treat const Ref, RefPtr, CheckedRef,
CheckedPtr Objective-C ivars as a safe pointer origin in WebKit
checkers.
2025-02-11 10:00:09 -08:00
Ryosuke Niwa
05860f9b38
[WebKit checkers] Recognize ensureFoo functions (#119681)
In WebKit, we often write Foo::ensureBar function which lazily
initializes m_bar and returns a raw pointer or a raw reference to m_bar.
Such a return value is safe to use for the duration of a member function
call in Foo so long as m_bar is const so that it never gets unset or
updated with a new value once it's initialized.

This PR adds support for recognizing these types of functions and
treating its return value as a safe origin of a function argument
(including "this") or a local variable.
2024-12-13 01:48:29 -08:00
Ryosuke Niwa
f7e868fe43
Fix a bug that CXXConstructExpr wasn't recognized by tryToFindPtrOrigin (#119336)
Prior to this PR, only CXXTemporaryObjectExpr, not CXXConstructExpr was
recognized in tryToFindPtrOrigin.
2024-12-12 09:37:04 -08:00
Ryosuke Niwa
6be567bfc2
[Webkit Checkers] Treat const member variables as a safe origin (#115594)
Treat const Ref, RefPtr, CheckedRef, CheckedPtr member variables as safe
pointer origin in WebKit's local variable and call arguments checkers.
2024-11-14 21:11:08 -08:00
Ryosuke Niwa
5c20891b2b
[WebKit Checkers] Allow a guardian CheckedPtr/CheckedRef (#110222)
This PR makes WebKit checkers allow a guardian variable which is
CheckedPtr or CheckedRef as in addition to RefPtr or Ref.
2024-10-25 08:52:56 -07:00
Ryosuke Niwa
0fc3e4093c
[alpha.webkit.UncountedCallArgsChecker] Skip std::forward in tryToFindPtrOrigin. (#111222)
Ignore std::forward when it appears while looking for the pointer
origin.
2024-10-10 10:00:42 -07:00
Ryosuke Niwa
33533baf63
[alpha.webkit.UncountedCallArgsChecker] Add support for Objective-C++ property access (#108669)
Treat a function call or property access via a Objective-C++ selector
which returns a Ref/RefPtr as safe.
2024-09-17 18:39:22 -07:00
Ryosuke Niwa
cf8b93d823
[alpha.webkit.UncountedCallArgsChecker] Allow explicit instantiation of Ref/RefPtr on call arguments. (#91875)
Co-authored-by: Ryosuke Niwa <rniwa@apple.com>
2024-05-13 21:05:41 -07:00
Ryosuke Niwa
21be818223
[analyzer] Support determining origins in a conditional operator in WebKit checkers. (#91143)
This PR adds the support for determining the origin of a pointer in a
conditional operator.

Because such an expression can have two distinct origins each of which
needs to be visited, this PR refactors tryToFindPtrOrigin to take a
callback instead of returning a pair.

The callback is called for the second operand and the third operand of
the conditioanl operator (i.e. E2 and E3 in E1 ? E2 : E3).

Also treat nullptr and integer literal as safe pointer origins in the
local variable checker.
2024-05-09 21:32:28 -07:00
Ryosuke Niwa
b86accceee
[alpha.webkit.UncountedCallArgsChecker] Treat (foo())->bar() like foo()->bar(). (#91052) 2024-05-06 15:59:08 -07:00
Fraser Cormack
4b369ff076 [analyzer][NFC] Fix unused variable warning 2024-03-20 11:43:38 +00:00
Ryosuke Niwa
c3b87a8810
[alpha.webkit.UncountedCallArgsChecker] Allow ASSERT and atomic<T> operations in a trivial function (#82063) 2024-02-19 16:11:49 -08:00
Ryosuke Niwa
a7982d5e7a
[analyzer] UncountedCallArgsChecker: Detect & ignore trivial function calls. (#81808)
This PR introduces the concept of a "trivial function" which applies to
a function that only calls other trivial functions and contain literals
and expressions that don't result in heap mutations (specifically it
does not call deref). This is implemented using ConstStmtVisitor and
checking each statement and expression's trivialness.

This PR also introduces the concept of a "ingleton function", which is a
static member function or a free standing function which ends with the
suffix "singleton". Such a function's return value is understood to be
safe to call any function with.
2024-02-14 18:46:30 -08:00
Ryosuke Niwa
7249692bd2
[analyzer] Detect a return value of Ref<T> & RefPtr<T> (#81580)
This PR makes the checker not emit warning when a function is called
with a return value of another function when the return value is of type
Ref<T> or RefPtr<T>.
2024-02-14 14:47:40 -08:00
Ryosuke Niwa
93a2a8cb7f
Fix a crash in clang::isGetterOfRefCounted by checking nullptr in tryToFindPtrOrigin (#80768) 2024-02-06 08:28:15 -08:00
Kazu Hirata
2d861436a9 [clang] Remove remaining uses of llvm::Optional (NFC)
This patch removes several "using" declarations and #include
"llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 13:37:25 -08:00
Kazu Hirata
6ad0788c33 [clang] Use std::optional instead of llvm::Optional (NFC)
This patch replaces (llvm::|)Optional< with std::optional<.  I'll post
a separate patch to remove #include "llvm/ADT/Optional.h".

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 12:31:01 -08:00
Kazu Hirata
a1580d7b59 [clang] Add #include <optional> (NFC)
This patch adds #include <optional> to those files containing
llvm::Optional<...> or Optional<...>.

I'll post a separate patch to actually replace llvm::Optional with
std::optional.

This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-14 11:07:21 -08:00