This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Brace-balancing warning patch (vs. egcs-970917).


Hi all,

	Please tell me if this is the wrong way to do this.

This checks that the number of `{' == the number of `}' in a
compilation unit (C only; C++ patch in the works), and issues a
warning.

I don't know of _any_ real-world code that does this, and the tracking
down a missing } in a header file can often suck without this
assistance.  (Especially runaway extern "C" { ... } in C++).

Enjoy.
---------------------------------------------------------------- Changelog
Fri Sep 26 12:19:33 1997  Paul Russell  <Paul.Russell@RustCorp.com.au>

	* input.h: Added indent_level to struct file_stack to track { vs }.

	* c-lex.c (check_newline): Add {}-count & balance warning.

---------------------------------------------------------------- Patch
*** gcc/c-lex.c.~1~	Fri Aug 15 15:32:53 1997
--- gcc/c-lex.c	Sat Sep 27 11:34:57 1997
*************** char *token_buffer;	/* Pointer to token 
*** 118,123 ****
--- 118,125 ----
  			   Actual allocated length is maxtoken + 2.
  			   This is not static because objc-parse.y uses it.  */
  
+ static int indent_level = 0;        /* Number of { minus number of }. */
+ 
  /* Nonzero if end-of-file has been seen on input.  */
  static int end_of_file;
  
*************** linenum:
*** 747,752 ****
--- 749,755 ----
  	      input_file_stack->line = old_lineno;
  	      p->next = input_file_stack;
  	      p->name = input_filename;
+ 	      p->indent_level = indent_level;
  	      input_file_stack = p;
  	      input_file_stack_tick++;
  	      debug_start_source_file (input_filename);
*************** linenum:
*** 758,763 ****
--- 761,774 ----
  	      if (input_file_stack->next)
  		{
  		  struct file_stack *p = input_file_stack;
+ 		  if (indent_level != p->indent_level)
+ 		    {
+ 		      warning_with_file_and_line 
+ 			(p->name, old_lineno,
+ 			 "This file contains more `%c's than `%c's.",
+ 			 indent_level > p->indent_level ? '{' : '}',
+ 			 indent_level > p->indent_level ? '}' : '{');
+ 		    }
  		  input_file_stack = p->next;
  		  free (p);
  		  input_file_stack_tick++;
*************** yylex ()
*** 2122,2134 ****
  	      break;
  	    case '<':
  	      if (c1 == '%')
! 		{ value = '{'; goto done; }
  	      if (c1 == ':')
  		{ value = '['; goto done; }
  	      break;
  	    case '%':
  	      if (c1 == '>')
! 		{ value = '}'; goto done; }
  	      break;
  	    }
  	UNGETC (c1);
--- 2133,2145 ----
  	      break;
  	    case '<':
  	      if (c1 == '%')
! 		{ value = '{'; indent_level++; goto done; }
  	      if (c1 == ':')
  		{ value = '['; goto done; }
  	      break;
  	    case '%':
  	      if (c1 == '>')
! 		{ value = '}'; indent_level--; goto done; }
  	      break;
  	    }
  	UNGETC (c1);
*************** yylex ()
*** 2143,2148 ****
--- 2154,2169 ----
      case 0:
        /* Don't make yyparse think this is eof.  */
        value = 1;
+       break;
+ 
+     case '{':
+       indent_level++;
+       value = c;
+       break;
+ 
+     case '}':
+       indent_level--;
+       value = c;
        break;
  
      default:
*** gcc/input.h.~1~	Tue Aug 12 01:57:10 1997
--- gcc/input.h	Fri Sep 26 11:03:00 1997
*************** struct file_stack
*** 37,42 ****
--- 37,43 ----
      char *name;
      struct file_stack *next;
      int line;
+     int indent_level;
    };
  
  /* Stack of currently pending input files.
---------------------------------------------------------------- End Patch  

Rusty.
--
 .sig lost in the mail.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]