[clang] [clang] return first Decl for CanonicalDecl in TranslationUnitDecl (PR #110101)

2024-09-26 Thread Vipul Cariappa via cfe-commits

https://github.com/Vipul-Cariappa created 
https://github.com/llvm/llvm-project/pull/110101

Return the first `Decl` when using `TranslationUnitDecl::getCanonicalDecl`

>From 43d0e353e6e5870cf601ea83d072d8c05dbe94eb Mon Sep 17 00:00:00 2001
From: Vipul Cariappa 
Date: Thu, 26 Sep 2024 10:28:02 +
Subject: [PATCH] [clang] return first Decl for CanonicalDecl in
 TranslationUnitDecl

---
 clang/include/clang/AST/Decl.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 0600ecc4d14a18..7ff35d73df5997 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -133,6 +133,10 @@ class TranslationUnitDecl : public Decl,
   static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
 return static_cast(const_cast(DC));
   }
+
+  /// Retrieves the canonical declaration of this translation unit.
+  TranslationUnitDecl *getCanonicalDecl() override { return getFirstDecl(); }
+  const TranslationUnitDecl *getCanonicalDecl() const { return getFirstDecl(); 
}
 };
 
 /// Represents a `#pragma comment` line. Always a child of

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] return first Decl for CanonicalDecl in TranslationUnitDecl (PR #110101)

2024-09-26 Thread Vipul Cariappa via cfe-commits

Vipul-Cariappa wrote:

ping @vgvassilev 

https://github.com/llvm/llvm-project/pull/110101
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] return first Decl for CanonicalDecl in TranslationUnitDecl (PR #110101)

2024-09-26 Thread Vipul Cariappa via cfe-commits

https://github.com/Vipul-Cariappa updated 
https://github.com/llvm/llvm-project/pull/110101

>From 43d0e353e6e5870cf601ea83d072d8c05dbe94eb Mon Sep 17 00:00:00 2001
From: Vipul Cariappa 
Date: Thu, 26 Sep 2024 10:28:02 +
Subject: [PATCH 1/2] [clang] return first Decl for CanonicalDecl in
 TranslationUnitDecl

---
 clang/include/clang/AST/Decl.h | 4 
 1 file changed, 4 insertions(+)

diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 0600ecc4d14a18..7ff35d73df5997 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -133,6 +133,10 @@ class TranslationUnitDecl : public Decl,
   static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
 return static_cast(const_cast(DC));
   }
+
+  /// Retrieves the canonical declaration of this translation unit.
+  TranslationUnitDecl *getCanonicalDecl() override { return getFirstDecl(); }
+  const TranslationUnitDecl *getCanonicalDecl() const { return getFirstDecl(); 
}
 };
 
 /// Represents a `#pragma comment` line. Always a child of

>From bdb96340fb8cce8342d2cc74325dad89a224f572 Mon Sep 17 00:00:00 2001
From: Vipul Cariappa 
Date: Fri, 27 Sep 2024 04:44:32 +
Subject: [PATCH 2/2] add test for `TranslationUnitDecl::getCanonicalDecl`

---
 .../unittests/Interpreter/InterpreterTest.cpp | 22 +++
 1 file changed, 22 insertions(+)

diff --git a/clang/unittests/Interpreter/InterpreterTest.cpp 
b/clang/unittests/Interpreter/InterpreterTest.cpp
index a2e960f1431117..30b051e747f928 100644
--- a/clang/unittests/Interpreter/InterpreterTest.cpp
+++ b/clang/unittests/Interpreter/InterpreterTest.cpp
@@ -381,4 +381,26 @@ TEST_F(InterpreterTest, Value) {
   EXPECT_TRUE(V9.isManuallyAlloc());
 }
 
+TEST_F(InterpreterTest, TranslationUnit_CanonicalDecl) {
+  std::vector Args;
+  std::unique_ptr Interp = createInterpreter(Args);
+
+  Sema &sema = Interp->getCompilerInstance()->getSema();
+
+  llvm::cantFail(Interp->ParseAndExecute("int x = 42;"));
+
+  TranslationUnitDecl *TU =
+  sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl();
+
+  llvm::cantFail(Interp->ParseAndExecute("long y = 84;"));
+
+  EXPECT_EQ(TU,
+sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
+
+  llvm::cantFail(Interp->ParseAndExecute("char z = 'z';"));
+
+  EXPECT_EQ(TU,
+sema.getASTContext().getTranslationUnitDecl()->getCanonicalDecl());
+}
+
 } // end anonymous namespace

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-13 Thread Vipul Cariappa via cfe-commits

https://github.com/Vipul-Cariappa created 
https://github.com/llvm/llvm-project/pull/127087

Fixes the following crash in clang-repl

```c++
clang-repl> try { throw 1; } catch { 0; }
In file included from <<< inputs >>>:1:
input_line_1:1:23: error: expected '('
1 | try { throw 1; } catch { 0; }
  |   ^
  |   (
clang-repl: 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/AST/DeclBase.cpp:1757:
 void clang::DeclContext::addHiddenDecl(clang::Decl*): Assertion 
`D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical 
context"' failed.
 #0 0x59b28459e6da llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/llvm/lib/Support/Unix/Signals.inc:804:22
 #1 0x59b28459eaed PrintStackTraceSignalHandler(void*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/llvm/lib/Support/Unix/Signals.inc:880:1
 #2 0x59b28459bf7f llvm::sys::RunSignalHandlers() 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/llvm/lib/Support/Signals.cpp:105:20
 #3 0x59b28459df8e SignalHandler(int, siginfo_t*, void*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/llvm/lib/Support/Unix/Signals.inc:418:13
 #4 0x77cdf444ea50 (/usr/lib/libc.so.6+0x42a50)
 #5 0x77cdf44aee3b pthread_kill (/usr/lib/libc.so.6+0xa2e3b)
 #6 0x77cdf444e928 raise (/usr/lib/libc.so.6+0x42928)
 #7 0x77cdf443156c abort (/usr/lib/libc.so.6+0x2556c)
 #8 0x77cdf44314d2 __assert_perror_fail (/usr/lib/libc.so.6+0x254d2)
 #9 0x77cdfc56 (/usr/lib/libc.so.6+0x38c56)
#10 0x59b28495bfc4 clang::DeclContext::addHiddenDecl(clang::Decl*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/AST/DeclBase.cpp:1759:3
#11 0x59b28495c0f5 clang::DeclContext::addDecl(clang::Decl*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/AST/DeclBase.cpp:1785:37
#12 0x59b28773cc2a clang::Sema::ActOnStartTopLevelStmtDecl(clang::Scope*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Sema/SemaDecl.cpp:20302:18
#13 0x59b286f1efdf clang::Parser::ParseTopLevelStmtDecl() 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Parse/ParseDecl.cpp:6024:62
#14 0x59b286ef18ee 
clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, 
clang::ParsedAttributes&, clang::ParsingDeclSpec*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Parse/Parser.cpp:1065:35
#15 0x59b286ef0702 
clang::Parser::ParseTopLevelDecl(clang::OpaquePtr&, 
clang::Sema::ModuleImportState&) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Parse/Parser.cpp:758:36
#16 0x59b28562dff2 clang::IncrementalParser::ParseOrWrapTopLevelDecl() 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Interpreter/IncrementalParser.cpp:66:36
#17 0x59b28562e5b7 clang::IncrementalParser::Parse(llvm::StringRef) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Interpreter/IncrementalParser.cpp:132:8
#18 0x59b28561832b clang::Interpreter::Parse(llvm::StringRef) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Interpreter/Interpreter.cpp:570:8
#19 0x59b285618cbd clang::Interpreter::ParseAndExecute(llvm::StringRef, 
clang::Value*) 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/lib/Interpreter/Interpreter.cpp:649:8
#20 0x59b2836f9343 main 
/home/vipul-cariappa/Documents/Workspace/cpp-py/llvms/llvm-project-a/clang/tools/clang-repl/ClangRepl.cpp:255:59
#21 0x77cdf443388e (/usr/lib/libc.so.6+0x2788e)
#22 0x77cdf443394a __libc_start_main (/usr/lib/libc.so.6+0x2794a)
#23 0x59b2836f7965 _start (./bin/clang-repl+0x73b8965)
fish: Job 1, './bin/clang-repl' terminated by signal SIGABRT (Abort)
```

With this change:
```c++
clang-repl> try { throw 1; } catch { 0; }
In file included from <<< inputs >>>:1:
input_line_1:1:23: error: expected '('
1 | try { throw 1; } catch { 0; }
  |   ^
  |   (
error: Parsing failed.
clang-repl> 1;
clang-repl> %quit
```

>From 0fe35a8790353a42eebadd20070f101e2c46e0de Mon Sep 17 00:00:00 2001
From: Vipul Cariappa 
Date: Thu, 13 Feb 2025 21:33:02 +0530
Subject: [PATCH] [clang-repl] fix error recovery while parsing completely
 fails

---
 clang/lib/Parse/ParseDecl.cpp   | 2 +-
 clang/unittests/Interpreter/InterpreterTest.cpp | 7 +++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 75b5e11f8327c..7ae136af47391 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -6024,7 +6024,7 @@ Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
   TopLevelStmtDecl *TL

[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-13 Thread Vipul Cariappa via cfe-commits

Vipul-Cariappa wrote:

cc @vgvassilev 

https://github.com/llvm/llvm-project/pull/127087
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-repl] fix error recovery while parsing completely fails (PR #127087)

2025-02-18 Thread Vipul Cariappa via cfe-commits

Vipul-Cariappa wrote:

My theory with limited testing: The crash is caused by assertion failure at 
https://github.com/llvm/llvm-project/blob/eb7c947272952d40d3235d89652a10da52cb2b4d/clang/lib/AST/DeclBase.cpp#L1757C1-L1758C54.
 So if we disable assertions it does not crash, and crashes otherwise.
Maybe you can confirm this.
There is a flaw in the logic of recovering from syntax/semantic errors. 

https://github.com/llvm/llvm-project/pull/127087
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits