-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang-repl] VarDecl emitted outside TopLevelStmtDecl #83028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@vgvassilev Is this a known issue? Any idea how to fix it? |
@weliveindetail, without looking into the details I suspect we need to push a function scope when parsing the top-level statements. We already do that in llvm-project/clang/lib/Parse/ParseDecl.cpp Line 5679 in d7c80bb
I'd wrap the broken code into a function and see what else clang pushes to parse |
Thanks for the quick reply! Some observations follow. Clang always wraps function bodies into
Pushing a
I guess this is important for cases, where we have only a declaration, but no actual statement to be executed, e.g.:
Is this something we do explicitly in incremental mode? If so, we should probably make it conditional. Apart from that. What is the reason for |
I am aware of: void f()
try {}
catch(...) {} that's modeled with:
However, that does not invalidate your point.
We have not considered this case where the control statements define variables which need to have a declaration context where they live. I am wondering why when parsing a for loop we put the VarDecl on file scope. Regular clang does not seem to put the the VarDecl on function scope. We must be doing something weird...
As part of the design work we decided to have a distinguished top-level entity which in clang is a Decl. Many interfaces, including CodeGen, work with declarations and that was the minimal, non-intrusive approach to enable what we needed. |
On current mainline I am getting
Thanks, that's reasonable. So, this is the REPL's equivalent for clang's
Yes exactly. No worries, I will figure it out. Just wanted to check, if we know anything about it already. |
I meant
Yes, but this "FunctionDecl" is part of the static initialization process, eg. it is run as if it was called on file scope. |
I have a first patch here which mimics Clang's handling in terms of redefinition errors: weliveindetail@f86a8a6 It's not trivial to fix the asserts, because we have no notion of incremental mode in CG right? With this the frontend is happy, but it still generates the extra top-level |
We do: it should be available via |
This is my take on fixing this issue: vgvassilev@96b93df It still has 3 failing tests to be debugged: Clang :: Interpreter/error-recovery-pragmas.cpp I can probably spend more time on this in a week but I thought that might get you going to fix the issue on your own. |
Thanks! In https://github.com/weliveindetail/llvm-project/commits/clang-repl-fix-var-def-in-control-stmts I just added the CompoundScope again and all tests pass for me on Ubuntu 22.04:
Execution seems to be exactly what we need. What is confusing is that this affects the AST dump, for example:
When I apply your patch, the
I will look into it a little more and see if I can figure it out. If you have any ideas, let me know. Thx |
In my patch we made the |
Drop duplicate test-case
clang-format
ActOnForStmt() requires a CompoundScope when processing a NullStmt body
Add TopLevelStmtDecl to outer DeclContext (and push new scopes afterwards)
I was debugging through |
Drop duplicate test-case
clang-format
ActOnForStmt() requires a CompoundScope when processing a NullStmt body
Add TopLevelStmtDecl to outer DeclContext (and push new scopes afterwards)
Fixed with 4b70d17 |
@llvm/issue-subscribers-clang-frontend Author: Stefan Gränitz (weliveindetail)
Works:
```
$ clang-repl
clang-repl> int a = 42;
clang-repl> int i = 0;
clang-repl> for (; i<42; i+=1) a-=1;
clang-repl> %quit
```
Fails:
Other side-effects include incorrect behavior for definitions inside
I guess the |
Works:
Fails:
Other side-effects include incorrect behavior for definitions inside
if
conditions:I guess the
VarDecl 0x5555d89626c0
forb
should be emitted inside theTopLevelStmtDecl 0x5555d8986cb0
or even insideIfStmt 0x5555d8986c70
.The text was updated successfully, but these errors were encountered: