xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/JumpDiagnostics.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- JumpDiagnostics.cpp - Protected scope jump analysis ------*- C++ -*-=//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the JumpScopeChecker class, which is used to diagnose
11f4a2713aSLionel Sambuc // jumps that enter a protected scope in an invalid way.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/Sema/SemaInternal.h"
16f4a2713aSLionel Sambuc #include "clang/AST/DeclCXX.h"
17f4a2713aSLionel Sambuc #include "clang/AST/Expr.h"
18f4a2713aSLionel Sambuc #include "clang/AST/ExprCXX.h"
19f4a2713aSLionel Sambuc #include "clang/AST/StmtCXX.h"
20f4a2713aSLionel Sambuc #include "clang/AST/StmtObjC.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/BitVector.h"
22f4a2713aSLionel Sambuc using namespace clang;
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc namespace {
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc /// JumpScopeChecker - This object is used by Sema to diagnose invalid jumps
27f4a2713aSLionel Sambuc /// into VLA and other protected scopes.  For example, this rejects:
28f4a2713aSLionel Sambuc ///    goto L;
29f4a2713aSLionel Sambuc ///    int a[n];
30f4a2713aSLionel Sambuc ///  L:
31f4a2713aSLionel Sambuc ///
32f4a2713aSLionel Sambuc class JumpScopeChecker {
33f4a2713aSLionel Sambuc   Sema &S;
34f4a2713aSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc   /// Permissive - True when recovering from errors, in which case precautions
36*0a6a1f1dSLionel Sambuc   /// are taken to handle incomplete scope information.
37*0a6a1f1dSLionel Sambuc   const bool Permissive;
38*0a6a1f1dSLionel Sambuc 
39f4a2713aSLionel Sambuc   /// GotoScope - This is a record that we use to keep track of all of the
40f4a2713aSLionel Sambuc   /// scopes that are introduced by VLAs and other things that scope jumps like
41f4a2713aSLionel Sambuc   /// gotos.  This scope tree has nothing to do with the source scope tree,
42f4a2713aSLionel Sambuc   /// because you can have multiple VLA scopes per compound statement, and most
43f4a2713aSLionel Sambuc   /// compound statements don't introduce any scopes.
44f4a2713aSLionel Sambuc   struct GotoScope {
45f4a2713aSLionel Sambuc     /// ParentScope - The index in ScopeMap of the parent scope.  This is 0 for
46f4a2713aSLionel Sambuc     /// the parent scope is the function body.
47f4a2713aSLionel Sambuc     unsigned ParentScope;
48f4a2713aSLionel Sambuc 
49f4a2713aSLionel Sambuc     /// InDiag - The note to emit if there is a jump into this scope.
50f4a2713aSLionel Sambuc     unsigned InDiag;
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc     /// OutDiag - The note to emit if there is an indirect jump out
53f4a2713aSLionel Sambuc     /// of this scope.  Direct jumps always clean up their current scope
54f4a2713aSLionel Sambuc     /// in an orderly way.
55f4a2713aSLionel Sambuc     unsigned OutDiag;
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc     /// Loc - Location to emit the diagnostic.
58f4a2713aSLionel Sambuc     SourceLocation Loc;
59f4a2713aSLionel Sambuc 
GotoScope__anonc436f3760111::JumpScopeChecker::GotoScope60f4a2713aSLionel Sambuc     GotoScope(unsigned parentScope, unsigned InDiag, unsigned OutDiag,
61f4a2713aSLionel Sambuc               SourceLocation L)
62f4a2713aSLionel Sambuc       : ParentScope(parentScope), InDiag(InDiag), OutDiag(OutDiag), Loc(L) {}
63f4a2713aSLionel Sambuc   };
64f4a2713aSLionel Sambuc 
65f4a2713aSLionel Sambuc   SmallVector<GotoScope, 48> Scopes;
66f4a2713aSLionel Sambuc   llvm::DenseMap<Stmt*, unsigned> LabelAndGotoScopes;
67f4a2713aSLionel Sambuc   SmallVector<Stmt*, 16> Jumps;
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc   SmallVector<IndirectGotoStmt*, 4> IndirectJumps;
70f4a2713aSLionel Sambuc   SmallVector<LabelDecl*, 4> IndirectJumpTargets;
71f4a2713aSLionel Sambuc public:
72f4a2713aSLionel Sambuc   JumpScopeChecker(Stmt *Body, Sema &S);
73f4a2713aSLionel Sambuc private:
74f4a2713aSLionel Sambuc   void BuildScopeInformation(Decl *D, unsigned &ParentScope);
75f4a2713aSLionel Sambuc   void BuildScopeInformation(VarDecl *D, const BlockDecl *BDecl,
76f4a2713aSLionel Sambuc                              unsigned &ParentScope);
77f4a2713aSLionel Sambuc   void BuildScopeInformation(Stmt *S, unsigned &origParentScope);
78f4a2713aSLionel Sambuc 
79f4a2713aSLionel Sambuc   void VerifyJumps();
80f4a2713aSLionel Sambuc   void VerifyIndirectJumps();
81f4a2713aSLionel Sambuc   void NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes);
82f4a2713aSLionel Sambuc   void DiagnoseIndirectJump(IndirectGotoStmt *IG, unsigned IGScope,
83f4a2713aSLionel Sambuc                             LabelDecl *Target, unsigned TargetScope);
84f4a2713aSLionel Sambuc   void CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
85f4a2713aSLionel Sambuc                  unsigned JumpDiag, unsigned JumpDiagWarning,
86f4a2713aSLionel Sambuc                  unsigned JumpDiagCXX98Compat);
87*0a6a1f1dSLionel Sambuc   void CheckGotoStmt(GotoStmt *GS);
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   unsigned GetDeepestCommonScope(unsigned A, unsigned B);
90f4a2713aSLionel Sambuc };
91f4a2713aSLionel Sambuc } // end anonymous namespace
92f4a2713aSLionel Sambuc 
93*0a6a1f1dSLionel Sambuc #define CHECK_PERMISSIVE(x) (assert(Permissive || !(x)), (Permissive && (x)))
94f4a2713aSLionel Sambuc 
JumpScopeChecker(Stmt * Body,Sema & s)95*0a6a1f1dSLionel Sambuc JumpScopeChecker::JumpScopeChecker(Stmt *Body, Sema &s)
96*0a6a1f1dSLionel Sambuc     : S(s), Permissive(s.hasAnyUnrecoverableErrorsInThisFunction()) {
97f4a2713aSLionel Sambuc   // Add a scope entry for function scope.
98f4a2713aSLionel Sambuc   Scopes.push_back(GotoScope(~0U, ~0U, ~0U, SourceLocation()));
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   // Build information for the top level compound statement, so that we have a
101f4a2713aSLionel Sambuc   // defined scope record for every "goto" and label.
102f4a2713aSLionel Sambuc   unsigned BodyParentScope = 0;
103f4a2713aSLionel Sambuc   BuildScopeInformation(Body, BodyParentScope);
104f4a2713aSLionel Sambuc 
105f4a2713aSLionel Sambuc   // Check that all jumps we saw are kosher.
106f4a2713aSLionel Sambuc   VerifyJumps();
107f4a2713aSLionel Sambuc   VerifyIndirectJumps();
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc /// GetDeepestCommonScope - Finds the innermost scope enclosing the
111f4a2713aSLionel Sambuc /// two scopes.
GetDeepestCommonScope(unsigned A,unsigned B)112f4a2713aSLionel Sambuc unsigned JumpScopeChecker::GetDeepestCommonScope(unsigned A, unsigned B) {
113f4a2713aSLionel Sambuc   while (A != B) {
114f4a2713aSLionel Sambuc     // Inner scopes are created after outer scopes and therefore have
115f4a2713aSLionel Sambuc     // higher indices.
116f4a2713aSLionel Sambuc     if (A < B) {
117f4a2713aSLionel Sambuc       assert(Scopes[B].ParentScope < B);
118f4a2713aSLionel Sambuc       B = Scopes[B].ParentScope;
119f4a2713aSLionel Sambuc     } else {
120f4a2713aSLionel Sambuc       assert(Scopes[A].ParentScope < A);
121f4a2713aSLionel Sambuc       A = Scopes[A].ParentScope;
122f4a2713aSLionel Sambuc     }
123f4a2713aSLionel Sambuc   }
124f4a2713aSLionel Sambuc   return A;
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc typedef std::pair<unsigned,unsigned> ScopePair;
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc /// GetDiagForGotoScopeDecl - If this decl induces a new goto scope, return a
130f4a2713aSLionel Sambuc /// diagnostic that should be emitted if control goes over it. If not, return 0.
GetDiagForGotoScopeDecl(Sema & S,const Decl * D)131*0a6a1f1dSLionel Sambuc static ScopePair GetDiagForGotoScopeDecl(Sema &S, const Decl *D) {
132f4a2713aSLionel Sambuc   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
133f4a2713aSLionel Sambuc     unsigned InDiag = 0;
134*0a6a1f1dSLionel Sambuc     unsigned OutDiag = 0;
135*0a6a1f1dSLionel Sambuc 
136f4a2713aSLionel Sambuc     if (VD->getType()->isVariablyModifiedType())
137f4a2713aSLionel Sambuc       InDiag = diag::note_protected_by_vla;
138f4a2713aSLionel Sambuc 
139f4a2713aSLionel Sambuc     if (VD->hasAttr<BlocksAttr>())
140f4a2713aSLionel Sambuc       return ScopePair(diag::note_protected_by___block,
141f4a2713aSLionel Sambuc                        diag::note_exits___block);
142f4a2713aSLionel Sambuc 
143f4a2713aSLionel Sambuc     if (VD->hasAttr<CleanupAttr>())
144f4a2713aSLionel Sambuc       return ScopePair(diag::note_protected_by_cleanup,
145f4a2713aSLionel Sambuc                        diag::note_exits_cleanup);
146f4a2713aSLionel Sambuc 
147*0a6a1f1dSLionel Sambuc     if (VD->hasLocalStorage()) {
148*0a6a1f1dSLionel Sambuc       switch (VD->getType().isDestructedType()) {
149*0a6a1f1dSLionel Sambuc       case QualType::DK_objc_strong_lifetime:
150*0a6a1f1dSLionel Sambuc       case QualType::DK_objc_weak_lifetime:
151f4a2713aSLionel Sambuc         return ScopePair(diag::note_protected_by_objc_ownership,
152f4a2713aSLionel Sambuc                          diag::note_exits_objc_ownership);
153*0a6a1f1dSLionel Sambuc 
154*0a6a1f1dSLionel Sambuc       case QualType::DK_cxx_destructor:
155*0a6a1f1dSLionel Sambuc         OutDiag = diag::note_exits_dtor;
156*0a6a1f1dSLionel Sambuc         break;
157*0a6a1f1dSLionel Sambuc 
158*0a6a1f1dSLionel Sambuc       case QualType::DK_none:
159*0a6a1f1dSLionel Sambuc         break;
160f4a2713aSLionel Sambuc       }
161f4a2713aSLionel Sambuc     }
162f4a2713aSLionel Sambuc 
163*0a6a1f1dSLionel Sambuc     const Expr *Init = VD->getInit();
164*0a6a1f1dSLionel Sambuc     if (S.Context.getLangOpts().CPlusPlus && VD->hasLocalStorage() && Init) {
165f4a2713aSLionel Sambuc       // C++11 [stmt.dcl]p3:
166f4a2713aSLionel Sambuc       //   A program that jumps from a point where a variable with automatic
167f4a2713aSLionel Sambuc       //   storage duration is not in scope to a point where it is in scope
168f4a2713aSLionel Sambuc       //   is ill-formed unless the variable has scalar type, class type with
169f4a2713aSLionel Sambuc       //   a trivial default constructor and a trivial destructor, a
170f4a2713aSLionel Sambuc       //   cv-qualified version of one of these types, or an array of one of
171f4a2713aSLionel Sambuc       //   the preceding types and is declared without an initializer.
172f4a2713aSLionel Sambuc 
173f4a2713aSLionel Sambuc       // C++03 [stmt.dcl.p3:
174f4a2713aSLionel Sambuc       //   A program that jumps from a point where a local variable
175f4a2713aSLionel Sambuc       //   with automatic storage duration is not in scope to a point
176f4a2713aSLionel Sambuc       //   where it is in scope is ill-formed unless the variable has
177f4a2713aSLionel Sambuc       //   POD type and is declared without an initializer.
178f4a2713aSLionel Sambuc 
179*0a6a1f1dSLionel Sambuc       InDiag = diag::note_protected_by_variable_init;
180f4a2713aSLionel Sambuc 
181*0a6a1f1dSLionel Sambuc       // For a variable of (array of) class type declared without an
182*0a6a1f1dSLionel Sambuc       // initializer, we will have call-style initialization and the initializer
183*0a6a1f1dSLionel Sambuc       // will be the CXXConstructExpr with no intervening nodes.
184*0a6a1f1dSLionel Sambuc       if (const CXXConstructExpr *CCE = dyn_cast<CXXConstructExpr>(Init)) {
185*0a6a1f1dSLionel Sambuc         const CXXConstructorDecl *Ctor = CCE->getConstructor();
186*0a6a1f1dSLionel Sambuc         if (Ctor->isTrivial() && Ctor->isDefaultConstructor() &&
187*0a6a1f1dSLionel Sambuc             VD->getInitStyle() == VarDecl::CallInit) {
188f4a2713aSLionel Sambuc           if (OutDiag)
189f4a2713aSLionel Sambuc             InDiag = diag::note_protected_by_variable_nontriv_destructor;
190*0a6a1f1dSLionel Sambuc           else if (!Ctor->getParent()->isPOD())
191f4a2713aSLionel Sambuc             InDiag = diag::note_protected_by_variable_non_pod;
192*0a6a1f1dSLionel Sambuc           else
193*0a6a1f1dSLionel Sambuc             InDiag = 0;
194*0a6a1f1dSLionel Sambuc         }
195*0a6a1f1dSLionel Sambuc       }
196*0a6a1f1dSLionel Sambuc     }
197*0a6a1f1dSLionel Sambuc 
198f4a2713aSLionel Sambuc     return ScopePair(InDiag, OutDiag);
199f4a2713aSLionel Sambuc   }
200f4a2713aSLionel Sambuc 
201*0a6a1f1dSLionel Sambuc   if (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
202f4a2713aSLionel Sambuc     if (TD->getUnderlyingType()->isVariablyModifiedType())
203*0a6a1f1dSLionel Sambuc       return ScopePair(isa<TypedefDecl>(TD)
204*0a6a1f1dSLionel Sambuc                            ? diag::note_protected_by_vla_typedef
205*0a6a1f1dSLionel Sambuc                            : diag::note_protected_by_vla_type_alias,
206*0a6a1f1dSLionel Sambuc                        0);
207f4a2713aSLionel Sambuc   }
208f4a2713aSLionel Sambuc 
209f4a2713aSLionel Sambuc   return ScopePair(0U, 0U);
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc /// \brief Build scope information for a declaration that is part of a DeclStmt.
BuildScopeInformation(Decl * D,unsigned & ParentScope)213f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(Decl *D, unsigned &ParentScope) {
214f4a2713aSLionel Sambuc   // If this decl causes a new scope, push and switch to it.
215*0a6a1f1dSLionel Sambuc   std::pair<unsigned,unsigned> Diags = GetDiagForGotoScopeDecl(S, D);
216f4a2713aSLionel Sambuc   if (Diags.first || Diags.second) {
217f4a2713aSLionel Sambuc     Scopes.push_back(GotoScope(ParentScope, Diags.first, Diags.second,
218f4a2713aSLionel Sambuc                                D->getLocation()));
219f4a2713aSLionel Sambuc     ParentScope = Scopes.size()-1;
220f4a2713aSLionel Sambuc   }
221f4a2713aSLionel Sambuc 
222f4a2713aSLionel Sambuc   // If the decl has an initializer, walk it with the potentially new
223f4a2713aSLionel Sambuc   // scope we just installed.
224f4a2713aSLionel Sambuc   if (VarDecl *VD = dyn_cast<VarDecl>(D))
225f4a2713aSLionel Sambuc     if (Expr *Init = VD->getInit())
226f4a2713aSLionel Sambuc       BuildScopeInformation(Init, ParentScope);
227f4a2713aSLionel Sambuc }
228f4a2713aSLionel Sambuc 
229f4a2713aSLionel Sambuc /// \brief Build scope information for a captured block literal variables.
BuildScopeInformation(VarDecl * D,const BlockDecl * BDecl,unsigned & ParentScope)230f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(VarDecl *D,
231f4a2713aSLionel Sambuc                                              const BlockDecl *BDecl,
232f4a2713aSLionel Sambuc                                              unsigned &ParentScope) {
233f4a2713aSLionel Sambuc   // exclude captured __block variables; there's no destructor
234f4a2713aSLionel Sambuc   // associated with the block literal for them.
235f4a2713aSLionel Sambuc   if (D->hasAttr<BlocksAttr>())
236f4a2713aSLionel Sambuc     return;
237f4a2713aSLionel Sambuc   QualType T = D->getType();
238f4a2713aSLionel Sambuc   QualType::DestructionKind destructKind = T.isDestructedType();
239f4a2713aSLionel Sambuc   if (destructKind != QualType::DK_none) {
240f4a2713aSLionel Sambuc     std::pair<unsigned,unsigned> Diags;
241f4a2713aSLionel Sambuc     switch (destructKind) {
242f4a2713aSLionel Sambuc       case QualType::DK_cxx_destructor:
243f4a2713aSLionel Sambuc         Diags = ScopePair(diag::note_enters_block_captures_cxx_obj,
244f4a2713aSLionel Sambuc                           diag::note_exits_block_captures_cxx_obj);
245f4a2713aSLionel Sambuc         break;
246f4a2713aSLionel Sambuc       case QualType::DK_objc_strong_lifetime:
247f4a2713aSLionel Sambuc         Diags = ScopePair(diag::note_enters_block_captures_strong,
248f4a2713aSLionel Sambuc                           diag::note_exits_block_captures_strong);
249f4a2713aSLionel Sambuc         break;
250f4a2713aSLionel Sambuc       case QualType::DK_objc_weak_lifetime:
251f4a2713aSLionel Sambuc         Diags = ScopePair(diag::note_enters_block_captures_weak,
252f4a2713aSLionel Sambuc                           diag::note_exits_block_captures_weak);
253f4a2713aSLionel Sambuc         break;
254f4a2713aSLionel Sambuc       case QualType::DK_none:
255f4a2713aSLionel Sambuc         llvm_unreachable("non-lifetime captured variable");
256f4a2713aSLionel Sambuc     }
257f4a2713aSLionel Sambuc     SourceLocation Loc = D->getLocation();
258f4a2713aSLionel Sambuc     if (Loc.isInvalid())
259f4a2713aSLionel Sambuc       Loc = BDecl->getLocation();
260f4a2713aSLionel Sambuc     Scopes.push_back(GotoScope(ParentScope,
261f4a2713aSLionel Sambuc                                Diags.first, Diags.second, Loc));
262f4a2713aSLionel Sambuc     ParentScope = Scopes.size()-1;
263f4a2713aSLionel Sambuc   }
264f4a2713aSLionel Sambuc }
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc /// BuildScopeInformation - The statements from CI to CE are known to form a
267f4a2713aSLionel Sambuc /// coherent VLA scope with a specified parent node.  Walk through the
268f4a2713aSLionel Sambuc /// statements, adding any labels or gotos to LabelAndGotoScopes and recursively
269f4a2713aSLionel Sambuc /// walking the AST as needed.
BuildScopeInformation(Stmt * S,unsigned & origParentScope)270f4a2713aSLionel Sambuc void JumpScopeChecker::BuildScopeInformation(Stmt *S, unsigned &origParentScope) {
271f4a2713aSLionel Sambuc   // If this is a statement, rather than an expression, scopes within it don't
272f4a2713aSLionel Sambuc   // propagate out into the enclosing scope.  Otherwise we have to worry
273f4a2713aSLionel Sambuc   // about block literals, which have the lifetime of their enclosing statement.
274f4a2713aSLionel Sambuc   unsigned independentParentScope = origParentScope;
275f4a2713aSLionel Sambuc   unsigned &ParentScope = ((isa<Expr>(S) && !isa<StmtExpr>(S))
276f4a2713aSLionel Sambuc                             ? origParentScope : independentParentScope);
277f4a2713aSLionel Sambuc 
278f4a2713aSLionel Sambuc   bool SkipFirstSubStmt = false;
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc   // If we found a label, remember that it is in ParentScope scope.
281f4a2713aSLionel Sambuc   switch (S->getStmtClass()) {
282f4a2713aSLionel Sambuc   case Stmt::AddrLabelExprClass:
283f4a2713aSLionel Sambuc     IndirectJumpTargets.push_back(cast<AddrLabelExpr>(S)->getLabel());
284f4a2713aSLionel Sambuc     break;
285f4a2713aSLionel Sambuc 
286f4a2713aSLionel Sambuc   case Stmt::IndirectGotoStmtClass:
287f4a2713aSLionel Sambuc     // "goto *&&lbl;" is a special case which we treat as equivalent
288f4a2713aSLionel Sambuc     // to a normal goto.  In addition, we don't calculate scope in the
289f4a2713aSLionel Sambuc     // operand (to avoid recording the address-of-label use), which
290f4a2713aSLionel Sambuc     // works only because of the restricted set of expressions which
291f4a2713aSLionel Sambuc     // we detect as constant targets.
292f4a2713aSLionel Sambuc     if (cast<IndirectGotoStmt>(S)->getConstantTarget()) {
293f4a2713aSLionel Sambuc       LabelAndGotoScopes[S] = ParentScope;
294f4a2713aSLionel Sambuc       Jumps.push_back(S);
295f4a2713aSLionel Sambuc       return;
296f4a2713aSLionel Sambuc     }
297f4a2713aSLionel Sambuc 
298f4a2713aSLionel Sambuc     LabelAndGotoScopes[S] = ParentScope;
299f4a2713aSLionel Sambuc     IndirectJumps.push_back(cast<IndirectGotoStmt>(S));
300f4a2713aSLionel Sambuc     break;
301f4a2713aSLionel Sambuc 
302f4a2713aSLionel Sambuc   case Stmt::SwitchStmtClass:
303f4a2713aSLionel Sambuc     // Evaluate the condition variable before entering the scope of the switch
304f4a2713aSLionel Sambuc     // statement.
305f4a2713aSLionel Sambuc     if (VarDecl *Var = cast<SwitchStmt>(S)->getConditionVariable()) {
306f4a2713aSLionel Sambuc       BuildScopeInformation(Var, ParentScope);
307f4a2713aSLionel Sambuc       SkipFirstSubStmt = true;
308f4a2713aSLionel Sambuc     }
309f4a2713aSLionel Sambuc     // Fall through
310f4a2713aSLionel Sambuc 
311f4a2713aSLionel Sambuc   case Stmt::GotoStmtClass:
312f4a2713aSLionel Sambuc     // Remember both what scope a goto is in as well as the fact that we have
313f4a2713aSLionel Sambuc     // it.  This makes the second scan not have to walk the AST again.
314f4a2713aSLionel Sambuc     LabelAndGotoScopes[S] = ParentScope;
315f4a2713aSLionel Sambuc     Jumps.push_back(S);
316f4a2713aSLionel Sambuc     break;
317f4a2713aSLionel Sambuc 
318f4a2713aSLionel Sambuc   case Stmt::CXXTryStmtClass: {
319f4a2713aSLionel Sambuc     CXXTryStmt *TS = cast<CXXTryStmt>(S);
320f4a2713aSLionel Sambuc     unsigned newParentScope;
321f4a2713aSLionel Sambuc     Scopes.push_back(GotoScope(ParentScope,
322f4a2713aSLionel Sambuc                                diag::note_protected_by_cxx_try,
323f4a2713aSLionel Sambuc                                diag::note_exits_cxx_try,
324f4a2713aSLionel Sambuc                                TS->getSourceRange().getBegin()));
325f4a2713aSLionel Sambuc     if (Stmt *TryBlock = TS->getTryBlock())
326f4a2713aSLionel Sambuc       BuildScopeInformation(TryBlock, (newParentScope = Scopes.size()-1));
327f4a2713aSLionel Sambuc 
328f4a2713aSLionel Sambuc     // Jump from the catch into the try is not allowed either.
329f4a2713aSLionel Sambuc     for (unsigned I = 0, E = TS->getNumHandlers(); I != E; ++I) {
330f4a2713aSLionel Sambuc       CXXCatchStmt *CS = TS->getHandler(I);
331f4a2713aSLionel Sambuc       Scopes.push_back(GotoScope(ParentScope,
332f4a2713aSLionel Sambuc                                  diag::note_protected_by_cxx_catch,
333f4a2713aSLionel Sambuc                                  diag::note_exits_cxx_catch,
334f4a2713aSLionel Sambuc                                  CS->getSourceRange().getBegin()));
335f4a2713aSLionel Sambuc       BuildScopeInformation(CS->getHandlerBlock(),
336f4a2713aSLionel Sambuc                             (newParentScope = Scopes.size()-1));
337f4a2713aSLionel Sambuc     }
338f4a2713aSLionel Sambuc     return;
339f4a2713aSLionel Sambuc   }
340f4a2713aSLionel Sambuc 
341f4a2713aSLionel Sambuc   default:
342f4a2713aSLionel Sambuc     break;
343f4a2713aSLionel Sambuc   }
344f4a2713aSLionel Sambuc 
345f4a2713aSLionel Sambuc   for (Stmt::child_range CI = S->children(); CI; ++CI) {
346f4a2713aSLionel Sambuc     if (SkipFirstSubStmt) {
347f4a2713aSLionel Sambuc       SkipFirstSubStmt = false;
348f4a2713aSLionel Sambuc       continue;
349f4a2713aSLionel Sambuc     }
350f4a2713aSLionel Sambuc 
351f4a2713aSLionel Sambuc     Stmt *SubStmt = *CI;
352*0a6a1f1dSLionel Sambuc     if (!SubStmt) continue;
353f4a2713aSLionel Sambuc 
354f4a2713aSLionel Sambuc     // Cases, labels, and defaults aren't "scope parents".  It's also
355f4a2713aSLionel Sambuc     // important to handle these iteratively instead of recursively in
356f4a2713aSLionel Sambuc     // order to avoid blowing out the stack.
357f4a2713aSLionel Sambuc     while (true) {
358f4a2713aSLionel Sambuc       Stmt *Next;
359f4a2713aSLionel Sambuc       if (CaseStmt *CS = dyn_cast<CaseStmt>(SubStmt))
360f4a2713aSLionel Sambuc         Next = CS->getSubStmt();
361f4a2713aSLionel Sambuc       else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SubStmt))
362f4a2713aSLionel Sambuc         Next = DS->getSubStmt();
363f4a2713aSLionel Sambuc       else if (LabelStmt *LS = dyn_cast<LabelStmt>(SubStmt))
364f4a2713aSLionel Sambuc         Next = LS->getSubStmt();
365f4a2713aSLionel Sambuc       else
366f4a2713aSLionel Sambuc         break;
367f4a2713aSLionel Sambuc 
368f4a2713aSLionel Sambuc       LabelAndGotoScopes[SubStmt] = ParentScope;
369f4a2713aSLionel Sambuc       SubStmt = Next;
370f4a2713aSLionel Sambuc     }
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc     // If this is a declstmt with a VLA definition, it defines a scope from here
373f4a2713aSLionel Sambuc     // to the end of the containing context.
374f4a2713aSLionel Sambuc     if (DeclStmt *DS = dyn_cast<DeclStmt>(SubStmt)) {
375f4a2713aSLionel Sambuc       // The decl statement creates a scope if any of the decls in it are VLAs
376f4a2713aSLionel Sambuc       // or have the cleanup attribute.
377*0a6a1f1dSLionel Sambuc       for (auto *I : DS->decls())
378*0a6a1f1dSLionel Sambuc         BuildScopeInformation(I, ParentScope);
379f4a2713aSLionel Sambuc       continue;
380f4a2713aSLionel Sambuc     }
381f4a2713aSLionel Sambuc     // Disallow jumps into any part of an @try statement by pushing a scope and
382f4a2713aSLionel Sambuc     // walking all sub-stmts in that scope.
383f4a2713aSLionel Sambuc     if (ObjCAtTryStmt *AT = dyn_cast<ObjCAtTryStmt>(SubStmt)) {
384f4a2713aSLionel Sambuc       unsigned newParentScope;
385f4a2713aSLionel Sambuc       // Recursively walk the AST for the @try part.
386f4a2713aSLionel Sambuc       Scopes.push_back(GotoScope(ParentScope,
387f4a2713aSLionel Sambuc                                  diag::note_protected_by_objc_try,
388f4a2713aSLionel Sambuc                                  diag::note_exits_objc_try,
389f4a2713aSLionel Sambuc                                  AT->getAtTryLoc()));
390f4a2713aSLionel Sambuc       if (Stmt *TryPart = AT->getTryBody())
391f4a2713aSLionel Sambuc         BuildScopeInformation(TryPart, (newParentScope = Scopes.size()-1));
392f4a2713aSLionel Sambuc 
393f4a2713aSLionel Sambuc       // Jump from the catch to the finally or try is not valid.
394f4a2713aSLionel Sambuc       for (unsigned I = 0, N = AT->getNumCatchStmts(); I != N; ++I) {
395f4a2713aSLionel Sambuc         ObjCAtCatchStmt *AC = AT->getCatchStmt(I);
396f4a2713aSLionel Sambuc         Scopes.push_back(GotoScope(ParentScope,
397f4a2713aSLionel Sambuc                                    diag::note_protected_by_objc_catch,
398f4a2713aSLionel Sambuc                                    diag::note_exits_objc_catch,
399f4a2713aSLionel Sambuc                                    AC->getAtCatchLoc()));
400f4a2713aSLionel Sambuc         // @catches are nested and it isn't
401f4a2713aSLionel Sambuc         BuildScopeInformation(AC->getCatchBody(),
402f4a2713aSLionel Sambuc                               (newParentScope = Scopes.size()-1));
403f4a2713aSLionel Sambuc       }
404f4a2713aSLionel Sambuc 
405f4a2713aSLionel Sambuc       // Jump from the finally to the try or catch is not valid.
406f4a2713aSLionel Sambuc       if (ObjCAtFinallyStmt *AF = AT->getFinallyStmt()) {
407f4a2713aSLionel Sambuc         Scopes.push_back(GotoScope(ParentScope,
408f4a2713aSLionel Sambuc                                    diag::note_protected_by_objc_finally,
409f4a2713aSLionel Sambuc                                    diag::note_exits_objc_finally,
410f4a2713aSLionel Sambuc                                    AF->getAtFinallyLoc()));
411f4a2713aSLionel Sambuc         BuildScopeInformation(AF, (newParentScope = Scopes.size()-1));
412f4a2713aSLionel Sambuc       }
413f4a2713aSLionel Sambuc 
414f4a2713aSLionel Sambuc       continue;
415f4a2713aSLionel Sambuc     }
416f4a2713aSLionel Sambuc 
417f4a2713aSLionel Sambuc     unsigned newParentScope;
418f4a2713aSLionel Sambuc     // Disallow jumps into the protected statement of an @synchronized, but
419f4a2713aSLionel Sambuc     // allow jumps into the object expression it protects.
420f4a2713aSLionel Sambuc     if (ObjCAtSynchronizedStmt *AS = dyn_cast<ObjCAtSynchronizedStmt>(SubStmt)){
421f4a2713aSLionel Sambuc       // Recursively walk the AST for the @synchronized object expr, it is
422f4a2713aSLionel Sambuc       // evaluated in the normal scope.
423f4a2713aSLionel Sambuc       BuildScopeInformation(AS->getSynchExpr(), ParentScope);
424f4a2713aSLionel Sambuc 
425f4a2713aSLionel Sambuc       // Recursively walk the AST for the @synchronized part, protected by a new
426f4a2713aSLionel Sambuc       // scope.
427f4a2713aSLionel Sambuc       Scopes.push_back(GotoScope(ParentScope,
428f4a2713aSLionel Sambuc                                  diag::note_protected_by_objc_synchronized,
429f4a2713aSLionel Sambuc                                  diag::note_exits_objc_synchronized,
430f4a2713aSLionel Sambuc                                  AS->getAtSynchronizedLoc()));
431f4a2713aSLionel Sambuc       BuildScopeInformation(AS->getSynchBody(),
432f4a2713aSLionel Sambuc                             (newParentScope = Scopes.size()-1));
433f4a2713aSLionel Sambuc       continue;
434f4a2713aSLionel Sambuc     }
435f4a2713aSLionel Sambuc 
436f4a2713aSLionel Sambuc     // Disallow jumps into the protected statement of an @autoreleasepool.
437f4a2713aSLionel Sambuc     if (ObjCAutoreleasePoolStmt *AS = dyn_cast<ObjCAutoreleasePoolStmt>(SubStmt)){
438f4a2713aSLionel Sambuc       // Recursively walk the AST for the @autoreleasepool part, protected by a new
439f4a2713aSLionel Sambuc       // scope.
440f4a2713aSLionel Sambuc       Scopes.push_back(GotoScope(ParentScope,
441f4a2713aSLionel Sambuc                                  diag::note_protected_by_objc_autoreleasepool,
442f4a2713aSLionel Sambuc                                  diag::note_exits_objc_autoreleasepool,
443f4a2713aSLionel Sambuc                                  AS->getAtLoc()));
444f4a2713aSLionel Sambuc       BuildScopeInformation(AS->getSubStmt(), (newParentScope = Scopes.size()-1));
445f4a2713aSLionel Sambuc       continue;
446f4a2713aSLionel Sambuc     }
447f4a2713aSLionel Sambuc 
448f4a2713aSLionel Sambuc     // Disallow jumps past full-expressions that use blocks with
449f4a2713aSLionel Sambuc     // non-trivial cleanups of their captures.  This is theoretically
450f4a2713aSLionel Sambuc     // implementable but a lot of work which we haven't felt up to doing.
451f4a2713aSLionel Sambuc     if (ExprWithCleanups *EWC = dyn_cast<ExprWithCleanups>(SubStmt)) {
452f4a2713aSLionel Sambuc       for (unsigned i = 0, e = EWC->getNumObjects(); i != e; ++i) {
453f4a2713aSLionel Sambuc         const BlockDecl *BDecl = EWC->getObject(i);
454*0a6a1f1dSLionel Sambuc         for (const auto &CI : BDecl->captures()) {
455*0a6a1f1dSLionel Sambuc           VarDecl *variable = CI.getVariable();
456f4a2713aSLionel Sambuc           BuildScopeInformation(variable, BDecl, ParentScope);
457f4a2713aSLionel Sambuc         }
458f4a2713aSLionel Sambuc       }
459f4a2713aSLionel Sambuc     }
460f4a2713aSLionel Sambuc 
461*0a6a1f1dSLionel Sambuc     // Disallow jumps out of scopes containing temporaries lifetime-extended to
462*0a6a1f1dSLionel Sambuc     // automatic storage duration.
463*0a6a1f1dSLionel Sambuc     if (MaterializeTemporaryExpr *MTE =
464*0a6a1f1dSLionel Sambuc             dyn_cast<MaterializeTemporaryExpr>(SubStmt)) {
465*0a6a1f1dSLionel Sambuc       if (MTE->getStorageDuration() == SD_Automatic) {
466*0a6a1f1dSLionel Sambuc         SmallVector<const Expr *, 4> CommaLHS;
467*0a6a1f1dSLionel Sambuc         SmallVector<SubobjectAdjustment, 4> Adjustments;
468*0a6a1f1dSLionel Sambuc         const Expr *ExtendedObject =
469*0a6a1f1dSLionel Sambuc             MTE->GetTemporaryExpr()->skipRValueSubobjectAdjustments(
470*0a6a1f1dSLionel Sambuc                 CommaLHS, Adjustments);
471*0a6a1f1dSLionel Sambuc         if (ExtendedObject->getType().isDestructedType()) {
472*0a6a1f1dSLionel Sambuc           Scopes.push_back(GotoScope(ParentScope, 0,
473*0a6a1f1dSLionel Sambuc                                      diag::note_exits_temporary_dtor,
474*0a6a1f1dSLionel Sambuc                                      ExtendedObject->getExprLoc()));
475*0a6a1f1dSLionel Sambuc           ParentScope = Scopes.size()-1;
476*0a6a1f1dSLionel Sambuc         }
477*0a6a1f1dSLionel Sambuc       }
478*0a6a1f1dSLionel Sambuc     }
479*0a6a1f1dSLionel Sambuc 
480f4a2713aSLionel Sambuc     // Recursively walk the AST.
481f4a2713aSLionel Sambuc     BuildScopeInformation(SubStmt, ParentScope);
482f4a2713aSLionel Sambuc   }
483f4a2713aSLionel Sambuc }
484f4a2713aSLionel Sambuc 
485f4a2713aSLionel Sambuc /// VerifyJumps - Verify each element of the Jumps array to see if they are
486f4a2713aSLionel Sambuc /// valid, emitting diagnostics if not.
VerifyJumps()487f4a2713aSLionel Sambuc void JumpScopeChecker::VerifyJumps() {
488f4a2713aSLionel Sambuc   while (!Jumps.empty()) {
489f4a2713aSLionel Sambuc     Stmt *Jump = Jumps.pop_back_val();
490f4a2713aSLionel Sambuc 
491f4a2713aSLionel Sambuc     // With a goto,
492f4a2713aSLionel Sambuc     if (GotoStmt *GS = dyn_cast<GotoStmt>(Jump)) {
493*0a6a1f1dSLionel Sambuc       // The label may not have a statement if it's coming from inline MS ASM.
494*0a6a1f1dSLionel Sambuc       if (GS->getLabel()->getStmt()) {
495f4a2713aSLionel Sambuc         CheckJump(GS, GS->getLabel()->getStmt(), GS->getGotoLoc(),
496f4a2713aSLionel Sambuc                   diag::err_goto_into_protected_scope,
497*0a6a1f1dSLionel Sambuc                   diag::ext_goto_into_protected_scope,
498f4a2713aSLionel Sambuc                   diag::warn_cxx98_compat_goto_into_protected_scope);
499*0a6a1f1dSLionel Sambuc       }
500*0a6a1f1dSLionel Sambuc       CheckGotoStmt(GS);
501f4a2713aSLionel Sambuc       continue;
502f4a2713aSLionel Sambuc     }
503f4a2713aSLionel Sambuc 
504f4a2713aSLionel Sambuc     // We only get indirect gotos here when they have a constant target.
505f4a2713aSLionel Sambuc     if (IndirectGotoStmt *IGS = dyn_cast<IndirectGotoStmt>(Jump)) {
506f4a2713aSLionel Sambuc       LabelDecl *Target = IGS->getConstantTarget();
507f4a2713aSLionel Sambuc       CheckJump(IGS, Target->getStmt(), IGS->getGotoLoc(),
508f4a2713aSLionel Sambuc                 diag::err_goto_into_protected_scope,
509*0a6a1f1dSLionel Sambuc                 diag::ext_goto_into_protected_scope,
510f4a2713aSLionel Sambuc                 diag::warn_cxx98_compat_goto_into_protected_scope);
511f4a2713aSLionel Sambuc       continue;
512f4a2713aSLionel Sambuc     }
513f4a2713aSLionel Sambuc 
514f4a2713aSLionel Sambuc     SwitchStmt *SS = cast<SwitchStmt>(Jump);
515f4a2713aSLionel Sambuc     for (SwitchCase *SC = SS->getSwitchCaseList(); SC;
516f4a2713aSLionel Sambuc          SC = SC->getNextSwitchCase()) {
517*0a6a1f1dSLionel Sambuc       if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(SC)))
518*0a6a1f1dSLionel Sambuc         continue;
519f4a2713aSLionel Sambuc       SourceLocation Loc;
520f4a2713aSLionel Sambuc       if (CaseStmt *CS = dyn_cast<CaseStmt>(SC))
521f4a2713aSLionel Sambuc         Loc = CS->getLocStart();
522f4a2713aSLionel Sambuc       else if (DefaultStmt *DS = dyn_cast<DefaultStmt>(SC))
523f4a2713aSLionel Sambuc         Loc = DS->getLocStart();
524f4a2713aSLionel Sambuc       else
525f4a2713aSLionel Sambuc         Loc = SC->getLocStart();
526f4a2713aSLionel Sambuc       CheckJump(SS, SC, Loc, diag::err_switch_into_protected_scope, 0,
527f4a2713aSLionel Sambuc                 diag::warn_cxx98_compat_switch_into_protected_scope);
528f4a2713aSLionel Sambuc     }
529f4a2713aSLionel Sambuc   }
530f4a2713aSLionel Sambuc }
531f4a2713aSLionel Sambuc 
532f4a2713aSLionel Sambuc /// VerifyIndirectJumps - Verify whether any possible indirect jump
533f4a2713aSLionel Sambuc /// might cross a protection boundary.  Unlike direct jumps, indirect
534f4a2713aSLionel Sambuc /// jumps count cleanups as protection boundaries:  since there's no
535f4a2713aSLionel Sambuc /// way to know where the jump is going, we can't implicitly run the
536f4a2713aSLionel Sambuc /// right cleanups the way we can with direct jumps.
537f4a2713aSLionel Sambuc ///
538f4a2713aSLionel Sambuc /// Thus, an indirect jump is "trivial" if it bypasses no
539f4a2713aSLionel Sambuc /// initializations and no teardowns.  More formally, an indirect jump
540f4a2713aSLionel Sambuc /// from A to B is trivial if the path out from A to DCA(A,B) is
541f4a2713aSLionel Sambuc /// trivial and the path in from DCA(A,B) to B is trivial, where
542f4a2713aSLionel Sambuc /// DCA(A,B) is the deepest common ancestor of A and B.
543f4a2713aSLionel Sambuc /// Jump-triviality is transitive but asymmetric.
544f4a2713aSLionel Sambuc ///
545f4a2713aSLionel Sambuc /// A path in is trivial if none of the entered scopes have an InDiag.
546f4a2713aSLionel Sambuc /// A path out is trivial is none of the exited scopes have an OutDiag.
547f4a2713aSLionel Sambuc ///
548f4a2713aSLionel Sambuc /// Under these definitions, this function checks that the indirect
549f4a2713aSLionel Sambuc /// jump between A and B is trivial for every indirect goto statement A
550f4a2713aSLionel Sambuc /// and every label B whose address was taken in the function.
VerifyIndirectJumps()551f4a2713aSLionel Sambuc void JumpScopeChecker::VerifyIndirectJumps() {
552f4a2713aSLionel Sambuc   if (IndirectJumps.empty()) return;
553f4a2713aSLionel Sambuc 
554f4a2713aSLionel Sambuc   // If there aren't any address-of-label expressions in this function,
555f4a2713aSLionel Sambuc   // complain about the first indirect goto.
556f4a2713aSLionel Sambuc   if (IndirectJumpTargets.empty()) {
557f4a2713aSLionel Sambuc     S.Diag(IndirectJumps[0]->getGotoLoc(),
558f4a2713aSLionel Sambuc            diag::err_indirect_goto_without_addrlabel);
559f4a2713aSLionel Sambuc     return;
560f4a2713aSLionel Sambuc   }
561f4a2713aSLionel Sambuc 
562f4a2713aSLionel Sambuc   // Collect a single representative of every scope containing an
563f4a2713aSLionel Sambuc   // indirect goto.  For most code bases, this substantially cuts
564f4a2713aSLionel Sambuc   // down on the number of jump sites we'll have to consider later.
565f4a2713aSLionel Sambuc   typedef std::pair<unsigned, IndirectGotoStmt*> JumpScope;
566f4a2713aSLionel Sambuc   SmallVector<JumpScope, 32> JumpScopes;
567f4a2713aSLionel Sambuc   {
568f4a2713aSLionel Sambuc     llvm::DenseMap<unsigned, IndirectGotoStmt*> JumpScopesMap;
569f4a2713aSLionel Sambuc     for (SmallVectorImpl<IndirectGotoStmt*>::iterator
570f4a2713aSLionel Sambuc            I = IndirectJumps.begin(), E = IndirectJumps.end(); I != E; ++I) {
571f4a2713aSLionel Sambuc       IndirectGotoStmt *IG = *I;
572*0a6a1f1dSLionel Sambuc       if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(IG)))
573*0a6a1f1dSLionel Sambuc         continue;
574f4a2713aSLionel Sambuc       unsigned IGScope = LabelAndGotoScopes[IG];
575f4a2713aSLionel Sambuc       IndirectGotoStmt *&Entry = JumpScopesMap[IGScope];
576f4a2713aSLionel Sambuc       if (!Entry) Entry = IG;
577f4a2713aSLionel Sambuc     }
578f4a2713aSLionel Sambuc     JumpScopes.reserve(JumpScopesMap.size());
579f4a2713aSLionel Sambuc     for (llvm::DenseMap<unsigned, IndirectGotoStmt*>::iterator
580f4a2713aSLionel Sambuc            I = JumpScopesMap.begin(), E = JumpScopesMap.end(); I != E; ++I)
581f4a2713aSLionel Sambuc       JumpScopes.push_back(*I);
582f4a2713aSLionel Sambuc   }
583f4a2713aSLionel Sambuc 
584f4a2713aSLionel Sambuc   // Collect a single representative of every scope containing a
585f4a2713aSLionel Sambuc   // label whose address was taken somewhere in the function.
586f4a2713aSLionel Sambuc   // For most code bases, there will be only one such scope.
587f4a2713aSLionel Sambuc   llvm::DenseMap<unsigned, LabelDecl*> TargetScopes;
588f4a2713aSLionel Sambuc   for (SmallVectorImpl<LabelDecl*>::iterator
589f4a2713aSLionel Sambuc          I = IndirectJumpTargets.begin(), E = IndirectJumpTargets.end();
590f4a2713aSLionel Sambuc        I != E; ++I) {
591f4a2713aSLionel Sambuc     LabelDecl *TheLabel = *I;
592*0a6a1f1dSLionel Sambuc     if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(TheLabel->getStmt())))
593*0a6a1f1dSLionel Sambuc       continue;
594f4a2713aSLionel Sambuc     unsigned LabelScope = LabelAndGotoScopes[TheLabel->getStmt()];
595f4a2713aSLionel Sambuc     LabelDecl *&Target = TargetScopes[LabelScope];
596f4a2713aSLionel Sambuc     if (!Target) Target = TheLabel;
597f4a2713aSLionel Sambuc   }
598f4a2713aSLionel Sambuc 
599f4a2713aSLionel Sambuc   // For each target scope, make sure it's trivially reachable from
600f4a2713aSLionel Sambuc   // every scope containing a jump site.
601f4a2713aSLionel Sambuc   //
602f4a2713aSLionel Sambuc   // A path between scopes always consists of exitting zero or more
603f4a2713aSLionel Sambuc   // scopes, then entering zero or more scopes.  We build a set of
604f4a2713aSLionel Sambuc   // of scopes S from which the target scope can be trivially
605f4a2713aSLionel Sambuc   // entered, then verify that every jump scope can be trivially
606f4a2713aSLionel Sambuc   // exitted to reach a scope in S.
607f4a2713aSLionel Sambuc   llvm::BitVector Reachable(Scopes.size(), false);
608f4a2713aSLionel Sambuc   for (llvm::DenseMap<unsigned,LabelDecl*>::iterator
609f4a2713aSLionel Sambuc          TI = TargetScopes.begin(), TE = TargetScopes.end(); TI != TE; ++TI) {
610f4a2713aSLionel Sambuc     unsigned TargetScope = TI->first;
611f4a2713aSLionel Sambuc     LabelDecl *TargetLabel = TI->second;
612f4a2713aSLionel Sambuc 
613f4a2713aSLionel Sambuc     Reachable.reset();
614f4a2713aSLionel Sambuc 
615f4a2713aSLionel Sambuc     // Mark all the enclosing scopes from which you can safely jump
616f4a2713aSLionel Sambuc     // into the target scope.  'Min' will end up being the index of
617f4a2713aSLionel Sambuc     // the shallowest such scope.
618f4a2713aSLionel Sambuc     unsigned Min = TargetScope;
619f4a2713aSLionel Sambuc     while (true) {
620f4a2713aSLionel Sambuc       Reachable.set(Min);
621f4a2713aSLionel Sambuc 
622f4a2713aSLionel Sambuc       // Don't go beyond the outermost scope.
623f4a2713aSLionel Sambuc       if (Min == 0) break;
624f4a2713aSLionel Sambuc 
625f4a2713aSLionel Sambuc       // Stop if we can't trivially enter the current scope.
626f4a2713aSLionel Sambuc       if (Scopes[Min].InDiag) break;
627f4a2713aSLionel Sambuc 
628f4a2713aSLionel Sambuc       Min = Scopes[Min].ParentScope;
629f4a2713aSLionel Sambuc     }
630f4a2713aSLionel Sambuc 
631f4a2713aSLionel Sambuc     // Walk through all the jump sites, checking that they can trivially
632f4a2713aSLionel Sambuc     // reach this label scope.
633f4a2713aSLionel Sambuc     for (SmallVectorImpl<JumpScope>::iterator
634f4a2713aSLionel Sambuc            I = JumpScopes.begin(), E = JumpScopes.end(); I != E; ++I) {
635f4a2713aSLionel Sambuc       unsigned Scope = I->first;
636f4a2713aSLionel Sambuc 
637f4a2713aSLionel Sambuc       // Walk out the "scope chain" for this scope, looking for a scope
638f4a2713aSLionel Sambuc       // we've marked reachable.  For well-formed code this amortizes
639f4a2713aSLionel Sambuc       // to O(JumpScopes.size() / Scopes.size()):  we only iterate
640f4a2713aSLionel Sambuc       // when we see something unmarked, and in well-formed code we
641f4a2713aSLionel Sambuc       // mark everything we iterate past.
642f4a2713aSLionel Sambuc       bool IsReachable = false;
643f4a2713aSLionel Sambuc       while (true) {
644f4a2713aSLionel Sambuc         if (Reachable.test(Scope)) {
645f4a2713aSLionel Sambuc           // If we find something reachable, mark all the scopes we just
646f4a2713aSLionel Sambuc           // walked through as reachable.
647f4a2713aSLionel Sambuc           for (unsigned S = I->first; S != Scope; S = Scopes[S].ParentScope)
648f4a2713aSLionel Sambuc             Reachable.set(S);
649f4a2713aSLionel Sambuc           IsReachable = true;
650f4a2713aSLionel Sambuc           break;
651f4a2713aSLionel Sambuc         }
652f4a2713aSLionel Sambuc 
653f4a2713aSLionel Sambuc         // Don't walk out if we've reached the top-level scope or we've
654f4a2713aSLionel Sambuc         // gotten shallower than the shallowest reachable scope.
655f4a2713aSLionel Sambuc         if (Scope == 0 || Scope < Min) break;
656f4a2713aSLionel Sambuc 
657f4a2713aSLionel Sambuc         // Don't walk out through an out-diagnostic.
658f4a2713aSLionel Sambuc         if (Scopes[Scope].OutDiag) break;
659f4a2713aSLionel Sambuc 
660f4a2713aSLionel Sambuc         Scope = Scopes[Scope].ParentScope;
661f4a2713aSLionel Sambuc       }
662f4a2713aSLionel Sambuc 
663f4a2713aSLionel Sambuc       // Only diagnose if we didn't find something.
664f4a2713aSLionel Sambuc       if (IsReachable) continue;
665f4a2713aSLionel Sambuc 
666f4a2713aSLionel Sambuc       DiagnoseIndirectJump(I->second, I->first, TargetLabel, TargetScope);
667f4a2713aSLionel Sambuc     }
668f4a2713aSLionel Sambuc   }
669f4a2713aSLionel Sambuc }
670f4a2713aSLionel Sambuc 
671f4a2713aSLionel Sambuc /// Return true if a particular error+note combination must be downgraded to a
672f4a2713aSLionel Sambuc /// warning in Microsoft mode.
IsMicrosoftJumpWarning(unsigned JumpDiag,unsigned InDiagNote)673f4a2713aSLionel Sambuc static bool IsMicrosoftJumpWarning(unsigned JumpDiag, unsigned InDiagNote) {
674f4a2713aSLionel Sambuc   return (JumpDiag == diag::err_goto_into_protected_scope &&
675f4a2713aSLionel Sambuc          (InDiagNote == diag::note_protected_by_variable_init ||
676f4a2713aSLionel Sambuc           InDiagNote == diag::note_protected_by_variable_nontriv_destructor));
677f4a2713aSLionel Sambuc }
678f4a2713aSLionel Sambuc 
679f4a2713aSLionel Sambuc /// Return true if a particular note should be downgraded to a compatibility
680f4a2713aSLionel Sambuc /// warning in C++11 mode.
IsCXX98CompatWarning(Sema & S,unsigned InDiagNote)681f4a2713aSLionel Sambuc static bool IsCXX98CompatWarning(Sema &S, unsigned InDiagNote) {
682f4a2713aSLionel Sambuc   return S.getLangOpts().CPlusPlus11 &&
683f4a2713aSLionel Sambuc          InDiagNote == diag::note_protected_by_variable_non_pod;
684f4a2713aSLionel Sambuc }
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc /// Produce primary diagnostic for an indirect jump statement.
DiagnoseIndirectJumpStmt(Sema & S,IndirectGotoStmt * Jump,LabelDecl * Target,bool & Diagnosed)687f4a2713aSLionel Sambuc static void DiagnoseIndirectJumpStmt(Sema &S, IndirectGotoStmt *Jump,
688f4a2713aSLionel Sambuc                                      LabelDecl *Target, bool &Diagnosed) {
689f4a2713aSLionel Sambuc   if (Diagnosed)
690f4a2713aSLionel Sambuc     return;
691f4a2713aSLionel Sambuc   S.Diag(Jump->getGotoLoc(), diag::err_indirect_goto_in_protected_scope);
692f4a2713aSLionel Sambuc   S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
693f4a2713aSLionel Sambuc   Diagnosed = true;
694f4a2713aSLionel Sambuc }
695f4a2713aSLionel Sambuc 
696f4a2713aSLionel Sambuc /// Produce note diagnostics for a jump into a protected scope.
NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes)697f4a2713aSLionel Sambuc void JumpScopeChecker::NoteJumpIntoScopes(ArrayRef<unsigned> ToScopes) {
698*0a6a1f1dSLionel Sambuc   if (CHECK_PERMISSIVE(ToScopes.empty()))
699*0a6a1f1dSLionel Sambuc     return;
700f4a2713aSLionel Sambuc   for (unsigned I = 0, E = ToScopes.size(); I != E; ++I)
701f4a2713aSLionel Sambuc     if (Scopes[ToScopes[I]].InDiag)
702f4a2713aSLionel Sambuc       S.Diag(Scopes[ToScopes[I]].Loc, Scopes[ToScopes[I]].InDiag);
703f4a2713aSLionel Sambuc }
704f4a2713aSLionel Sambuc 
705f4a2713aSLionel Sambuc /// Diagnose an indirect jump which is known to cross scopes.
DiagnoseIndirectJump(IndirectGotoStmt * Jump,unsigned JumpScope,LabelDecl * Target,unsigned TargetScope)706f4a2713aSLionel Sambuc void JumpScopeChecker::DiagnoseIndirectJump(IndirectGotoStmt *Jump,
707f4a2713aSLionel Sambuc                                             unsigned JumpScope,
708f4a2713aSLionel Sambuc                                             LabelDecl *Target,
709f4a2713aSLionel Sambuc                                             unsigned TargetScope) {
710*0a6a1f1dSLionel Sambuc   if (CHECK_PERMISSIVE(JumpScope == TargetScope))
711*0a6a1f1dSLionel Sambuc     return;
712f4a2713aSLionel Sambuc 
713f4a2713aSLionel Sambuc   unsigned Common = GetDeepestCommonScope(JumpScope, TargetScope);
714f4a2713aSLionel Sambuc   bool Diagnosed = false;
715f4a2713aSLionel Sambuc 
716f4a2713aSLionel Sambuc   // Walk out the scope chain until we reach the common ancestor.
717f4a2713aSLionel Sambuc   for (unsigned I = JumpScope; I != Common; I = Scopes[I].ParentScope)
718f4a2713aSLionel Sambuc     if (Scopes[I].OutDiag) {
719f4a2713aSLionel Sambuc       DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
720f4a2713aSLionel Sambuc       S.Diag(Scopes[I].Loc, Scopes[I].OutDiag);
721f4a2713aSLionel Sambuc     }
722f4a2713aSLionel Sambuc 
723f4a2713aSLionel Sambuc   SmallVector<unsigned, 10> ToScopesCXX98Compat;
724f4a2713aSLionel Sambuc 
725f4a2713aSLionel Sambuc   // Now walk into the scopes containing the label whose address was taken.
726f4a2713aSLionel Sambuc   for (unsigned I = TargetScope; I != Common; I = Scopes[I].ParentScope)
727f4a2713aSLionel Sambuc     if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
728f4a2713aSLionel Sambuc       ToScopesCXX98Compat.push_back(I);
729f4a2713aSLionel Sambuc     else if (Scopes[I].InDiag) {
730f4a2713aSLionel Sambuc       DiagnoseIndirectJumpStmt(S, Jump, Target, Diagnosed);
731f4a2713aSLionel Sambuc       S.Diag(Scopes[I].Loc, Scopes[I].InDiag);
732f4a2713aSLionel Sambuc     }
733f4a2713aSLionel Sambuc 
734f4a2713aSLionel Sambuc   // Diagnose this jump if it would be ill-formed in C++98.
735f4a2713aSLionel Sambuc   if (!Diagnosed && !ToScopesCXX98Compat.empty()) {
736f4a2713aSLionel Sambuc     S.Diag(Jump->getGotoLoc(),
737f4a2713aSLionel Sambuc            diag::warn_cxx98_compat_indirect_goto_in_protected_scope);
738f4a2713aSLionel Sambuc     S.Diag(Target->getStmt()->getIdentLoc(), diag::note_indirect_goto_target);
739f4a2713aSLionel Sambuc     NoteJumpIntoScopes(ToScopesCXX98Compat);
740f4a2713aSLionel Sambuc   }
741f4a2713aSLionel Sambuc }
742f4a2713aSLionel Sambuc 
743f4a2713aSLionel Sambuc /// CheckJump - Validate that the specified jump statement is valid: that it is
744f4a2713aSLionel Sambuc /// jumping within or out of its current scope, not into a deeper one.
CheckJump(Stmt * From,Stmt * To,SourceLocation DiagLoc,unsigned JumpDiagError,unsigned JumpDiagWarning,unsigned JumpDiagCXX98Compat)745f4a2713aSLionel Sambuc void JumpScopeChecker::CheckJump(Stmt *From, Stmt *To, SourceLocation DiagLoc,
746f4a2713aSLionel Sambuc                                unsigned JumpDiagError, unsigned JumpDiagWarning,
747f4a2713aSLionel Sambuc                                  unsigned JumpDiagCXX98Compat) {
748*0a6a1f1dSLionel Sambuc   if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(From)))
749*0a6a1f1dSLionel Sambuc     return;
750*0a6a1f1dSLionel Sambuc   if (CHECK_PERMISSIVE(!LabelAndGotoScopes.count(To)))
751*0a6a1f1dSLionel Sambuc     return;
752f4a2713aSLionel Sambuc 
753*0a6a1f1dSLionel Sambuc   unsigned FromScope = LabelAndGotoScopes[From];
754f4a2713aSLionel Sambuc   unsigned ToScope = LabelAndGotoScopes[To];
755f4a2713aSLionel Sambuc 
756f4a2713aSLionel Sambuc   // Common case: exactly the same scope, which is fine.
757f4a2713aSLionel Sambuc   if (FromScope == ToScope) return;
758f4a2713aSLionel Sambuc 
759f4a2713aSLionel Sambuc   unsigned CommonScope = GetDeepestCommonScope(FromScope, ToScope);
760f4a2713aSLionel Sambuc 
761f4a2713aSLionel Sambuc   // It's okay to jump out from a nested scope.
762f4a2713aSLionel Sambuc   if (CommonScope == ToScope) return;
763f4a2713aSLionel Sambuc 
764f4a2713aSLionel Sambuc   // Pull out (and reverse) any scopes we might need to diagnose skipping.
765f4a2713aSLionel Sambuc   SmallVector<unsigned, 10> ToScopesCXX98Compat;
766f4a2713aSLionel Sambuc   SmallVector<unsigned, 10> ToScopesError;
767f4a2713aSLionel Sambuc   SmallVector<unsigned, 10> ToScopesWarning;
768f4a2713aSLionel Sambuc   for (unsigned I = ToScope; I != CommonScope; I = Scopes[I].ParentScope) {
769*0a6a1f1dSLionel Sambuc     if (S.getLangOpts().MSVCCompat && JumpDiagWarning != 0 &&
770f4a2713aSLionel Sambuc         IsMicrosoftJumpWarning(JumpDiagError, Scopes[I].InDiag))
771f4a2713aSLionel Sambuc       ToScopesWarning.push_back(I);
772f4a2713aSLionel Sambuc     else if (IsCXX98CompatWarning(S, Scopes[I].InDiag))
773f4a2713aSLionel Sambuc       ToScopesCXX98Compat.push_back(I);
774f4a2713aSLionel Sambuc     else if (Scopes[I].InDiag)
775f4a2713aSLionel Sambuc       ToScopesError.push_back(I);
776f4a2713aSLionel Sambuc   }
777f4a2713aSLionel Sambuc 
778f4a2713aSLionel Sambuc   // Handle warnings.
779f4a2713aSLionel Sambuc   if (!ToScopesWarning.empty()) {
780f4a2713aSLionel Sambuc     S.Diag(DiagLoc, JumpDiagWarning);
781f4a2713aSLionel Sambuc     NoteJumpIntoScopes(ToScopesWarning);
782f4a2713aSLionel Sambuc   }
783f4a2713aSLionel Sambuc 
784f4a2713aSLionel Sambuc   // Handle errors.
785f4a2713aSLionel Sambuc   if (!ToScopesError.empty()) {
786f4a2713aSLionel Sambuc     S.Diag(DiagLoc, JumpDiagError);
787f4a2713aSLionel Sambuc     NoteJumpIntoScopes(ToScopesError);
788f4a2713aSLionel Sambuc   }
789f4a2713aSLionel Sambuc 
790f4a2713aSLionel Sambuc   // Handle -Wc++98-compat warnings if the jump is well-formed.
791f4a2713aSLionel Sambuc   if (ToScopesError.empty() && !ToScopesCXX98Compat.empty()) {
792f4a2713aSLionel Sambuc     S.Diag(DiagLoc, JumpDiagCXX98Compat);
793f4a2713aSLionel Sambuc     NoteJumpIntoScopes(ToScopesCXX98Compat);
794f4a2713aSLionel Sambuc   }
795f4a2713aSLionel Sambuc }
796f4a2713aSLionel Sambuc 
CheckGotoStmt(GotoStmt * GS)797*0a6a1f1dSLionel Sambuc void JumpScopeChecker::CheckGotoStmt(GotoStmt *GS) {
798*0a6a1f1dSLionel Sambuc   if (GS->getLabel()->isMSAsmLabel()) {
799*0a6a1f1dSLionel Sambuc     S.Diag(GS->getGotoLoc(), diag::err_goto_ms_asm_label)
800*0a6a1f1dSLionel Sambuc         << GS->getLabel()->getIdentifier();
801*0a6a1f1dSLionel Sambuc     S.Diag(GS->getLabel()->getLocation(), diag::note_goto_ms_asm_label)
802*0a6a1f1dSLionel Sambuc         << GS->getLabel()->getIdentifier();
803*0a6a1f1dSLionel Sambuc   }
804*0a6a1f1dSLionel Sambuc }
805*0a6a1f1dSLionel Sambuc 
DiagnoseInvalidJumps(Stmt * Body)806f4a2713aSLionel Sambuc void Sema::DiagnoseInvalidJumps(Stmt *Body) {
807f4a2713aSLionel Sambuc   (void)JumpScopeChecker(Body, *this);
808f4a2713aSLionel Sambuc }
809