From: Douglas Gregor Date: Fri, 17 Apr 2009 16:55:36 +0000 (+0000) Subject: PCH support for declaration statements, and a test for PredefinedExpr X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84f2170062014d268951902164bed0d8bdea0e82;p=clang PCH support for declaration statements, and a test for PredefinedExpr git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69356 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/Stmt.h b/include/clang/AST/Stmt.h index fbb5552ca7..d0531f95a5 100644 --- a/include/clang/AST/Stmt.h +++ b/include/clang/AST/Stmt.h @@ -244,6 +244,9 @@ public: SourceLocation endLoc) : Stmt(DeclStmtClass), DG(dg), StartLoc(startLoc), EndLoc(endLoc) {} + /// \brief Build an empty declaration statement. + explicit DeclStmt(EmptyShell Empty) : Stmt(DeclStmtClass, Empty) { } + virtual void Destroy(ASTContext& Ctx); /// isSingleDecl - This method returns true if this DeclStmt refers @@ -257,10 +260,13 @@ public: const DeclGroupRef getDeclGroup() const { return DG; } DeclGroupRef getDeclGroup() { return DG; } + void setDeclGroup(DeclGroupRef DGR) { DG = DGR; } SourceLocation getStartLoc() const { return StartLoc; } + void setStartLoc(SourceLocation L) { StartLoc = L; } SourceLocation getEndLoc() const { return EndLoc; } - + void setEndLoc(SourceLocation L) { EndLoc = L; } + SourceRange getSourceRange() const { return SourceRange(StartLoc, EndLoc); } diff --git a/include/clang/Frontend/PCHBitCodes.h b/include/clang/Frontend/PCHBitCodes.h index 7c316c691c..f5b023430a 100644 --- a/include/clang/Frontend/PCHBitCodes.h +++ b/include/clang/Frontend/PCHBitCodes.h @@ -399,6 +399,8 @@ namespace clang { STMT_BREAK, /// \brief A ReturnStmt record. STMT_RETURN, + /// \brief A DeclStmt record. + STMT_DECL, /// \brief A PredefinedExpr record. EXPR_PREDEFINED, /// \brief A DeclRefExpr record. diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp index 0dcdf8d3fd..50378966e1 100644 --- a/lib/Frontend/PCHReader.cpp +++ b/lib/Frontend/PCHReader.cpp @@ -257,6 +257,7 @@ namespace { unsigned VisitContinueStmt(ContinueStmt *S); unsigned VisitBreakStmt(BreakStmt *S); unsigned VisitReturnStmt(ReturnStmt *S); + unsigned VisitDeclStmt(DeclStmt *S); unsigned VisitExpr(Expr *E); unsigned VisitPredefinedExpr(PredefinedExpr *E); unsigned VisitDeclRefExpr(DeclRefExpr *E); @@ -406,6 +407,25 @@ unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) { return 1; } +unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) { + VisitStmt(S); + S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + + if (Idx + 1 == Record.size()) { + // Single declaration + S->setDeclGroup(DeclGroupRef(Reader.GetDecl(Record[Idx++]))); + } else { + llvm::SmallVector Decls; + Decls.reserve(Record.size() - Idx); + for (unsigned N = Record.size(); Idx != N; ++Idx) + Decls.push_back(Reader.GetDecl(Record[Idx])); + S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Reader.getContext(), + &Decls[0], Decls.size()))); + } + return 0; +} + unsigned PCHStmtReader::VisitExpr(Expr *E) { VisitStmt(E); E->setType(Reader.GetType(Record[Idx++])); @@ -2184,8 +2204,11 @@ Stmt *PCHReader::ReadStmt() { S = new (Context) ReturnStmt(Empty); break; + case pch::STMT_DECL: + S = new (Context) DeclStmt(Empty); + break; + case pch::EXPR_PREDEFINED: - // FIXME: untested (until we can serialize function bodies). S = new (Context) PredefinedExpr(Empty); break; diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp index 91490549ea..4b4e897166 100644 --- a/lib/Frontend/PCHWriter.cpp +++ b/lib/Frontend/PCHWriter.cpp @@ -459,6 +459,7 @@ namespace { void VisitContinueStmt(ContinueStmt *S); void VisitBreakStmt(BreakStmt *S); void VisitReturnStmt(ReturnStmt *S); + void VisitDeclStmt(DeclStmt *S); void VisitExpr(Expr *E); void VisitPredefinedExpr(PredefinedExpr *E); void VisitDeclRefExpr(DeclRefExpr *E); @@ -600,6 +601,16 @@ void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) { Code = pch::STMT_RETURN; } +void PCHStmtWriter::VisitDeclStmt(DeclStmt *S) { + VisitStmt(S); + Writer.AddSourceLocation(S->getStartLoc(), Record); + Writer.AddSourceLocation(S->getEndLoc(), Record); + DeclGroupRef DG = S->getDeclGroup(); + for (DeclGroupRef::iterator D = DG.begin(), DEnd = DG.end(); D != DEnd; ++D) + Writer.AddDeclRef(*D, Record); + Code = pch::STMT_DECL; +} + void PCHStmtWriter::VisitExpr(Expr *E) { VisitStmt(E); Writer.AddTypeRef(E->getType(), Record); diff --git a/test/PCH/stmts.c b/test/PCH/stmts.c index e9d03e8774..a2278bf16c 100644 --- a/test/PCH/stmts.c +++ b/test/PCH/stmts.c @@ -7,3 +7,4 @@ void g0(void) { f0(5); } int g1(int x) { return f1(x); } +const char* query_name(void) { return what_is_my_name(); } diff --git a/test/PCH/stmts.h b/test/PCH/stmts.h index 7290d2efcb..685811ad08 100644 --- a/test/PCH/stmts.h +++ b/test/PCH/stmts.h @@ -39,10 +39,12 @@ void f0(int x) { x++; } while (x < 10); - for (; x < 20; ++x) { - if (x == 12) + for (int y = x; y < 20; ++y) { + if (x + y == 12) return; } + + int z = x, *y, j = 5; } int f1(int x) { @@ -56,3 +58,5 @@ int f1(int x) { return x*2; } + +const char* what_is_my_name(void) { return __func__; }