https://gcc.gnu.org/g:e55195c4f2f809419c25b9d7c2ffdcc017f010a4

commit r16-5363-ge55195c4f2f809419c25b9d7c2ffdcc017f010a4
Author: David Malcolm <[email protected]>
Date:   Mon Nov 17 14:08:59 2025 -0500

    SARIF output: fix diagnostics within C++ member fns [PR122626]
    
    When building hierarchical logical locations, we were stopping traveral
    upwards before any TRANSLATION_UNIT_DECL for decls, but not for types,
    leading to an assertion failure when producing SARIF output for
    diagnostics within C++ member fns.
    
    Fixed thusly.
    
    gcc/testsuite/ChangeLog:
            PR analyzer/122626
            * g++.dg/analyzer/malloc.C: Add sarif output to verify the fix for
            PR analyzer/122626.
    
    gcc/ChangeLog:
            PR analyzer/122626
            * tree-logical-location.cc
            (tree_logical_location_manager::get_parent): Return null when
            TYPE_CONTEXT is a TRANSLATION_UNIT_DECL so that we don't fail
            the assertion in assert_valid_tree.
    
    Signed-off-by: David Malcolm <[email protected]>

Diff:
---
 gcc/testsuite/g++.dg/analyzer/malloc.C | 5 +++++
 gcc/tree-logical-location.cc           | 2 ++
 2 files changed, 7 insertions(+)

diff --git a/gcc/testsuite/g++.dg/analyzer/malloc.C 
b/gcc/testsuite/g++.dg/analyzer/malloc.C
index d4ef831fe623..f2da9884466e 100644
--- a/gcc/testsuite/g++.dg/analyzer/malloc.C
+++ b/gcc/testsuite/g++.dg/analyzer/malloc.C
@@ -1,6 +1,9 @@
 // { dg-do compile }
 /* { dg-skip-if "requires hosted libstdc++ for stdlib free" { ! hostedlib } } 
*/
 
+/* Verify we don't ICE on SARIF output (PR analyzer/122626).  */
+/* { dg-additional-options "-fdiagnostics-add-output=sarif" } */
+
 #include <stdlib.h>
 
 void test_1 (void *ptr)
@@ -24,3 +27,5 @@ void test_2 (void *ptr)
   free (ptr); // { dg-message "first 'free' here" }
   s2 a (ptr); // { dg-message "passing freed pointer 'ptr' in call to 's2::s2' 
from 'test_2'" }
 }
+
+/* { dg-final { verify-sarif-file } } */
diff --git a/gcc/tree-logical-location.cc b/gcc/tree-logical-location.cc
index b39327feca5e..879bdf1df69e 100644
--- a/gcc/tree-logical-location.cc
+++ b/gcc/tree-logical-location.cc
@@ -152,6 +152,8 @@ tree_logical_location_manager::get_parent (key k) const
     {
       if (!TYPE_CONTEXT (node))
        return key ();
+      if (TREE_CODE (TYPE_CONTEXT (node)) == TRANSLATION_UNIT_DECL)
+       return key ();
       return key_from_tree (TYPE_CONTEXT (node));
     }
   return key ();

Reply via email to