4 * Ctags generates tags from preprocessing results.
6 * Copyright (C) 2006 Christopher Li
8 * Licensed under the Open Software License version 1.1
19 static struct symbol_list
*taglist
= NULL
;
21 static void examine_symbol(struct symbol
*sym
);
23 #define MAX(_x,_y) ((_x) > (_y) ? (_x) : (_y))
25 static int cmp_sym(const void *m
, const void *n
)
27 const struct ident
*a
= ((const struct symbol
*)m
)->ident
;
28 const struct ident
*b
= ((const struct symbol
*)n
)->ident
;
29 int ret
= strncmp(a
->name
, b
->name
, MAX(a
->len
, b
->len
));
31 const struct position a_pos
= ((const struct symbol
*)m
)->pos
;
32 const struct position b_pos
= ((const struct symbol
*)n
)->pos
;
34 ret
= strcmp(stream_name(a_pos
.stream
),
35 stream_name(b_pos
.stream
));
37 return a_pos
.line
< b_pos
.line
;
42 static void show_tag_header(FILE *fp
)
44 fprintf(fp
, "!_TAG_FILE_FORMAT\t2\t/extended format; --format=1 will not append ;\" to lines/\n");
45 fprintf(fp
, "!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted, 2=foldcase/\n");
46 fprintf(fp
, "!_TAG_PROGRAM_AUTHOR\tChristopher Li\t/sparse@chrisli.org/\n");
47 fprintf(fp
, "!_TAG_PROGRAM_NAME\tSparse Ctags\t//\n");
48 fprintf(fp
, "!_TAG_PROGRAM_URL\thttp://www.kernel.org/pub/software/devel/sparse/\t/official site/\n");
49 fprintf(fp
, "!_TAG_PROGRAM_VERSION\t0.01\t//\n");
52 static inline void show_symbol_tag(FILE *fp
, struct symbol
*sym
)
54 fprintf(fp
, "%s\t%s\t%d;\"\t%c\tfile:\n", show_ident(sym
->ident
),
55 stream_name(sym
->pos
.stream
), sym
->pos
.line
, (int)sym
->kind
);
58 static void show_tags(struct symbol_list
*list
)
61 struct ident
*ident
= NULL
;
62 struct position pos
= {};
63 static const char *filename
;
69 fp
= fopen("tags", "w");
71 perror("open tags file");
75 FOR_EACH_PTR(list
, sym
) {
76 if (ident
== sym
->ident
&& pos
.line
== sym
->pos
.line
&&
77 !strcmp(filename
, stream_name(sym
->pos
.stream
)))
80 show_symbol_tag(fp
, sym
);
83 filename
= stream_name(sym
->pos
.stream
);
84 } END_FOR_EACH_PTR(sym
);
88 static inline void add_tag(struct symbol
*sym
)
90 if (sym
->ident
&& !sym
->visited
) {
92 add_symbol(&taglist
, sym
);
96 static inline void examine_members(struct symbol_list
*list
)
100 FOR_EACH_PTR(list
, sym
) {
103 } END_FOR_EACH_PTR(sym
);
106 static void examine_symbol(struct symbol
*sym
)
108 struct symbol
*base
= sym
;
110 if (!sym
|| sym
->visited
)
112 if (sym
->ident
&& sym
->ident
->reserved
)
116 base
= sym
->ctype
.base_type
;
120 if (base
->type
== SYM_FN
)
122 examine_symbol(base
);
126 examine_members(sym
->symbol_list
);
130 examine_members(sym
->symbol_list
);
139 examine_symbol(sym
->ctype
.base_type
);
145 die("unknown symbol %s namespace:%d type:%d\n", show_ident(sym
->ident
),
146 sym
->namespace, sym
->type
);
153 static void examine_namespace(struct symbol
*sym
)
157 if (sym
->ident
&& sym
->ident
->reserved
)
160 switch(sym
->namespace) {
164 case NS_PREPROCESSOR
:
177 die("unknown namespace %d symbol:%s type:%d\n", sym
->namespace,
178 show_ident(sym
->ident
), sym
->type
);
183 static inline void examine_symbol_list(struct symbol_list
*list
)
189 FOR_EACH_PTR(list
, sym
) {
190 examine_namespace(sym
);
191 } END_FOR_EACH_PTR(sym
);
194 int main(int argc
, char **argv
)
196 struct string_list
*filelist
= NULL
;
199 examine_symbol_list(sparse_initialize(argc
, argv
, &filelist
));
200 FOR_EACH_PTR_NOTAG(filelist
, file
) {
202 examine_symbol_list(file_scope
->symbols
);
203 } END_FOR_EACH_PTR_NOTAG(file
);
204 examine_symbol_list(global_scope
->symbols
);
205 sort_list((struct ptr_list
**)&taglist
, cmp_sym
);