llvm / llvm/llvm-project

[clang-repl] Assertion on invalid declaration inside namespace

Open
#206,670 1 comment 0 reactions 0 assignees View on GitHub
clang-repl
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

clang-repl crashes with an assertion failure when parsing an invalid declaration inside a namespace.

```
(xeus-python) anutosh491@Anutoshs-MacBook-Air bin % ./clang-repl
clang-repl> namespace v1 { int x = }
In file included from <<< inputs >>>:1:
input_line_1:1:24: error: expected expression
1 | namespace v1 { int x = }
| ^
input_line_1:1:25: error: expected ';' after top level declarator
1 | namespace v1 { int x = }
| ^
| ;
Assertion failed: (D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context"), function addHiddenDecl, file DeclBase.cpp, line 1797.
#0 0x0000000105f71190 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x101e31190)
#1 0x0000000105f6ed78 llvm::sys::RunSignalHandlers() (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x101e2ed78)
#2 0x0000000105f71dd4 SignalHandler(int, __siginfo*, void*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x101e31dd4)
#3 0x000000018b5f7744 (/usr/lib/system/libsystem_platform.dylib+0x1804fb744)
#4 0x000000018b5ed8d8 (/usr/lib/system/libsystem_pthread.dylib+0x1804f18d8)
#5 0x000000018b4f4644 (/usr/lib/system/libsystem_c.dylib+0x1803f8644)
#6 0x000000018b4f38a0 (/usr/lib/system/libsystem_c.dylib+0x1803f78a0)
#7 0x0000000106255b08 clang::DeclContext::addHiddenDecl(clang::Decl*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x102115b08)
#8 0x0000000106255b5c clang::DeclContext::addDecl(clang::Decl*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x102115b5c)
#9 0x0000000107bc0be0 clang::Sema::ActOnStartTopLevelStmtDecl(clang::Scope*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x103a80be0)
#10 0x000000010774c014 clang::Parser::ParseTopLevelStmtDecl() (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x10360c014)
#11 0x00000001077f4868 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1036b4868)
#12 0x00000001077583e8 clang::Parser::ParseInnerNamespace(llvm::SmallVector const&, unsigned int, clang::SourceLocation&, clang::ParsedAttributes&, clang::BalancedDelimiterTracker&) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1036183e8)
#13 0x0000000107757b74 clang::Parser::ParseNamespace(clang::DeclaratorContext, clang::SourceLocation&, clang::SourceLocation) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x103617b74)
#14 0x000000010773db14 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1035fdb14)
#15 0x00000001077f4158 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1036b4158)
#16 0x00000001077f2d80 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1036b2d80)
#17 0x00000001077f28ec clang::Parser::ParseFirstTopLevelDecl(clang::OpaquePtr&, clang::Sema::ModuleImportState&) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x1036b28ec)
#18 0x0000000106ab4498 clang::IncrementalParser::ParseOrWrapTopLevelDecl() (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x102974498)
#19 0x0000000106ab558c clang::IncrementalParser::Parse(llvm::StringRef) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x10297558c)
#20 0x0000000106abc464 clang::Interpreter::Parse(llvm::StringRef) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x10297c464)
#21 0x0000000106abb9e0 clang::Interpreter::ParseAndExecute(llvm::StringRef, clang::Value*) (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x10297b9e0)
#22 0x000000010414567c main (/Users/anutosh491/work/llvm-project/build-assert/bin/clang-repl+0x10000567c)
#23 0x000000018b22fe00
zsh: abort ./clang-repl
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the crash with clang-repl using `namespace v1 { int x = }`, then trace the reported path through clang::Parser::ParseInnerNamespace, clang::Sema::ActOnStartTopLevelStmtDecl, and DeclContext::addHiddenDecl. Add a regression test for the invalid declaration and verify that clang-repl reports diagnostics without asserting or aborting.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
cli, compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.