[clang] 159073b - [clang-repl] Support compound statement as a top-level statement.

Vassil Vassilev via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 17 01:09:40 PST 2023


Author: Vassil Vassilev
Date: 2023-02-17T09:09:04Z
New Revision: 159073bc0a873b9aa2bc559a8ceba970b049cda2

URL: https://github.com/llvm/llvm-project/commit/159073bc0a873b9aa2bc559a8ceba970b049cda2
DIFF: https://github.com/llvm/llvm-project/commit/159073bc0a873b9aa2bc559a8ceba970b049cda2.diff

LOG: [clang-repl] Support compound statement as a top-level statement.

This patch teaches our incremental compilation infrastructure to push and pop a
fake function scope making the Parser happy when parsing compound statements as
part of a top-leve statement declaration.

Differential revision: https://reviews.llvm.org/D139798

Added: 
    

Modified: 
    clang/lib/Parse/ParseDecl.cpp
    clang/test/Interpreter/execute-stmts.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index d32f26b9c32e7..6465d859b1dab 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -5400,7 +5400,9 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
   // Parse a top-level-stmt.
   Parser::StmtVector Stmts;
   ParsedStmtContext SubStmtCtx = ParsedStmtContext();
+  Actions.PushFunctionScope();
   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
+  Actions.PopFunctionScopeInfo();
   if (!R.isUsable())
     return nullptr;
 

diff  --git a/clang/test/Interpreter/execute-stmts.cpp b/clang/test/Interpreter/execute-stmts.cpp
index 567b783ad8cb3..2d4c17e0c91e6 100644
--- a/clang/test/Interpreter/execute-stmts.cpp
+++ b/clang/test/Interpreter/execute-stmts.cpp
@@ -34,5 +34,10 @@ printf("i = %d\n", i);
 for (; i > 4; --i) printf("i = %d\n", i);
 // CHECK-NEXT: i = 5
 
+{++i;}
+
+for (; i > 4; --i) { printf("i = %d\n", i); };
+// CHECK-NEXT: i = 5
+
 int j = i; printf("j = %d\n", j);
 // CHECK-NEXT: j = 4


        


More information about the cfe-commits mailing list