LCOV - code coverage report
Current view: top level - gcc/c-family - c-spellcheck.cc (source / functions) Hit Total Coverage
Test: gcc.info Lines: 30 30 100.0 %
Date: 2020-03-28 11:57:23 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* Find near-matches for macros.
       2                 :            :    Copyright (C) 2016-2020 Free Software Foundation, Inc.
       3                 :            : 
       4                 :            : This file is part of GCC.
       5                 :            : 
       6                 :            : GCC is free software; you can redistribute it and/or modify it under
       7                 :            : the terms of the GNU General Public License as published by the Free
       8                 :            : Software Foundation; either version 3, or (at your option) any later
       9                 :            : version.
      10                 :            : 
      11                 :            : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12                 :            : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13                 :            : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14                 :            : for more details.
      15                 :            : 
      16                 :            : You should have received a copy of the GNU General Public License
      17                 :            : along with GCC; see the file COPYING3.  If not see
      18                 :            : <http://www.gnu.org/licenses/>.  */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : #include "system.h"
      22                 :            : #include "coretypes.h"
      23                 :            : #include "tm.h"
      24                 :            : #include "tree.h"
      25                 :            : #include "cpplib.h"
      26                 :            : #include "spellcheck-tree.h"
      27                 :            : #include "c-family/c-spellcheck.h"
      28                 :            : #include "selftest.h"
      29                 :            : 
      30                 :            : /* Return true iff STR begin with an underscore and either an uppercase
      31                 :            :    letter or another underscore, and is thus, for C and C++, reserved for
      32                 :            :    use by the implementation.  */
      33                 :            : 
      34                 :            : bool
      35                 :    9507920 : name_reserved_for_implementation_p (const char *str)
      36                 :            : {
      37                 :    9507920 :   if (str[0] != '_')
      38                 :            :     return false;
      39                 :    9091130 :   return (str[1] == '_' || ISUPPER(str[1]));
      40                 :            : }
      41                 :            : 
      42                 :            : /* Return true iff HASHNODE is a macro that should be offered as a
      43                 :            :    suggestion for a misspelling.  */
      44                 :            : 
      45                 :            : static bool
      46                 :   15793900 : should_suggest_as_macro_p (cpp_hashnode *hashnode)
      47                 :            : {
      48                 :   15793900 :   if (!cpp_macro_p (hashnode))
      49                 :            :     return false;
      50                 :            : 
      51                 :            :   /* Don't suggest names reserved for the implementation, but do
      52                 :            :      suggest the builtin macros such as __FILE__, __LINE__ etc.  */
      53                 :    2644760 :   if (cpp_user_macro_p (hashnode)
      54                 :    2644760 :       && name_reserved_for_implementation_p ((const char *)hashnode->ident.str))
      55                 :    2329110 :     return false;
      56                 :            : 
      57                 :            :   return true;
      58                 :            : }
      59                 :            : 
      60                 :            : /* A callback for cpp_forall_identifiers, for use by best_macro_match's ctor.
      61                 :            :    Process HASHNODE and update the best_macro_match instance pointed to be
      62                 :            :    USER_DATA.  */
      63                 :            : 
      64                 :            : static int
      65                 :   15793900 : find_closest_macro_cpp_cb (cpp_reader *, cpp_hashnode *hashnode,
      66                 :            :                            void *user_data)
      67                 :            : {
      68                 :   15793900 :   if (!should_suggest_as_macro_p (hashnode))
      69                 :            :     return 1;
      70                 :            : 
      71                 :     315650 :   best_macro_match *bmm = (best_macro_match *)user_data;
      72                 :     315650 :   bmm->consider (hashnode);
      73                 :            : 
      74                 :            :   /* Keep iterating.  */
      75                 :     315650 :   return 1;
      76                 :            : }
      77                 :            : 
      78                 :            : /* Constructor for best_macro_match.
      79                 :            :    Use find_closest_macro_cpp_cb to find the closest matching macro to
      80                 :            :    NAME within distance < best_distance_so_far. */
      81                 :            : 
      82                 :       5377 : best_macro_match::best_macro_match (tree goal,
      83                 :            :                                     edit_distance_t best_distance_so_far,
      84                 :            :                                     cpp_reader *reader)
      85                 :       5377 : : best_match <goal_t, candidate_t> (goal, best_distance_so_far)
      86                 :            : {
      87                 :       5377 :   cpp_forall_identifiers (reader, find_closest_macro_cpp_cb, this);
      88                 :       5377 : }
      89                 :            : 
      90                 :            : #if CHECKING_P
      91                 :            : 
      92                 :            : namespace selftest {
      93                 :            : 
      94                 :            : /* Selftests.  */
      95                 :            : 
      96                 :            : /* Verify that name_reserved_for_implementation_p is sane.  */
      97                 :            : 
      98                 :            : static void
      99                 :          2 : test_name_reserved_for_implementation_p ()
     100                 :            : {
     101                 :          2 :   ASSERT_FALSE (name_reserved_for_implementation_p (""));
     102                 :          2 :   ASSERT_FALSE (name_reserved_for_implementation_p ("foo"));
     103                 :          2 :   ASSERT_FALSE (name_reserved_for_implementation_p ("_"));
     104                 :          2 :   ASSERT_FALSE (name_reserved_for_implementation_p ("_foo"));
     105                 :          2 :   ASSERT_FALSE (name_reserved_for_implementation_p ("_42"));
     106                 :          2 :   ASSERT_TRUE (name_reserved_for_implementation_p ("_Foo"));
     107                 :          2 :   ASSERT_TRUE (name_reserved_for_implementation_p ("__"));
     108                 :          2 :   ASSERT_TRUE (name_reserved_for_implementation_p ("__foo"));
     109                 :          2 : }
     110                 :            : 
     111                 :            : /* Run all of the selftests within this file.  */
     112                 :            : 
     113                 :            : void
     114                 :          2 : c_spellcheck_cc_tests ()
     115                 :            : {
     116                 :          2 :   test_name_reserved_for_implementation_p ();
     117                 :          2 : }
     118                 :            : 
     119                 :            : } // namespace selftest
     120                 :            : 
     121                 :            : #endif /* #if CHECKING_P */

Generated by: LCOV version 1.0

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 --enable-host-shared. GCC test suite is run with the built compiler.