LCOV - code coverage report
Current view: top level - gcc - gimple-ssa-nonnull-compare.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 41 41
Test Date: 2026-08-22 16:33:35 Functions: 100.0 % 4 4
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* -Wnonnull-compare warning support.
       2              :    Copyright (C) 2016-2026 Free Software Foundation, Inc.
       3              :    Contributed by Jakub Jelinek <jakub@redhat.com>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "tree.h"
      26              : #include "gimple.h"
      27              : #include "tree-pass.h"
      28              : #include "ssa.h"
      29              : #include "diagnostic-core.h"
      30              : #include "tree-dfa.h"
      31              : 
      32              : /* Warn about comparison of nonnull_arg_p argument initial values
      33              :    with NULL.  */
      34              : 
      35              : static void
      36       745543 : do_warn_nonnull_compare (function *fun, tree arg)
      37              : {
      38      1228257 :   if (!POINTER_TYPE_P (TREE_TYPE (arg))
      39      1200884 :       && TREE_CODE (TREE_TYPE (arg)) != OFFSET_TYPE)
      40       671971 :     return;
      41              : 
      42       290254 :   if (!nonnull_arg_p (arg))
      43              :     return;
      44              : 
      45        82277 :   tree d = ssa_default_def (fun, arg);
      46        82277 :   if (d == NULL_TREE)
      47              :     return;
      48              : 
      49        73572 :   use_operand_p use_p;
      50        73572 :   imm_use_iterator iter;
      51              : 
      52       266027 :   FOR_EACH_IMM_USE_FAST (use_p, iter, d)
      53              :     {
      54       192455 :       gimple *stmt = USE_STMT (use_p);
      55       192455 :       tree op = NULL_TREE;
      56       192455 :       location_t loc = gimple_location (stmt);
      57       192455 :       if (gimple_code (stmt) == GIMPLE_COND)
      58          328 :         switch (gimple_cond_code (stmt))
      59              :           {
      60          312 :           case EQ_EXPR:
      61          312 :           case NE_EXPR:
      62          312 :             if (gimple_cond_lhs (stmt) == d)
      63          218 :               op = gimple_cond_rhs (stmt);
      64              :             break;
      65              :           default:
      66              :             break;
      67              :           }
      68       192127 :       else if (is_gimple_assign (stmt))
      69       129964 :         switch (gimple_assign_rhs_code (stmt))
      70              :           {
      71          278 :           case EQ_EXPR:
      72          278 :           case NE_EXPR:
      73          278 :             if (gimple_assign_rhs1 (stmt) == d)
      74          229 :               op = gimple_assign_rhs2 (stmt);
      75              :             break;
      76              :           default:
      77              :             break;
      78              :           }
      79          447 :       if (op
      80         1062 :           && (POINTER_TYPE_P (TREE_TYPE (arg))
      81          447 :               ? integer_zerop (op) : integer_minus_onep (op))
      82          961 :           && !warning_suppressed_p (stmt, OPT_Wnonnull_compare))
      83           42 :         warning_at (loc, OPT_Wnonnull_compare,
      84              :                     "%<nonnull%> argument %qD compared to NULL", arg);
      85              :     }
      86              : }
      87              : 
      88              : namespace {
      89              : 
      90              : const pass_data pass_data_warn_nonnull_compare =
      91              : {
      92              :   GIMPLE_PASS, /* type */
      93              :   "*nonnullcmp", /* name */
      94              :   OPTGROUP_NONE, /* optinfo_flags */
      95              :   TV_NONE, /* tv_id */
      96              :   PROP_ssa, /* properties_required */
      97              :   0, /* properties_provided */
      98              :   0, /* properties_destroyed */
      99              :   0, /* todo_flags_start */
     100              :   0, /* todo_flags_finish */
     101              : };
     102              : 
     103              : class pass_warn_nonnull_compare : public gimple_opt_pass
     104              : {
     105              : public:
     106       294196 :   pass_warn_nonnull_compare (gcc::context *ctxt)
     107       588392 :     : gimple_opt_pass (pass_data_warn_nonnull_compare, ctxt)
     108              :   {}
     109              : 
     110              :   /* opt_pass methods: */
     111      3004029 :   bool gate (function *) final override { return warn_nonnull_compare; }
     112              : 
     113              :   unsigned int execute (function *) final override;
     114              : 
     115              : }; // class pass_warn_nonnull_compare
     116              : 
     117              : unsigned int
     118       239786 : pass_warn_nonnull_compare::execute (function *fun)
     119              : {
     120       239786 :   if (fun->static_chain_decl)
     121            1 :     do_warn_nonnull_compare (fun, fun->static_chain_decl);
     122              : 
     123       985328 :   for (tree arg = DECL_ARGUMENTS (cfun->decl); arg; arg = DECL_CHAIN (arg))
     124       745542 :     do_warn_nonnull_compare (fun, arg);
     125       239786 :   return 0;
     126              : }
     127              : 
     128              : } // anon namespace
     129              : 
     130              : gimple_opt_pass *
     131       294196 : make_pass_warn_nonnull_compare (gcc::context *ctxt)
     132              : {
     133       294196 :   return new pass_warn_nonnull_compare (ctxt);
     134              : }
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.