================
@@ -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;
+  template <typename Class>
+  using non_ptr_t = typename std::remove_pointer<ptr_t<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 (non_ptr_t<CXXBaseSpecifier &> BS : CRD->bases()) {
+        if (getDerived().visitBaseSpecifierPre(&BS))
+          getDerived().visit(BS.getType());
+        getDerived().visitBaseSpecifierPost(&BS);
----------------
Fznamznon wrote:

No, it doesn't. Here is a const version of bases() which returns 
const_iterator_range 
https://clang.llvm.org/doxygen/classclang_1_1CXXRecordDecl.html#a6bfd1901e4d4fd2e05a62613249bce1a
 which, surprise, has const in it 
https://clang.llvm.org/doxygen/DeclCXX_8h_source.html#l00520 .
The error I see is

```
source/llorg/llvm-project/clang/include/clang/AST/SubobjectVisitor.h:63:7: 
error: binding reference of type ‘clang::CXXBaseSpecifier&’ to ‘const 
clang::CXXBaseSpecifier’ discards qualifiers
   63 |       for (CXXBaseSpecifier & BS : CRD->bases()) {
      |       ^~~
source/llorg/llvm-project/clang/include/clang/AST/SubobjectVisitor.h:63:31: 
warning: loop variable ‘BS’ of type ‘clang::CXXBaseSpecifier&’ binds to a 
temporary constructed from type ‘const clang::CXXBaseSpecif
ier’ [-Wrange-loop-construct]
   63 |       for (CXXBaseSpecifier & BS : CRD->bases()) {
      |                               ^~
source/llorg/llvm-project/clang/include/clang/AST/SubobjectVisitor.h:63:31: 
note: use non-reference type ‘clang::CXXBaseSpecifier’ to make the copy 
explicit or ‘const clang::CXXBaseSpecifier&’ to prevent copying
```
There is no ArrayRef<CXXBaseSpecifier> version of `CXXRecordDecl::bases()`, you 
are probably confused by 
https://github.com/llvm/llvm-project/blob/4a955e932e3a622c601e8fbd78f975191c0ea856/clang/include/clang/AST/DeclCXX.h#L368
 which is a member of the inner class `DefinitionData` .

https://github.com/llvm/llvm-project/pull/192957
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to