[PATCH] D151575: [clang][diagnostics] Always show include stacks on errors

Zenong Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 26 11:18:19 PDT 2023


SlaterLatiao created this revision.
Herald added a project: All.
SlaterLatiao requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- Fixes PR#62001.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151575

Files:
  clang/lib/Frontend/DiagnosticRenderer.cpp
  clang/test/Misc/Inputs/include-stack-on-error-1.h
  clang/test/Misc/Inputs/include-stack-on-error-2.h
  clang/test/Misc/Inputs/include-stack-on-error-3.h
  clang/test/Misc/include-stack-on-error-1.cpp
  clang/test/Misc/include-stack-on-error-2.cpp


Index: clang/test/Misc/include-stack-on-error-2.cpp
===================================================================
--- /dev/null
+++ clang/test/Misc/include-stack-on-error-2.cpp
@@ -0,0 +1,15 @@
+// Test duplicate include stacks that are not on a new error are skipped.
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-show-note-include-stack -I %S/Inputs %s 2>&1 | FileCheck %s -check-prefix=NOTESTACK
+
+#include "include-stack-on-error-1.h"
+#include "include-stack-on-error-3.h"
+
+// NOTESTACK:  In file included from
+// NOTESTACK: error: no matching function for call to 'b1'
+// This include should be skipped due to duplicate include location.
+// NOTESTACK-NOT:  In file included from
+// NOTESTACK: note: candidate function not viable
+// NOTESTACK:  In file included from
+// NOTESTACK: error: invalid operands to binary expression
+// NOTESTACK:  In file included from
+// NOTESTACK: note: in instantiation of function template specialization
\ No newline at end of file
Index: clang/test/Misc/include-stack-on-error-1.cpp
===================================================================
--- /dev/null
+++ clang/test/Misc/include-stack-on-error-1.cpp
@@ -0,0 +1,25 @@
+// Test duplicate include stacks on a new error are not skipped.
+// RUN: not %clang_cc1 -fsyntax-only -I %S/Inputs %s 2>&1 | FileCheck %s -check-prefix=STACK
+// RUN: not %clang_cc1 -fsyntax-only -fdiagnostics-show-note-include-stack -I %S/Inputs %s 2>&1 | FileCheck %s -check-prefix=NOTESTACK
+
+#include "include-stack-on-error-1.h"
+#include "include-stack-on-error-2.h"
+
+// STACK:  In file included from
+// STACK: error: no matching function for call to 'b1'
+// STACK-NOT:  In file included from
+// STACK: note: candidate function not viable
+// STACK:  In file included from
+// STACK: error: invalid operands to binary expression
+// STACK-NOT:  In file included from
+// STACK: note: in instantiation of function template specialization
+
+// NOTESTACK:  In file included from
+// NOTESTACK: error: no matching function for call to 'b1'
+// NOTESTACK:  In file included from
+// NOTESTACK: note: candidate function not viable
+// NOTESTACK:  In file included from
+// NOTESTACK: error: invalid operands to binary expression
+// NOTESTACK:  In file included from
+// NOTESTACK: note: in instantiation of function template specialization
+
Index: clang/test/Misc/Inputs/include-stack-on-error-3.h
===================================================================
--- /dev/null
+++ clang/test/Misc/Inputs/include-stack-on-error-3.h
@@ -0,0 +1,5 @@
+void b1();
+void c() {
+    b1(0);
+    b2("0", "0");
+}
\ No newline at end of file
Index: clang/test/Misc/Inputs/include-stack-on-error-2.h
===================================================================
--- /dev/null
+++ clang/test/Misc/Inputs/include-stack-on-error-2.h
@@ -0,0 +1,4 @@
+void c() {
+    b1(0);
+    b2("0", "0");
+}
\ No newline at end of file
Index: clang/test/Misc/Inputs/include-stack-on-error-1.h
===================================================================
--- /dev/null
+++ clang/test/Misc/Inputs/include-stack-on-error-1.h
@@ -0,0 +1,2 @@
+void b1();
+template <class T> void b2(T x, T y) { x + y; }
\ No newline at end of file
Index: clang/lib/Frontend/DiagnosticRenderer.cpp
===================================================================
--- clang/lib/Frontend/DiagnosticRenderer.cpp
+++ clang/lib/Frontend/DiagnosticRenderer.cpp
@@ -167,6 +167,10 @@
       PLoc.isInvalid() ? FullSourceLoc()
                        : FullSourceLoc(PLoc.getIncludeLoc(), Loc.getManager());
 
+  // Reset `LastIncludeLoc` on a new error so that the include stacks are
+  // not skipped.
+  if (Level == DiagnosticsEngine::Error)
+    LastIncludeLoc = SourceLocation();
   // Skip redundant include stacks altogether.
   if (LastIncludeLoc == IncludeLoc)
     return;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151575.526136.patch
Type: text/x-patch
Size: 3849 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230526/9398a996/attachment.bin>


More information about the cfe-commits mailing list