LCOV - code coverage report
Current view: top level - gcc/analyzer - analyzer-logging.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 100.0 % 52 52
Test Date: 2026-05-11 19:44:49 Functions: 100.0 % 5 5
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Hierarchical log messages for the analyzer.
       2              :    Copyright (C) 2014-2026 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              : /* Adapted from jit-logging.h.  */
      22              : 
      23              : #ifndef ANALYZER_LOGGING_H
      24              : #define ANALYZER_LOGGING_H
      25              : 
      26              : #include "diagnostic-core.h"
      27              : 
      28              : namespace text_art { class canvas; }
      29              : 
      30              : namespace ana {
      31              : 
      32              : /* A logger encapsulates a logging stream: a way to send
      33              :    lines of pertinent information to a FILE *.  */
      34              : 
      35              : class logger
      36              : {
      37              :  public:
      38              :   logger (FILE *f_out, int flags, int verbosity, const pretty_printer &reference_pp);
      39              :   ~logger ();
      40              : 
      41              :   void incref (const char *reason);
      42              :   void decref (const char *reason);
      43              : 
      44              :   void log (const char *fmt, ...)
      45              :     ATTRIBUTE_GCC_DIAG(2, 3);
      46              :   void log_va (const char *fmt, va_list *ap)
      47              :     ATTRIBUTE_GCC_DIAG(2, 0);
      48              :   void start_log_line ();
      49              :   void log_partial (const char *fmt, ...)
      50              :     ATTRIBUTE_GCC_DIAG(2, 3);
      51              :   void log_va_partial (const char *fmt, va_list *ap)
      52              :     ATTRIBUTE_GCC_DIAG(2, 0);
      53              :   void end_log_line ();
      54              : 
      55              :   void log_canvas (const text_art::canvas &);
      56              : 
      57              :   void enter_scope (const char *scope_name);
      58              :   void enter_scope (const char *scope_name, const char *fmt, va_list *ap)
      59              :     ATTRIBUTE_GCC_DIAG(3, 0);
      60              :   void exit_scope (const char *scope_name);
      61         3359 :   void inc_indent () { m_indent_level++; }
      62         3359 :   void dec_indent () { m_indent_level--; }
      63              : 
      64         4048 :   pretty_printer *get_printer () const { return m_pp.get (); }
      65            5 :   FILE *get_file () const { return m_f_out; }
      66              : 
      67              : private:
      68              :   DISABLE_COPY_AND_ASSIGN (logger);
      69              : 
      70              :   int m_refcount;
      71              :   FILE *m_f_out;
      72              :   int m_indent_level;
      73              :   bool m_log_refcount_changes;
      74              :   std::unique_ptr<pretty_printer> m_pp;
      75              : };
      76              : 
      77              : /* The class log_scope is an RAII-style class intended to make
      78              :    it easy to notify a logger about entering and exiting the body of a
      79              :    given function.  */
      80              : 
      81              : class log_scope
      82              : {
      83              : public:
      84              :   log_scope (logger *logger, const char *name);
      85              :   log_scope (logger *logger, const char *name, const char *fmt, ...)
      86              :     ATTRIBUTE_GCC_DIAG(4, 5);
      87              :   ~log_scope ();
      88              : 
      89              :  private:
      90              :   DISABLE_COPY_AND_ASSIGN (log_scope);
      91              : 
      92              :   logger *m_logger;
      93              :   const char *m_name;
      94              : };
      95              : 
      96              : /* The constructor for log_scope.
      97              : 
      98              :    The normal case is that the logger is nullptr, in which case this should
      99              :    be largely a no-op.
     100              : 
     101              :    If we do have a logger, notify it that we're entering the given scope.
     102              :    We also need to hold a reference on it, to avoid a use-after-free
     103              :    when logging the cleanup of the owner of the logger.  */
     104              : 
     105              : inline
     106      4547741 : log_scope::log_scope (logger *logger, const char *name) :
     107      4547741 :  m_logger (logger),
     108      4547741 :  m_name (name)
     109              : {
     110      4547741 :   if (m_logger)
     111              :     {
     112         2043 :       m_logger->incref ("log_scope ctor");
     113         2043 :       m_logger->enter_scope (m_name);
     114              :     }
     115      4547741 : }
     116              : 
     117              : inline
     118      1631786 : log_scope::log_scope (logger *logger, const char *name, const char *fmt, ...):
     119      1631786 :  m_logger (logger),
     120      1631786 :  m_name (name)
     121              : {
     122      1631786 :   if (m_logger)
     123              :     {
     124          528 :       m_logger->incref ("log_scope ctor");
     125          528 :       va_list ap;
     126          528 :       va_start (ap, fmt);
     127          528 :       m_logger->enter_scope (m_name, fmt, &ap);
     128          528 :       va_end (ap);
     129              :     }
     130      1631786 : }
     131              : 
     132              : 
     133              : /* The destructor for log_scope; essentially the opposite of
     134              :    the constructor.  */
     135              : 
     136              : inline
     137      6179521 : log_scope::~log_scope ()
     138              : {
     139      6179521 :   if (m_logger)
     140              :     {
     141         2571 :       m_logger->exit_scope (m_name);
     142         2571 :       m_logger->decref ("log_scope dtor");
     143              :     }
     144      6179521 : }
     145              : 
     146              : class log_nesting_level
     147              : {
     148              : public:
     149              :   log_nesting_level (logger *logger, const char *fmt, ...)
     150              :     ATTRIBUTE_GCC_DIAG(3, 4);
     151              :   ~log_nesting_level ();
     152              : 
     153              : private:
     154              :   logger *m_logger;
     155              : };
     156              : 
     157              : inline
     158       488764 : log_nesting_level::log_nesting_level (logger *logger, const char *fmt, ...)
     159       488764 : : m_logger (logger)
     160              : {
     161       488764 :   if (logger)
     162              :     {
     163          246 :       va_list ap;
     164          246 :       va_start (ap, fmt);
     165              : 
     166          246 :       logger->start_log_line ();
     167          246 :       logger->log_va_partial (fmt, &ap);
     168          246 :       logger->end_log_line ();
     169              : 
     170          246 :       logger->inc_indent ();
     171              : 
     172          246 :       va_end (ap);
     173              :     }
     174       488764 : }
     175              : 
     176              : 
     177              : /* The destructor for log_nesting_level; essentially the opposite of
     178              :    the constructor.  */
     179              : 
     180              : inline
     181       488764 : log_nesting_level::~log_nesting_level ()
     182              : {
     183       488764 :   if (m_logger)
     184          246 :     m_logger->dec_indent ();
     185       415609 : }
     186              : 
     187              : /* A log_user is something that potentially uses a logger (which could be
     188              :    nullptr).
     189              : 
     190              :    The log_user class keeps the reference-count of a logger up-to-date.  */
     191              : 
     192              : class log_user
     193              : {
     194              :  public:
     195              :   log_user (logger *logger);
     196              :   ~log_user ();
     197              : 
     198      8311432 :   logger * get_logger () const { return m_logger; }
     199              :   void set_logger (logger * logger);
     200              : 
     201              :   void log (const char *fmt, ...) const
     202              :     ATTRIBUTE_GCC_DIAG(2, 3);
     203              : 
     204              :   void start_log_line () const;
     205              :   void end_log_line () const;
     206              : 
     207              :   void enter_scope (const char *scope_name);
     208              :   void exit_scope (const char *scope_name);
     209              : 
     210              :   pretty_printer *get_logger_pp () const
     211              :   {
     212              :     gcc_assert (m_logger);
     213              :     return m_logger->get_printer ();
     214              :   }
     215              : 
     216         3428 :   FILE *get_logger_file () const
     217              :   {
     218         3423 :     if (m_logger == nullptr)
     219              :       return nullptr;
     220            5 :     return m_logger->get_file ();
     221              :   }
     222              : 
     223              :  private:
     224              :   DISABLE_COPY_AND_ASSIGN (log_user);
     225              : 
     226              :   logger *m_logger;
     227              : };
     228              : 
     229              : /* A shortcut for calling log from a log_user, handling the common
     230              :    case where the underlying logger is nullptr via a no-op.  */
     231              : 
     232              : inline void
     233        52981 : log_user::log (const char *fmt, ...) const
     234              : {
     235        52981 :   if (m_logger)
     236              :     {
     237          283 :       va_list ap;
     238          283 :       va_start (ap, fmt);
     239          283 :       m_logger->log_va (fmt, &ap);
     240          283 :       va_end (ap);
     241              :     }
     242        52981 : }
     243              : 
     244              : /* A shortcut for starting a log line from a log_user,
     245              :    handling the common case where the underlying logger is nullptr via
     246              :    a no-op.  */
     247              : 
     248              : inline void
     249              : log_user::start_log_line () const
     250              : {
     251              :   if (m_logger)
     252              :     m_logger->start_log_line ();
     253              : }
     254              : 
     255              : /* A shortcut for ending a log line from a log_user,
     256              :    handling the common case where the underlying logger is nullptr via
     257              :    a no-op.  */
     258              : 
     259              : inline void
     260              : log_user::end_log_line () const
     261              : {
     262              :   if (m_logger)
     263              :     m_logger->end_log_line ();
     264              : }
     265              : 
     266              : /* A shortcut for recording entry into a scope from a log_user,
     267              :    handling the common case where the underlying logger is nullptr via
     268              :    a no-op.  */
     269              : 
     270              : inline void
     271              : log_user::enter_scope (const char *scope_name)
     272              : {
     273              :   if (m_logger)
     274              :     m_logger->enter_scope (scope_name);
     275              : }
     276              : 
     277              : /* A shortcut for recording exit from a scope from a log_user,
     278              :    handling the common case where the underlying logger is nullptr via
     279              :    a no-op.  */
     280              : 
     281              : inline void
     282              : log_user::exit_scope (const char *scope_name)
     283              : {
     284              :   if (m_logger)
     285              :     m_logger->exit_scope (scope_name);
     286              : }
     287              : 
     288              : /* If the given logger is non-NULL, log entry/exit of this scope to
     289              :    it, identifying it using __PRETTY_FUNCTION__.  */
     290              : 
     291              : #define LOG_SCOPE(LOGGER)               \
     292              :   log_scope s (LOGGER, __PRETTY_FUNCTION__)
     293              : 
     294              : /* If the given logger is non-NULL, log entry/exit of this scope to
     295              :    it, identifying it using __func__.  */
     296              : 
     297              : #define LOG_FUNC(LOGGER) \
     298              :   log_scope s (LOGGER, __func__)
     299              : 
     300              : #define LOG_FUNC_1(LOGGER, FMT, A0)     \
     301              :   log_scope s (LOGGER, __func__, FMT, A0)
     302              : 
     303              : #define LOG_FUNC_2(LOGGER, FMT, A0, A1)         \
     304              :   log_scope s (LOGGER, __func__, FMT, A0, A1)
     305              : 
     306              : #define LOG_FUNC_3(LOGGER, FMT, A0, A1, A2)     \
     307              :   log_scope s (LOGGER, __func__, FMT, A0, A1, A2)
     308              : 
     309              : #define LOG_FUNC_4(LOGGER, FMT, A0, A1, A2, A3) \
     310              :   log_scope s (LOGGER, __func__, FMT, A0, A1, A2, A3)
     311              : 
     312              : } // namespace ana
     313              : 
     314              : #endif /* ANALYZER_LOGGING_H */
        

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.