[clang] [Clang] [NFC] Introduce `DynamicRecursiveASTVisitor` (PR #110040)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Oct 22 04:56:04 PDT 2024
================
@@ -0,0 +1,268 @@
+//===--- DynamicRecursiveASTVisitor.h - Virtual AST 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 DynamicRecursiveASTVisitor interface, which acts
+// identically to RecursiveASTVisitor, except that it uses virtual dispatch
+// instead of CRTP, which greatly improves compile times and binary size.
+//
+// However, it also comes with limitations in that some of the more seldom
+// utilised features of RecursiveASTVisitor are not supported.
+//
+// Prefer to use this over RecursiveASTVisitor whenever possible.
+//
+//===----------------------------------------------------------------------===//
+#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.
+///
+/// Instead of functions (e.g. shouldVisitImplicitCode()), this class
+/// uses member variables (e.g. ShouldVisitImplicitCode) to control
+/// visitation behaviour.
+///
+/// RAV features that are NOT supported:
+///
+/// - Visiting attributes,
----------------
Sirraide wrote:
So for pretty much all the features listed here, the reason why they’re not supported is because the number of visitors across the entire codebase that use those features, from what I’ve seen so far, is about 3–5 (combined that is, not per feature); supporting e.g. visiting attributes or overriding WalkUpFromX is definitely possible, but it adds a lot more entries to the vtable and virtual calls to the visitation process, and at least @Endilll and I concluded that it wasn’t really worth it. We could add support for these later, but I just don’t think it’s necessary at the moment.
The exception is post-order traversal: I don’t see how implementing that in the dynamic visitor would be possible at all (iirc because part of the RAV implementation of post-order traversal only really works w/ CRTP).
https://github.com/llvm/llvm-project/pull/110040
More information about the cfe-commits
mailing list