On Apr 27, 2009, at 2:33 PM, Sebastian Redl wrote: > Author: cornedbee > Date: Mon Apr 27 16:33:24 2009 > New Revision: 70256 > > URL: http://llvm.org/viewvc/llvm-project?rev=70256&view=rev > Log: > Track down return statements in the handlers of a function-try-block > of constructors. Meh ...
Hi Sebastian, Instead of doing this as a treewalk, how about handling this in sema of a return. Just check to see if the context is a construction function whose body is a try block there? -Chris > > > Modified: > cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td > cfe/trunk/lib/Sema/Sema.h > cfe/trunk/lib/Sema/SemaDecl.cpp > cfe/trunk/lib/Sema/SemaDeclCXX.cpp > cfe/trunk/test/SemaCXX/exceptions.cpp > cfe/trunk/www/cxx_status.html > > Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original) > +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Apr 27 > 16:33:24 2009 > @@ -1204,6 +1204,8 @@ > "cannot throw object of incomplete type %0">; > def err_throw_incomplete_ptr : Error< > "cannot throw pointer to object of incomplete type %0">; > +def err_return_in_constructor_handler : Error< > + "return in the catch of a function try block of a constructor is > illegal">; > > def err_invalid_use_of_function_type : Error< > "a function type is not allowed here">; > > Modified: cfe/trunk/lib/Sema/Sema.h > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/lib/Sema/Sema.h (original) > +++ cfe/trunk/lib/Sema/Sema.h Mon Apr 27 16:33:24 2009 > @@ -60,6 +60,7 @@ > class ArrayType; > class LabelStmt; > class SwitchStmt; > + class CXXTryStmt; > class ExtVectorType; > class TypedefDecl; > class TemplateDecl; > @@ -1225,6 +1226,7 @@ > virtual OwningStmtResult ActOnCXXTryBlock(SourceLocation TryLoc, > StmtArg TryBlock, > MultiStmtArg Handlers); > + void DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt > *TryBlock); > > // > = > = > =-------------------------------------------------------------------- > ===// > // Expression Parsing Callbacks: SemaExpr.cpp. > > Modified: cfe/trunk/lib/Sema/SemaDecl.cpp > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/lib/Sema/SemaDecl.cpp (original) > +++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Apr 27 16:33:24 2009 > @@ -3054,6 +3054,11 @@ > if (CurFunctionNeedsScopeChecking) > DiagnoseInvalidJumps(Body); > > + // C++ constructors that have function-try-blocks can't have > return statements > + // in the handlers of that block. (C++ [except.handle]p14) Verify > this. > + if (isa<CXXConstructorDecl>(dcl) && isa<CXXTryStmt>(Body)) > + > DiagnoseReturnInConstructorExceptionHandler(cast<CXXTryStmt>(Body)); > + > return D; > } > > > Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original) > +++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Mon Apr 27 16:33:24 2009 > @@ -2664,3 +2664,24 @@ > } > Fn->setDeleted(); > } > + > +static void SearchForReturnInStmt(Sema &Self, Stmt *S) { > + for (Stmt::child_iterator CI = S->child_begin(), E = S- > >child_end(); CI != E; > + ++CI) { > + Stmt *SubStmt = *CI; > + if (!SubStmt) > + continue; > + if (isa<ReturnStmt>(SubStmt)) > + Self.Diag(SubStmt->getSourceRange().getBegin(), > + diag::err_return_in_constructor_handler); > + if (!isa<Expr>(SubStmt)) > + SearchForReturnInStmt(Self, SubStmt); > + } > +} > + > +void Sema::DiagnoseReturnInConstructorExceptionHandler(CXXTryStmt > *TryBlock) { > + for (unsigned I = 0, E = TryBlock->getNumHandlers(); I != E; ++I) { > + CXXCatchStmt *Handler = TryBlock->getHandler(I); > + SearchForReturnInStmt(*this, Handler); > + } > +} > > Modified: cfe/trunk/test/SemaCXX/exceptions.cpp > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/exceptions.cpp?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/test/SemaCXX/exceptions.cpp (original) > +++ cfe/trunk/test/SemaCXX/exceptions.cpp Mon Apr 27 16:33:24 2009 > @@ -68,3 +68,32 @@ > goto l2; // expected-error {{illegal goto into protected scope}} > goto l1; > } > + > +struct BadReturn { > + BadReturn() try { > + } catch(...) { > + // Try to hide > + try { > + } catch(...) { > + { > + if (0) > + return; // expected-error {{return in the catch of a > function try block of a constructor is illegal}} > + } > + } > + } > + BadReturn(int); > +}; > + > +BadReturn::BadReturn(int) try { > +} catch(...) { > + // Try to hide > + try { > + } catch(int) { > + return; // expected-error {{return in the catch of a function > try block of a constructor is illegal}} > + } catch(...) { > + { > + if (0) > + return; // expected-error {{return in the catch of a > function try block of a constructor is illegal}} > + } > + } > +} > > Modified: cfe/trunk/www/cxx_status.html > URL: > http://llvm.org/viewvc/llvm-project/cfe/trunk/www/cxx_status.html?rev=70256&r1=70255&r2=70256&view=diff > > = > = > = > = > = > = > = > = > ====================================================================== > --- cfe/trunk/www/cxx_status.html (original) > +++ cfe/trunk/www/cxx_status.html Mon Apr 27 16:33:24 2009 > @@ -1664,7 +1664,7 @@ > <td class="na" align="center">N/A</td> > <td class="advanced" align="center"></td> > <td></td> > - <td>Not all constraints are checked, such as existence of return > statements in function-try-block handlers of constructors</td> > + <td>Not all constraints are checked</td> > </tr> > <tr> > <td> 15.4 [except.spec]</td> > > > _______________________________________________ > cfe-commits mailing list > [email protected] > http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits _______________________________________________ cfe-commits mailing list [email protected] http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
