From bfcf8cdbd72f368f3662002ab73fd3d9e39e94eb Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 29 Nov 2007 23:05:17 +0000 Subject: [PATCH] Added method "HandleTopLevelDeclaration" to ASTConsumer. This will eventually be the new hook that ASTStreamer calls to feed top-level Decls to ASTConsumers. The difference between "HandleTopLevelDeclaration" and "HandleTopLevelDecl" is that "HandleTopLevelDecl" is currently called by ASTStreamer for every top-level declaration, including those that appear within a Decl chain. Using the new interface, ASTStreamer would only call HandleTopLevelDeclaration for Decls that appear that the beginning of a Decl chain (i.e., a group of related decls). To preserve the behavior that all subclasses of ASTConsumer currently expect, the default implementation of HandleTopLevelDeclaration simply calls HandleTopLevelDecl, and for decl chains it calls HandleTopLevelDecl for each Decl* in a chain of Decls. The advantage of this interface is that some subclasses of ASTConsumer only really want the Decl chain, and not each individual Decl passed to them. This extra level of indirection allows subclasses to override the default behavior if they so desire. llvm-svn: 44444 --- clang/AST/ASTConsumer.cpp | 27 +++++++++++++++++++++++++++ clang/include/clang/AST/ASTConsumer.h | 19 +++++++++++++------ 2 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 clang/AST/ASTConsumer.cpp diff --git a/clang/AST/ASTConsumer.cpp b/clang/AST/ASTConsumer.cpp new file mode 100644 index 000000000000..0cf614102d02 --- /dev/null +++ b/clang/AST/ASTConsumer.cpp @@ -0,0 +1,27 @@ +//===--- ASTConsumer.cpp - Abstract interface for reading ASTs --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the ASTConsumer class. +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/ASTConsumer.h" +#include "clang/AST/Decl.h" + +using namespace clang; + +void ASTConsumer::HandleTopLevelDeclaration(Decl* d) { + if (ScopedDecl* sd = dyn_cast(d)) + while (sd) { + HandleTopLevelDecl(sd); + sd = sd->getNextDeclarator(); + } + else + HandleTopLevelDecl(d); +} diff --git a/clang/include/clang/AST/ASTConsumer.h b/clang/include/clang/AST/ASTConsumer.h index 160260f030bc..7221ab789b33 100644 --- a/clang/include/clang/AST/ASTConsumer.h +++ b/clang/include/clang/AST/ASTConsumer.h @@ -16,6 +16,7 @@ namespace clang { class ASTContext; + class Decl; /// ASTConsumer - This is an abstract interface that should be implemented by /// clients that read ASTs. This abstraction layer allows the client to be @@ -26,13 +27,19 @@ public: /// Initialize - This is called to initialize the consumer, providing the /// ASTContext and the file ID of the primary file. - virtual void Initialize(ASTContext &Context, unsigned MainFileID) { - } + virtual void Initialize(ASTContext &Context, unsigned MainFileID) {} - /// HandleTopLevelDecl - Handle the specified top-level declaration. - /// - virtual void HandleTopLevelDecl(Decl *D) { - } + /// HandleTopLevelDecl - Handle the specified top-level declaration. This is + /// called by HandleTopLevelDeclaration to process every top-level Decl*. + virtual void HandleTopLevelDecl(Decl *D) {}; + + + /// HandleTopLevelDeclaration - Handle the specified top-level declaration. + /// This is called only for Decl* that are the head of a chain of + /// Decl's (in the case that the Decl* is a ScopedDecl*). Subclasses + /// can override its behavior; by default it calls HandleTopLevelDecl + /// for every Decl* in a decl chain. + virtual void HandleTopLevelDeclaration(Decl *D); /// PrintStats - If desired, print any statistics. virtual void PrintStats() {