[clang] [WIP] Implement `-dump-deserialized-declaration-ranges` flag. (PR #133910)
Ilya Biryukov via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 4 06:16:27 PDT 2025
================
@@ -49,6 +51,150 @@ LLVM_INSTANTIATE_REGISTRY(FrontendPluginRegistry)
namespace {
+/// DeserializedDeclsLineRangePrinter dumps ranges of deserialized declarations
+/// to aid debugging and bug minimization. It implements ASTConsumer and
+/// ASTDeserializationListener, so that an object of
+/// DeserializedDeclsLineRangePrinter registers as its own listener. The
+/// ASTDeserializationListener interface provides the DeclRead callback that we
+/// use to collect the deserialized Decls. Note that printing or otherwise
+/// processing them as this point is dangerous, since that could trigger
+/// additional deserialization and crash compilation. Therefore, we process the
+/// collected Decls in HandleTranslationUnit method of ASTConsumer. This is a
+/// safe point, since we know that by this point all the Decls needed by the
+/// compiler frontend have been deserialized. In case our processing causes
+/// further deserialization, DeclRead from the listener might be called again.
+/// However, at that point we don't accept any more Decls for processing.
+class DeserializedDeclsLineRangePrinter : public ASTDeserializationListener,
+ public ASTConsumer {
+public:
+ explicit DeserializedDeclsLineRangePrinter(
+ SourceManager &SM, std::unique_ptr<llvm::raw_fd_ostream> OS)
+ : ASTDeserializationListener(), SM(SM), OS(std::move(OS)) {}
+
+ void DeclRead(GlobalDeclID ID, const Decl *D) override {
+ if (!IsCollectingDecls) {
+ return;
+ }
+ if (!D || isa<TranslationUnitDecl>(D) || isa<LinkageSpecDecl>(D) ||
+ isa<NamespaceDecl>(D))
+ return;
+ if (auto *DC = D->getDeclContext(); !DC || !DC->isFileContext())
----------------
ilya-biryukov wrote:
NIT: add a comment explaining why we only report top-level declarations.
In most cases, it's impossible to get a hold of non-top-level entities with deserializing the top-level entities they are a part of. Therefore we choose to work at namespace-level granularity to simplify the implementation and reduce the number of cases we need to care about.
I can also see us making this more fine-grained in the future for other things that get deserialized in a more fine-grained manner, e.g. for class members.
https://github.com/llvm/llvm-project/pull/133910
More information about the cfe-commits
mailing list