summaryrefslogtreecommitdiff
path: root/glafp-utils/etags/wells-fixes
blob: 242e4e1002315160223ec5521376a4c4fc63d706 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
From jbw@bigbird.bu.edu Sun Apr  5 14:58:19 1992
From: jbw@bigbird.bu.edu (Joe Wells)
Newsgroups: gnu.emacs.bug
Subject: numerous bug fixes for etags
Date: 3 Apr 92 19:46:52 GMT
Distribution: gnu
Organization: GNUs Not Usenet

Enclosed is a patch with numerous changes for etags.  The following are
the changes and bugs fixed:

1. Improvement: don't waste time calling strncmp (with "struct", "union",
   "enum", "typedef") if the lengths are different.

2. Bug: pfnote placed a NUL in the line buffer after the tag token which caused
   things like `{' or `/*' to be ignored, thus severely screwing up the
   parser.  Or it did something horrible, I don't remember for sure.

3. Improvement: record defining occurrences of struct/union/enum tags.
   This works even if combined with a typedef definition, for example:

     typedef struct XXX {
       .....
     } YYY;

4. Bug: when a tag token was the last item on the line before the newline
   character, garbage would be seen as the token.  This is because when a
   NUL was seen in the buffer the buffer was replaced with the next line.

5. Bug: tag tokens immediately followed by a `{' with no intervening space
   were not recorded.  This was only a problem because of improvement 3
   above.

6. Bug: a newline in the middle of a comment zeroed the `number' variable.
   (Just for good measure I made it not zero `number' in strings even when
   the newline is not preceded by `\', in case someone wants to run etags
   on illegal code :-)  (`number' is used only on lines that begin with
   `#'.)

7. Bug: handling of #define lines was severely broken.  I don't actually
   remember what etags did with them, but it was really bad.  It now
   records macro definitions.

8. Bug: when a tag token was the last item on the line except for
   whitespace, etags would replace the contents of the line buffer and
   then later do various string comparisons and inspections against
   garbage values instead of against the token.  Fixing this required
   copying the token into a buffer.  (This is roughly the same as bug 4
   above, but in a different function.)

9. Bug: when a tag token was the last item on the line before the newline
   (and under various other circumstances), etags would skip over the NUL
   in the buffer and skip to the first non-whitespace character in the
   buffer.

10. Improvement (possibly bug fix): parse typedefs even when we aren't
    going to print them out.  I seem to remember that this fixed some bug,
    but I don't remember the specific case that would trigger the bug.

11. An unfinished attempt to detect and record global variable
    definitions.

The changes are to the 18.57 version of etags, but the only change in
18.58 to the C code handling is to initialize some variables when starting
on a new file, so these changes are orthogonal.

-- 
Enjoy,

Joe Wells <jbw@cs.bu.edu>

Member of the League for Programming Freedom --- send e-mail for details

----------------------------------------------------------------------
--- etags.c-dist	Tue Jan  8 14:08:38 1991
+++ etags.c	Sat Apr  4 00:41:22 1992
@@ -62,7 +62,10 @@
 
 /* cause token checking for typedef, struct, union, enum to distinguish
    keywords from identifier-prefixes (e.g. struct vs struct_tag).  */
-#define istoken(s, tok, len) (!strncmp(s,tok,len) && endtoken(*((s)+(len))))
+#define istoken(s, t, len) \
+  (((len) == (sizeof (t) -1)) && \
+   ((strncmp(s, t, len)) == 0) && \
+   (endtoken(*((s)+(len)))))
 
 struct	nd_st {			/* sorting structure			*/
 	char	*name;			/* function or type name	*/
@@ -505,6 +508,7 @@
 {
   register char *fp;
   register NODE *np;
+  char save;
   char *altname;
   char tem[51];
 
@@ -538,6 +542,7 @@
   np->left = np->right = 0;
   if (eflag)
     {
+      save = linestart[linelen];
       linestart[linelen] = 0;
     }
   else if (xflag == 0)
@@ -546,6 +551,7 @@
       linestart = tem;
     }
   np->pat = savestr (linestart);
+  linestart[linelen] = save;
   if (head == NULL)
     head = np;
   else
@@ -725,6 +731,17 @@
   number = 0; \
 }
 
+/* These two are part of a never-finished attempt to record global */
+/* variable definitions.  This is nearly impossible in C without the full */
+/* power of a C compiler due to C's stupid grammar. */
+logical infunc;
+int idents_in_decl;
+
+/* indicates whether the next token (if any) is the tag corresponding to */
+/* `struct', `union', or `enum' */
+logical next_token_is_tag;
+
+
 C_entries ()
 {
   register int c;
@@ -731,6 +748,15 @@
   register char *token, *tp, *lp;
   logical incomm, inquote, inchar, midtoken;
   int level;
+
+  /* there are certain things that must be done when the end of line is */
+  /* encountered, but they must be delayed until after other things are */
+  /* done. */
+  logical new_line_flag;
+
+  /* same as new_line_flag for left braces. */
+  logical left_brace_flag;
+
   char tok[BUFSIZ];
 
   lineno = 0;
@@ -739,17 +765,22 @@
   *lp = 0;
 
   number = 0;
-  gotone = midtoken = inquote = inchar = incomm = FALSE;
+  gotone = midtoken = inquote = inchar = incomm = infunc = FALSE;
+  new_line_flag = FALSE;
+  left_brace_flag = FALSE;
   level = 0;
+  idents_in_decl = 0;
+  next_token_is_tag = FALSE;
 
   while (!feof (inf))
     {
       c = *lp++;
       if (c == 0)
-	{
-	  CNL;
-	  gotone = FALSE;
-	}
+	new_line_flag = TRUE;
+#ifdef DEBUG
+      if (next_token_is_tag)
+	fprintf (stderr, "E: c: [%c], lp[-2]: [%c]\n", c, lp[-2]);
+#endif /* DEBUG */
       if (c == '\\')
 	{
 	  c = *lp++;
@@ -756,15 +787,18 @@
 	  if (c == 0)
 	    CNL_SAVE_NUMBER;
 	  c = ' ';
+	  /* is this correct?  I thought escaped newlines disappeared even */
+	  /* before the token breaker got to see things. */ 
 	} 
       else if (incomm)
 	{
+	  new_line_flag = FALSE;
 	  if (c == '*')
 	    {
 	      while ((c = *lp++) == '*')
 		continue;
 	      if (c == 0)
-		CNL;
+		CNL_SAVE_NUMBER;
 	      if (c == '/')
 		incomm = FALSE;
 	    }
@@ -771,6 +805,7 @@
 	}
       else if (inquote)
 	{
+	  new_line_flag = FALSE;
 	  /*
 	  * Too dumb to know about \" not being magic, but
 	  * they usually occur in pairs anyway.
@@ -781,6 +816,7 @@
 	}
       else if (inchar)
 	{
+	  new_line_flag = FALSE;
 	  if (c == '\'')
 	    inchar = FALSE;
 	  continue;
@@ -809,8 +845,8 @@
 	    {
 	      tydef=middle;
 	    }
-	  level++;
-	  continue;
+	  left_brace_flag = TRUE;
+	  break;
 	case '}':
 	  if (lp == lb.buffer + 1)
 	    level = 0;	/* reset */
@@ -820,8 +856,17 @@
 	    {
 	      tydef=end;
 	    }
+	  if (level == 0 && infunc)
+	    {
+	      idents_in_decl = 0;
+	      infunc = FALSE;
+	    }
 	  continue;
 	}
+#ifdef DEBUG
+      if (next_token_is_tag)
+	fprintf (stderr, "D: c: [%c], lp[-2]: [%c]\n", c, lp[-2]);
+#endif /* DEBUG */
       if (!level && !inquote && !incomm && gotone == FALSE)
 	{
 	  if (midtoken)
@@ -852,6 +897,11 @@
 				   tp-token+1);
 			  tok[tp-token+1] = 0;
 			  pfnote(tok, f, lb1.buffer, endpos, line, linestart);
+#ifdef DEBUG
+			  fprintf (stderr,
+				 "f: %d, infunc %d, tok: %s\nlb1.buffer: %s\n",
+				   f, infunc, tok, lb1.buffer);
+#endif /* DEBUG */
 			}
 		      else
 			{
@@ -858,8 +908,15 @@
 			  strncpy (tok, token, tp-token+1);
 			  tok[tp-token+1] = 0;
 			  pfnote(tok, f, lb.buffer, endpos, line, linestart);
+#ifdef DEBUG
+			  fprintf (stderr,
+			         "f: %d, infunc %d, tok: %s\nlb.buffer:  %s\n",
+				   f, infunc, tok, lb.buffer);
+#endif /* DEBUG */
 			}
 		      gotone = f;	/* function */
+		      if (f)
+			infunc = TRUE;
 		    }
 		  midtoken = FALSE;
 		  token = lp - 1;
@@ -873,8 +930,32 @@
 	      midtoken = TRUE;
 	    }
 	}
+#ifdef DEBUG
+      if (next_token_is_tag)
+	fprintf (stderr, "F: c: [%c], lp[-2]: [%c]\n", c, lp[-2]);
+#endif /* DEBUG */
+      if (left_brace_flag)
+	{
+	  left_brace_flag = FALSE;
+	  next_token_is_tag = FALSE;
+	  level++;
+	}
+      if (new_line_flag)
+	{
+	  new_line_flag = FALSE;
+	  number = 0;
+	  gotone = FALSE;
+	}
+      if (lp > lb.buffer && lp[-1] == 0)
+	CNL_SAVE_NUMBER;
       if (c == ';'  &&  tydef==end)	/* clean with typedefs */
 	tydef=none;
+      if (c == ';' && level == 0 && !infunc)
+	idents_in_decl = 0;
+#ifdef DEBUG
+      if (next_token_is_tag)
+	fprintf (stderr, "G: c: [%c], lp[-2]: [%c]\n", c, lp[-2]);
+#endif /* DEBUG */
     }
 }
 
@@ -891,70 +972,176 @@
   reg char *lp = *lpp;
   reg char c;
   static logical next_token_is_func;
+
+  /* indicates that the next token will be a macro defined with #define */
+  static logical next_token_is_macro;
+
   logical firsttok;	/* T if have seen first token in ()'s */
-  int bad, win;
+  int bad = FALSE, win = FALSE;
+  int length = (lp - 1) - token;
+  
+  /* used to keep a copy of the token when we have to fill the line buffer */
+  /* with the contents of the next line */
+  static char *tok_buffer = NULL;
+  static long tok_buffer_size = 0;
 
   *f = 1;			/* a function */
   c = lp[-1];
-  bad = FALSE;
   if (!number)
     {		/* space is not allowed in macro defs	*/
-      while (iswhite(c))
+      while ((c == 0) || iswhite(c))
 	{
-	  c = *lp++;
-	  if (c == 0)
+#ifdef DEBUG
+	  if (next_token_is_tag)
+	    fprintf (stderr, "C: token: %s, c: [%c], lp - token: %d\n",
+		     token, c, lp - token);
+#endif /* DEBUG */
+	  while (c == 0)
 	    {
 	      if (feof (inf))
-		break;
+		goto break_while_1;
+	      if (token != tok_buffer)
+		{
+		  if (length + 1 > tok_buffer_size)
+		    if (tok_buffer_size == 0)
+		      {
+			tok_buffer_size = length + 1;
+			tok_buffer = (char *) xmalloc (tok_buffer_size);
+		      }
+		    else
+		      {
+			tok_buffer_size = length + 1;
+			tok_buffer =
+			  (char *) xrealloc (tok_buffer, tok_buffer_size);
+		      }
+		  strncpy (tok_buffer, token, length);
+		  tok_buffer[length] = '\0';
+		  token = tok_buffer;
+		}
 	      CNL;
+	      c = *lp++;
 	    }
+	  while ((c != 0) && iswhite(c))
+	    c = *lp++;
 	}
-      /* the following tries to make it so that a #define a b(c)	*/
-      /* doesn't count as a define of b.				*/
+    break_while_1:
+      ;
     }
   else
     {
+      /* the following tries to make it so that a #define a b(c)	*/
+      /* doesn't count as a define of b.				*/
       number++;
-      if (number >= 4  || (number==2 && strncmp (token, "define", 6)))
-	{
-	  gotone = TRUE;
-	badone:
+#ifdef DEBUG
+      fprintf (stderr, "number: %d, n_t_i_m: %d, token: %s\n",
+	       number, next_token_is_macro, token);
+#endif /* DEBUG */
+      if (number == 2 && strncmp (token, "define", 6) == 0)
+	next_token_is_macro = TRUE;
+      else if (number == 3 && next_token_is_macro)
+	{
+	  next_token_is_macro = FALSE;
+	  while ((c != 0) && iswhite(c))
+	    {
+#ifdef DEBUG
+	      fprintf (stderr, "c: %c, %d\n", c, c);
+#endif /* DEBUG */
+	      c = *lp++;
+	    }
+#ifdef DEBUG
+	  fprintf (stderr, "c: %c, %d\n", c, c);
+#endif /* DEBUG */
+	  if (c == 0)		/* ignore plain "#define FLAG" */
 	  bad = TRUE;
+	  else
+	    win = TRUE;
+	  /* speed up skipping the rest of this line */
+	  gotone = TRUE;
+	  /* never treat a macro as a function, because it doesn't have a */
+	  /* function body, which is what "f" really stands for (now). */
+	  *f = FALSE;
 	  goto ret;
 	}
+      else
+	/* speed up skipping the rest of this line */
+	gotone = TRUE;
+      goto badone;
     }
+  idents_in_decl ++;
+#ifdef DEBUG
+  fprintf (stderr,
+	   "A: iid: %d, tydef: %d, ntit: %d, ntif: %d, c: %c, token: %.*s\n",
+	   idents_in_decl, tydef, next_token_is_tag, next_token_is_func, c,
+	   length + 5, token);
+#endif /* DEBUG */
   /* check for the typedef cases		*/
-  if (tflag && istoken(token, "typedef", 7))
+  if (istoken (token, "typedef", length))
     {
       tydef=begin;
       goto badone;
     }
-  if (tydef==begin && (istoken(token, "struct", 6) ||
-		       istoken(token, "union", 5) || istoken(token, "enum", 4)))
-  {
+#ifdef DEBUG
+  fprintf (stderr, "D\n");
+#endif /* DEBUG */
+  if (istoken (token, "struct", length) ||
+      istoken (token, "union", length) ||
+      istoken (token, "enum", length))
+    {
+      next_token_is_tag = 1;
+#ifdef DEBUG
+      fprintf (stderr, "A: token: %s\n", token);
+#endif /* DEBUG */
+      if (tydef == begin)
     tydef=tag_ok;      
     goto badone;
   }
-  if (tydef==tag_ok)
+  else if (next_token_is_tag)
     {
+#ifdef DEBUG
+      fprintf (stderr, "B: token: %s, c: [%c]\n", token, c);
+#endif /* DEBUG */
+      if (tydef==tag_ok)
       tydef=middle;
-      goto badone;
+      next_token_is_tag = 0;
+      *f = 0;
+      /* only notice when a tag is being defined, not when it is merely */
+      /* being used. */
+      if (c == '{')
+	win = TRUE;
+      else
+	bad = TRUE;
+      goto ret;
     }
+#ifdef DEBUG
+  fprintf (stderr, "E\n");
+#endif /* DEBUG */
   if (tydef==begin)		/* e.g. typedef ->int<- */
     {
       tydef=end;
       goto badone;
     }
+#ifdef DEBUG
+  fprintf (stderr, "F\n");
+#endif /* DEBUG */
   if (tydef==middle && level == 0) /* e.g. typedef struct tag ->struct_t<- */
     {
       tydef=end;
     }
+#ifdef DEBUG
+  fprintf (stderr, "G\n");
+#endif /* DEBUG */
   if (tydef==end)
     {
       *f = 0;
-      win = 1;
+#ifdef DEBUG
+      fprintf (stderr, "C token: %s\n", token);
+#endif /* DEBUG */
+      win = tflag;
       goto ret;
     }
+#ifdef DEBUG
+  fprintf (stderr, "H\n");
+#endif /* DEBUG */
   /* Detect GNUmacs's function-defining macros. */
   if (!number && !strncmp (token, "DEF", 3))
 	 
@@ -962,6 +1149,9 @@
       next_token_is_func = 1;
       goto badone;
     }
+#ifdef DEBUG
+  fprintf (stderr, "I\n");
+#endif /* DEBUG */
   if (next_token_is_func)
     {
       next_token_is_func = 0;
@@ -968,9 +1158,15 @@
       win = 1;
       goto ret;
     }
+#ifdef DEBUG
+  fprintf (stderr, "J\n");
+#endif /* DEBUG */
   if (c != '(')
     goto badone;
   firsttok = FALSE;
+#ifdef DEBUG
+  fprintf (stderr, "K\n");
+#endif /* DEBUG */
   while ((c = *lp++) != ')')
     {
       if (c == 0)
@@ -999,9 +1195,20 @@
 	}
     }
   win = isgood (c);
+  
 ret:
+#ifdef DEBUG
+  fprintf (stderr,
+	   "B: iid: %d, tydef: %d, ntit: %d, ntif: %d, c: %c, token: %.*s\n",
+	   idents_in_decl, tydef, next_token_is_tag, next_token_is_func, c,
+	   length + 5, token);
+#endif /* DEBUG */
   *lpp = lp - 1;
   return !bad && win;
+
+ badone:
+  bad = TRUE;
+  goto ret;
 }
 
 getline (atchar)
----------------------------------------------------------------------