Truly independent web browser
Meta: Adapt clang plugins to clang 22 AST APIs
Timothy Flynn2ce3c18416b8
diff --git a/Meta/ClangPlugins/KmallocAllocationPluginAction.cpp b/Meta/ClangPlugins/KmallocAllocationPluginAction.cppindex 8a3981a8268..5000afc45f6 100644--- a/Meta/ClangPlugins/KmallocAllocationPluginAction.cpp+++ b/Meta/ClangPlugins/KmallocAllocationPluginAction.cpp@@ -214,9 +214,9 @@ bool KmallocAllocationVisitor::TraverseTypeTraitExpr(clang::TypeTraitExpr* exprereturn traverse_unevaluated(expression, [&](auto* node) { return RecursiveASTVisitor::TraverseTypeTraitExpr(node); });}-bool KmallocAllocationVisitor::TraverseDecltypeTypeLoc(clang::DecltypeTypeLoc type_loc)+bool KmallocAllocationVisitor::TraverseDecltypeTypeLoc(clang::DecltypeTypeLoc type_loc, bool traverse_qualifier){- return traverse_unevaluated(type_loc, [&](auto node) { return RecursiveASTVisitor::TraverseDecltypeTypeLoc(node); });+ return traverse_unevaluated(type_loc, [&](auto node) { return RecursiveASTVisitor::TraverseDecltypeTypeLoc(node, traverse_qualifier); });}bool KmallocAllocationVisitor::VisitCXXNewExpr(clang::CXXNewExpr* new_expression)diff --git a/Meta/ClangPlugins/KmallocAllocationPluginAction.h b/Meta/ClangPlugins/KmallocAllocationPluginAction.hindex 6fbf3d01406..3a7508ea242 100644--- a/Meta/ClangPlugins/KmallocAllocationPluginAction.h+++ b/Meta/ClangPlugins/KmallocAllocationPluginAction.h@@ -32,7 +32,7 @@ public:bool TraverseCXXNoexceptExpr(clang::CXXNoexceptExpr*);bool TraverseUnaryExprOrTypeTraitExpr(clang::UnaryExprOrTypeTraitExpr*);bool TraverseTypeTraitExpr(clang::TypeTraitExpr*);- bool TraverseDecltypeTypeLoc(clang::DecltypeTypeLoc);+ bool TraverseDecltypeTypeLoc(clang::DecltypeTypeLoc, bool traverse_qualifier);bool VisitCXXNewExpr(clang::CXXNewExpr*);bool VisitCXXConstructExpr(clang::CXXConstructExpr*);bool VisitCXXUnresolvedConstructExpr(clang::CXXUnresolvedConstructExpr*);diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cppindex ac80d040d28..cc3da808f08 100644--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp@@ -179,19 +179,14 @@ static ContainsGCPtrResult record_contains_gc_ptr(clang::CXXRecordDecl const* restatic ContainsGCPtrResult type_contains_gc_ptr(clang::QualType const& type, std::set<clang::CXXRecordDecl const*>& visited){- // Handle elaborated types- clang::QualType actual_type = type;- if (auto const* elaborated = llvm::dyn_cast<clang::ElaboratedType>(type.getTypePtr()))- actual_type = elaborated->desugar();-// Check for JS::Value directly- if (auto const* record = actual_type->getAsCXXRecordDecl()) {+ if (auto const* record = type->getAsCXXRecordDecl()) {if (record->getQualifiedNameAsString() == "JS::Value")return ContainsGCPtrResult::Yes;}// Check for raw pointers to Cell types (these should use GC::Ptr instead)- if (auto const* pointer_type = actual_type->getAs<clang::PointerType>()) {+ if (auto const* pointer_type = type->getAs<clang::PointerType>()) {if (auto const* pointee = pointer_type->getPointeeCXXRecordDecl()) {if (pointee->hasDefinition() && record_inherits_from_cell(*pointee))return ContainsGCPtrResult::Yes;@@ -199,7 +194,7 @@ static ContainsGCPtrResult type_contains_gc_ptr(clang::QualType const& type, std}// Check for raw references to Cell types (these should use GC::Ref instead)- if (auto const* reference_type = actual_type->getAs<clang::ReferenceType>()) {+ if (auto const* reference_type = type->getAs<clang::ReferenceType>()) {if (auto const* pointee = reference_type->getPointeeCXXRecordDecl()) {if (pointee->hasDefinition() && record_inherits_from_cell(*pointee))return ContainsGCPtrResult::Yes;@@ -207,7 +202,7 @@ static ContainsGCPtrResult type_contains_gc_ptr(clang::QualType const& type, std}// Check for template specializations (GC::Ptr, GC::Ref, Vector, HashMap, etc.)- if (auto const* specialization = actual_type->getAs<clang::TemplateSpecializationType>()) {+ if (auto const* specialization = type->getAs<clang::TemplateSpecializationType>()) {auto template_name = specialization->getTemplateName().getAsTemplateDecl()->getQualifiedNameAsString();// Direct GC pointer types@@ -233,7 +228,7 @@ static ContainsGCPtrResult type_contains_gc_ptr(clang::QualType const& type, std}// Check for record types (structs/classes) that might contain GC pointers- if (auto const* record = actual_type->getAsCXXRecordDecl()) {+ if (auto const* record = type->getAsCXXRecordDecl()) {return record_contains_gc_ptr(record, visited);}@@ -348,11 +343,7 @@ static std::optional<QualTypeGCInfo> validate_qualified_type(clang::QualType constatic std::optional<QualTypeGCInfo> validate_field_qualified_type(clang::FieldDecl const* field_decl){- auto type = field_decl->getType();- if (auto const* elaborated_type = llvm::dyn_cast<clang::ElaboratedType>(type.getTypePtr()))- type = elaborated_type->desugar();-- for (auto const& qualified_type : get_all_qualified_types(type)) {+ for (auto const& qualified_type : get_all_qualified_types(field_decl->getType())) {if (auto error = validate_qualified_type(qualified_type))return error;}@@ -612,8 +603,6 @@ bool LibJSGCVisitor::VisitCXXRecordDecl(clang::CXXRecordDecl* record)if (!fields_that_are_visited.contains(field->getNameAsString())) {// Check if the substruct type has a visit_edges methodauto field_type = field->getType();- if (auto const* elaborated = llvm::dyn_cast<clang::ElaboratedType>(field_type.getTypePtr()))- field_type = elaborated->desugar();// For smart pointer types (OwnPtr, RefPtr, etc.), check the pointed-to typeclang::CXXRecordDecl const* type_to_check = nullptr;@@ -724,15 +713,7 @@ bool LibJSGCVisitor::VisitCXXMethodDecl(clang::CXXMethodDecl* method)continue;// Get the record decl that the qualifier refers to- auto const* qualifier = member_expr->getQualifier();- if (!qualifier)- continue;-- auto const* qualifier_type = qualifier->getAsType();- if (!qualifier_type)- continue;-- auto const* qualifier_record = qualifier_type->getAsCXXRecordDecl();+ auto const* qualifier_record = member_expr->getQualifier().getAsRecordDecl();if (!qualifier_record)continue;diff --git a/Tests/ClangPlugins/KmallocAllocationTests/decltype_allocation.cpp b/Tests/ClangPlugins/KmallocAllocationTests/decltype_allocation.cppnew file mode 100644index 00000000000..6913af45d7d--- /dev/null+++ b/Tests/ClangPlugins/KmallocAllocationTests/decltype_allocation.cpp@@ -0,0 +1,29 @@+/*+ * Copyright (c) 2026, Timothy Flynn <[email protected]>+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++// RUN: %clang++ -Xclang -verify %plugin_opts% -c %s -o %t 2>&1++// expected-note@+1 {{'Untagged' is defined here}}+struct Untagged {+};++// Allocations in decltype operands are unevaluated and must not trigger allocator diagnostics.+// expected-warning@+1 {{expression with side effects has no effect in an unevaluated context}}+using Pointer = decltype(new Untagged);++template<typename T>+using PointerTo = decltype(new T);++static_assert(__is_same(PointerTo<Untagged>, Pointer));++void test()+{+ // Traversing decltype must not suppress diagnostics for subsequent evaluated allocations.+ Pointer pointer;+ // expected-error@+1 {{'Untagged' is allocated with the system allocator. Add AK_ALLOC_WITH_KMALLOC to the class, or specialize AllocatedWithSystemAllocator or AllocatedWithCustomAllocator for it}}+ pointer = new Untagged;+ delete pointer;+}