diff options
-rw-r--r-- | include/clang/AST/Decl.h | 4 | ||||
-rw-r--r-- | include/clang/AST/DeclBase.h | 7 | ||||
-rw-r--r-- | lib/AST/Decl.cpp | 16 |
3 files changed, 27 insertions, 0 deletions
diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index 69a52869d8..e7c7496d8b 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -413,6 +413,8 @@ public: PreviousDeclaration = PrevDecl; } + virtual Decl *getPrimaryDecl() const; + /// hasLocalStorage - Returns true if a variable with function scope /// is a non-static local variable. bool hasLocalStorage() const { @@ -811,6 +813,8 @@ public: void setPreviousDeclaration(FunctionDecl * PrevDecl); + virtual Decl *getPrimaryDecl() const; + unsigned getBuiltinID(ASTContext &Context) const; unsigned getNumParmVarDeclsFromType() const; diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index 0bce2f84c7..019a0fe965 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -311,6 +311,13 @@ public: // be defined inside or outside a function etc). bool isDefinedOutsideFunctionOrMethod() const; + /// \brief When there are multiple re-declarations (e.g. for functions), + /// this will return the primary one which all of them point to. + virtual Decl *getPrimaryDecl() const { return const_cast<Decl*>(this); } + + /// \brief Whether this particular Decl is a primary one. + bool isPrimaryDecl() const { return getPrimaryDecl() == this; } + /// getBody - If this Decl represents a declaration for a body of code, /// such as a function or method definition, this method returns the /// top-level Stmt* of that body. Otherwise this method returns null. diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 3d02150b65..bcbf091a19 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -358,6 +358,14 @@ 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(); + + return const_cast<VarDecl *>(Prim); +} + //===----------------------------------------------------------------------===// // FunctionDecl Implementation //===----------------------------------------------------------------------===// @@ -569,6 +577,14 @@ FunctionDecl::setPreviousDeclaration(FunctionDecl *PrevDecl) { } } +Decl *FunctionDecl::getPrimaryDecl() const { + const FunctionDecl *Prim = this; + while (Prim->getPreviousDeclaration()) + Prim = Prim->getPreviousDeclaration(); + + return const_cast<FunctionDecl *>(Prim); +} + /// getOverloadedOperator - Which C++ overloaded operator this /// function represents, if any. OverloadedOperatorKind FunctionDecl::getOverloadedOperator() const { |