[clang] [clang-repl] Stop interpreter init on action failure (PR #217147)

Jeaye Wilkerson via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 24 10:53:38 PDT 2026


https://github.com/jeaye updated https://github.com/llvm/llvm-project/pull/217147

>From 00cbfa068047eb2d98cd8fb4168ab930de4ee1dd Mon Sep 17 00:00:00 2001
From: jeaye <contact at jeaye.com>
Date: Tue, 18 Aug 2026 13:10:09 -0700
Subject: [PATCH] [clang-repl] Stop interpreter init on action failure
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

`clang::Interpreter` previously ignored the result of its initial
`CompilerInstance::ExecuteAction()` call and always constructed the
`IncrementalParser`. When the initial action failed, the parser could be
created from an invalid compiler state.

In practice, this failure happens if the incremental PCH included
has expired, due to system headers changing. This happens quite
regularly, after system updates, and is affecting jank.

To fix this, the `Interpreter` now returns an error immediately when the
initial action fails or records a diagnostic error.
---
 clang/lib/Interpreter/Interpreter.cpp | 17 +++++++++++++++--
 clang/test/Interpreter/stale-pch.cpp  | 23 +++++++++++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)
 create mode 100644 clang/test/Interpreter/stale-pch.cpp

diff --git a/clang/lib/Interpreter/Interpreter.cpp b/clang/lib/Interpreter/Interpreter.cpp
index 0536fdcd548a2..ef1ca31538352 100644
--- a/clang/lib/Interpreter/Interpreter.cpp
+++ b/clang/lib/Interpreter/Interpreter.cpp
@@ -206,6 +206,15 @@ CreateCI(const llvm::opt::ArgStringList &Argv) {
   return std::move(Clang);
 }
 
+static llvm::Error ExecuteIncrementalAction(CompilerInstance &CI,
+                                            IncrementalAction &Act) {
+  if (!CI.ExecuteAction(Act) || CI.getDiagnostics().hasErrorOccurred()) {
+    return llvm::createStringError(llvm::errc::not_supported,
+                                   "Failed to execute incremental action");
+  }
+  return llvm::Error::success();
+}
+
 } // anonymous namespace
 
 namespace clang {
@@ -349,7 +358,10 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
   if (ErrOut)
     return;
 
-  CI->ExecuteAction(*Act);
+  if (llvm::Error E = ExecuteIncrementalAction(*CI, *Act)) {
+    ErrOut = joinErrors(std::move(ErrOut), std::move(E));
+    return;
+  }
 
   IncrParser =
       std::make_unique<IncrementalParser>(*CI, Act.get(), ErrOut, PTUs);
@@ -490,7 +502,8 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
 
   Interp->DeviceAct = std::move(DeviceAct);
 
-  DCI->ExecuteAction(*Interp->DeviceAct);
+  if (llvm::Error E = ExecuteIncrementalAction(*DCI, *Interp->DeviceAct))
+    return std::move(E);
 
   Interp->DeviceCI = std::move(DCI);
 
diff --git a/clang/test/Interpreter/stale-pch.cpp b/clang/test/Interpreter/stale-pch.cpp
new file mode 100644
index 0000000000000..410a91fe7d8f0
--- /dev/null
+++ b/clang/test/Interpreter/stale-pch.cpp
@@ -0,0 +1,23 @@
+// REQUIRES: host-supports-jit
+// UNSUPPORTED: system-aix
+//
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+//
+// RUN: %if system-windows %{ \
+// RUN: %clang -fmax-type-align=16 -Xclang -fdeprecated-macro -fno-stack-protector -Xclang -fwrapv -Xclang -fblocks -Xclang -fskip-odr-check-in-gmf -fexceptions -fcxx-exceptions -fgnuc-version=0 -target %host-jit-triple -Xclang -fblocks -Xclang -fmax-type-align=8 -Xclang -fincremental-extensions -Xclang -emit-pch -x c++-header -o %t/include.pch %t/include.hpp \
+// RUN: %} \
+// RUN: %else %{ \
+// RUN: %clang -fPIC -fmax-type-align=16 -Xclang -fdeprecated-macro -fno-stack-protector -Xclang -fwrapv -Xclang -fblocks -Xclang -fskip-odr-check-in-gmf -fexceptions -fcxx-exceptions -fgnuc-version=0 -target %host-jit-triple -Xclang -fblocks -Xclang -fmax-type-align=8 -Xclang -fincremental-extensions -Xclang -emit-pch -x c++-header -o %t/include.pch %t/include.hpp \
+// RUN: %}
+// RUN: echo '// changed after PCH creation' >> %t/include.hpp
+//
+// RUN: not clang-repl -Xcc -fgnuc-version=0 -Xcc -fno-stack-protector -Xcc -fwrapv -Xcc -fblocks -Xcc -fskip-odr-check-in-gmf -Xcc -fmax-type-align=8 -Xcc -include-pch -Xcc %t/include.pch < /dev/null 2>&1 | FileCheck %s
+
+//--- include.hpp
+
+int f_pch() { return 5; }
+
+// CHECK: fatal error: file '{{.*}}include.hpp' has been modified since the precompiled header
+// CHECK: clang-repl: Failed to execute incremental action



More information about the cfe-commits mailing list