From: Jordan Rose Date: Fri, 28 Sep 2012 22:21:39 +0000 (+0000) Subject: Pull ScopeInfo implementation into its own file. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=62b379873d03911c9d8061411088a1298b37d93a;p=platform%2Fupstream%2Fllvm.git Pull ScopeInfo implementation into its own file. The infrastructure for -Warc-repeated-use-of-weak got a little too heavy to leave sitting at the top of Sema.cpp. No functionality change. llvm-svn: 164856 --- diff --git a/clang/include/clang/Sema/ScopeInfo.h b/clang/include/clang/Sema/ScopeInfo.h index c43da96..6f0fc16 100644 --- a/clang/include/clang/Sema/ScopeInfo.h +++ b/clang/include/clang/Sema/ScopeInfo.h @@ -7,7 +7,8 @@ // //===----------------------------------------------------------------------===// // -// This file defines FunctionScopeInfo and BlockScopeInfo. +// This file defines FunctionScopeInfo and its subclasses, which contain +// information about a single function, block, lambda, or method body. // //===----------------------------------------------------------------------===// diff --git a/clang/lib/Sema/CMakeLists.txt b/clang/lib/Sema/CMakeLists.txt index 872ac89..f554b5c 100644 --- a/clang/lib/Sema/CMakeLists.txt +++ b/clang/lib/Sema/CMakeLists.txt @@ -14,6 +14,7 @@ add_clang_library(clangSema IdentifierResolver.cpp JumpDiagnostics.cpp Scope.cpp + ScopeInfo.cpp Sema.cpp SemaAccess.cpp SemaAttr.cpp diff --git a/clang/lib/Sema/ScopeInfo.cpp b/clang/lib/Sema/ScopeInfo.cpp new file mode 100644 index 0000000..bb9420d --- /dev/null +++ b/clang/lib/Sema/ScopeInfo.cpp @@ -0,0 +1,181 @@ +//===--- ScopeInfo.cpp - Information about a semantic context -------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements FunctionScopeInfo and its subclasses, which contain +// information about a single function, block, lambda, or method body. +// +//===----------------------------------------------------------------------===// + +#include "clang/Sema/ScopeInfo.h" +#include "clang/AST/Decl.h" +#include "clang/AST/DeclObjC.h" +#include "clang/AST/Expr.h" +#include "clang/AST/ExprCXX.h" +#include "clang/AST/ExprObjC.h" + +using namespace clang; +using namespace sema; + +void FunctionScopeInfo::Clear() { + HasBranchProtectedScope = false; + HasBranchIntoScope = false; + HasIndirectGoto = false; + + SwitchStack.clear(); + Returns.clear(); + ErrorTrap.reset(); + PossiblyUnreachableDiags.clear(); + WeakObjectUses.clear(); +} + +static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) { + if (PropE->isExplicitProperty()) + return PropE->getExplicitProperty(); + + return PropE->getImplicitPropertyGetter(); +} + +static bool isSelfExpr(const Expr *E) { + E = E->IgnoreParenImpCasts(); + + const DeclRefExpr *DRE = dyn_cast(E); + if (!DRE) + return false; + + const ImplicitParamDecl *Param = dyn_cast(DRE->getDecl()); + if (!Param) + return false; + + const ObjCMethodDecl *M = dyn_cast(Param->getDeclContext()); + if (!M) + return false; + + return M->getSelfDecl() == Param; +} + +FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy +FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) { + E = E->IgnoreParenCasts(); + + const NamedDecl *D = 0; + bool IsExact = false; + + switch (E->getStmtClass()) { + case Stmt::DeclRefExprClass: + D = cast(E)->getDecl(); + IsExact = isa(D); + break; + case Stmt::MemberExprClass: { + const MemberExpr *ME = cast(E); + D = ME->getMemberDecl(); + IsExact = isa(ME->getBase()->IgnoreParenImpCasts()); + break; + } + case Stmt::ObjCIvarRefExprClass: { + const ObjCIvarRefExpr *IE = cast(E); + D = IE->getDecl(); + IsExact = isSelfExpr(IE->getBase()); + break; + } + case Stmt::PseudoObjectExprClass: { + const PseudoObjectExpr *POE = cast(E); + const ObjCPropertyRefExpr *BaseProp = + dyn_cast(POE->getSyntacticForm()); + if (BaseProp) { + D = getBestPropertyDecl(BaseProp); + + const Expr *DoubleBase = BaseProp->getBase(); + if (const OpaqueValueExpr *OVE = dyn_cast(DoubleBase)) + DoubleBase = OVE->getSourceExpr(); + + IsExact = isSelfExpr(DoubleBase); + } + break; + } + default: + break; + } + + return BaseInfoTy(D, IsExact); +} + + +FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( + const ObjCPropertyRefExpr *PropE) + : Base(0, true), Property(getBestPropertyDecl(PropE)) { + + if (PropE->isObjectReceiver()) { + const OpaqueValueExpr *OVE = cast(PropE->getBase()); + const Expr *E = OVE->getSourceExpr(); + Base = getBaseInfo(E); + } else if (PropE->isClassReceiver()) { + Base.setPointer(PropE->getClassReceiver()); + } else { + assert(PropE->isSuperReceiver()); + } +} + +FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( + const DeclRefExpr *DRE) + : Base(0, true), Property(DRE->getDecl()) { + assert(isa(Property)); +} + +FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( + const ObjCIvarRefExpr *IvarE) + : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { +} + +void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { + E = E->IgnoreParenImpCasts(); + + if (const PseudoObjectExpr *POE = dyn_cast(E)) { + markSafeWeakUse(POE->getSyntacticForm()); + return; + } + + if (const ConditionalOperator *Cond = dyn_cast(E)) { + markSafeWeakUse(Cond->getTrueExpr()); + markSafeWeakUse(Cond->getFalseExpr()); + return; + } + + if (const BinaryConditionalOperator *Cond = + dyn_cast(E)) { + markSafeWeakUse(Cond->getCommon()); + markSafeWeakUse(Cond->getFalseExpr()); + return; + } + + // Has this weak object been seen before? + FunctionScopeInfo::WeakObjectUseMap::iterator Uses; + if (const ObjCPropertyRefExpr *RefExpr = dyn_cast(E)) + Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(RefExpr)); + else if (const ObjCIvarRefExpr *IvarE = dyn_cast(E)) + Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(IvarE)); + else if (const DeclRefExpr *DRE = dyn_cast(E)) + Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(DRE)); + else + return; + + if (Uses == WeakObjectUses.end()) + return; + + // Has there been a read from the object using this Expr? + FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = + std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true)); + if (ThisUse == Uses->second.rend()) + return; + + ThisUse->markSafe(); +} + +FunctionScopeInfo::~FunctionScopeInfo() { } +BlockScopeInfo::~BlockScopeInfo() { } +LambdaScopeInfo::~LambdaScopeInfo() { } diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 8555562..9a6cfaa 100644 --- a/clang/lib/Sema/Sema.cpp +++ b/clang/lib/Sema/Sema.cpp @@ -43,165 +43,6 @@ using namespace clang; using namespace sema; -FunctionScopeInfo::~FunctionScopeInfo() { } - -void FunctionScopeInfo::Clear() { - HasBranchProtectedScope = false; - HasBranchIntoScope = false; - HasIndirectGoto = false; - - SwitchStack.clear(); - Returns.clear(); - ErrorTrap.reset(); - PossiblyUnreachableDiags.clear(); - WeakObjectUses.clear(); -} - -static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) { - if (PropE->isExplicitProperty()) - return PropE->getExplicitProperty(); - - return PropE->getImplicitPropertyGetter(); -} - -static bool isSelfExpr(const Expr *E) { - E = E->IgnoreParenImpCasts(); - - const DeclRefExpr *DRE = dyn_cast(E); - if (!DRE) - return false; - - const ImplicitParamDecl *Param = dyn_cast(DRE->getDecl()); - if (!Param) - return false; - - const ObjCMethodDecl *M = dyn_cast(Param->getDeclContext()); - if (!M) - return false; - - return M->getSelfDecl() == Param; -} - -FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy -FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) { - E = E->IgnoreParenCasts(); - - const NamedDecl *D = 0; - bool IsExact = false; - - switch (E->getStmtClass()) { - case Stmt::DeclRefExprClass: - D = cast(E)->getDecl(); - IsExact = isa(D); - break; - case Stmt::MemberExprClass: { - const MemberExpr *ME = cast(E); - D = ME->getMemberDecl(); - IsExact = isa(ME->getBase()->IgnoreParenImpCasts()); - break; - } - case Stmt::ObjCIvarRefExprClass: { - const ObjCIvarRefExpr *IE = cast(E); - D = IE->getDecl(); - IsExact = isSelfExpr(IE->getBase()); - break; - } - case Stmt::PseudoObjectExprClass: { - const PseudoObjectExpr *POE = cast(E); - const ObjCPropertyRefExpr *BaseProp = - dyn_cast(POE->getSyntacticForm()); - if (BaseProp) { - D = getBestPropertyDecl(BaseProp); - - const Expr *DoubleBase = BaseProp->getBase(); - if (const OpaqueValueExpr *OVE = dyn_cast(DoubleBase)) - DoubleBase = OVE->getSourceExpr(); - - IsExact = isSelfExpr(DoubleBase); - } - break; - } - default: - break; - } - - return BaseInfoTy(D, IsExact); -} - - -FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( - const ObjCPropertyRefExpr *PropE) - : Base(0, true), Property(getBestPropertyDecl(PropE)) { - - if (PropE->isObjectReceiver()) { - const OpaqueValueExpr *OVE = cast(PropE->getBase()); - const Expr *E = OVE->getSourceExpr(); - Base = getBaseInfo(E); - } else if (PropE->isClassReceiver()) { - Base.setPointer(PropE->getClassReceiver()); - } else { - assert(PropE->isSuperReceiver()); - } -} - -FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( - const DeclRefExpr *DRE) - : Base(0, true), Property(DRE->getDecl()) { - assert(isa(Property)); -} - -FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy( - const ObjCIvarRefExpr *IvarE) - : Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) { -} - -void FunctionScopeInfo::markSafeWeakUse(const Expr *E) { - E = E->IgnoreParenImpCasts(); - - if (const PseudoObjectExpr *POE = dyn_cast(E)) { - markSafeWeakUse(POE->getSyntacticForm()); - return; - } - - if (const ConditionalOperator *Cond = dyn_cast(E)) { - markSafeWeakUse(Cond->getTrueExpr()); - markSafeWeakUse(Cond->getFalseExpr()); - return; - } - - if (const BinaryConditionalOperator *Cond = - dyn_cast(E)) { - markSafeWeakUse(Cond->getCommon()); - markSafeWeakUse(Cond->getFalseExpr()); - return; - } - - // Has this weak object been seen before? - FunctionScopeInfo::WeakObjectUseMap::iterator Uses; - if (const ObjCPropertyRefExpr *RefExpr = dyn_cast(E)) - Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(RefExpr)); - else if (const ObjCIvarRefExpr *IvarE = dyn_cast(E)) - Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(IvarE)); - else if (const DeclRefExpr *DRE = dyn_cast(E)) - Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(DRE)); - else - return; - - if (Uses == WeakObjectUses.end()) - return; - - // Has there been a read from the object using this Expr? - FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse = - std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true)); - if (ThisUse == Uses->second.rend()) - return; - - ThisUse->markSafe(); -} - -BlockScopeInfo::~BlockScopeInfo() { } -LambdaScopeInfo::~LambdaScopeInfo() { } - PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context, const Preprocessor &PP) { PrintingPolicy Policy = Context.getPrintingPolicy();