[cfe-commits] r60888 - in /cfe/trunk: include/clang/Parse/Parser.h lib/Parse/ParseExpr.cpp lib/Parse/ParseObjc.cpp lib/Parse/ParseStmt.cpp lib/Parse/Parser.cpp
Sebastian Redl
sebastian.redl at getdesigned.at
Thu Dec 11 11:30:54 PST 2008
Author: cornedbee
Date: Thu Dec 11 13:30:53 2008
New Revision: 60888
URL: http://llvm.org/viewvc/llvm-project?rev=60888&view=rev
Log:
Convert a number of statement parsers to smart pointers.
Modified:
cfe/trunk/include/clang/Parse/Parser.h
cfe/trunk/lib/Parse/ParseExpr.cpp
cfe/trunk/lib/Parse/ParseObjc.cpp
cfe/trunk/lib/Parse/ParseStmt.cpp
cfe/trunk/lib/Parse/Parser.cpp
Modified: cfe/trunk/include/clang/Parse/Parser.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/Parser.h?rev=60888&r1=60887&r2=60888&view=diff
==============================================================================
--- cfe/trunk/include/clang/Parse/Parser.h (original)
+++ cfe/trunk/include/clang/Parse/Parser.h Thu Dec 11 13:30:53 2008
@@ -87,6 +87,18 @@
typedef Action::OwningExprResult OwningExprResult;
typedef Action::OwningStmtResult OwningStmtResult;
+ /// Adorns a ExprResult with Actions to make it an OwningExprResult
+ OwningExprResult Owned(ExprResult res) {
+ return OwningExprResult(Actions, res);
+ }
+ /// Adorns a StmtResult with Actions to make it an OwningStmtResult
+ OwningStmtResult Owned(StmtResult res) {
+ return OwningStmtResult(Actions, res);
+ }
+
+ OwningExprResult ExprError() { return OwningExprResult(Actions, true); }
+ OwningStmtResult StmtError() { return OwningStmtResult(Actions, true); }
+
// Parsing methods.
/// ParseTranslationUnit - All in one method that initializes parses, and
@@ -609,18 +621,20 @@
SourceLocation NameLoc,
IdentifierInfo *ReceiverName,
ExprTy *ReceiverExpr);
-
+
//===--------------------------------------------------------------------===//
// C99 6.8: Statements and Blocks.
-
- StmtResult ParseStatement() { return ParseStatementOrDeclaration(true); }
- StmtResult ParseStatementOrDeclaration(bool OnlyStatement = false);
- StmtResult ParseLabeledStatement();
- StmtResult ParseCaseStatement();
- StmtResult ParseDefaultStatement();
- StmtResult ParseCompoundStatement(bool isStmtExpr = false);
- StmtResult ParseCompoundStatementBody(bool isStmtExpr = false);
- StmtResult ParseIfStatement();
+
+ OwningStmtResult ParseStatement() {
+ return ParseStatementOrDeclaration(true);
+ }
+ OwningStmtResult ParseStatementOrDeclaration(bool OnlyStatement = false);
+ OwningStmtResult ParseLabeledStatement();
+ OwningStmtResult ParseCaseStatement();
+ OwningStmtResult ParseDefaultStatement();
+ OwningStmtResult ParseCompoundStatement(bool isStmtExpr = false);
+ OwningStmtResult ParseCompoundStatementBody(bool isStmtExpr = false);
+ OwningStmtResult ParseIfStatement();
StmtResult ParseSwitchStatement();
StmtResult ParseWhileStatement();
StmtResult ParseDoStatement();
Modified: cfe/trunk/lib/Parse/ParseExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseExpr.cpp?rev=60888&r1=60887&r2=60888&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseExpr.cpp (original)
+++ cfe/trunk/lib/Parse/ParseExpr.cpp Thu Dec 11 13:30:53 2008
@@ -1038,7 +1038,7 @@
if (ExprType >= CompoundStmt && Tok.is(tok::l_brace)) {
Diag(Tok, diag::ext_gnu_statement_expr);
- OwningStmtResult Stmt(Actions, ParseCompoundStatement(true));
+ OwningStmtResult Stmt(ParseCompoundStatement(true));
ExprType = CompoundStmt;
// If the substmt parsed correctly, build the AST node.
@@ -1192,7 +1192,7 @@
OwningExprResult Result(Actions, true);
if (Tok.is(tok::l_brace)) {
- OwningStmtResult Stmt(Actions, ParseCompoundStatementBody());
+ OwningStmtResult Stmt(ParseCompoundStatementBody());
if (!Stmt.isInvalid()) {
Result = Actions.ActOnBlockStmtExpr(CaretLoc, Stmt.release(), CurScope);
} else {
Modified: cfe/trunk/lib/Parse/ParseObjc.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseObjc.cpp?rev=60888&r1=60887&r2=60888&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseObjc.cpp (original)
+++ cfe/trunk/lib/Parse/ParseObjc.cpp Thu Dec 11 13:30:53 2008
@@ -1216,7 +1216,7 @@
// statements can always hold declarations.
ParseScope BodyScope(this, Scope::DeclScope);
- OwningStmtResult SynchBody(Actions, ParseCompoundStatementBody());
+ OwningStmtResult SynchBody(ParseCompoundStatementBody());
BodyScope.Exit();
if (SynchBody.isInvalid())
@@ -1247,7 +1247,7 @@
OwningStmtResult CatchStmts(Actions);
OwningStmtResult FinallyStmt(Actions);
ParseScope TryScope(this, Scope::DeclScope);
- OwningStmtResult TryBody(Actions, ParseCompoundStatementBody());
+ OwningStmtResult TryBody(ParseCompoundStatementBody());
TryScope.Exit();
if (TryBody.isInvalid())
TryBody = Actions.ActOnNullStmt(Tok.getLocation());
@@ -1357,9 +1357,9 @@
// Tell the actions module that we have entered a method definition with the
// specified Declarator for the method.
Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
-
- OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
-
+
+ OwningStmtResult FnBody(ParseCompoundStatementBody());
+
// If the function body could not be parsed, make a bogus compoundstmt.
if (FnBody.isInvalid())
FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
Modified: cfe/trunk/lib/Parse/ParseStmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseStmt.cpp?rev=60888&r1=60887&r2=60888&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParseStmt.cpp (original)
+++ cfe/trunk/lib/Parse/ParseStmt.cpp Thu Dec 11 13:30:53 2008
@@ -72,7 +72,8 @@
/// [OBC] '@' 'throw' expression ';'
/// [OBC] '@' 'throw' ';'
///
-Parser::StmtResult Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
+Parser::OwningStmtResult
+Parser::ParseStatementOrDeclaration(bool OnlyStatement) {
const char *SemiError = 0;
OwningStmtResult Res(Actions);
@@ -85,7 +86,7 @@
case tok::at: // May be a @try or @throw statement
{
AtLoc = ConsumeToken(); // consume @
- return ParseObjCAtStatement(AtLoc);
+ return Owned(ParseObjCAtStatement(AtLoc));
}
case tok::identifier:
@@ -100,10 +101,10 @@
SourceLocation DeclStart = Tok.getLocation();
DeclTy *Decl = ParseDeclaration(Declarator::BlockContext);
// FIXME: Pass in the right location for the end of the declstmt.
- return Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart);
+ return Owned(Actions.ActOnDeclStmt(Decl, DeclStart, DeclStart));
} else if (Tok.is(tok::r_brace)) {
Diag(Tok, diag::err_expected_statement);
- return true;
+ return StmtError();
} else {
// expression[opt] ';'
OwningExprResult Expr(Actions, ParseExpression());
@@ -112,36 +113,36 @@
// doing this opens us up to the possibility of infinite loops if
// ParseExpression does not consume any tokens.
SkipUntil(tok::semi);
- return true;
+ return StmtError();
}
// Otherwise, eat the semicolon.
ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
- return Actions.ActOnExprStmt(Expr.release());
+ return Owned(Actions.ActOnExprStmt(Expr.release()));
}
-
+
case tok::kw_case: // C99 6.8.1: labeled-statement
return ParseCaseStatement();
case tok::kw_default: // C99 6.8.1: labeled-statement
return ParseDefaultStatement();
-
+
case tok::l_brace: // C99 6.8.2: compound-statement
return ParseCompoundStatement();
case tok::semi: // C99 6.8.3p3: expression[opt] ';'
- return Actions.ActOnNullStmt(ConsumeToken());
-
+ return Owned(Actions.ActOnNullStmt(ConsumeToken()));
+
case tok::kw_if: // C99 6.8.4.1: if-statement
return ParseIfStatement();
case tok::kw_switch: // C99 6.8.4.2: switch-statement
- return ParseSwitchStatement();
-
+ return Owned(ParseSwitchStatement());
+
case tok::kw_while: // C99 6.8.5.1: while-statement
- return ParseWhileStatement();
+ return Owned(ParseWhileStatement());
case tok::kw_do: // C99 6.8.5.2: do-statement
Res = ParseDoStatement();
SemiError = "do/while loop";
break;
case tok::kw_for: // C99 6.8.5.3: for-statement
- return ParseForStatement();
+ return Owned(ParseForStatement());
case tok::kw_goto: // C99 6.8.6.1: goto-statement
Res = ParseGotoStatement();
@@ -159,15 +160,15 @@
Res = ParseReturnStatement();
SemiError = "return statement";
break;
-
+
case tok::kw_asm:
bool msAsm = false;
Res = ParseAsmStatement(msAsm);
- if (msAsm) return Res.result();
+ if (msAsm) return move(Res);
SemiError = "asm statement";
break;
}
-
+
// If we reached this code, the statement must end in a semicolon.
if (Tok.is(tok::semi)) {
ConsumeToken();
@@ -176,7 +177,7 @@
// Skip until we see a } or ;, but don't eat it.
SkipUntil(tok::r_brace, true, true);
}
- return Res.result();
+ return move(Res);
}
/// ParseLabeledStatement - We have an identifier and a ':' after it.
@@ -185,7 +186,7 @@
/// identifier ':' statement
/// [GNU] identifier ':' attributes[opt] statement
///
-Parser::StmtResult Parser::ParseLabeledStatement() {
+Parser::OwningStmtResult Parser::ParseLabeledStatement() {
assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
"Not an identifier!");
@@ -193,7 +194,7 @@
ConsumeToken(); // eat the identifier.
assert(Tok.is(tok::colon) && "Not a label!");
-
+
// identifier ':' statement
SourceLocation ColonLoc = ConsumeToken();
@@ -203,15 +204,15 @@
// TODO: save these somewhere.
AttrList = ParseAttributes();
- OwningStmtResult SubStmt(Actions, ParseStatement());
+ OwningStmtResult SubStmt(ParseStatement());
// Broken substmt shouldn't prevent the label from being added to the AST.
if (SubStmt.isInvalid())
SubStmt = Actions.ActOnNullStmt(ColonLoc);
- return Actions.ActOnLabelStmt(IdentTok.getLocation(),
- IdentTok.getIdentifierInfo(),
- ColonLoc, SubStmt.release());
+ return Owned(Actions.ActOnLabelStmt(IdentTok.getLocation(),
+ IdentTok.getIdentifierInfo(),
+ ColonLoc, SubStmt.release()));
}
/// ParseCaseStatement
@@ -221,14 +222,14 @@
///
/// Note that this does not parse the 'statement' at the end.
///
-Parser::StmtResult Parser::ParseCaseStatement() {
+Parser::OwningStmtResult Parser::ParseCaseStatement() {
assert(Tok.is(tok::kw_case) && "Not a case stmt!");
SourceLocation CaseLoc = ConsumeToken(); // eat the 'case'.
OwningExprResult LHS(Actions, ParseConstantExpression());
if (LHS.isInvalid()) {
SkipUntil(tok::colon);
- return true;
+ return StmtError();
}
// GNU case range extension.
@@ -241,32 +242,33 @@
RHS = ParseConstantExpression();
if (RHS.isInvalid()) {
SkipUntil(tok::colon);
- return true;
+ return StmtError();
}
}
if (Tok.isNot(tok::colon)) {
Diag(Tok, diag::err_expected_colon_after) << "'case'";
SkipUntil(tok::colon);
- return true;
+ return StmtError();
}
-
+
SourceLocation ColonLoc = ConsumeToken();
-
+
// Diagnose the common error "switch (X) { case 4: }", which is not valid.
if (Tok.is(tok::r_brace)) {
Diag(Tok, diag::err_label_end_of_compound_statement);
- return true;
+ return StmtError();
}
-
- OwningStmtResult SubStmt(Actions, ParseStatement());
+
+ OwningStmtResult SubStmt(ParseStatement());
// Broken substmt shouldn't prevent the case from being added to the AST.
if (SubStmt.isInvalid())
SubStmt = Actions.ActOnNullStmt(ColonLoc);
-
- return Actions.ActOnCaseStmt(CaseLoc, LHS.release(), DotDotDotLoc,
- RHS.release(), ColonLoc, SubStmt.release());
+
+ return Owned(Actions.ActOnCaseStmt(CaseLoc, LHS.release(), DotDotDotLoc,
+ RHS.release(), ColonLoc,
+ SubStmt.release()));
}
/// ParseDefaultStatement
@@ -274,30 +276,30 @@
/// 'default' ':' statement
/// Note that this does not parse the 'statement' at the end.
///
-Parser::StmtResult Parser::ParseDefaultStatement() {
+Parser::OwningStmtResult Parser::ParseDefaultStatement() {
assert(Tok.is(tok::kw_default) && "Not a default stmt!");
SourceLocation DefaultLoc = ConsumeToken(); // eat the 'default'.
if (Tok.isNot(tok::colon)) {
Diag(Tok, diag::err_expected_colon_after) << "'default'";
SkipUntil(tok::colon);
- return true;
+ return StmtError();
}
-
+
SourceLocation ColonLoc = ConsumeToken();
-
+
// Diagnose the common error "switch (X) {... default: }", which is not valid.
if (Tok.is(tok::r_brace)) {
Diag(Tok, diag::err_label_end_of_compound_statement);
- return true;
+ return StmtError();
}
- OwningStmtResult SubStmt(Actions, ParseStatement());
+ OwningStmtResult SubStmt(ParseStatement());
if (SubStmt.isInvalid())
- return true;
-
- return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
- SubStmt.release(), CurScope);
+ return StmtError();
+
+ return Owned(Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
+ SubStmt.release(), CurScope));
}
@@ -328,16 +330,15 @@
/// [OMP] barrier-directive
/// [OMP] flush-directive
///
-Parser::StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
+Parser::OwningStmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
-
+
// Enter a scope to hold everything within the compound stmt. Compound
// statements can always hold declarations.
ParseScope CompoundScope(this, Scope::DeclScope);
// Parse the statements in the body.
- OwningStmtResult Body(Actions, ParseCompoundStatementBody(isStmtExpr));
- return Body.result();
+ return ParseCompoundStatementBody(isStmtExpr);
}
@@ -345,7 +346,7 @@
/// ActOnCompoundStmt action. This expects the '{' to be the current token, and
/// consume the '}' at the end of the block. It does not manipulate the scope
/// stack.
-Parser::StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
+Parser::OwningStmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
SourceLocation LBraceLoc = ConsumeBrace(); // eat the '{'.
// TODO: "__label__ X, Y, Z;" is the GNU "Local Label" extension. These are
@@ -397,20 +398,20 @@
R = Actions.ActOnExprStmt(Res.release());
}
}
-
+
if (R.isUsable())
Stmts.push_back(R.release());
}
-
+
// We broke out of the while loop because we found a '}' or EOF.
if (Tok.isNot(tok::r_brace)) {
Diag(Tok, diag::err_expected_rbrace);
- return true;
+ return StmtError();
}
-
+
SourceLocation RBraceLoc = ConsumeBrace();
- return Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc,
- Stmts.take(), Stmts.size(), isStmtExpr);
+ return Owned(Actions.ActOnCompoundStmt(LBraceLoc, RBraceLoc, Stmts.take(),
+ Stmts.size(), isStmtExpr));
}
/// ParseIfStatement
@@ -420,14 +421,14 @@
/// [C++] 'if' '(' condition ')' statement
/// [C++] 'if' '(' condition ')' statement 'else' statement
///
-Parser::StmtResult Parser::ParseIfStatement() {
+Parser::OwningStmtResult Parser::ParseIfStatement() {
assert(Tok.is(tok::kw_if) && "Not an if stmt!");
SourceLocation IfLoc = ConsumeToken(); // eat the 'if'.
if (Tok.isNot(tok::l_paren)) {
Diag(Tok, diag::err_expected_lparen_after) << "if";
SkipUntil(tok::semi);
- return true;
+ return StmtError();
}
bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
@@ -458,7 +459,7 @@
if (CondExp.isInvalid()) {
SkipUntil(tok::semi);
- return true;
+ return StmtError();
}
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
@@ -484,11 +485,11 @@
// Read the 'then' stmt.
SourceLocation ThenStmtLoc = Tok.getLocation();
- OwningStmtResult ThenStmt(Actions, ParseStatement());
+ OwningStmtResult ThenStmt(ParseStatement());
// Pop the 'if' scope if needed.
InnerScope.Exit();
-
+
// If it has an else, parse it.
SourceLocation ElseLoc;
SourceLocation ElseStmtLoc;
@@ -496,7 +497,7 @@
if (Tok.is(tok::kw_else)) {
ElseLoc = ConsumeToken();
-
+
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do
// this if the body isn't a compound statement to avoid push/pop in common
@@ -506,7 +507,7 @@
// The substatement in a selection-statement (each substatement, in the else
// form of the if statement) implicitly defines a local scope.
//
- ParseScope InnerScope(this, Scope::DeclScope,
+ ParseScope InnerScope(this, Scope::DeclScope,
C99orCXX && Tok.isNot(tok::l_brace));
bool WithinElse = CurScope->isWithinElse();
@@ -518,7 +519,7 @@
// Pop the 'else' scope if needed.
InnerScope.Exit();
}
-
+
IfScope.Exit();
// If the then or else stmt is invalid and the other is valid (and present),
@@ -528,7 +529,7 @@
(ThenStmt.isInvalid() && ElseStmt.get() == 0) ||
(ThenStmt.get() == 0 && ElseStmt.isInvalid())) {
// Both invalid, or one is invalid and other is non-present: return error.
- return true;
+ return StmtError();
}
// Now if either are invalid, replace with a ';'.
@@ -537,8 +538,8 @@
if (ElseStmt.isInvalid())
ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
- return Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
- ElseLoc, ElseStmt.release());
+ return Owned(Actions.ActOnIfStmt(IfLoc, CondExp.release(), ThenStmt.release(),
+ ElseLoc, ElseStmt.release()));
}
/// ParseSwitchStatement
@@ -603,9 +604,9 @@
//
ParseScope InnerScope(this, Scope::DeclScope,
C99orCXX && Tok.isNot(tok::l_brace));
-
+
// Read the body statement.
- OwningStmtResult Body(Actions, ParseStatement());
+ OwningStmtResult Body(ParseStatement());
// Pop the body scope if needed.
InnerScope.Exit();
@@ -616,7 +617,7 @@
}
SwitchScope.Exit();
-
+
return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.release(),
Body.release());
}
@@ -683,7 +684,7 @@
C99orCXX && Tok.isNot(tok::l_brace));
// Read the body statement.
- OwningStmtResult Body(Actions, ParseStatement());
+ OwningStmtResult Body(ParseStatement());
// Pop the body scope if needed.
InnerScope.Exit();
@@ -725,7 +726,7 @@
Tok.isNot(tok::l_brace));
// Read the body statement.
- OwningStmtResult Body(Actions, ParseStatement());
+ OwningStmtResult Body(ParseStatement());
// Pop the body scope if needed.
InnerScope.Exit();
@@ -894,7 +895,7 @@
C99orCXX && Tok.isNot(tok::l_brace));
// Read the body statement.
- OwningStmtResult Body(Actions, ParseStatement());
+ OwningStmtResult Body(ParseStatement());
// Pop the body scope if needed.
InnerScope.Exit();
@@ -1207,11 +1208,11 @@
// Do not enter a scope for the brace, as the arguments are in the same scope
// (the function body) as the body itself. Instead, just read the statement
// list and put it into a CompoundStmt for safe keeping.
- OwningStmtResult FnBody(Actions, ParseCompoundStatementBody());
-
+ OwningStmtResult FnBody(ParseCompoundStatementBody());
+
// If the function body could not be parsed, make a bogus compoundstmt.
if (FnBody.isInvalid())
- FnBody = Actions.ActOnCompoundStmt(L, R, 0, 0, false);
-
+ FnBody = Owned(Actions.ActOnCompoundStmt(L, R, 0, 0, false));
+
return Actions.ActOnFinishFunctionBody(Decl, FnBody.release());
}
Modified: cfe/trunk/lib/Parse/Parser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/Parser.cpp?rev=60888&r1=60887&r2=60888&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/Parser.cpp (original)
+++ cfe/trunk/lib/Parse/Parser.cpp Thu Dec 11 13:30:53 2008
@@ -666,7 +666,7 @@
Parser::OwningExprResult Parser::ParseAsmStringLiteral() {
if (!isTokenStringLiteral()) {
Diag(Tok, diag::err_expected_string_literal);
- return OwningExprResult(true);
+ return ExprError();
}
OwningExprResult Res(Actions, ParseStringLiteralExpression());
@@ -688,7 +688,7 @@
if (Tok.isNot(tok::l_paren)) {
Diag(Tok, diag::err_expected_lparen_after) << "asm";
- return OwningExprResult(true);
+ return ExprError();
}
ConsumeParen();
More information about the cfe-commits
mailing list