From 7bf792fdde4641d865eba4a068d862d5300bd1e4 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Tue, 14 Jul 2009 03:19:57 +0000 Subject: [PATCH] Introduce FunctionDecl::getFirstDeclaration() and VarDecl::getFirstDeclaration(). For multiple redeclarations they return the first one. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@75602 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Decl.h | 8 ++++++++ lib/AST/Decl.cpp | 28 ++++++++++++++++++---------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index d033c24046..e83833f7d9 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -413,6 +413,10 @@ public: PreviousDeclaration = PrevDecl; } + /// \brief For multiple redeclarations returns the first one, otherwise + /// returns itself. + const VarDecl *getFirstDeclaration() const; + virtual Decl *getPrimaryDecl() const; /// hasLocalStorage - Returns true if a variable with function scope @@ -811,6 +815,10 @@ public: return PreviousDeclaration; } + /// \brief For multiple redeclarations returns the first one, otherwise + /// returns itself. + const FunctionDecl *getFirstDeclaration() const; + void setPreviousDeclaration(FunctionDecl * PrevDecl); virtual Decl *getPrimaryDecl() const; diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index fe9885ef26..5ef0c09093 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -358,12 +358,16 @@ const Expr *VarDecl::getDefinition(const VarDecl *&Def) const { return Def? Def->getInit() : 0; } -Decl *VarDecl::getPrimaryDecl() const { - const VarDecl *Prim = this; - while (Prim->getPreviousDeclaration()) - Prim = Prim->getPreviousDeclaration(); +const VarDecl *VarDecl::getFirstDeclaration() const { + const VarDecl *First = this; + while (First->getPreviousDeclaration()) + First = First->getPreviousDeclaration(); + + return First; +} - return const_cast(Prim); +Decl *VarDecl::getPrimaryDecl() const { + return const_cast(getFirstDeclaration()); } //===----------------------------------------------------------------------===// @@ -577,12 +581,16 @@ FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { } } -Decl *FunctionDecl::getPrimaryDecl() const { - const FunctionDecl *Prim = this; - while (Prim->getPreviousDeclaration()) - Prim = Prim->getPreviousDeclaration(); +const FunctionDecl *FunctionDecl::getFirstDeclaration() const { + const FunctionDecl *First = this; + while (First->getPreviousDeclaration()) + First = First->getPreviousDeclaration(); + + return First; +} - return const_cast(Prim); +Decl *FunctionDecl::getPrimaryDecl() const { + return const_cast(getFirstDeclaration()); } /// getOverloadedOperator - Which C++ overloaded operator this -- 2.50.1