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
diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h
index 0f489d2..fbb5552 100644
--- a/include/clang/AST/Stmt.h
+++ b/include/clang/AST/Stmt.h
@@ -931,9 +931,16 @@
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 06c120e..7c316c6 100644
--- a/include/clang/Frontend/PCHBitCodes.h
+++ b/include/clang/Frontend/PCHBitCodes.h
@@ -397,6 +397,8 @@
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 d2fb8ab..0dcdf8d 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -256,6 +256,7 @@
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 @@
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 @@
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 @@
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 29c9eb8..9149054 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -458,6 +458,7 @@
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 @@
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 @@
}
StmtsToEmit.clear();
+ SwitchCaseIDs.clear();
}
unsigned PCHWriter::RecordSwitchCaseID(SwitchCase *S) {
diff --git a/test/PCH/stmts.c b/test/PCH/stmts.c
index b71db63..e9d03e8 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 d67ae18..7290d2e 100644
--- a/test/PCH/stmts.h
+++ b/test/PCH/stmts.h
@@ -17,6 +17,13 @@
break;
default:
+ switch (x >> 1) {
+ case 7:
+ // fall through
+ case 9:
+ break;
+ }
+ x += 2;
break;
}
@@ -32,5 +39,20 @@
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;
}