diff mbox

[2/9] ENABLE_CHECKING refactoring: libcpp

Message ID 561307B6.7000707@gmail.com
State New
Headers show

Commit Message

Mikhail Maltsev Oct. 5, 2015, 11:28 p.m. UTC
libcpp/ChangeLog:

2015-10-05  Mikhail Maltsev  <maltsevm@gmail.com>

	* include/line-map.h: Fix use of ENABLE_CHECKING.
	* init.c: Likewise.
	* macro.c (struct macro_arg_token_iter, set_arg_token,
	macro_arg_token_iter_init, macro_arg_token_iter_forward,
	macro_arg_token_iter_get_token, macro_arg_token_iter_get_location,
	alloc_expanded_arg_mem, _cpp_backup_tokens): Likewise.
	* system.h (gcc_assert): Define.

Comments

Bernd Schmidt Oct. 6, 2015, 12:40 p.m. UTC | #1
I'm not entirely sure what to make of this series. There seem to be good 
bits in there but also some things I find questionable. I'll add some 
comments on things that occur to me.

On 10/06/2015 01:28 AM, Mikhail Maltsev wrote:
>
> 	* include/line-map.h: Fix use of ENABLE_CHECKING.

Fix how? What's wrong with it?

>   /* Sanity-checks are dependent on command-line options, so it is
>      called as a subroutine of cpp_read_main_file ().  */
> -#if ENABLE_CHECKING
> +#if CHECKING_P
>   static void sanity_checks (cpp_reader *);
>   static void sanity_checks (cpp_reader *pfile)
>   {

Ok, this one seems to be a real problem (should have been #ifdef), but...

> -#ifdef ENABLE_CHECKING
> +#if CHECKING_P

I fail to see the point of this change.

> -#ifdef ENABLE_CHECKING
> -      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
> -	  || !track_macro_exp_p)
> -	/* We can't set the location of a stringified argument
> -	   token and we can't set any location if we aren't tracking
> -	   macro expansion locations.   */
> -	abort ();
> -#endif
> +      /* We can't set the location of a stringified argument
> +	 token and we can't set any location if we aren't tracking
> +	 macro expansion locations.   */
> +      gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
> +			   && track_macro_exp_p);

This kind of change seems good. I think the patch series would benefit 
if it was separated thematically rather than by sets of files. I.e., 
merge all changes like this one into one patch or maybe a few if it 
grows too large.

> +/* Redefine abort to report an internal error w/o coredump, and
> +   reporting the location of the error in the source file.  */
> +extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
> +#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
> +
> +/* Use gcc_assert(EXPR) to test invariants.  */
> +#if ENABLE_ASSERT_CHECKING
> +#define gcc_assert(EXPR) 						\
> +   ((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
> +#elif (GCC_VERSION >= 4005)
> +#define gcc_assert(EXPR) 						\
> +  ((void)(__builtin_expect (!(EXPR), 0) ? __builtin_unreachable (), 0 : 0))
> +#else
> +/* Include EXPR, so that unused variable warnings do not occur.  */
> +#define gcc_assert(EXPR) ((void)(0 && (EXPR)))
> +#endif

Probably a good thing, but it looks like libcpp has grown its own 
variant linemap_assert; we should check whether that can be replaced.

Also, the previous patch already introduces a use of gcc_assert, or at 
least a reference to it, and it's only defined here. The two 
modifications of libcpp/system.h should probably be merged into one.


Bernd
Jeff Law Oct. 12, 2015, 8:57 p.m. UTC | #2
On 10/06/2015 06:40 AM, Bernd Schmidt wrote:
> I'm not entirely sure what to make of this series. There seem to be good
> bits in there but also some things I find questionable. I'll add some
> comments on things that occur to me.
Maybe we should start pulling out the bits that we think are ready & 
good and start installing them independently.

I'm less concerned about getting conditional compilation out of the lib* 
directories right now than I am the core of the compiler.  So I wouldn't 
lose any sleep if we extracted the obviously good bits for libcpp tested 
& installed those, then table the rest of the libcpp stuff and focused 
on the core compiler.

Thoughts?


>
> On 10/06/2015 01:28 AM, Mikhail Maltsev wrote:
>>
>>     * include/line-map.h: Fix use of ENABLE_CHECKING.
>
> Fix how? What's wrong with it?
>
>>   /* Sanity-checks are dependent on command-line options, so it is
>>      called as a subroutine of cpp_read_main_file ().  */
>> -#if ENABLE_CHECKING
>> +#if CHECKING_P
>>   static void sanity_checks (cpp_reader *);
>>   static void sanity_checks (cpp_reader *pfile)
>>   {
>
> Ok, this one seems to be a real problem (should have been #ifdef), but...
Agreed.

>
>> -#ifdef ENABLE_CHECKING
>> +#if CHECKING_P
>
> I fail to see the point of this change.
I'm guessing (and Mikhail, please correct me if I'm wrong), but I think 
he's trying to get away from ENABLE_CHECKING and instead use a macro 
which is always defined to a value.


>
>> -#ifdef ENABLE_CHECKING
>> -      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
>> -      || !track_macro_exp_p)
>> -    /* We can't set the location of a stringified argument
>> -       token and we can't set any location if we aren't tracking
>> -       macro expansion locations.   */
>> -    abort ();
>> -#endif
>> +      /* We can't set the location of a stringified argument
>> +     token and we can't set any location if we aren't tracking
>> +     macro expansion locations.   */
>> +      gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
>> +               && track_macro_exp_p);
>
> This kind of change seems good. I think the patch series would benefit
> if it was separated thematically rather than by sets of files. I.e.,
> merge all changes like this one into one patch or maybe a few if it
> grows too large.
That would work for me.  I just asked Mikhail to try and break down the 
monster patch into something that could be digested.  Ultimately my goal 
was to make it possible to start reviewing and installing these bits and 
keep making progress on removing the conditionally compiled code.

>
>> +/* Redefine abort to report an internal error w/o coredump, and
>> +   reporting the location of the error in the source file.  */
>> +extern void fancy_abort (const char *, int, const char *)
>> ATTRIBUTE_NORETURN;
>> +#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
>> +
>> +/* Use gcc_assert(EXPR) to test invariants.  */
>> +#if ENABLE_ASSERT_CHECKING
>> +#define gcc_assert(EXPR)                         \
>> +   ((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__),
>> 0 : 0))
>> +#elif (GCC_VERSION >= 4005)
>> +#define gcc_assert(EXPR)                         \
>> +  ((void)(__builtin_expect (!(EXPR), 0) ? __builtin_unreachable (), 0
>> : 0))
>> +#else
>> +/* Include EXPR, so that unused variable warnings do not occur.  */
>> +#define gcc_assert(EXPR) ((void)(0 && (EXPR)))
>> +#endif
>
> Probably a good thing, but it looks like libcpp has grown its own
> variant linemap_assert; we should check whether that can be replaced.
>
> Also, the previous patch already introduces a use of gcc_assert, or at
> least a reference to it, and it's only defined here. The two
> modifications of libcpp/system.h should probably be merged into one.
Agreed.

jeff
Jeff Law Oct. 21, 2015, 9:19 p.m. UTC | #3
On 10/06/2015 06:40 AM, Bernd Schmidt wrote:
> I'm not entirely sure what to make of this series. There seem to be good
> bits in there but also some things I find questionable. I'll add some
> comments on things that occur to me.
>
> On 10/06/2015 01:28 AM, Mikhail Maltsev wrote:
>>
>>     * include/line-map.h: Fix use of ENABLE_CHECKING.
>
> Fix how? What's wrong with it?
I think it was just a poor choice of words for the ChangeLog.


> Probably a good thing, but it looks like libcpp has grown its own
> variant linemap_assert; we should check whether that can be replaced.
Agreed.  Let's push it onto the queue of things to clean up.

jeff
diff mbox

Patch

From e7e3856c2c748cf0c7ed35ed843ec4ce516c9b4f Mon Sep 17 00:00:00 2001
From: Mikhail Maltsev <maltsevm@gmail.com>
Date: Tue, 22 Sep 2015 02:51:31 +0300
Subject: [PATCH 2/9] libcpp

---
 libcpp/include/line-map.h |  2 +-
 libcpp/init.c             |  2 +-
 libcpp/macro.c            | 38 +++++++++++++++-----------------------
 libcpp/system.h           | 17 +++++++++++++++++
 4 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/libcpp/include/line-map.h b/libcpp/include/line-map.h
index bc747c1..e718fc2 100644
--- a/libcpp/include/line-map.h
+++ b/libcpp/include/line-map.h
@@ -272,7 +272,7 @@  struct GTY((tag ("2"))) line_map_macro : public line_map {
   source_location expansion;
 };
 
-#if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
+#if CHECKING_P && (GCC_VERSION >= 2007)
 
 /* Assertion macro to be used in line-map code.  */
 #define linemap_assert(EXPR)                  \
diff --git a/libcpp/init.c b/libcpp/init.c
index 2d5626f..0419e95 100644
--- a/libcpp/init.c
+++ b/libcpp/init.c
@@ -535,7 +535,7 @@  cpp_init_builtins (cpp_reader *pfile, int hosted)
 
 /* Sanity-checks are dependent on command-line options, so it is
    called as a subroutine of cpp_read_main_file ().  */
-#if ENABLE_CHECKING
+#if CHECKING_P
 static void sanity_checks (cpp_reader *);
 static void sanity_checks (cpp_reader *pfile)
 {
diff --git a/libcpp/macro.c b/libcpp/macro.c
index 786c21b..60a0753 100644
--- a/libcpp/macro.c
+++ b/libcpp/macro.c
@@ -73,7 +73,7 @@  struct macro_arg_token_iter
      -ftrack-macro-expansion is used this location tracks loci across
      macro expansion.  */
   const source_location *location_ptr;
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   /* The number of times the iterator went forward. This useful only
      when checking is enabled.  */
   size_t num_forwards;
@@ -1310,14 +1310,11 @@  set_arg_token (macro_arg *arg, const cpp_token *token,
 
   if (loc != NULL)
     {
-#ifdef ENABLE_CHECKING
-      if (kind == MACRO_ARG_TOKEN_STRINGIFIED
-	  || !track_macro_exp_p)
-	/* We can't set the location of a stringified argument
-	   token and we can't set any location if we aren't tracking
-	   macro expansion locations.   */
-	abort ();
-#endif
+      /* We can't set the location of a stringified argument
+	 token and we can't set any location if we aren't tracking
+	 macro expansion locations.   */
+      gcc_checking_assert (kind != MACRO_ARG_TOKEN_STRINGIFIED
+			   && track_macro_exp_p);
       *loc = location;
     }
 }
@@ -1403,7 +1400,7 @@  macro_arg_token_iter_init (macro_arg_token_iter *iter,
   iter->location_ptr = NULL;
   if (track_macro_exp_p)
     iter->location_ptr = get_arg_token_location (arg, kind);
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   iter->num_forwards = 0;
   if (track_macro_exp_p
       && token_ptr != NULL
@@ -1428,14 +1425,14 @@  macro_arg_token_iter_forward (macro_arg_token_iter *it)
 	it->location_ptr++;
       break;
     case MACRO_ARG_TOKEN_STRINGIFIED:
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
       if (it->num_forwards > 0)
 	abort ();
 #endif
       break;
     }
 
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   it->num_forwards++;
 #endif
 }
@@ -1444,7 +1441,7 @@  macro_arg_token_iter_forward (macro_arg_token_iter *it)
 static const cpp_token *
 macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
 {
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
       && it->num_forwards > 0)
     abort ();
@@ -1458,7 +1455,7 @@  macro_arg_token_iter_get_token (const macro_arg_token_iter *it)
 static source_location
 macro_arg_token_iter_get_location (const macro_arg_token_iter *it)
 {
-#ifdef ENABLE_CHECKING
+#if CHECKING_P
   if (it->kind == MACRO_ARG_TOKEN_STRINGIFIED
       && it->num_forwards > 0)
     abort ();
@@ -2144,11 +2141,9 @@  tokens_buff_add_token (_cpp_buff *buffer,
 static void
 alloc_expanded_arg_mem (cpp_reader *pfile, macro_arg *arg, size_t capacity)
 {
-#ifdef ENABLE_CHECKING
-  if (arg->expanded != NULL
-      || arg->expanded_virt_locs != NULL)
-    abort ();
-#endif
+  gcc_checking_assert (arg->expanded == NULL
+		       && arg->expanded_virt_locs == NULL);
+
   arg->expanded = XNEWVEC (const cpp_token *, capacity);
   if (CPP_OPTION (pfile, track_macro_expansion))
     arg->expanded_virt_locs = XNEWVEC (source_location, capacity);
@@ -2709,10 +2704,7 @@  _cpp_backup_tokens (cpp_reader *pfile, unsigned int count)
 	    {
 	      macro_context *m = pfile->context->c.mc;
 	      m->cur_virt_loc--;
-#ifdef ENABLE_CHECKING
-	      if (m->cur_virt_loc < m->virt_locs)
-		abort ();
-#endif
+	      gcc_checking_assert (m->cur_virt_loc >= m->virt_locs);
 	    }
 	  else
 	    abort ();
diff --git a/libcpp/system.h b/libcpp/system.h
index e070bc9..aa336b4 100644
--- a/libcpp/system.h
+++ b/libcpp/system.h
@@ -391,6 +391,23 @@  extern void abort (void);
 #define __builtin_expect(a, b) (a)
 #endif
 
+/* Redefine abort to report an internal error w/o coredump, and
+   reporting the location of the error in the source file.  */
+extern void fancy_abort (const char *, int, const char *) ATTRIBUTE_NORETURN;
+#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)
+
+/* Use gcc_assert(EXPR) to test invariants.  */
+#if ENABLE_ASSERT_CHECKING
+#define gcc_assert(EXPR) 						\
+   ((void)(!(EXPR) ? fancy_abort (__FILE__, __LINE__, __FUNCTION__), 0 : 0))
+#elif (GCC_VERSION >= 4005)
+#define gcc_assert(EXPR) 						\
+  ((void)(__builtin_expect (!(EXPR), 0) ? __builtin_unreachable (), 0 : 0))
+#else
+/* Include EXPR, so that unused variable warnings do not occur.  */
+#define gcc_assert(EXPR) ((void)(0 && (EXPR)))
+#endif
+
 #ifdef ENABLE_CHECKING
 #define gcc_checking_assert(EXPR) gcc_assert (EXPR)
 #define CHECKING_P 1
-- 
2.1.4