From: Zhongxing Xu Date: Fri, 17 Jul 2009 07:29:51 +0000 (+0000) Subject: Refactor code into a new CallExpr::getDirectCallee() method. Simplify some X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a00425414e8c209cabc25d1826b200aeb94259af;p=clang Refactor code into a new CallExpr::getDirectCallee() method. Simplify some code with the new method. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76164 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 1fc35ba8d5..9d967dec03 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -964,7 +964,10 @@ public: const Expr *getCallee() const { return cast(SubExprs[FN]); } Expr *getCallee() { return cast(SubExprs[FN]); } void setCallee(Expr *F) { SubExprs[FN] = F; } - + + /// \brief If the callee is a FunctionDecl, return it. Otherwise return 0. + FunctionDecl *getDirectCallee(); + /// getNumArgs - Return the number of actual arguments to this call. /// unsigned getNumArgs() const { return NumArgs; } diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp index 63a6d3153c..320a4f1f72 100644 --- a/lib/AST/Expr.cpp +++ b/lib/AST/Expr.cpp @@ -224,6 +224,16 @@ void CallExpr::Destroy(ASTContext& C) { C.Deallocate(this); } +FunctionDecl *CallExpr::getDirectCallee() { + Expr *CEE = getCallee()->IgnoreParenCasts(); + if (DeclRefExpr *DRE = dyn_cast(CEE)) { + // FIXME: We can follow objective-c methods and C++ member functions... + return dyn_cast(DRE->getDecl()); + } + + return 0; +} + /// setNumArgs - This changes the number of arguments present in this call. /// Any orphaned expressions are deleted by this, and any new operands are set /// to null. diff --git a/lib/Analysis/CFG.cpp b/lib/Analysis/CFG.cpp index 884bc177fd..02fe22c00f 100644 --- a/lib/Analysis/CFG.cpp +++ b/lib/Analysis/CFG.cpp @@ -475,14 +475,9 @@ CFGBlock* CFGBuilder::WalkAST(Stmt* Terminator, bool AlwaysAddStmt = false) { case Stmt::CallExprClass: { bool NoReturn = false; CallExpr *C = cast(Terminator); - Expr *CEE = C->getCallee()->IgnoreParenCasts(); - if (DeclRefExpr *DRE = dyn_cast(CEE)) { - // FIXME: We can follow objective-c methods and C++ member functions... - if (FunctionDecl *FD = dyn_cast(DRE->getDecl())) { - if (FD->hasAttr()) - NoReturn = true; - } - } + if (FunctionDecl *FD = C->getDirectCallee()) + if (FD->hasAttr()) + NoReturn = true; if (!NoReturn) break; diff --git a/lib/Analysis/CallGraph.cpp b/lib/Analysis/CallGraph.cpp index 16f14d787f..d49e8ec11b 100644 --- a/lib/Analysis/CallGraph.cpp +++ b/lib/Analysis/CallGraph.cpp @@ -66,21 +66,10 @@ public: } void CGBuilder::VisitCallExpr(CallExpr *CE) { - Expr *Callee = CE->getCallee(); - - if (CastExpr *CE = dyn_cast(Callee)) - Callee = CE->getSubExpr(); - - if (DeclRefExpr *DRE = dyn_cast(Callee)) { - Decl *D = DRE->getDecl(); - if (FunctionDecl *CalleeDecl = dyn_cast(D)) { - - Entity *Ent = Entity::get(CalleeDecl, G.getProgram()); - - CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent); - - CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode); - } + if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) { + Entity *Ent = Entity::get(CalleeDecl, G.getProgram()); + CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent); + CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode); } }