https://github.com/p4vook created 
https://github.com/llvm/llvm-project/pull/75629

Check if the last translation unit or its first declaration are actually empty 
and do not nead cleanup.

Previously this caused segmentation fault on empty PTUs.

Add a regression test.

Fixes: #72980

>From 2c4ab0990b55be21ca820d84eebb46b1814bb0aa Mon Sep 17 00:00:00 2001
From: Pavel Kalugin <pa...@pavelthebest.me>
Date: Fri, 15 Dec 2023 15:05:45 +0300
Subject: [PATCH] [clang-repl] fix segfault in CleanUpPTU()

Check if the last translation unit or its first declaration
are actually empty and do not nead cleanup.

Previously this caused segmentation fault on empty PTUs.

Add a regression test.

Fixes: #72980
Signed-off-by: Pavel Kalugin <pa...@pavelthebest.me>
---
 clang/lib/Interpreter/IncrementalParser.cpp     |  8 ++++++++
 clang/test/Interpreter/anonymous-scope-fail.cpp | 10 ++++++++++
 2 files changed, 18 insertions(+)
 create mode 100644 clang/test/Interpreter/anonymous-scope-fail.cpp

diff --git a/clang/lib/Interpreter/IncrementalParser.cpp 
b/clang/lib/Interpreter/IncrementalParser.cpp
index 370bcbfee8b014..f894af881134bb 100644
--- a/clang/lib/Interpreter/IncrementalParser.cpp
+++ b/clang/lib/Interpreter/IncrementalParser.cpp
@@ -373,7 +373,15 @@ std::unique_ptr<llvm::Module> 
IncrementalParser::GenModule() {
 
 void IncrementalParser::CleanUpPTU(PartialTranslationUnit &PTU) {
   TranslationUnitDecl *MostRecentTU = PTU.TUPart;
+  if (!MostRecentTU) {
+    return;
+  }
+
   TranslationUnitDecl *FirstTU = MostRecentTU->getFirstDecl();
+  if (!FirstTU) {
+    return;
+  }
+
   if (StoredDeclsMap *Map = FirstTU->getPrimaryContext()->getLookupPtr()) {
     for (auto I = Map->begin(); I != Map->end(); ++I) {
       StoredDeclsList &List = I->second;
diff --git a/clang/test/Interpreter/anonymous-scope-fail.cpp 
b/clang/test/Interpreter/anonymous-scope-fail.cpp
new file mode 100644
index 00000000000000..c32b42d2859d97
--- /dev/null
+++ b/clang/test/Interpreter/anonymous-scope-fail.cpp
@@ -0,0 +1,10 @@
+// RUN: clang-repl "int x = 10;" "{ int t; a::b(t); }" "int y = 10;"
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-aix
+// RUN: cat %s | not clang-repl | FileCheck %s
+{ int t; a::b(t); }
+extern "C" int printf(const char *, ...);
+int i = 42;
+auto r1 = printf("i = %d\n", i);
+// CHECK: i = 42
+%quit

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

Reply via email to