[clang] [Clang] [NFC] Introduce `DynamicRecursiveASTVisitor` (PR #105195)

via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 21 07:11:17 PDT 2024


================
@@ -0,0 +1,252 @@
+#ifndef LLVM_CLANG_AST_DYNAMIC_RECURSIVE_AST_VISITOR_H
+#define LLVM_CLANG_AST_DYNAMIC_RECURSIVE_AST_VISITOR_H
+
+#include "clang/AST/Attr.h"
+#include "clang/AST/ExprConcepts.h"
+#include "clang/AST/TypeLoc.h"
+
+namespace clang {
+class ASTContext;
+
+/// Recursive AST visitor that supports extension via dynamic dispatch.
+///
+/// This only supports some of the more common visitation operations; in
+/// particular, it does not support overriding WalkUpFromX or post-order
+/// traversal.
+///
+/// Features that are currently not supported:
+///
+///   - Visiting attributes
+///   - Post-order traversal
+///   - Overriding WalkUpFromX
+///   - Overriding getStmtChildren()
+///
+/// \see RecursiveASTVisitor
+class DynamicRecursiveASTVisitor {
+public:
+  /// Whether this visitor should recurse into template instantiations.
+  bool ShouldVisitTemplateInstantiations = false;
+
+  /// Whether this visitor should recurse into the types of TypeLocs.
+  bool ShouldWalkTypesOfTypeLocs = true;
+
+  /// Whether this visitor should recurse into implicit code, e.g.
+  /// implicit constructors and destructors.
+  bool ShouldVisitImplicitCode = false;
+
+  /// Whether this visitor should recurse into lambda body
+  bool ShouldVisitLambdaBody = true;
+
+protected:
+  DynamicRecursiveASTVisitor() = default;
+
+public:
+  virtual void anchor();
+
+  // Copying/moving a polymorphic type is a bad idea.
+  DynamicRecursiveASTVisitor(DynamicRecursiveASTVisitor &&) = delete;
+  DynamicRecursiveASTVisitor(const DynamicRecursiveASTVisitor &) = delete;
+  DynamicRecursiveASTVisitor &operator=(DynamicRecursiveASTVisitor &&) = delete;
+  DynamicRecursiveASTVisitor &
+  operator=(const DynamicRecursiveASTVisitor &) = delete;
----------------
Sirraide wrote:

Afaik, you’re generally not supposed to make polymorphic types copyable or movable because of object slicing issues. It would be *possible* if you want to require everyone who uses it to be careful with it, but I don’t think we ever really need to copy or move an AST visitor, so I figured it would be easier to just disallow it.

https://github.com/llvm/llvm-project/pull/105195


More information about the cfe-commits mailing list