From: Sebastian Redl Date: Sun, 21 Dec 2008 12:04:03 +0000 (+0000) Subject: Convert a few Stmt actions to smart pointers. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a60528cdac7deee3991c2b48af4df4f315e49e9d;p=clang Convert a few Stmt actions to smart pointers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@61309 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/Driver/PrintParserCallbacks.cpp b/Driver/PrintParserCallbacks.cpp index dff931bba4..02b33fcc0f 100644 --- a/Driver/PrintParserCallbacks.cpp +++ b/Driver/PrintParserCallbacks.cpp @@ -241,27 +241,29 @@ namespace { //===--------------------------------------------------------------------===// // Statement Parsing Callbacks. //===--------------------------------------------------------------------===// - - virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc) { + + virtual OwningStmtResult ActOnNullStmt(SourceLocation SemiLoc) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - - virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, - StmtTy **Elts, unsigned NumElts, - bool isStmtExpr) { + + virtual OwningStmtResult ActOnCompoundStmt(SourceLocation L, + SourceLocation R, + MultiStmtArg Elts, + bool isStmtExpr) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - virtual StmtResult ActOnDeclStmt(DeclTy *Decl, SourceLocation StartLoc, - SourceLocation EndLoc) { + virtual OwningStmtResult ActOnDeclStmt(DeclTy *Decl, + SourceLocation StartLoc, + SourceLocation EndLoc) { llvm::cout << __FUNCTION__ << "\n"; - return 0; + return StmtEmpty(); } - virtual StmtResult ActOnExprStmt(ExprTy *Expr) { + virtual OwningStmtResult ActOnExprStmt(ExprArg Expr) { llvm::cout << __FUNCTION__ << "\n"; - return StmtResult(Expr); + return OwningStmtResult(*this, Expr.release()); } /// ActOnCaseStmt - Note that this handles the GNU 'case 1 ... 4' extension, diff --git a/include/clang/Parse/Action.h b/include/clang/Parse/Action.h index 47ee53bcc4..3031206dd6 100644 --- a/include/clang/Parse/Action.h +++ b/include/clang/Parse/Action.h @@ -342,25 +342,25 @@ public: //===--------------------------------------------------------------------===// // Statement Parsing Callbacks. //===--------------------------------------------------------------------===// - - virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc) { - return 0; + + virtual OwningStmtResult ActOnNullStmt(SourceLocation SemiLoc) { + return StmtEmpty(); } - - virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, - StmtTy **Elts, unsigned NumElts, - bool isStmtExpr) { - return 0; + + virtual OwningStmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, + MultiStmtArg Elts, + bool isStmtExpr) { + return StmtEmpty(); } - virtual StmtResult ActOnDeclStmt(DeclTy *Decl, SourceLocation StartLoc, + virtual OwningStmtResult ActOnDeclStmt(DeclTy *Decl, SourceLocation StartLoc, SourceLocation EndLoc) { - return 0; + return StmtEmpty(); } - - virtual StmtResult ActOnExprStmt(ExprTy *Expr) { - return StmtResult(Expr); + + virtual OwningStmtResult ActOnExprStmt(ExprArg Expr) { + return OwningStmtResult(*this, Expr.release()); } - + /// ActOnCaseStmt - Note that this handles the GNU 'case 1 ... 4' extension, /// which can specify an RHS value. virtual StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, diff --git a/include/clang/Parse/Parser.h b/include/clang/Parse/Parser.h index cfc9e3b7cc..df6eca6432 100644 --- a/include/clang/Parse/Parser.h +++ b/include/clang/Parse/Parser.h @@ -90,6 +90,7 @@ public: typedef Action::OwningTemplateArgResult OwningTemplateArgResult; typedef Action::ExprArg ExprArg; + typedef Action::MultiStmtArg MultiStmtArg; /// Adorns a ExprResult with Actions to make it an OwningExprResult OwningExprResult Owned(ExprResult res) { diff --git a/lib/Parse/AstGuard.h b/lib/Parse/AstGuard.h index 5d17aa66b6..a65caedeb3 100644 --- a/lib/Parse/AstGuard.h +++ b/lib/Parse/AstGuard.h @@ -23,7 +23,7 @@ namespace clang /// automatically freeing them on destruction unless it's been disowned. /// Instantiated for statements and expressions (Action::DeleteStmt and /// Action::DeleteExpr). - template + template class ASTVector : public llvm::SmallVector { private: Action &Actions; @@ -50,6 +50,8 @@ namespace clang Owns = false; return &(*this)[0]; } + + Action &getActions() const { return Actions; } }; /// A SmallVector of statements, with stack size 32 (as that is the only one @@ -57,6 +59,11 @@ namespace clang typedef ASTVector<&Action::DeleteStmt, 32> StmtVector; /// A SmallVector of expressions, with stack size 12 (the maximum used.) typedef ASTVector<&Action::DeleteExpr, 12> ExprVector; + + template inline + ASTMultiPtr move_convert(ASTVector &vec) { + return ASTMultiPtr(vec.getActions(), vec.take(), vec.size()); + } } #endif diff --git a/lib/Parse/ParseObjc.cpp b/lib/Parse/ParseObjc.cpp index e70d2024c7..759de7a064 100644 --- a/lib/Parse/ParseObjc.cpp +++ b/lib/Parse/ParseObjc.cpp @@ -1365,7 +1365,8 @@ Parser::DeclTy *Parser::ParseObjCMethodDefinition() { // If the function body could not be parsed, make a bogus compoundstmt. if (FnBody.isInvalid()) - FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false); + FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, + MultiStmtArg(Actions), false); // Leave the function body scope. BodyScope.Exit(); @@ -1392,7 +1393,7 @@ Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) { } // Otherwise, eat the semicolon. ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); - return Owned(Actions.ActOnExprStmt(Res.release())); + return Actions.ActOnExprStmt(move_convert(Res)); } Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) { diff --git a/lib/Parse/ParseStmt.cpp b/lib/Parse/ParseStmt.cpp index d22fbb7044..c605bf2352 100644 --- a/lib/Parse/ParseStmt.cpp +++ b/lib/Parse/ParseStmt.cpp @@ -101,7 +101,7 @@ Parser::ParseStatementOrDeclaration(bool OnlyStatement) { SourceLocation DeclStart = Tok.getLocation(); DeclTy *Decl = ParseDeclaration(Declarator::BlockContext); // FIXME: Pass in the right location for the end of the declstmt. - return Owned(Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart)); + return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart); } else if (Tok.is(tok::r_brace)) { Diag(Tok, diag::err_expected_statement); return StmtError(); @@ -117,7 +117,7 @@ Parser::ParseStatementOrDeclaration(bool OnlyStatement) { } // Otherwise, eat the semicolon. ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); - return Owned(Actions.ActOnExprStmt(Expr.release())); + return Actions.ActOnExprStmt(move_convert(Expr)); } case tok::kw_case: // C99 6.8.1: labeled-statement @@ -128,7 +128,7 @@ Parser::ParseStatementOrDeclaration(bool OnlyStatement) { case tok::l_brace: // C99 6.8.2: compound-statement return ParseCompoundStatement(); case tok::semi: // C99 6.8.3p3: expression[opt] ';' - return Owned(Actions.ActOnNullStmt(ConsumeToken())); + return Actions.ActOnNullStmt(ConsumeToken()); case tok::kw_if: // C99 6.8.4.1: if-statement return ParseIfStatement(); @@ -395,7 +395,7 @@ Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { // Eat the semicolon at the end of stmt and convert the expr into a // statement. ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr); - R = Actions.ActOnExprStmt(Res.release()); + R = Actions.ActOnExprStmt(move_convert(Res)); } } @@ -410,8 +410,8 @@ Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) { } SourceLocation RBraceLoc = ConsumeBrace(); - return Owned(Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, Stmts.take(), - Stmts.size(), isStmtExpr)); + return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, move_convert(Stmts), + isStmtExpr); } /// ParseParenExprOrCondition: @@ -863,7 +863,7 @@ Parser::OwningStmtResult Parser::ParseForStatement() { // Turn the expression into a stmt. if (!Value.isInvalid()) - FirstPart = Actions.ActOnExprStmt(Value.release()); + FirstPart = Actions.ActOnExprStmt(move_convert(Value)); if (Tok.is(tok::semi)) { ConsumeToken(); @@ -900,7 +900,7 @@ Parser::OwningStmtResult Parser::ParseForStatement() { Value = ParseExpression(); if (!Value.isInvalid()) { // Turn the expression into a stmt. - ThirdPart = Actions.ActOnExprStmt(Value.release()); + ThirdPart = Actions.ActOnExprStmt(move_convert(Value)); } } } @@ -1039,7 +1039,7 @@ Parser::OwningStmtResult Parser::FuzzyParseMicrosoftAsmStatement() { Tok.isNot(tok::r_brace) && Tok.isNot(tok::semi) && Tok.isNot(tok::eof)); } - return Owned(Actions.ActOnNullStmt(Tok.getLocation())); + return Actions.ActOnNullStmt(Tok.getLocation()); } /// ParseAsmStatement - Parse a GNU extended asm statement. @@ -1239,7 +1239,7 @@ Parser::DeclTy *Parser::ParseFunctionStatementBody(DeclTy *Decl, // If the function body could not be parsed, make a bogus compoundstmt. if (FnBody.isInvalid()) - FnBody = Owned(Actions.ActOnCompoundStmt(L, R, 0, 0, false)); + FnBody = Actions.ActOnCompoundStmt(L, R, MultiStmtArg(Actions), false); return Actions.ActOnFinishFunctionBody(Decl, move_convert(FnBody)); } diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 9cab3308c5..e5146a23bb 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -552,14 +552,14 @@ public: //===--------------------------------------------------------------------===// // Statement Parsing Callbacks: SemaStmt.cpp. public: - virtual StmtResult ActOnExprStmt(ExprTy *Expr); - - virtual StmtResult ActOnNullStmt(SourceLocation SemiLoc); - virtual StmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, - StmtTy **Elts, unsigned NumElts, - bool isStmtExpr); - virtual StmtResult ActOnDeclStmt(DeclTy *Decl, SourceLocation StartLoc, - SourceLocation EndLoc); + virtual OwningStmtResult ActOnExprStmt(ExprArg Expr); + + virtual OwningStmtResult ActOnNullStmt(SourceLocation SemiLoc); + virtual OwningStmtResult ActOnCompoundStmt(SourceLocation L, SourceLocation R, + MultiStmtArg Elts, + bool isStmtExpr); + virtual OwningStmtResult ActOnDeclStmt(DeclTy *Decl, SourceLocation StartLoc, + SourceLocation EndLoc); virtual StmtResult ActOnCaseStmt(SourceLocation CaseLoc, ExprTy *LHSVal, SourceLocation DotDotDotLoc, ExprTy *RHSVal, SourceLocation ColonLoc, StmtTy *SubStmt); diff --git a/lib/Sema/SemaStmt.cpp b/lib/Sema/SemaStmt.cpp index 97cad60a19..22a9617eb7 100644 --- a/lib/Sema/SemaStmt.cpp +++ b/lib/Sema/SemaStmt.cpp @@ -20,31 +20,31 @@ #include "clang/Basic/Diagnostic.h" using namespace clang; -Sema::StmtResult Sema::ActOnExprStmt(ExprTy *expr) { - Expr *E = static_cast(expr); +Sema::OwningStmtResult Sema::ActOnExprStmt(ExprArg expr) { + Expr *E = static_cast(expr.release()); assert(E && "ActOnExprStmt(): missing expression"); - + // C99 6.8.3p2: The expression in an expression statement is evaluated as a // void expression for its side effects. Conversion to void allows any // operand, even incomplete types. - + // Same thing in for stmt first clause (when expr) and third clause. - return E; + return Owned(static_cast(E)); } -Sema::StmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { - return new NullStmt(SemiLoc); +Sema::OwningStmtResult Sema::ActOnNullStmt(SourceLocation SemiLoc) { + return Owned(new NullStmt(SemiLoc)); } -Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc, - SourceLocation EndLoc) { +Sema::OwningStmtResult Sema::ActOnDeclStmt(DeclTy *decl, + SourceLocation StartLoc, + SourceLocation EndLoc) { if (decl == 0) - return true; - + return StmtError(); + ScopedDecl *SD = cast(static_cast(decl)); - - + // This is a temporary hack until we are always passing around // DeclGroupRefs. llvm::SmallVector decls; @@ -56,21 +56,22 @@ Sema::StmtResult Sema::ActOnDeclStmt(DeclTy *decl, SourceLocation StartLoc, } assert (!decls.empty()); - + if (decls.size() == 1) { DeclGroupOwningRef DG(*decls.begin()); - return new DeclStmt(DG, StartLoc, EndLoc); + return Owned(new DeclStmt(DG, StartLoc, EndLoc)); } else { DeclGroupOwningRef DG(DeclGroup::Create(Context, decls.size(), &decls[0])); - return new DeclStmt(DG, StartLoc, EndLoc); + return Owned(new DeclStmt(DG, StartLoc, EndLoc)); } } -Action::StmtResult +Action::OwningStmtResult Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, - StmtTy **elts, unsigned NumElts, bool isStmtExpr) { - Stmt **Elts = reinterpret_cast(elts); + MultiStmtArg elts, bool isStmtExpr) { + unsigned NumElts = elts.size(); + Stmt **Elts = reinterpret_cast(elts.release()); // If we're in C89 mode, check that we don't have any decls after stmts. If // so, emit an extension diagnostic. if (!getLangOptions().C99 && !getLangOptions().CPlusPlus) { @@ -111,11 +112,11 @@ Sema::ActOnCompoundStmt(SourceLocation L, SourceLocation R, else if (const UnaryOperator *UO = dyn_cast(E)) Diag(UO->getOperatorLoc(), diag::warn_unused_expr) << UO->getSubExpr()->getSourceRange(); - else + else Diag(E->getExprLoc(), diag::warn_unused_expr) << E->getSourceRange(); } - - return new CompoundStmt(Elts, NumElts, L, R); + + return Owned(new CompoundStmt(Elts, NumElts, L, R)); } Action::StmtResult