LCOV - code coverage report
Current view: top level - gcc/analyzer - analyzer-language.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 53 50
Test Date: 2024-11-30 13:30:02 Functions: 100.0 % 7 7
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Interface between analyzer and frontends.
       2                 :             :    Copyright (C) 2022-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by David Malcolm <dmalcolm@redhat.com>.
       4                 :             : 
       5                 :             : This file is part of GCC.
       6                 :             : 
       7                 :             : GCC is free software; you can redistribute it and/or modify it
       8                 :             : 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, but
      13                 :             : WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :             : 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 "tree.h"
      25                 :             : #include "stringpool.h"
      26                 :             : #include "analyzer/analyzer.h"
      27                 :             : #include "analyzer/analyzer-language.h"
      28                 :             : #include "analyzer/analyzer-logging.h"
      29                 :             : #include "diagnostic.h"
      30                 :             : 
      31                 :             : /* Map from identifier to INTEGER_CST.  */
      32                 :             : static GTY (()) hash_map <tree, tree> *analyzer_stashed_constants;
      33                 :             : 
      34                 :             : #if ENABLE_ANALYZER
      35                 :             : 
      36                 :             : namespace ana {
      37                 :             : static vec<finish_translation_unit_callback>
      38                 :             :     *finish_translation_unit_callbacks;
      39                 :             : 
      40                 :             : void
      41                 :           2 : register_finish_translation_unit_callback (
      42                 :             :     finish_translation_unit_callback callback)
      43                 :             : {
      44                 :           2 :   if (!finish_translation_unit_callbacks)
      45                 :           1 :     vec_alloc (finish_translation_unit_callbacks, 1);
      46                 :           2 :   finish_translation_unit_callbacks->safe_push (callback);
      47                 :           2 : }
      48                 :             : 
      49                 :             : static void
      50                 :        1429 : run_callbacks (logger *logger, const translation_unit &tu)
      51                 :             : {
      52                 :        1433 :   for (auto const &cb : finish_translation_unit_callbacks)
      53                 :             :     {
      54                 :           2 :       cb (logger, tu);
      55                 :             :     }
      56                 :        1429 : }
      57                 :             : 
      58                 :             : /* Call into TU to try to find a value for NAME.
      59                 :             :    If found, stash its value within analyzer_stashed_constants.  */
      60                 :             : 
      61                 :             : static void
      62                 :        7145 : maybe_stash_named_constant (logger *logger,
      63                 :             :                             const translation_unit &tu,
      64                 :             :                             const char *name)
      65                 :             : {
      66                 :        7145 :   LOG_FUNC_1 (logger, "name: %qs", name);
      67                 :             : 
      68                 :        7145 :   if (!analyzer_stashed_constants)
      69                 :        1429 :     analyzer_stashed_constants = hash_map<tree, tree>::create_ggc ();
      70                 :             : 
      71                 :        7145 :   tree id = get_identifier (name);
      72                 :        7145 :   if (tree t = tu.lookup_constant_by_id (id))
      73                 :             :     {
      74                 :          78 :       gcc_assert (TREE_CODE (t) == INTEGER_CST);
      75                 :          78 :       analyzer_stashed_constants->put (id, t);
      76                 :          78 :       if (logger)
      77                 :           0 :         logger->log ("%qs: %qE", name, t);
      78                 :             :     }
      79                 :             :   else
      80                 :             :     {
      81                 :        7067 :       if (logger)
      82                 :          10 :         logger->log ("%qs: not found", name);
      83                 :             :     }
      84                 :        7145 : }
      85                 :             : 
      86                 :             : /* Call into TU to try to find values for the names we care about.
      87                 :             :    If found, stash their values within analyzer_stashed_constants.  */
      88                 :             : 
      89                 :             : static void
      90                 :        1429 : stash_named_constants (logger *logger, const translation_unit &tu)
      91                 :             : {
      92                 :        1429 :   LOG_SCOPE (logger);
      93                 :             : 
      94                 :             :   /* Stash named constants for use by sm-fd.cc  */
      95                 :        1429 :   maybe_stash_named_constant (logger, tu, "O_ACCMODE");
      96                 :        1429 :   maybe_stash_named_constant (logger, tu, "O_RDONLY");
      97                 :        1429 :   maybe_stash_named_constant (logger, tu, "O_WRONLY");
      98                 :        1429 :   maybe_stash_named_constant (logger, tu, "SOCK_STREAM");
      99                 :        1429 :   maybe_stash_named_constant (logger, tu, "SOCK_DGRAM");
     100                 :        1429 : }
     101                 :             : 
     102                 :             : /* Hook for frontend to call into analyzer when TU finishes.
     103                 :             :    This exists so that the analyzer can stash named constant values from
     104                 :             :    header files (e.g. macros and enums) for later use when modeling the
     105                 :             :    behaviors of APIs.
     106                 :             : 
     107                 :             :    By doing it this way, the analyzer can use the precise values for those
     108                 :             :    constants from the user's headers, rather than attempting to model them
     109                 :             :    as properties of the target.  */
     110                 :             : 
     111                 :             : void
     112                 :        1429 : on_finish_translation_unit (const translation_unit &tu)
     113                 :             : {
     114                 :             :   /* Bail if the analyzer isn't enabled.  */
     115                 :        1429 :   if (!flag_analyzer)
     116                 :           0 :     return;
     117                 :             : 
     118                 :        1429 :   FILE *logfile = get_or_create_any_logfile ();
     119                 :        1429 :   log_user the_logger (NULL);
     120                 :        1429 :   if (logfile)
     121                 :           2 :     the_logger.set_logger (new logger (logfile, 0, 0,
     122                 :           2 :                                        *global_dc->get_reference_printer ()));
     123                 :        1429 :   stash_named_constants (the_logger.get_logger (), tu);
     124                 :             : 
     125                 :        1429 :   run_callbacks (the_logger.get_logger (), tu);
     126                 :        1429 : }
     127                 :             : 
     128                 :             : /* Lookup NAME in the named constants stashed when the frontend TU finished.
     129                 :             :    Return either an INTEGER_CST, or NULL_TREE.  */
     130                 :             : 
     131                 :             : tree
     132                 :       16079 : get_stashed_constant_by_name (const char *name)
     133                 :             : {
     134                 :       16079 :   if (!analyzer_stashed_constants)
     135                 :             :     return NULL_TREE;
     136                 :        6925 :   tree id = get_identifier (name);
     137                 :        6925 :   if (tree *slot = analyzer_stashed_constants->get (id))
     138                 :             :     {
     139                 :          97 :       gcc_assert (TREE_CODE (*slot) == INTEGER_CST);
     140                 :             :       return *slot;
     141                 :             :     }
     142                 :             :   return NULL_TREE;
     143                 :             : }
     144                 :             : 
     145                 :             : /* Log all stashed named constants to LOGGER.  */
     146                 :             : 
     147                 :             : void
     148                 :           2 : log_stashed_constants (logger *logger)
     149                 :             : {
     150                 :           2 :   gcc_assert (logger);
     151                 :           2 :   LOG_SCOPE (logger);
     152                 :           2 :   if (analyzer_stashed_constants)
     153                 :           4 :     for (auto iter : *analyzer_stashed_constants)
     154                 :           0 :       logger->log ("%qE: %qE", iter.first, iter.second);
     155                 :           2 : }
     156                 :             : 
     157                 :             : } // namespace ana
     158                 :             : 
     159                 :             : #endif /* #if ENABLE_ANALYZER */
     160                 :             : 
     161                 :             : #include "gt-analyzer-language.h"
        

Generated by: LCOV version 2.1-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.