1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
34 /* cscope - interactive C symbol cross-reference
36 * build cross-reference file
48 static char const rcsid[] = "$Id: crossref.c,v 1.15 2009/08/28 14:28:27 nhorman Exp $";
51 /* convert long to a string */
52 #define ltobase(value) n = value; \
53 s = buf + (sizeof(buf) - 1); \
60 *--s = i - n * BASE + '!'; \
64 #define SYMBOLINC 20 /* symbol list size increment */
66 long dboffset; /* new database offset */
67 BOOL errorsfound; /* prompt before clearing messages */
68 long lineoffset; /* source line database offset */
69 long npostings; /* number of postings */
70 int nsrcoffset; /* number of file name database offsets */
71 long *srcoffset; /* source file name database offsets */
72 unsigned long symbols; /* number of symbols */
74 static char *filename; /* file name for warning messages */
75 static long fcnoffset; /* function name database offset */
76 static long macrooffset; /* macro name database offset */
77 static unsigned long msymbols = SYMBOLINC; /* maximum number of symbols */
79 struct symbol { /* symbol data */
81 unsigned int first; /* index of first character in text */
82 unsigned int last; /* index of last+1 character in text */
83 unsigned int length; /* symbol length */
84 unsigned int fcn_level; /* function level of the symbol */
86 static struct symbol *symbol;
88 static void putcrossref(void);
89 static void savesymbol(int token, int num);
92 crossref(char *srcfile)
95 unsigned int length; /* symbol length */
96 unsigned int entry_no; /* function level of the symbol */
97 int token; /* current token */
100 if (! ((stat(srcfile, &st) == 0)
101 && S_ISREG(st.st_mode))) {
108 /* open the source file */
109 if ((yyin = myfopen(srcfile, "r")) == NULL) {
114 filename = srcfile; /* save the file name for warning messages */
115 putfilename(srcfile); /* output the file name */
119 /* read the source file */
120 initscanner(srcfile);
121 fcnoffset = macrooffset = 0;
123 if (symbol == NULL) {
124 symbol = mymalloc(msymbols * sizeof(struct symbol));
128 /* get the next token */
129 switch (token = yylex()) {
131 /* if requested, truncate C symbols */
132 length = last - first;
133 if (trun_syms == YES && length > 8 &&
134 token != INCLUDE && token != NEWFILE) {
138 /* see if the token has a symbol */
140 savesymbol(token, entry_no);
143 /* update entry_no if see function entry */
144 if (token == FCNDEF) {
147 /* see if the symbol is already in the list */
148 for (i = 0; i < symbols; ++i) {
149 if (length == symbol[i].length
150 && strncmp(my_yytext + first,
151 my_yytext + symbol[i].first,
153 && entry_no == symbol[i].fcn_level
154 && token == symbol[i].type
155 ) { /* could be a::a() */
159 if (i == symbols) { /* if not already in list */
160 savesymbol(token, entry_no);
164 case NEWLINE: /* end of line containing symbols */
165 entry_no = 0; /* reset entry_no for each line */
167 --yyleng; /* remove the newline */
169 putcrossref(); /* output the symbols and source line */
170 lineno = myylineno; /* save the symbol line number */
172 /* HBB 20010425: replaced yyleng-- by this chunk: */
179 case LEXERR: /* Lexer error, abort further parsing of this file */
180 case LEXEOF: /* end of file; last line may not have \n */
182 /* if there were symbols, output them and the source line */
186 (void) fclose(yyin); /* close the source file */
188 /* output the leading tab expected by the next call */
195 /* save the symbol in the list */
198 savesymbol(int token, int num)
200 /* make sure there is room for the symbol */
201 if (symbols == msymbols) {
202 msymbols += SYMBOLINC;
203 symbol = myrealloc(symbol, msymbols * sizeof(struct symbol));
205 /* save the symbol */
206 symbol[symbols].type = token;
207 symbol[symbols].first = first;
208 symbol[symbols].last = last;
209 symbol[symbols].length = last - first;
210 symbol[symbols].fcn_level = num;
214 /* output the file name */
217 putfilename(char *srcfile)
219 /* check for file system out of space */
220 /* note: dbputc is not used to avoid lint complaint */
221 if (putc(NEWFILE, newrefs) == EOF) {
222 cannotwrite(newreffile);
226 if (invertedindex == YES) {
227 srcoffset[nsrcoffset++] = dboffset;
230 fcnoffset = macrooffset = 0;
233 /* output the symbols and source line */
240 BOOL blank; /* blank indicator */
241 unsigned int symput = 0; /* symbols output */
244 /* output the source line */
245 lineoffset = dboffset;
246 dboffset += fprintf(newrefs, "%d ", lineno);
247 #ifdef PRINTF_RETVAL_BROKEN
248 dboffset = ftell(newrefs); /* fprintf doesn't return chars written */
251 /* HBB 20010425: added this line: */
252 my_yytext[my_yyleng] = '\0';
255 for (i = 0; i < my_yyleng; ++i) {
257 /* change a tab to a blank and compress blanks */
258 if ((c = my_yytext[i]) == ' ' || c == '\t') {
260 } else if (symput < symbols && i == symbol[symput].first) {
261 /* look for the start of a symbol */
263 /* check for compressed blanks */
268 dbputc('\n'); /* symbols start on a new line */
270 /* output any symbol type */
271 if ((type = symbol[symput].type) != IDENT) {
277 /* output the symbol */
278 j = symbol[symput].last;
281 if (invertedindex == YES) {
282 putposting(my_yytext + i, type);
284 writestring(my_yytext + i);
290 /* HBB: try to save some time by early-out handling of
291 * non-compressed mode */
292 if (compress == NO) {
297 j = i + strcspn(my_yytext+i, "\t ");
299 && j >= symbol[symput].first)
300 j = symbol[symput].first;
303 writestring(my_yytext + i);
306 /* finished this 'i', continue with the blank */
310 /* check for compressed blanks */
313 c = DICODE_COMPRESS(' ', c);
317 } else if (IS_A_DICODE(c, my_yytext[i + 1])
319 && i + 1 != symbol[symput].first) {
320 /* compress digraphs */
321 c = DICODE_COMPRESS(c, my_yytext[i + 1]);
327 /* skip compressed characters */
331 /* skip blanks before a preprocesor keyword */
332 /* note: don't use isspace() because \f and \v
333 are used for keywords */
334 while ((j = my_yytext[i]) == ' ' || j == '\t') {
337 /* skip the rest of the keyword */
338 while (isalpha((unsigned char)my_yytext[i])) {
341 /* skip space after certain keywords */
342 if (keyword[c].delim != '\0') {
343 while ((j = my_yytext[i]) == ' ' || j == '\t') {
347 /* skip a '(' after certain keywords */
348 if (keyword[c].delim == '('
349 && my_yytext[i] == '(') {
352 --i; /* compensate for ++i in for() */
353 } /* if compressed char */
354 } /* else: not a symbol */
357 /* ignore trailing blanks */
361 /* output any #define end marker */
362 /* note: must not be part of #define so putsource() doesn't discard it
363 so findcalledbysub() can find it and return */
364 if (symput < symbols && symbol[symput].type == DEFINEEND) {
368 dbputc('\n'); /* mark beginning of next source line */
374 /* HBB 20000421: new function, for avoiding memory leaks */
375 /* free the cross reference symbol table */
385 /* output the inverted index posting */
388 putposting(char *term, int type)
392 int digits; /* digits output */
393 long offset; /* function/macro database offset */
394 char buf[11]; /* number buffer */
396 /* get the function or macro name offset */
398 if (macrooffset != 0) {
399 offset = macrooffset;
401 /* then update them to avoid negative relative name offset */
404 macrooffset = dboffset;
408 return; /* null term */
410 fcnoffset = dboffset;
414 return; /* null term */
416 /* ignore a null term caused by a enum/struct/union without a tag */
420 /* skip any #include secondary type char (< or ") */
421 if (type == INCLUDE) {
424 /* output the posting, which should be as small as possible to reduce
425 the temp file size and sort time */
426 (void) fputs(term, postings);
427 (void) putc(' ', postings);
429 /* the line offset is padded so postings for the same term will sort
430 in ascending line offset order to order the references as they
431 appear withing a source file */
433 for (i = PRECISION - digits; i > 0; --i) {
434 (void) putc('!', postings);
437 (void) putc(*s, postings);
438 } while (*++s != '\0');
440 /* postings are also sorted by type */
441 (void) putc(type, postings);
443 /* function or macro name offset */
445 (void) putc(' ', postings);
448 (void) putc(*s, postings);
449 } while (*++s != '\0');
451 if (putc('\n', postings) == EOF) {
458 /* put the string into the new database */
466 if (compress == NO) {
467 /* Save some I/O overhead by using puts() instead of putc(): */
471 /* compress digraphs */
472 for (i = 0; (c = s[i]) != '\0'; ++i) {
473 if (/* dicode1[c] && dicode2[(unsigned char) s[i + 1]] */
474 IS_A_DICODE(c, s[i + 1])) {
475 /* c = (0200 - 2) + dicode1[c] + dicode2[(unsigned char) s[i + 1]]; */
476 c = DICODE_COMPRESS(c, s[i + 1]);
483 /* print a warning message with the file name and line number */
489 (void) fprintf(stderr, "cscope: \"%s\", line %d: warning: %s\n", filename,