LCOV - code coverage report
Current view: top level - gcc/jit - jit-logging.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 93.7 % 63 59
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 11 11
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Internals of libgccjit: logging
       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              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "toplev.h" /* for print_version */
      25              : 
      26              : #include "jit-logging.h"
      27              : 
      28              : namespace gcc {
      29              : 
      30              : namespace jit {
      31              : 
      32              : /* Implementation of class gcc::jit::logger.  */
      33              : 
      34              : /* The constructor for gcc::jit::logger, used by
      35              :    gcc_jit_context_set_logfile.  */
      36              : 
      37         1157 : logger::logger (FILE *f_out,
      38              :                 int, /* flags */
      39              :                 int /* verbosity */) :
      40         1157 :   m_refcount (0),
      41         1157 :   m_f_out (f_out),
      42         1157 :   m_indent_level (0),
      43         1157 :   m_log_refcount_changes (false)
      44              : {
      45              :   /* Begin the log by writing the GCC version.  */
      46         1157 :   print_version (f_out, "JIT:", false);
      47         1157 : }
      48              : 
      49              : /* The destructor for gcc::jit::logger, invoked via
      50              :    the decref method when the refcount hits zero.
      51              :    Note that we do not close the underlying FILE * (m_f_out).  */
      52              : 
      53         1157 : logger::~logger ()
      54              : {
      55              :   /* This should be the last message emitted.  */
      56         1157 :   log ("%s", __PRETTY_FUNCTION__);
      57         1157 :   gcc_assert (m_refcount == 0);
      58         1157 : }
      59              : 
      60              : /* Increment the reference count of the gcc::jit::logger.  */
      61              : 
      62              : void
      63       140340 : logger::incref (const char *reason)
      64              : {
      65       140340 :   m_refcount++;
      66       140340 :   if (m_log_refcount_changes)
      67            0 :     log ("%s: reason: %s refcount now %i ",
      68              :          __PRETTY_FUNCTION__, reason, m_refcount);
      69       140340 : }
      70              : 
      71              : /* Decrement the reference count of the gcc::jit::logger,
      72              :    deleting it if nothing is referring to it.  */
      73              : 
      74              : void
      75       140340 : logger::decref (const char *reason)
      76              : {
      77       140340 :   gcc_assert (m_refcount > 0);
      78       140340 :   --m_refcount;
      79       140340 :   if (m_log_refcount_changes)
      80            0 :     log ("%s: reason: %s refcount now %i",
      81              :          __PRETTY_FUNCTION__, reason, m_refcount);
      82       140340 :   if (m_refcount == 0)
      83         1157 :     delete this;
      84       140340 : }
      85              : 
      86              : /* Write a formatted message to the log, by calling the log_va method.  */
      87              : 
      88              : void
      89       295480 : logger::log (const char *fmt, ...)
      90              : {
      91       295480 :   va_list ap;
      92       295480 :   va_start (ap, fmt);
      93       295480 :   log_va (fmt, ap);
      94       295480 :   va_end (ap);
      95       295480 : }
      96              : 
      97              : /* Write an indented line to the log file.
      98              : 
      99              :    We explicitly flush after each line: if something crashes the process,
     100              :    we want the logfile/stream to contain the most up-to-date hint about the
     101              :    last thing that was happening, without it being hidden in an in-process
     102              :    buffer.  */
     103              : 
     104              : void
     105       333213 : logger::log_va (const char *fmt, va_list ap)
     106              : {
     107       333213 :   fprintf (m_f_out, "JIT: ");
     108      1117225 :   for (int i = 0; i < m_indent_level; i++)
     109       450799 :     fputc (' ', m_f_out);
     110       333213 :   vfprintf (m_f_out, fmt, ap);
     111       333213 :   fprintf (m_f_out, "\n");
     112       333213 :   fflush (m_f_out);
     113       333213 : }
     114              : 
     115              : /* Record the entry within a particular scope, indenting subsequent
     116              :    log lines accordingly.  */
     117              : 
     118              : void
     119       138739 : logger::enter_scope (const char *scope_name)
     120              : {
     121       138739 :   log ("entering: %s", scope_name);
     122       138739 :   m_indent_level += 1;
     123       138739 : }
     124              : 
     125              : /* Record the exit from a particular scope, restoring the indent level to
     126              :    before the scope was entered.  */
     127              : 
     128              : void
     129       138739 : logger::exit_scope (const char *scope_name)
     130              : {
     131       138739 :   if (m_indent_level)
     132       138739 :     m_indent_level -= 1;
     133              :   else
     134            0 :     log ("(mismatching indentation)");
     135       138739 :   log ("exiting: %s", scope_name);
     136       138739 : }
     137              : 
     138              : /* Implementation of class gcc::jit::log_user.  */
     139              : 
     140              : /* The constructor for gcc::jit::log_user.  */
     141              : 
     142         6017 : log_user::log_user (logger *logger) : m_logger (logger)
     143              : {
     144         6017 :   if (m_logger)
     145         1686 :     m_logger->incref("log_user ctor");
     146         6017 : }
     147              : 
     148              : /* The destructor for gcc::jit::log_user.  */
     149              : 
     150         5970 : log_user::~log_user ()
     151              : {
     152         5970 :   if (m_logger)
     153         2855 :     m_logger->decref("log_user dtor");
     154         5970 : }
     155              : 
     156              : /* Set the logger for a gcc::jit::log_user, managing the reference counts
     157              :    of the old and new logger (either of which might be NULL).  */
     158              : 
     159              : void
     160         1169 : log_user::set_logger (logger *logger)
     161              : {
     162         1169 :   if (logger)
     163         1169 :     logger->incref ("log_user::set_logger");
     164         1169 :   if (m_logger)
     165            0 :     m_logger->decref ("log_user::set_logger");
     166         1169 :   m_logger = logger;
     167         1169 : }
     168              : 
     169              : } // namespace gcc::jit
     170              : 
     171              : } // namespace gcc
        

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