public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Brace-balancing warning patch (vs. egcs-970917).
@ 1997-09-28 15:52 Paul Rusty Russell
  0 siblings, 0 replies; only message in thread
From: Paul Rusty Russell @ 1997-09-28 15:52 UTC (permalink / raw)
  To: gcc2; +Cc: egcs

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.

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-09-28 15:52 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-09-28 15:52 Brace-balancing warning patch (vs. egcs-970917) Paul Rusty Russell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).