================
@@ -0,0 +1,98 @@
+//===---------- SubobjectVisitor.h - Subobject Visitor ----------*- C++
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the SubobjectVisitor interface, which recursively
+// traverses subobjects within a type.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_CLANG_AST_SUBOBJECTVISITOR_H
+#define LLVM_CLANG_AST_SUBOBJECTVISITOR_H
+
+#include "clang/AST/Type.h"
+
+namespace clang {
+
+template <template <typename> class Ptr, typename Derived>
+class SubobjectVisitorBase {
+ ASTContext &Ctx;
+ template <typename Class> using ptr_t = typename Ptr<Class>::type;
+
+public:
+ SubobjectVisitorBase(ASTContext &Ctx) : Ctx(Ctx) {}
+
+ /// Return a reference to the derived class.
+ Derived &getDerived() { return *static_cast<Derived *>(this); }
+
+ void visit(QualType QT) {
+ // If the type is an array, visit its element type. Separate traversal of
+ // arrays is not needed because the array will be encountered as a
+ // FieldDecl.
+
+ if (QT->isArrayType()) {
+ QualType ElTy =
+ cast<ConstantArrayType>(Ctx.getAsArrayType(QT))->getElementType();
+ getDerived().visit(ElTy);
+ return;
+ }
+
+ if (ptr_t<RecordDecl> RD = QT->getAsRecordDecl()) {
+ getDerived().traverseRecord(RD);
+ return;
+ }
+ }
+
+ void traverseRecord(ptr_t<RecordDecl> RD) {
+ if (ptr_t<CXXRecordDecl> CRD = dyn_cast<CXXRecordDecl>(RD)) {
+ for (CXXBaseSpecifier& BS : CRD->bases()) {
+ if (!getDerived().visitBaseSpecifierPre(&BS))
+ continue;
+ getDerived().visit(BS.getType());
+ getDerived().visitBaseSpecifierPost(&BS);
+ }
+ }
+ for (ptr_t<FieldDecl> FD : RD->fields()) {
+ if (!getDerived().visitFieldDeclPre(FD))
+ continue;
+ getDerived().visit(FD->getType());
+ getDerived().visitFieldDeclPost(FD);
+ }
----------------
tahonermann wrote:
Per comment elsewhere, I gave you a bad implementation of `traverseRecord()`. I
think the post-traversal visitation should always be performed; only the call
to `visit()` should be skipped.
```suggestion
if (ptr_t<CXXRecordDecl> CRD = dyn_cast<CXXRecordDecl>(RD)) {
for (CXXBaseSpecifier& BS : CRD->bases()) {
if (getDerived().visitBaseSpecifierPre(&BS))
getDerived().visit(BS.getType());
getDerived().visitBaseSpecifierPost(&BS);
}
}
for (ptr_t<FieldDecl> FD : RD->fields()) {
if (getDerived().visitFieldDeclPre(FD))
getDerived().visit(FD->getType());
getDerived().visitFieldDeclPost(FD);
}
```
https://github.com/llvm/llvm-project/pull/192957
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits