diff options
author | Douglas Gregor <dgregor@apple.com> | 2009-04-17 16:34:57 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2009-04-17 16:34:57 +0000 |
commit | 0de9d8857b715c2f45c987651f4ce06d73330d93 (patch) | |
tree | 0a392290e4ca6fd486ff567d0813ec1bef93c82f | |
parent | 76458501a8963fa11b91c9337a487de6871169b4 (diff) |
PCH support for return statements.
Optimize PCH encoding for switch-case statements slightly, by making
the switch-case numbering local to a particular statement.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69355 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/AST/Stmt.h | 9 | ||||
-rw-r--r-- | include/clang/Frontend/PCHBitCodes.h | 2 | ||||
-rw-r--r-- | lib/Frontend/PCHReader.cpp | 13 | ||||
-rw-r--r-- | lib/Frontend/PCHWriter.cpp | 9 | ||||
-rw-r--r-- | test/PCH/stmts.c | 5 | ||||
-rw-r--r-- | test/PCH/stmts.h | 24 |
6 files changed, 58 insertions, 4 deletions
diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index 0f489d2291..fbb5552ca7 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -931,9 +931,16 @@ class ReturnStmt : public Stmt { public: ReturnStmt(SourceLocation RL, Expr *E = 0) : Stmt(ReturnStmtClass), RetExpr((Stmt*) E), RetLoc(RL) {} - + + /// \brief Build an empty return expression. + explicit ReturnStmt(EmptyShell Empty) : Stmt(ReturnStmtClass, Empty) { } + const Expr *getRetValue() const; Expr *getRetValue(); + void setRetValue(Expr *E) { RetExpr = reinterpret_cast<Stmt*>(E); } + + SourceLocation getReturnLoc() const { return RetLoc; } + void setReturnLoc(SourceLocation L) { RetLoc = L; } virtual SourceRange getSourceRange() const; diff --git a/include/clang/Frontend/PCHBitCodes.h b/include/clang/Frontend/PCHBitCodes.h index 06c120e465..7c316c691c 100644 --- a/include/clang/Frontend/PCHBitCodes.h +++ b/include/clang/Frontend/PCHBitCodes.h @@ -397,6 +397,8 @@ namespace clang { STMT_CONTINUE, /// \brief A BreakStmt record. STMT_BREAK, + /// \brief A ReturnStmt record. + STMT_RETURN, /// \brief A PredefinedExpr record. EXPR_PREDEFINED, /// \brief A DeclRefExpr record. diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index d2fb8ab3cb..0dcdf8d3fd 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -256,6 +256,7 @@ namespace { unsigned VisitForStmt(ForStmt *S); unsigned VisitContinueStmt(ContinueStmt *S); unsigned VisitBreakStmt(BreakStmt *S); + unsigned VisitReturnStmt(ReturnStmt *S); unsigned VisitExpr(Expr *E); unsigned VisitPredefinedExpr(PredefinedExpr *E); unsigned VisitDeclRefExpr(DeclRefExpr *E); @@ -398,6 +399,13 @@ unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) { return 0; } +unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) { + VisitStmt(S); + S->setRetValue(cast_or_null<Expr>(StmtStack.back())); + S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + return 1; +} + unsigned PCHStmtReader::VisitExpr(Expr *E) { VisitStmt(E); E->setType(Reader.GetType(Record[Idx++])); @@ -2172,6 +2180,10 @@ Stmt *PCHReader::ReadStmt() { S = new (Context) BreakStmt(Empty); break; + case pch::STMT_RETURN: + S = new (Context) ReturnStmt(Empty); + break; + case pch::EXPR_PREDEFINED: // FIXME: untested (until we can serialize function bodies). S = new (Context) PredefinedExpr(Empty); @@ -2311,6 +2323,7 @@ Stmt *PCHReader::ReadStmt() { StmtStack.push_back(S); } assert(StmtStack.size() == 1 && "Extra expressions on stack!"); + SwitchCaseStmts.clear(); return StmtStack.back(); } diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp index 29c9eb81f4..91490549ea 100644 --- a/lib/Frontend/PCHWriter.cpp +++ b/lib/Frontend/PCHWriter.cpp @@ -458,6 +458,7 @@ namespace { void VisitForStmt(ForStmt *S); void VisitContinueStmt(ContinueStmt *S); void VisitBreakStmt(BreakStmt *S); + void VisitReturnStmt(ReturnStmt *S); void VisitExpr(Expr *E); void VisitPredefinedExpr(PredefinedExpr *E); void VisitDeclRefExpr(DeclRefExpr *E); @@ -592,6 +593,13 @@ void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) { Code = pch::STMT_BREAK; } +void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) { + VisitStmt(S); + Writer.WriteSubStmt(S->getRetValue()); + Writer.AddSourceLocation(S->getReturnLoc(), Record); + Code = pch::STMT_RETURN; +} + void PCHStmtWriter::VisitExpr(Expr *E) { VisitStmt(E); Writer.AddTypeRef(E->getType(), Record); @@ -1822,6 +1830,7 @@ void PCHWriter::FlushStmts() { } StmtsToEmit.clear(); + SwitchCaseIDs.clear(); } unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) { diff --git a/test/PCH/stmts.c b/test/PCH/stmts.c index b71db63e67..e9d03e8774 100644 --- a/test/PCH/stmts.c +++ b/test/PCH/stmts.c @@ -1,8 +1,9 @@ // Test this without pch. -// RUN: clang-cc -fblocks -include %S/stmts.h -fsyntax-only -emit-llvm -o - %s +// RUN: clang-cc -fblocks -include %S/stmts.h -fsyntax-only -ast-print -o - %s // Test with pch. // RUN: clang-cc -emit-pch -fblocks -o %t %S/stmts.h && -// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -emit-llvm -o - %s +// RUN: clang-cc -fblocks -include-pch %t -fsyntax-only -ast-print -o - %s void g0(void) { f0(5); } +int g1(int x) { return f1(x); } diff --git a/test/PCH/stmts.h b/test/PCH/stmts.h index d67ae187bb..7290d2efcb 100644 --- a/test/PCH/stmts.h +++ b/test/PCH/stmts.h @@ -17,6 +17,13 @@ void f0(int x) { break; default: + switch (x >> 1) { + case 7: + // fall through + case 9: + break; + } + x += 2; break; } @@ -32,5 +39,20 @@ void f0(int x) { x++; } while (x < 10); - for (; x < 20; ++x) ; + for (; x < 20; ++x) { + if (x == 12) + return; + } +} + +int f1(int x) { + switch (x) { + case 17: + return 12; + + default: + break; + } + + return x*2; } |