Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit a55d32d

Browse files
committed
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@164856 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 7a27048 commit a55d32d

File tree

4 files changed

+184
-160
lines changed

4 files changed

+184
-160
lines changed

Diff for: include/clang/Sema/ScopeInfo.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99
//
10-
// This file defines FunctionScopeInfo and BlockScopeInfo.
10+
// This file defines FunctionScopeInfo and its subclasses, which contain
11+
// information about a single function, block, lambda, or method body.
1112
//
1213
//===----------------------------------------------------------------------===//
1314

Diff for: lib/Sema/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ add_clang_library(clangSema
1414
IdentifierResolver.cpp
1515
JumpDiagnostics.cpp
1616
Scope.cpp
17+
ScopeInfo.cpp
1718
Sema.cpp
1819
SemaAccess.cpp
1920
SemaAttr.cpp

Diff for: lib/Sema/ScopeInfo.cpp

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//===--- ScopeInfo.cpp - Information about a semantic context -------------===//
2+
//
3+
// The LLVM Compiler Infrastructure
4+
//
5+
// This file is distributed under the University of Illinois Open Source
6+
// License. See LICENSE.TXT for details.
7+
//
8+
//===----------------------------------------------------------------------===//
9+
//
10+
// This file implements FunctionScopeInfo and its subclasses, which contain
11+
// information about a single function, block, lambda, or method body.
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "clang/Sema/ScopeInfo.h"
16+
#include "clang/AST/Decl.h"
17+
#include "clang/AST/DeclObjC.h"
18+
#include "clang/AST/Expr.h"
19+
#include "clang/AST/ExprCXX.h"
20+
#include "clang/AST/ExprObjC.h"
21+
22+
using namespace clang;
23+
using namespace sema;
24+
25+
void FunctionScopeInfo::Clear() {
26+
HasBranchProtectedScope = false;
27+
HasBranchIntoScope = false;
28+
HasIndirectGoto = false;
29+
30+
SwitchStack.clear();
31+
Returns.clear();
32+
ErrorTrap.reset();
33+
PossiblyUnreachableDiags.clear();
34+
WeakObjectUses.clear();
35+
}
36+
37+
static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
38+
if (PropE->isExplicitProperty())
39+
return PropE->getExplicitProperty();
40+
41+
return PropE->getImplicitPropertyGetter();
42+
}
43+
44+
static bool isSelfExpr(const Expr *E) {
45+
E = E->IgnoreParenImpCasts();
46+
47+
const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
48+
if (!DRE)
49+
return false;
50+
51+
const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
52+
if (!Param)
53+
return false;
54+
55+
const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
56+
if (!M)
57+
return false;
58+
59+
return M->getSelfDecl() == Param;
60+
}
61+
62+
FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
63+
FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
64+
E = E->IgnoreParenCasts();
65+
66+
const NamedDecl *D = 0;
67+
bool IsExact = false;
68+
69+
switch (E->getStmtClass()) {
70+
case Stmt::DeclRefExprClass:
71+
D = cast<DeclRefExpr>(E)->getDecl();
72+
IsExact = isa<VarDecl>(D);
73+
break;
74+
case Stmt::MemberExprClass: {
75+
const MemberExpr *ME = cast<MemberExpr>(E);
76+
D = ME->getMemberDecl();
77+
IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
78+
break;
79+
}
80+
case Stmt::ObjCIvarRefExprClass: {
81+
const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
82+
D = IE->getDecl();
83+
IsExact = isSelfExpr(IE->getBase());
84+
break;
85+
}
86+
case Stmt::PseudoObjectExprClass: {
87+
const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
88+
const ObjCPropertyRefExpr *BaseProp =
89+
dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
90+
if (BaseProp) {
91+
D = getBestPropertyDecl(BaseProp);
92+
93+
const Expr *DoubleBase = BaseProp->getBase();
94+
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
95+
DoubleBase = OVE->getSourceExpr();
96+
97+
IsExact = isSelfExpr(DoubleBase);
98+
}
99+
break;
100+
}
101+
default:
102+
break;
103+
}
104+
105+
return BaseInfoTy(D, IsExact);
106+
}
107+
108+
109+
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
110+
const ObjCPropertyRefExpr *PropE)
111+
: Base(0, true), Property(getBestPropertyDecl(PropE)) {
112+
113+
if (PropE->isObjectReceiver()) {
114+
const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
115+
const Expr *E = OVE->getSourceExpr();
116+
Base = getBaseInfo(E);
117+
} else if (PropE->isClassReceiver()) {
118+
Base.setPointer(PropE->getClassReceiver());
119+
} else {
120+
assert(PropE->isSuperReceiver());
121+
}
122+
}
123+
124+
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
125+
const DeclRefExpr *DRE)
126+
: Base(0, true), Property(DRE->getDecl()) {
127+
assert(isa<VarDecl>(Property));
128+
}
129+
130+
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
131+
const ObjCIvarRefExpr *IvarE)
132+
: Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
133+
}
134+
135+
void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
136+
E = E->IgnoreParenImpCasts();
137+
138+
if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
139+
markSafeWeakUse(POE->getSyntacticForm());
140+
return;
141+
}
142+
143+
if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
144+
markSafeWeakUse(Cond->getTrueExpr());
145+
markSafeWeakUse(Cond->getFalseExpr());
146+
return;
147+
}
148+
149+
if (const BinaryConditionalOperator *Cond =
150+
dyn_cast<BinaryConditionalOperator>(E)) {
151+
markSafeWeakUse(Cond->getCommon());
152+
markSafeWeakUse(Cond->getFalseExpr());
153+
return;
154+
}
155+
156+
// Has this weak object been seen before?
157+
FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
158+
if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
159+
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(RefExpr));
160+
else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
161+
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(IvarE));
162+
else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
163+
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(DRE));
164+
else
165+
return;
166+
167+
if (Uses == WeakObjectUses.end())
168+
return;
169+
170+
// Has there been a read from the object using this Expr?
171+
FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
172+
std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
173+
if (ThisUse == Uses->second.rend())
174+
return;
175+
176+
ThisUse->markSafe();
177+
}
178+
179+
FunctionScopeInfo::~FunctionScopeInfo() { }
180+
BlockScopeInfo::~BlockScopeInfo() { }
181+
LambdaScopeInfo::~LambdaScopeInfo() { }

Diff for: lib/Sema/Sema.cpp

-159
Original file line numberDiff line numberDiff line change
@@ -43,165 +43,6 @@
4343
using namespace clang;
4444
using namespace sema;
4545

46-
FunctionScopeInfo::~FunctionScopeInfo() { }
47-
48-
void FunctionScopeInfo::Clear() {
49-
HasBranchProtectedScope = false;
50-
HasBranchIntoScope = false;
51-
HasIndirectGoto = false;
52-
53-
SwitchStack.clear();
54-
Returns.clear();
55-
ErrorTrap.reset();
56-
PossiblyUnreachableDiags.clear();
57-
WeakObjectUses.clear();
58-
}
59-
60-
static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
61-
if (PropE->isExplicitProperty())
62-
return PropE->getExplicitProperty();
63-
64-
return PropE->getImplicitPropertyGetter();
65-
}
66-
67-
static bool isSelfExpr(const Expr *E) {
68-
E = E->IgnoreParenImpCasts();
69-
70-
const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
71-
if (!DRE)
72-
return false;
73-
74-
const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl());
75-
if (!Param)
76-
return false;
77-
78-
const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext());
79-
if (!M)
80-
return false;
81-
82-
return M->getSelfDecl() == Param;
83-
}
84-
85-
FunctionScopeInfo::WeakObjectProfileTy::BaseInfoTy
86-
FunctionScopeInfo::WeakObjectProfileTy::getBaseInfo(const Expr *E) {
87-
E = E->IgnoreParenCasts();
88-
89-
const NamedDecl *D = 0;
90-
bool IsExact = false;
91-
92-
switch (E->getStmtClass()) {
93-
case Stmt::DeclRefExprClass:
94-
D = cast<DeclRefExpr>(E)->getDecl();
95-
IsExact = isa<VarDecl>(D);
96-
break;
97-
case Stmt::MemberExprClass: {
98-
const MemberExpr *ME = cast<MemberExpr>(E);
99-
D = ME->getMemberDecl();
100-
IsExact = isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts());
101-
break;
102-
}
103-
case Stmt::ObjCIvarRefExprClass: {
104-
const ObjCIvarRefExpr *IE = cast<ObjCIvarRefExpr>(E);
105-
D = IE->getDecl();
106-
IsExact = isSelfExpr(IE->getBase());
107-
break;
108-
}
109-
case Stmt::PseudoObjectExprClass: {
110-
const PseudoObjectExpr *POE = cast<PseudoObjectExpr>(E);
111-
const ObjCPropertyRefExpr *BaseProp =
112-
dyn_cast<ObjCPropertyRefExpr>(POE->getSyntacticForm());
113-
if (BaseProp) {
114-
D = getBestPropertyDecl(BaseProp);
115-
116-
const Expr *DoubleBase = BaseProp->getBase();
117-
if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(DoubleBase))
118-
DoubleBase = OVE->getSourceExpr();
119-
120-
IsExact = isSelfExpr(DoubleBase);
121-
}
122-
break;
123-
}
124-
default:
125-
break;
126-
}
127-
128-
return BaseInfoTy(D, IsExact);
129-
}
130-
131-
132-
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
133-
const ObjCPropertyRefExpr *PropE)
134-
: Base(0, true), Property(getBestPropertyDecl(PropE)) {
135-
136-
if (PropE->isObjectReceiver()) {
137-
const OpaqueValueExpr *OVE = cast<OpaqueValueExpr>(PropE->getBase());
138-
const Expr *E = OVE->getSourceExpr();
139-
Base = getBaseInfo(E);
140-
} else if (PropE->isClassReceiver()) {
141-
Base.setPointer(PropE->getClassReceiver());
142-
} else {
143-
assert(PropE->isSuperReceiver());
144-
}
145-
}
146-
147-
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
148-
const DeclRefExpr *DRE)
149-
: Base(0, true), Property(DRE->getDecl()) {
150-
assert(isa<VarDecl>(Property));
151-
}
152-
153-
FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy(
154-
const ObjCIvarRefExpr *IvarE)
155-
: Base(getBaseInfo(IvarE->getBase())), Property(IvarE->getDecl()) {
156-
}
157-
158-
void FunctionScopeInfo::markSafeWeakUse(const Expr *E) {
159-
E = E->IgnoreParenImpCasts();
160-
161-
if (const PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) {
162-
markSafeWeakUse(POE->getSyntacticForm());
163-
return;
164-
}
165-
166-
if (const ConditionalOperator *Cond = dyn_cast<ConditionalOperator>(E)) {
167-
markSafeWeakUse(Cond->getTrueExpr());
168-
markSafeWeakUse(Cond->getFalseExpr());
169-
return;
170-
}
171-
172-
if (const BinaryConditionalOperator *Cond =
173-
dyn_cast<BinaryConditionalOperator>(E)) {
174-
markSafeWeakUse(Cond->getCommon());
175-
markSafeWeakUse(Cond->getFalseExpr());
176-
return;
177-
}
178-
179-
// Has this weak object been seen before?
180-
FunctionScopeInfo::WeakObjectUseMap::iterator Uses;
181-
if (const ObjCPropertyRefExpr *RefExpr = dyn_cast<ObjCPropertyRefExpr>(E))
182-
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(RefExpr));
183-
else if (const ObjCIvarRefExpr *IvarE = dyn_cast<ObjCIvarRefExpr>(E))
184-
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(IvarE));
185-
else if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
186-
Uses = WeakObjectUses.find(FunctionScopeInfo::WeakObjectProfileTy(DRE));
187-
else
188-
return;
189-
190-
if (Uses == WeakObjectUses.end())
191-
return;
192-
193-
// Has there been a read from the object using this Expr?
194-
FunctionScopeInfo::WeakUseVector::reverse_iterator ThisUse =
195-
std::find(Uses->second.rbegin(), Uses->second.rend(), WeakUseTy(E, true));
196-
if (ThisUse == Uses->second.rend())
197-
return;
198-
199-
ThisUse->markSafe();
200-
}
201-
202-
BlockScopeInfo::~BlockScopeInfo() { }
203-
LambdaScopeInfo::~LambdaScopeInfo() { }
204-
20546
PrintingPolicy Sema::getPrintingPolicy(const ASTContext &Context,
20647
const Preprocessor &PP) {
20748
PrintingPolicy Policy = Context.getPrintingPolicy();

0 commit comments

Comments
 (0)