[clang] [clang-repl] Don't show the main file in include stacks (PR #239950)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 02:41:32 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Jonas Rembser (guitargeek)

<details>
<summary>Changes</summary>

clang-repl includes each input from its empty main file "<<< inputs >>>". Every diagnostic in an input therefore came with "In file included from <<< inputs >>>:1:", once, and again after any other include stack:
```
  clang-repl> int y = x;
  In file included from <<< inputs >>>:1:
  input_line_1:1:9: error: use of undeclared identifier 'x'
```

The main file is an implementation detail. End include stacks at an empty main file in incremental mode. Headers included from an input still show the input: "In file included from input_line_5:1:". A main file that includes a header, as in "clang -cc1 -fincremental-extensions file.cpp", still shows too.

Assisted-by: Claude Opus 5.5

This is another PR that addresses a problem when using Clang-Repl instead of Cling in ROOT.

---
Full diff: https://github.com/llvm/llvm-project/pull/239950.diff


4 Files Affected:

- (modified) clang/lib/Frontend/DiagnosticRenderer.cpp (+8) 
- (added) clang/test/Interpreter/Inputs/include-stack-main.cpp (+1) 
- (added) clang/test/Interpreter/Inputs/include-stack.h (+1) 
- (added) clang/test/Interpreter/include-stack.cpp (+24) 


``````````diff
diff --git a/clang/lib/Frontend/DiagnosticRenderer.cpp b/clang/lib/Frontend/DiagnosticRenderer.cpp
index 4ee2ac4c61a5d5..bded505e5deea7 100644
--- a/clang/lib/Frontend/DiagnosticRenderer.cpp
+++ b/clang/lib/Frontend/DiagnosticRenderer.cpp
@@ -165,6 +165,14 @@ void DiagnosticRenderer::emitIncludeStackRecursively(FullSourceLoc Loc) {
   if (PLoc.isInvalid())
     return;
 
+  // clang-repl includes each input from its empty main file. Unlike an
+  // #include in a main file, that is an implementation detail:
+  // "In file included from <<< inputs >>>:1:".
+  if (LangOpts.IncrementalExtensions &&
+      Loc.getFileID() == Loc.getManager().getMainFileID() &&
+      Loc.getBufferData().empty())
+    return;
+
   // If this source location was imported from a module, print the module
   // import stack rather than the
   // FIXME: We want submodule granularity here.
diff --git a/clang/test/Interpreter/Inputs/include-stack-main.cpp b/clang/test/Interpreter/Inputs/include-stack-main.cpp
new file mode 100644
index 00000000000000..197bd15b33f4c1
--- /dev/null
+++ b/clang/test/Interpreter/Inputs/include-stack-main.cpp
@@ -0,0 +1 @@
+#include "include-stack.h"
diff --git a/clang/test/Interpreter/Inputs/include-stack.h b/clang/test/Interpreter/Inputs/include-stack.h
new file mode 100644
index 00000000000000..f8f0843f13abe6
--- /dev/null
+++ b/clang/test/Interpreter/Inputs/include-stack.h
@@ -0,0 +1 @@
+int in_header = undeclared_in_header;
diff --git a/clang/test/Interpreter/include-stack.cpp b/clang/test/Interpreter/include-stack.cpp
new file mode 100644
index 00000000000000..9705fd96cc33e0
--- /dev/null
+++ b/clang/test/Interpreter/include-stack.cpp
@@ -0,0 +1,24 @@
+// REQUIRES: host-supports-jit
+// RUN: cat %s | clang-repl -Xcc -I%S/Inputs 2>&1 | FileCheck %s \
+// RUN:     --implicit-check-not="<<< inputs >>>"
+// RUN: not %clang_cc1 -fincremental-extensions -fsyntax-only -I%S/Inputs \
+// RUN:     %S/Inputs/include-stack-main.cpp 2>&1 | FileCheck %s --check-prefix=MAIN
+
+// clang-repl includes each input from its main file. That is an
+// implementation detail, which include stacks don't show.
+
+// A main file that includes a header still shows in its include stack.
+// MAIN: In file included from {{.*}}include-stack-main.cpp:1:
+// MAIN-NEXT: {{.*}}include-stack.h:1:17: error: use of undeclared identifier 'undeclared_in_header'
+
+int x = undeclared_in_input;
+// CHECK: input_line_{{[0-9]+}}:1:9: error: use of undeclared identifier 'undeclared_in_input'
+
+#include "include-stack.h"
+// CHECK: In file included from input_line_{{[0-9]+}}:1:
+// CHECK-NEXT: {{.*}}include-stack.h:1:17: error: use of undeclared identifier 'undeclared_in_header'
+
+int y = undeclared_again;
+// CHECK: input_line_{{[0-9]+}}:1:9: error: use of undeclared identifier 'undeclared_again'
+
+%quit

``````````

</details>


https://github.com/llvm/llvm-project/pull/239950


More information about the cfe-commits mailing list