]> granicus.if.org Git - clang/commitdiff
PCH support for do-while and for loops
authorDouglas Gregor <dgregor@apple.com>
Fri, 17 Apr 2009 00:29:51 +0000 (00:29 +0000)
committerDouglas Gregor <dgregor@apple.com>
Fri, 17 Apr 2009 00:29:51 +0000 (00:29 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@69334 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Stmt.h
include/clang/Frontend/PCHBitCodes.h
lib/Frontend/PCHReader.cpp
lib/Frontend/PCHWriter.cpp
test/PCH/stmts.h

index 23027076de31a775256eb80974efbc6610fa2754..0f489d2291721d1cb3aa82df0069628dad8fd288 100644 (file)
@@ -715,11 +715,19 @@ public:
     SubExprs[BODY] = body;
     DoLoc = DL;
   }  
+
+  /// \brief Build an empty do-while statement.
+  explicit DoStmt(EmptyShell Empty) : Stmt(DoStmtClass, Empty) { }
   
   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
   const Expr *getCond() const { return reinterpret_cast<Expr*>(SubExprs[COND]);}
+  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
   Stmt *getBody() { return SubExprs[BODY]; }
   const Stmt *getBody() const { return SubExprs[BODY]; }  
+  void setBody(Stmt *S) { SubExprs[BODY] = S; }
+
+  SourceLocation getDoLoc() const { return DoLoc; }
+  void setDoLoc(SourceLocation L) { DoLoc = L; }
 
   virtual SourceRange getSourceRange() const { 
     return SourceRange(DoLoc, SubExprs[BODY]->getLocEnd()); 
@@ -756,6 +764,9 @@ public:
     ForLoc = FL;
   }
   
+  /// \brief Build an empty for statement.
+  explicit ForStmt(EmptyShell Empty) : Stmt(ForStmtClass, Empty) { }
+
   Stmt *getInit() { return SubExprs[INIT]; }
   Expr *getCond() { return reinterpret_cast<Expr*>(SubExprs[COND]); }
   Expr *getInc()  { return reinterpret_cast<Expr*>(SubExprs[INC]); }
@@ -766,6 +777,14 @@ public:
   const Expr *getInc()  const { return reinterpret_cast<Expr*>(SubExprs[INC]); }
   const Stmt *getBody() const { return SubExprs[BODY]; }
 
+  void setInit(Stmt *S) { SubExprs[INIT] = S; }
+  void setCond(Expr *E) { SubExprs[COND] = reinterpret_cast<Stmt*>(E); }
+  void setInc(Expr *E) { SubExprs[INC] = reinterpret_cast<Stmt*>(E); }
+  void setBody(Stmt *S) { SubExprs[BODY] = S; }
+
+  SourceLocation getForLoc() const { return ForLoc; }
+  void setForLoc(SourceLocation L) { ForLoc = L; }
+
   virtual SourceRange getSourceRange() const { 
     return SourceRange(ForLoc, SubExprs[BODY]->getLocEnd()); 
   }
index d95ba85b82edac647c7fdbcb6986e6e5b9ce4395..06c120e4653c94994343205fd0876e5ad028ab48 100644 (file)
@@ -389,6 +389,10 @@ namespace clang {
       STMT_SWITCH,
       /// \brief A WhileStmt record.
       STMT_WHILE,
+      /// \brief A DoStmt record.
+      STMT_DO,
+      /// \brief A ForStmt record.
+      STMT_FOR,
       /// \brief A ContinueStmt record.
       STMT_CONTINUE,
       /// \brief A BreakStmt record.
index bdadd35e3f22f98377316e937040320c30f37821..d2fb8ab3cbd89479b0daf495645364165c22261b 100644 (file)
@@ -252,6 +252,8 @@ namespace {
     unsigned VisitIfStmt(IfStmt *S);
     unsigned VisitSwitchStmt(SwitchStmt *S);
     unsigned VisitWhileStmt(WhileStmt *S);
+    unsigned VisitDoStmt(DoStmt *S);
+    unsigned VisitForStmt(ForStmt *S);
     unsigned VisitContinueStmt(ContinueStmt *S);
     unsigned VisitBreakStmt(BreakStmt *S);
     unsigned VisitExpr(Expr *E);
@@ -366,6 +368,24 @@ unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) {
   return 2;
 }
 
+unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) {
+  VisitStmt(S);
+  S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
+  S->setBody(StmtStack.back());
+  S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  return 2;
+}
+
+unsigned PCHStmtReader::VisitForStmt(ForStmt *S) {
+  VisitStmt(S);
+  S->setInit(StmtStack[StmtStack.size() - 4]);
+  S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3]));
+  S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2]));
+  S->setBody(StmtStack.back());
+  S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
+  return 4;
+}
+
 unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) {
   VisitStmt(S);
   S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
@@ -2136,6 +2156,14 @@ Stmt *PCHReader::ReadStmt() {
       S = new (Context) WhileStmt(Empty);
       break;
 
+    case pch::STMT_DO:
+      S = new (Context) DoStmt(Empty);
+      break;
+      
+    case pch::STMT_FOR:
+      S = new (Context) ForStmt(Empty);
+      break;
+
     case pch::STMT_CONTINUE:
       S = new (Context) ContinueStmt(Empty);
       break;
index 0d514016692c52699c322d8c758b34c179a7796a..29c9eb81f444e16db757232cc1735632a63fb4c1 100644 (file)
@@ -454,6 +454,8 @@ namespace {
     void VisitIfStmt(IfStmt *S);
     void VisitSwitchStmt(SwitchStmt *S);
     void VisitWhileStmt(WhileStmt *S);
+    void VisitDoStmt(DoStmt *S);
+    void VisitForStmt(ForStmt *S);
     void VisitContinueStmt(ContinueStmt *S);
     void VisitBreakStmt(BreakStmt *S);
     void VisitExpr(Expr *E);
@@ -560,6 +562,24 @@ void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) {
   Code = pch::STMT_WHILE;
 }
 
+void PCHStmtWriter::VisitDoStmt(DoStmt *S) {
+  VisitStmt(S);
+  Writer.WriteSubStmt(S->getCond());
+  Writer.WriteSubStmt(S->getBody());
+  Writer.AddSourceLocation(S->getDoLoc(), Record);
+  Code = pch::STMT_DO;
+}
+
+void PCHStmtWriter::VisitForStmt(ForStmt *S) {
+  VisitStmt(S);
+  Writer.WriteSubStmt(S->getInit());
+  Writer.WriteSubStmt(S->getCond());
+  Writer.WriteSubStmt(S->getInc());
+  Writer.WriteSubStmt(S->getBody());
+  Writer.AddSourceLocation(S->getForLoc(), Record);
+  Code = pch::STMT_FOR;
+}
+
 void PCHStmtWriter::VisitContinueStmt(ContinueStmt *S) {
   VisitStmt(S);
   Writer.AddSourceLocation(S->getContinueLoc(), Record);
index 798058a176e376a9f62e57e87eaf49fb973cbc75..d67ae187bb695c4c595c775991d33fe3d6322ec3 100644 (file)
@@ -24,6 +24,13 @@ void f0(int x) {
     if (x > 30) {
       --x;
       continue;
-    }
+    } else if (x < 5)
+      break;
   }
+
+  do {
+    x++;
+  } while (x < 10);
+
+  for (; x < 20; ++x) ;
 }