blob: a8e31d2cfa2b8af2c2f4af6e9de7e198fbcaebf3 [file] [log] [blame]
Chris Lattner5af280c2009-04-19 04:46:21 +00001//===--- JumpDiagnostics.cpp - Analyze Jump Targets for VLA issues --------===//
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 the JumpScopeChecker class, which is used to diagnose
11// jumps that enter a VLA scope in an invalid way.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/Expr.h"
Chris Lattner16f00492009-04-26 01:32:48 +000017#include "clang/AST/StmtObjC.h"
Sebastian Redl972041f2009-04-27 20:27:31 +000018#include "clang/AST/StmtCXX.h"
Chris Lattner5af280c2009-04-19 04:46:21 +000019using namespace clang;
20
21namespace {
22
23/// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
24/// into VLA and other protected scopes. For example, this rejects:
25/// goto L;
26/// int a[n];
27/// L:
28///
29class JumpScopeChecker {
30 Sema &S;
Mike Stump1eb44332009-09-09 15:08:12 +000031
Chris Lattner5af280c2009-04-19 04:46:21 +000032 /// GotoScope - This is a record that we use to keep track of all of the
33 /// scopes that are introduced by VLAs and other things that scope jumps like
34 /// gotos. This scope tree has nothing to do with the source scope tree,
35 /// because you can have multiple VLA scopes per compound statement, and most
36 /// compound statements don't introduce any scopes.
37 struct GotoScope {
38 /// ParentScope - The index in ScopeMap of the parent scope. This is 0 for
39 /// the parent scope is the function body.
40 unsigned ParentScope;
Mike Stump1eb44332009-09-09 15:08:12 +000041
Chris Lattner5af280c2009-04-19 04:46:21 +000042 /// Diag - The diagnostic to emit if there is a jump into this scope.
43 unsigned Diag;
Mike Stump1eb44332009-09-09 15:08:12 +000044
Chris Lattner5af280c2009-04-19 04:46:21 +000045 /// Loc - Location to emit the diagnostic.
46 SourceLocation Loc;
Mike Stump1eb44332009-09-09 15:08:12 +000047
Chris Lattner5af280c2009-04-19 04:46:21 +000048 GotoScope(unsigned parentScope, unsigned diag, SourceLocation L)
49 : ParentScope(parentScope), Diag(diag), Loc(L) {}
50 };
Mike Stump1eb44332009-09-09 15:08:12 +000051
Chris Lattner5af280c2009-04-19 04:46:21 +000052 llvm::SmallVector<GotoScope, 48> Scopes;
53 llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
54 llvm::SmallVector<Stmt*, 16> Jumps;
55public:
56 JumpScopeChecker(Stmt *Body, Sema &S);
57private:
58 void BuildScopeInformation(Stmt *S, unsigned ParentScope);
59 void VerifyJumps();
60 void CheckJump(Stmt *From, Stmt *To,
61 SourceLocation DiagLoc, unsigned JumpDiag);
62};
63} // end anonymous namespace
64
65
66JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s) : S(s) {
67 // Add a scope entry for function scope.
68 Scopes.push_back(GotoScope(~0U, ~0U, SourceLocation()));
Mike Stump1eb44332009-09-09 15:08:12 +000069
Chris Lattner5af280c2009-04-19 04:46:21 +000070 // Build information for the top level compound statement, so that we have a
71 // defined scope record for every "goto" and label.
72 BuildScopeInformation(Body, 0);
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner5af280c2009-04-19 04:46:21 +000074 // Check that all jumps we saw are kosher.
75 VerifyJumps();
76}
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner5af280c2009-04-19 04:46:21 +000078/// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
79/// diagnostic that should be emitted if control goes over it. If not, return 0.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000080static unsigned GetDiagForGotoScopeDecl(const Decl *D) {
Chris Lattner5af280c2009-04-19 04:46:21 +000081 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
82 if (VD->getType()->isVariablyModifiedType())
83 return diag::note_protected_by_vla;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000084 if (VD->hasAttr<CleanupAttr>())
Chris Lattner5af280c2009-04-19 04:46:21 +000085 return diag::note_protected_by_cleanup;
Chris Lattnerbe6d2592009-07-19 20:17:11 +000086 if (VD->hasAttr<BlocksAttr>())
87 return diag::note_protected_by___block;
Chris Lattner5af280c2009-04-19 04:46:21 +000088 } else if (const TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
89 if (TD->getUnderlyingType()->isVariablyModifiedType())
90 return diag::note_protected_by_vla_typedef;
91 }
Mike Stump1eb44332009-09-09 15:08:12 +000092
Chris Lattner5af280c2009-04-19 04:46:21 +000093 return 0;
94}
95
96
97/// BuildScopeInformation - The statements from CI to CE are known to form a
98/// coherent VLA scope with a specified parent node. Walk through the
99/// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
100/// walking the AST as needed.
101void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned ParentScope) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner5af280c2009-04-19 04:46:21 +0000103 // If we found a label, remember that it is in ParentScope scope.
104 if (isa<LabelStmt>(S) || isa<DefaultStmt>(S) || isa<CaseStmt>(S)) {
105 LabelAndGotoScopes[S] = ParentScope;
106 } else if (isa<GotoStmt>(S) || isa<SwitchStmt>(S) ||
107 isa<IndirectGotoStmt>(S) || isa<AddrLabelExpr>(S)) {
108 // Remember both what scope a goto is in as well as the fact that we have
109 // it. This makes the second scan not have to walk the AST again.
110 LabelAndGotoScopes[S] = ParentScope;
111 Jumps.push_back(S);
112 }
Mike Stump1eb44332009-09-09 15:08:12 +0000113
Chris Lattner5af280c2009-04-19 04:46:21 +0000114 for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); CI != E;
115 ++CI) {
116 Stmt *SubStmt = *CI;
117 if (SubStmt == 0) continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner5af280c2009-04-19 04:46:21 +0000119 // FIXME: diagnose jumps past initialization: required in C++, warning in C.
120 // goto L; int X = 4; L: ;
Chris Lattner5af280c2009-04-19 04:46:21 +0000121
122 // If this is a declstmt with a VLA definition, it defines a scope from here
123 // to the end of the containing context.
124 if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
125 // The decl statement creates a scope if any of the decls in it are VLAs or
126 // have the cleanup attribute.
127 for (DeclStmt::decl_iterator I = DS->decl_begin(), E = DS->decl_end();
128 I != E; ++I) {
129 // If this decl causes a new scope, push and switch to it.
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000130 if (unsigned Diag = GetDiagForGotoScopeDecl(*I)) {
Chris Lattner5af280c2009-04-19 04:46:21 +0000131 Scopes.push_back(GotoScope(ParentScope, Diag, (*I)->getLocation()));
132 ParentScope = Scopes.size()-1;
133 }
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattner5af280c2009-04-19 04:46:21 +0000135 // If the decl has an initializer, walk it with the potentially new
136 // scope we just installed.
137 if (VarDecl *VD = dyn_cast<VarDecl>(*I))
138 if (Expr *Init = VD->getInit())
139 BuildScopeInformation(Init, ParentScope);
140 }
141 continue;
142 }
143
144 // Disallow jumps into any part of an @try statement by pushing a scope and
145 // walking all sub-stmts in that scope.
146 if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
147 // Recursively walk the AST for the @try part.
148 Scopes.push_back(GotoScope(ParentScope,diag::note_protected_by_objc_try,
149 AT->getAtTryLoc()));
150 if (Stmt *TryPart = AT->getTryBody())
151 BuildScopeInformation(TryPart, Scopes.size()-1);
152
153 // Jump from the catch to the finally or try is not valid.
154 for (ObjCAtCatchStmt *AC = AT->getCatchStmts(); AC;
155 AC = AC->getNextCatchStmt()) {
156 Scopes.push_back(GotoScope(ParentScope,
157 diag::note_protected_by_objc_catch,
158 AC->getAtCatchLoc()));
Mike Stump1eb44332009-09-09 15:08:12 +0000159 // @catches are nested and it isn't
Chris Lattner5af280c2009-04-19 04:46:21 +0000160 BuildScopeInformation(AC->getCatchBody(), Scopes.size()-1);
161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner5af280c2009-04-19 04:46:21 +0000163 // Jump from the finally to the try or catch is not valid.
164 if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
165 Scopes.push_back(GotoScope(ParentScope,
166 diag::note_protected_by_objc_finally,
167 AF->getAtFinallyLoc()));
168 BuildScopeInformation(AF, Scopes.size()-1);
169 }
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Chris Lattner5af280c2009-04-19 04:46:21 +0000171 continue;
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000174 // Disallow jumps into the protected statement of an @synchronized, but
175 // allow jumps into the object expression it protects.
176 if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
177 // Recursively walk the AST for the @synchronized object expr, it is
178 // evaluated in the normal scope.
179 BuildScopeInformation(AS->getSynchExpr(), ParentScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000180
Chris Lattner46c3c4b2009-04-21 06:01:00 +0000181 // Recursively walk the AST for the @synchronized part, protected by a new
182 // scope.
183 Scopes.push_back(GotoScope(ParentScope,
184 diag::note_protected_by_objc_synchronized,
185 AS->getAtSynchronizedLoc()));
186 BuildScopeInformation(AS->getSynchBody(), Scopes.size()-1);
187 continue;
188 }
Sebastian Redl972041f2009-04-27 20:27:31 +0000189
190 // Disallow jumps into any part of a C++ try statement. This is pretty
191 // much the same as for Obj-C.
192 if (CXXTryStmt *TS = dyn_cast<CXXTryStmt>(SubStmt)) {
193 Scopes.push_back(GotoScope(ParentScope, diag::note_protected_by_cxx_try,
194 TS->getSourceRange().getBegin()));
195 if (Stmt *TryBlock = TS->getTryBlock())
196 BuildScopeInformation(TryBlock, Scopes.size()-1);
197
198 // Jump from the catch into the try is not allowed either.
Mike Stump1eb44332009-09-09 15:08:12 +0000199 for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
Sebastian Redl972041f2009-04-27 20:27:31 +0000200 CXXCatchStmt *CS = TS->getHandler(I);
201 Scopes.push_back(GotoScope(ParentScope,
202 diag::note_protected_by_cxx_catch,
203 CS->getSourceRange().getBegin()));
204 BuildScopeInformation(CS->getHandlerBlock(), Scopes.size()-1);
205 }
206
207 continue;
208 }
209
Chris Lattner5af280c2009-04-19 04:46:21 +0000210 // Recursively walk the AST.
211 BuildScopeInformation(SubStmt, ParentScope);
212 }
213}
214
215/// VerifyJumps - Verify each element of the Jumps array to see if they are
216/// valid, emitting diagnostics if not.
217void JumpScopeChecker::VerifyJumps() {
218 while (!Jumps.empty()) {
219 Stmt *Jump = Jumps.pop_back_val();
Mike Stump1eb44332009-09-09 15:08:12 +0000220
221 // With a goto,
Chris Lattner5af280c2009-04-19 04:46:21 +0000222 if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
223 CheckJump(GS, GS->getLabel(), GS->getGotoLoc(),
224 diag::err_goto_into_protected_scope);
225 continue;
226 }
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattner5af280c2009-04-19 04:46:21 +0000228 if (SwitchStmt *SS = dyn_cast<SwitchStmt>(Jump)) {
229 for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
230 SC = SC->getNextSwitchCase()) {
231 assert(LabelAndGotoScopes.count(SC) && "Case not visited?");
232 CheckJump(SS, SC, SC->getLocStart(),
233 diag::err_switch_into_protected_scope);
234 }
235 continue;
236 }
237
238 unsigned DiagnosticScope;
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Chris Lattner5af280c2009-04-19 04:46:21 +0000240 // We don't know where an indirect goto goes, require that it be at the
241 // top level of scoping.
242 if (IndirectGotoStmt *IG = dyn_cast<IndirectGotoStmt>(Jump)) {
243 assert(LabelAndGotoScopes.count(Jump) &&
244 "Jump didn't get added to scopes?");
245 unsigned GotoScope = LabelAndGotoScopes[IG];
246 if (GotoScope == 0) continue; // indirect jump is ok.
247 S.Diag(IG->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
248 DiagnosticScope = GotoScope;
249 } else {
250 // We model &&Label as a jump for purposes of scope tracking. We actually
251 // don't care *where* the address of label is, but we require the *label
252 // itself* to be in scope 0. If it is nested inside of a VLA scope, then
253 // it is possible for an indirect goto to illegally enter the VLA scope by
254 // indirectly jumping to the label.
255 assert(isa<AddrLabelExpr>(Jump) && "Unknown jump type");
256 LabelStmt *TheLabel = cast<AddrLabelExpr>(Jump)->getLabel();
Mike Stump1eb44332009-09-09 15:08:12 +0000257
Chris Lattner5af280c2009-04-19 04:46:21 +0000258 assert(LabelAndGotoScopes.count(TheLabel) &&
259 "Referenced label didn't get added to scopes?");
260 unsigned LabelScope = LabelAndGotoScopes[TheLabel];
261 if (LabelScope == 0) continue; // Addr of label is ok.
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chris Lattner5af280c2009-04-19 04:46:21 +0000263 S.Diag(Jump->getLocStart(), diag::err_addr_of_label_in_protected_scope);
264 DiagnosticScope = LabelScope;
265 }
266
267 // Report all the things that would be skipped over by this &&label or
268 // indirect goto.
269 while (DiagnosticScope != 0) {
270 S.Diag(Scopes[DiagnosticScope].Loc, Scopes[DiagnosticScope].Diag);
271 DiagnosticScope = Scopes[DiagnosticScope].ParentScope;
272 }
273 }
274}
275
276/// CheckJump - Validate that the specified jump statement is valid: that it is
277/// jumping within or out of its current scope, not into a deeper one.
278void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To,
279 SourceLocation DiagLoc, unsigned JumpDiag) {
280 assert(LabelAndGotoScopes.count(From) && "Jump didn't get added to scopes?");
281 unsigned FromScope = LabelAndGotoScopes[From];
282
283 assert(LabelAndGotoScopes.count(To) && "Jump didn't get added to scopes?");
284 unsigned ToScope = LabelAndGotoScopes[To];
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Chris Lattner5af280c2009-04-19 04:46:21 +0000286 // Common case: exactly the same scope, which is fine.
287 if (FromScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattner5af280c2009-04-19 04:46:21 +0000289 // The only valid mismatch jump case happens when the jump is more deeply
290 // nested inside the jump target. Do a quick scan to see if the jump is valid
291 // because valid code is more common than invalid code.
292 unsigned TestScope = Scopes[FromScope].ParentScope;
293 while (TestScope != ~0U) {
294 // If we found the jump target, then we're jumping out of our current scope,
295 // which is perfectly fine.
296 if (TestScope == ToScope) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattner5af280c2009-04-19 04:46:21 +0000298 // Otherwise, scan up the hierarchy.
299 TestScope = Scopes[TestScope].ParentScope;
300 }
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattner5af280c2009-04-19 04:46:21 +0000302 // If we get here, then we know we have invalid code. Diagnose the bad jump,
303 // and then emit a note at each VLA being jumped out of.
304 S.Diag(DiagLoc, JumpDiag);
305
306 // Eliminate the common prefix of the jump and the target. Start by
307 // linearizing both scopes, reversing them as we go.
308 std::vector<unsigned> FromScopes, ToScopes;
309 for (TestScope = FromScope; TestScope != ~0U;
310 TestScope = Scopes[TestScope].ParentScope)
311 FromScopes.push_back(TestScope);
312 for (TestScope = ToScope; TestScope != ~0U;
313 TestScope = Scopes[TestScope].ParentScope)
314 ToScopes.push_back(TestScope);
315
316 // Remove any common entries (such as the top-level function scope).
317 while (!FromScopes.empty() && FromScopes.back() == ToScopes.back()) {
318 FromScopes.pop_back();
319 ToScopes.pop_back();
320 }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner5af280c2009-04-19 04:46:21 +0000322 // Emit diagnostics for whatever is left in ToScopes.
323 for (unsigned i = 0, e = ToScopes.size(); i != e; ++i)
324 S.Diag(Scopes[ToScopes[i]].Loc, Scopes[ToScopes[i]].Diag);
325}
326
327void Sema::DiagnoseInvalidJumps(Stmt *Body) {
328 JumpScopeChecker(Body, *this);
329}