LCOV - code coverage report
Current view: top level - gcc/analyzer - integer.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.2 % 254 247
Test Date: 2026-11-14 16:39:50 Functions: 95.7 % 23 22
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Arbitrary precision integers.
       2              :    Copyright (C) 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 "analyzer/common.h"
      22              : 
      23              : #include "diagnostics/markup.h"
      24              : 
      25              : #include "selftest.h"
      26              : #include "selftest-json.h"
      27              : 
      28              : #include "analyzer/integer.h"
      29              : 
      30              : #if ENABLE_ANALYZER
      31              : 
      32              : namespace ana {
      33              : 
      34              : void
      35        10829 : integer::dump_to_pp (pretty_printer *pp) const
      36              : {
      37        10829 :   char *str = nullptr;
      38        10829 :   gmp_asprintf (&str, "%Zd", m_mpz);
      39        10829 :   gcc_assert (str);
      40              : 
      41        10829 :   pp_string (pp, str);
      42              : 
      43        10829 :   void (*free_func_ptr) (void *, size_t) = nullptr;
      44        10829 :   mp_get_memory_functions (nullptr, nullptr, &free_func_ptr);
      45        10829 :   gcc_assert (free_func_ptr);
      46        10829 :   free_func_ptr (str, 0);
      47        10829 : }
      48              : 
      49              : DEBUG_FUNCTION void
      50            0 : integer::dump () const
      51              : {
      52            0 :   pretty_printer pp;
      53            0 :   pp.set_output_stream (stderr);
      54            0 :   dump_to_pp (&pp);
      55            0 :   pp_newline (&pp);
      56            0 :   pp_flush (&pp);
      57            0 : }
      58              : 
      59              : std::unique_ptr<json::value>
      60          555 : integer::to_json () const
      61              : {
      62          555 :   pretty_printer pp;
      63          555 :   dump_to_pp (&pp);
      64          555 :   return std::make_unique<json::string> (pp_formatted_text (&pp));
      65          555 : }
      66              : 
      67              : std::unique_ptr<diagnostics::markup::node>
      68           16 : integer::make_markup_node () const
      69              : {
      70           16 :   pretty_printer pp;
      71           16 :   dump_to_pp (&pp);
      72           16 :   return diagnostics::markup::make_text_node (pp_formatted_text (&pp));
      73           16 : }
      74              : 
      75              : integer
      76       865358 : convert_from_integer_cst (const_tree int_cst)
      77              : {
      78       865358 :   gcc_assert (int_cst);
      79       865358 :   gcc_assert (TREE_CODE (int_cst) == INTEGER_CST);
      80              : 
      81       865358 :   integer result;
      82       865358 :   wi::to_mpz (wi::to_wide (int_cst),
      83       865358 :               result.access_mpz (),
      84       865358 :               TYPE_SIGN (TREE_TYPE (int_cst)));
      85       865358 :   return result;
      86              : }
      87              : 
      88              : tree
      89       134072 : make_integer_cst_from_integer (tree type, const integer &value, bool wrap)
      90              : {
      91       134072 :   gcc_assert (type);
      92              : 
      93              :   /* Need to make a copy, as wi::from_mpz modifies its argument.  */
      94       134072 :   integer mutable_copy (value);
      95       134072 :   wide_int w = wi::from_mpz (type, mutable_copy.access_mpz (), wrap);
      96       134072 :   return wide_int_to_tree (type, w);
      97       134072 : }
      98              : 
      99              : #if CHECKING_P
     100              : 
     101              : namespace selftest {
     102              : 
     103              : static void
     104            4 : test_rule_of_5 ()
     105              : {
     106            4 :   integer a;
     107            4 :   ASSERT_EQ (a, 0);
     108              : 
     109            4 :   integer b (42);
     110            4 :   ASSERT_EQ (b, 42);
     111              : 
     112            4 :   integer c (b);
     113            4 :   ASSERT_EQ (c, 42);
     114              :   // Check that writes to b don't affect c after the copy:
     115            4 :   b = 43;
     116            4 :   ASSERT_EQ (b, 43);
     117            4 :   ASSERT_EQ (c, 42);
     118              : 
     119            4 :   integer d (std::move (c));
     120            4 :   ASSERT_EQ (d, 42);
     121            4 :   ASSERT_EQ (c, 0);
     122              : 
     123            4 :   c = integer (1066);
     124            4 :   ASSERT_EQ (c, 1066);
     125              : 
     126            4 :   a = std::move (d);
     127            4 :   ASSERT_EQ (a, 42);
     128            4 : }
     129              : 
     130              : static void
     131            4 : test_comparisons ()
     132              : {
     133              :   /* Equality.  */
     134            4 :   ASSERT_TRUE (integer (0) == integer (0));
     135            4 :   ASSERT_FALSE (integer (0) == integer (1));
     136            8 :   ASSERT_TRUE (integer (0) == 0);
     137            4 :   ASSERT_FALSE (integer (0) == 42);
     138              : 
     139              :   /* Inequality.  */
     140            4 :   ASSERT_TRUE (integer (0) != integer (1));
     141            4 :   ASSERT_FALSE (integer (0) != integer (0));
     142            4 :   ASSERT_TRUE (integer (0) != 1);
     143            8 :   ASSERT_FALSE (integer (0) != 0);
     144              : 
     145              :   // operator<
     146            4 :   ASSERT_TRUE (integer (-1) < integer (0));
     147            4 :   ASSERT_FALSE (integer (0) < integer (0));
     148            4 :   ASSERT_TRUE (integer (2) < integer (3));
     149            4 :   ASSERT_FALSE (integer (3) < integer (2));
     150              : 
     151              :   // Default ctor should give zero:
     152            4 :   ASSERT_TRUE (integer () == integer (0));
     153            4 : }
     154              : 
     155              : static void
     156            4 : test_addition ()
     157              : {
     158            8 :   ASSERT_EQ (integer (1) + integer (2),
     159              :              integer (3));
     160              : 
     161              :   // Reflexivity
     162           12 :   ASSERT_EQ (integer (1) + integer (2),
     163              :              integer (2) + integer (1));
     164              : 
     165              :   // Commutativity
     166           12 :   ASSERT_EQ ((integer (1) + integer (2)) + integer (3),
     167              :              integer (6));
     168           12 :   ASSERT_EQ (integer (1) + (integer (2) + integer (3)),
     169              :              integer (6));
     170            4 : }
     171              : 
     172              : static void
     173            4 : test_subtraction ()
     174              : {
     175              :   // Identity
     176            8 :   ASSERT_EQ (integer (2) - integer (0),
     177              :              integer (2));
     178              : 
     179              :   // Positive result
     180            8 :   ASSERT_EQ (integer (11) - integer (7),
     181              :              integer (4));
     182              : 
     183              :   // Negative reult
     184            8 :   ASSERT_EQ (integer (5) - integer (10),
     185              :              integer (-5));
     186              : 
     187              :   // Negative operands
     188            8 :   ASSERT_EQ (integer (-2) - integer (2),
     189              :              integer (-4));
     190            8 :   ASSERT_EQ (integer (2) - integer (-2),
     191              :              integer (4));
     192            4 : }
     193              : 
     194              : static void
     195            4 : test_multiplication ()
     196              : {
     197              :   // Zero:
     198            8 :   ASSERT_EQ (integer (2) * integer (0),
     199              :              integer (0));
     200            8 :   ASSERT_EQ (integer (0) * integer (2),
     201              :              integer (0));
     202              : 
     203              :   // Identity
     204            8 :   ASSERT_EQ (integer (3) * integer (1),
     205              :              integer (3));
     206            8 :   ASSERT_EQ (integer (1) * integer (3),
     207              :              integer (3));
     208              : 
     209              :   // Combinations of positive and negative
     210            8 :   ASSERT_EQ (integer (2) * integer (3),
     211              :              integer (6));
     212            8 :   ASSERT_EQ (integer (4) * integer (-5),
     213              :              integer (-20));
     214            8 :   ASSERT_EQ (integer (-4) * integer (5),
     215              :              integer (-20));
     216            8 :   ASSERT_EQ (integer (-4) * integer (-5),
     217              :              integer (20));
     218            4 : }
     219              : 
     220              : static void
     221            4 : test_division ()
     222              : {
     223            4 :   ASSERT_EQ (integer (0).floor_div (10),
     224              :              integer (0));
     225            4 :   ASSERT_EQ (integer (24).floor_div (10),
     226              :              integer (2));
     227            4 :   ASSERT_EQ (integer (3).floor_div (1),
     228              :              integer (3));
     229              : 
     230            4 :   ASSERT_EQ (integer (-8).floor_div (10),
     231              :              integer (-1));
     232            4 : }
     233              : 
     234              : static void
     235            4 : test_modulus ()
     236              : {
     237            4 :   ASSERT_EQ (integer (0).floor_mod (10),
     238              :              integer (0));
     239            4 :   ASSERT_EQ (integer (24).floor_mod (10),
     240              :              integer (4));
     241            4 :   ASSERT_EQ (integer (3).floor_mod (1),
     242              :              integer (0));
     243              : 
     244            4 :   ASSERT_EQ (integer (-8).floor_mod (10),
     245              :              integer (2));
     246            4 : }
     247              : 
     248              : static void
     249            4 : test_dumping ()
     250              : {
     251              :   // Positive
     252            4 :   {
     253            4 :     pretty_printer pp;
     254            4 :     integer (42).dump_to_pp (&pp);
     255            4 :     ASSERT_STREQ (pp_formatted_text (&pp), "42");
     256            4 :   }
     257              : 
     258              :   // Negative
     259            4 :   {
     260            4 :     pretty_printer pp;
     261            4 :     integer (-100).dump_to_pp (&pp);
     262            4 :     ASSERT_STREQ (pp_formatted_text (&pp), "-100");
     263            4 :   }
     264            4 : }
     265              : 
     266              : static void
     267            4 : test_very_large ()
     268              : {
     269              :   /* Large power of two.  */
     270            4 :   {
     271            4 :     const integer two_to_the_12 (1 << 12);
     272            4 :     const integer two_to_the_24 = two_to_the_12 * two_to_the_12;
     273            4 :     const integer two_to_the_48 = two_to_the_24 * two_to_the_24;
     274            4 :     const integer two_to_the_96 = two_to_the_48 * two_to_the_48;
     275            4 :     const integer two_to_the_192 = two_to_the_96 * two_to_the_96;
     276            4 :     {
     277            4 :       pretty_printer pp;
     278            4 :       two_to_the_192.dump_to_pp (&pp);
     279            4 :       ASSERT_STREQ
     280              :         (pp_formatted_text (&pp),
     281              :          "6277101735386680763835789423207666416102355444464034512896");
     282            4 :     }
     283              :     // Negation
     284            4 :     {
     285            4 :       pretty_printer pp;
     286            4 :       (-two_to_the_192).dump_to_pp (&pp);
     287            4 :       ASSERT_STREQ
     288              :         (pp_formatted_text (&pp),
     289              :          "-6277101735386680763835789423207666416102355444464034512896");
     290            4 :     }
     291            4 :   }
     292              : 
     293              :   /* Large power of ten.  */
     294            4 :   {
     295            4 :     const integer million (1000 * 1000);
     296            4 :     const integer ten_to_the_12 = million * million;
     297            4 :     const integer ten_to_the_24 = ten_to_the_12 * ten_to_the_12;
     298            4 :     const integer ten_to_the_48 = ten_to_the_24 * ten_to_the_24;
     299            4 :     const integer ten_to_the_96 = ten_to_the_48 * ten_to_the_48;
     300            4 :     const integer ten_to_the_192 = ten_to_the_96 * ten_to_the_96;
     301            4 :     {
     302            4 :       pretty_printer pp;
     303            4 :       ten_to_the_192.dump_to_pp (&pp);
     304            4 :       ASSERT_STREQ
     305              :         (pp_formatted_text (&pp),
     306              :          ("1"
     307              :           "000000000000000000000000000000000000000000000000"
     308              :           "000000000000000000000000000000000000000000000000"
     309              :           "000000000000000000000000000000000000000000000000"
     310              :           "000000000000000000000000000000000000000000000000"));
     311            4 :     }
     312              : 
     313              :     // Negation
     314            4 :     {
     315            4 :       pretty_printer pp;
     316            4 :       (-ten_to_the_192).dump_to_pp (&pp);
     317            4 :       ASSERT_STREQ
     318              :         (pp_formatted_text (&pp),
     319              :          ("-1"
     320              :           "000000000000000000000000000000000000000000000000"
     321              :           "000000000000000000000000000000000000000000000000"
     322              :           "000000000000000000000000000000000000000000000000"
     323              :           "000000000000000000000000000000000000000000000000"));
     324            4 :     }
     325              : 
     326              :     // Addition
     327            4 :     {
     328            4 :       pretty_printer pp;
     329            8 :       (ten_to_the_192 + ten_to_the_192).dump_to_pp (&pp);
     330            4 :       ASSERT_STREQ
     331              :         (pp_formatted_text (&pp),
     332              :          ("2"
     333              :           "000000000000000000000000000000000000000000000000"
     334              :           "000000000000000000000000000000000000000000000000"
     335              :           "000000000000000000000000000000000000000000000000"
     336              :           "000000000000000000000000000000000000000000000000"));
     337            4 :     }
     338            4 :   }
     339            4 : }
     340              : 
     341              : static void
     342            4 : test_pp_element ()
     343              : {
     344            4 :   pretty_printer pp;
     345            4 :   pp_element_integer e1 (integer (42));
     346            4 :   pp_element_integer e2 (integer (-100));
     347            4 :   pp_printf (&pp, "positive: %e negative: %e", &e1, &e2);
     348            4 :   ASSERT_STREQ (pp_formatted_text (&pp), "positive: 42 negative: -100");
     349            4 : }
     350              : 
     351              : static integer
     352           40 : get_min_value (tree integral_type)
     353              : {
     354           40 :   gcc_assert (integral_type);
     355           40 :   return convert_from_integer_cst (TYPE_MIN_VALUE (integral_type));
     356              : }
     357              : 
     358              : static integer
     359           40 : get_max_value (tree integral_type)
     360              : {
     361           40 :   gcc_assert (integral_type);
     362           40 :   return convert_from_integer_cst (TYPE_MAX_VALUE (integral_type));
     363              : }
     364              : 
     365              : static void
     366            4 : test_convert_from_integer_cst ()
     367              : {
     368            4 :   ASSERT_TRUE (get_min_value (signed_char_type_node) < 0);
     369            4 :   ASSERT_TRUE (get_max_value (signed_char_type_node) > 0);
     370              : 
     371            8 :   ASSERT_TRUE (get_min_value (unsigned_char_type_node) == 0);
     372            4 :   ASSERT_TRUE (get_max_value (unsigned_char_type_node) > 0);
     373              : 
     374            4 :   ASSERT_TRUE (get_min_value (integer_type_node) < 0);
     375            4 :   ASSERT_TRUE (get_max_value (integer_type_node) > 0);
     376            8 :   ASSERT_EQ (convert_from_integer_cst (integer_zero_node), 0);
     377            4 :   ASSERT_EQ (convert_from_integer_cst (integer_one_node), 1);
     378            4 :   ASSERT_EQ (convert_from_integer_cst (integer_minus_one_node), -1);
     379              : 
     380            8 :   ASSERT_TRUE (get_min_value (unsigned_type_node) == 0);
     381            4 :   ASSERT_TRUE (get_max_value (unsigned_type_node) > 0);
     382              : 
     383            4 :   ASSERT_TRUE (get_min_value (long_integer_type_node) < 0);
     384            4 :   ASSERT_TRUE (get_max_value (long_integer_type_node) > 0);
     385              : 
     386            8 :   ASSERT_TRUE (get_min_value (long_unsigned_type_node) == 0);
     387            4 :   ASSERT_TRUE (get_max_value (long_unsigned_type_node) > 0);
     388              : 
     389            4 :   ASSERT_TRUE (get_min_value (long_long_integer_type_node) < 0);
     390            4 :   ASSERT_TRUE (get_max_value (long_long_integer_type_node) > 0);
     391              : 
     392            8 :   ASSERT_TRUE (get_min_value (long_long_unsigned_type_node) == 0);
     393            4 :   ASSERT_TRUE (get_max_value (long_long_unsigned_type_node) > 0);
     394              : 
     395            8 :   ASSERT_TRUE (get_min_value (size_type_node) == 0);
     396            4 :   ASSERT_TRUE (get_max_value (size_type_node) > 0);
     397            8 :   ASSERT_EQ (convert_from_integer_cst (size_zero_node), 0);
     398            4 :   ASSERT_EQ (convert_from_integer_cst (size_one_node), 1);
     399              : 
     400            4 :   ASSERT_TRUE (get_min_value (ptrdiff_type_node) < 0);
     401            4 :   ASSERT_TRUE (get_max_value (ptrdiff_type_node) > 0);
     402            4 : }
     403              : 
     404              : // Verify that INT_CST round-trips through integer.
     405              : 
     406              : static void
     407          100 : assert_round_trip (const ::selftest::location &loc, tree int_cst)
     408              : {
     409          100 :   integer as_integer = convert_from_integer_cst (int_cst);
     410          100 :   tree round_tripped_cst = make_integer_cst_from_integer (TREE_TYPE (int_cst),
     411              :                                                           as_integer, false);
     412          100 :   ASSERT_EQ_AT (loc, TREE_TYPE (int_cst), TREE_TYPE (round_tripped_cst));
     413          100 :   ASSERT_TRUE_AT (loc, tree_int_cst_equal (int_cst, round_tripped_cst));
     414          100 : }
     415              : 
     416              : #define ASSERT_ROUND_TRIP(INT_CST)      \
     417              :   SELFTEST_BEGIN_STMT                                                   \
     418              :   assert_round_trip ((SELFTEST_LOCATION), (INT_CST));                   \
     419              :   SELFTEST_END_STMT
     420              : 
     421              : static void
     422            4 : test_round_trip_of_integer_cst ()
     423              : {
     424            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (signed_char_type_node));
     425            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (signed_char_type_node));
     426              : 
     427            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (unsigned_char_type_node));
     428            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (unsigned_char_type_node));
     429              : 
     430            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (integer_type_node));
     431            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (integer_type_node));
     432            4 :   ASSERT_ROUND_TRIP (integer_zero_node);
     433            4 :   ASSERT_ROUND_TRIP (integer_one_node);
     434            4 :   ASSERT_ROUND_TRIP (integer_minus_one_node);
     435              : 
     436            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (unsigned_type_node));
     437            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (unsigned_type_node));
     438              : 
     439            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (long_integer_type_node));
     440            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (long_integer_type_node));
     441              : 
     442            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (long_unsigned_type_node));
     443            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (long_unsigned_type_node));
     444              : 
     445            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (long_long_integer_type_node));
     446            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (long_long_integer_type_node));
     447              : 
     448            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (long_long_unsigned_type_node));
     449            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (long_long_unsigned_type_node));
     450              : 
     451            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (size_type_node));
     452            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (size_type_node));
     453            4 :   ASSERT_ROUND_TRIP (size_zero_node);
     454            4 :   ASSERT_ROUND_TRIP (size_one_node);
     455              : 
     456            4 :   ASSERT_ROUND_TRIP (TYPE_MIN_VALUE (ptrdiff_type_node));
     457            4 :   ASSERT_ROUND_TRIP (TYPE_MAX_VALUE (ptrdiff_type_node));
     458            4 : }
     459              : 
     460              : static void
     461            4 : test_to_json ()
     462              : {
     463            4 :   ASSERT_JSON_STRING_EQ (integer (42).to_json ().get (),
     464              :                          "42");
     465            4 : }
     466              : 
     467              : /* Run all of the selftests within this file.  */
     468              : 
     469              : void
     470            4 : analyzer_integer_cc_tests ()
     471              : {
     472            4 :   test_rule_of_5 ();
     473            4 :   test_comparisons ();
     474            4 :   test_addition ();
     475            4 :   test_subtraction ();
     476            4 :   test_multiplication ();
     477            4 :   test_division ();
     478            4 :   test_modulus ();
     479            4 :   test_dumping ();
     480            4 :   test_very_large ();
     481            4 :   test_pp_element ();
     482            4 :   test_convert_from_integer_cst ();
     483            4 :   test_round_trip_of_integer_cst ();
     484            4 :   test_to_json ();
     485            4 : }
     486              : 
     487              : } // namespace selftest
     488              : 
     489              : #endif /* CHECKING_P */
     490              : 
     491              : } // namespace ana
     492              : 
     493              : #endif /* #if ENABLE_ANALYZER */
        

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