ladybird/Tests/ClangPlugins/LibJSGCTests/substruct_raw_pointers.cpp
Andreas Kling 95b464fb8b ClangPlugins: Check for GC pointers in non-Cell substruct members
This extends the LibJSGC Clang plugin to detect GC pointers (GC::Ptr,
GC::Ref, JS::Value, etc.) inside non-GC-allocated struct/class members.

When a GC::Cell has a member of a non-Cell type that contains GC
pointers, we now enforce that:

1. The non-Cell type must have a visit_edges(GC::Cell::Visitor&) method
2. The Cell's visit_edges must access that member (presumably to call
   its visit_edges)

The check works recursively, so nested structs and containers like
Vector<GC::Ptr<T>> or HashMap<K, GC::Ptr<V>> are handled correctly.

GC infrastructure types (Root, Heap, etc.) and AK library types are
excluded from these checks as they handle visitation differently.
2026-01-07 12:48:58 +01:00

37 lines
1.1 KiB
C++

/*
* Copyright (c) 2026, Andreas Kling <andreas@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
// RUN: %clang++ -Xclang -verify %plugin_opts% -c %s -o %t 2>&1
#include <LibGC/Cell.h>
#include <LibGC/Ptr.h>
// A substruct that contains a raw pointer to a Cell type (fine on stack)
struct SubStructWithRawPointer {
// expected-error@+1 {{pointer to GC::Cell type should be wrapped in GC::Ptr}}
GC::Cell* m_raw_ptr;
};
// A substruct that contains a raw reference to a Cell type (fine on stack)
struct SubStructWithRawReference {
// expected-error@+1 {{reference to GC::Cell type should be wrapped in GC::Ref}}
GC::Cell& m_raw_ref;
};
class TestClass : public GC::Cell {
GC_CELL(TestClass, GC::Cell);
virtual void visit_edges(Visitor& visitor) override
{
Base::visit_edges(visitor);
}
// expected-error@+1 {{Member m_ptr_substruct contains GC pointers but its type has no visit_edges method}}
SubStructWithRawPointer m_ptr_substruct;
// expected-error@+1 {{Member m_ref_substruct contains GC pointers but its type has no visit_edges method}}
SubStructWithRawReference m_ref_substruct;
};