Fix typo in symbol.h: s/keywrods/keywords/
[smatch.git] / ctags.c
blob17dd1c118d3d32f2ff4a8ace7a67dda8dadaaf6c
1 /*
2 * Sparse Ctags
4 * Ctags generates tags from preprocessing results.
6 * Copyright (C) 2006 Christopher Li
8 * Licensed under the Open Software License version 1.1
9 */
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <fcntl.h>
16 #include "parse.h"
17 #include "scope.h"
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 struct ident *a = ((struct symbol *)m)->ident;
28 struct ident *b = ((struct symbol *)n)->ident;
29 int ret = strncmp(a->name, b->name, MAX(a->len, b->len));
30 if (!ret) {
31 struct position a = ((struct symbol *)m)->pos;
32 struct position b = ((struct symbol *)n)->pos;
34 ret = strcmp(stream_name(a.stream), stream_name(b.stream));
35 if (!ret)
36 return a.line < b.line;
38 return ret;
41 static void show_tag_header(FILE *fp)
43 fprintf(fp, "!_TAG_FILE_FORMAT\t2\t/extended format; --format=1 will not append ;\" to lines/\n");
44 fprintf(fp, "!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted, 2=foldcase/\n");
45 fprintf(fp, "!_TAG_PROGRAM_AUTHOR\tChristopher Li\t/sparse@chrisli.org/\n");
46 fprintf(fp, "!_TAG_PROGRAM_NAME\tSparse Ctags\t//\n");
47 fprintf(fp, "!_TAG_PROGRAM_URL\thttp://kernel.org/pub/linux/kernel/people/josh/sparse/\t/official site/\n");
48 fprintf(fp, "!_TAG_PROGRAM_VERSION\t0.01\t//\n");
51 static inline void show_symbol_tag(FILE *fp, struct symbol *sym)
53 fprintf(fp, "%s\t%s\t%d;\"\t%c\tfile:\n", show_ident(sym->ident),
54 stream_name(sym->pos.stream), sym->pos.line, (int)sym->kind);
57 static void show_tags(struct symbol_list *list)
59 struct symbol *sym;
60 struct ident *ident = NULL;
61 struct position pos = {};
62 static const char *filename;
63 FILE *fp;
65 if (!list)
66 return;
68 fp = fopen("tags", "w");
69 if (!fp) {
70 perror("open tags file");
71 return;
73 show_tag_header(fp);
74 FOR_EACH_PTR(list, sym) {
75 if (ident == sym->ident && pos.line == sym->pos.line &&
76 !strcmp(filename, stream_name(sym->pos.stream)))
77 continue;
79 show_symbol_tag(fp, sym);
80 ident = sym->ident;
81 pos = sym->pos;
82 filename = stream_name(sym->pos.stream);
83 } END_FOR_EACH_PTR(sym);
84 fclose(fp);
87 static inline void add_tag(struct symbol *sym)
89 if (sym->ident && !sym->visited) {
90 sym->visited = 1;
91 add_symbol(&taglist, sym);
95 static inline void examine_members(struct symbol_list *list)
97 struct symbol *sym;
99 FOR_EACH_PTR(list, sym) {
100 sym->kind = 'm';
101 examine_symbol(sym);
102 } END_FOR_EACH_PTR(sym);
105 static void examine_symbol(struct symbol *sym)
107 struct symbol *base = sym;
109 if (!sym || sym->visited)
110 return;
111 if (sym->ident && sym->ident->reserved)
112 return;
114 add_tag(sym);
115 base = sym->ctype.base_type;
117 switch (sym->type) {
118 case SYM_NODE:
119 if (base->type == SYM_FN)
120 sym->kind = 'f';
121 examine_symbol(base);
122 break;
123 case SYM_STRUCT:
124 sym->kind = 's';
125 examine_members(sym->symbol_list);
126 break;
127 case SYM_UNION:
128 sym->kind = 'u';
129 examine_members(sym->symbol_list);
130 break;
131 case SYM_ENUM:
132 sym->kind = 'e';
133 case SYM_PTR:
134 case SYM_TYPEOF:
135 case SYM_BITFIELD:
136 case SYM_FN:
137 case SYM_ARRAY:
138 examine_symbol(sym->ctype.base_type);
139 break;
140 case SYM_BASETYPE:
141 break;
143 default:
144 die("unknown symbol %s namespace:%d type:%d\n", show_ident(sym->ident),
145 sym->namespace, sym->type);
147 if (!sym->kind)
148 sym->kind = 'v';
149 return;
152 static void examine_namespace(struct symbol *sym)
154 if (sym->visited)
155 return;
156 if (sym->ident && sym->ident->reserved)
157 return;
159 switch(sym->namespace) {
160 case NS_LABEL:
161 sym->kind = 'l';
162 break;
163 case NS_PREPROCESSOR:
164 break;
165 case NS_MACRO:
166 case NS_UNDEF:
167 sym->kind = 'd';
168 break;
169 case NS_TYPEDEF:
170 sym->kind = 't';
171 case NS_SYMBOL:
172 case NS_STRUCT:
173 examine_symbol(sym);
174 break;
175 default:
176 die("unknown namespace %d symbol:%s type:%d\n", sym->namespace,
177 show_ident(sym->ident), sym->type);
179 add_tag(sym);
182 static inline void examine_symbol_list(struct symbol_list *list)
184 struct symbol *sym;
186 if (!list)
187 return;
188 FOR_EACH_PTR(list, sym) {
189 examine_namespace(sym);
190 } END_FOR_EACH_PTR(sym);
193 int main(int argc, char **argv)
195 struct string_list *filelist = NULL;
196 char *file;
198 examine_symbol_list(sparse_initialize(argc, argv, &filelist));
199 FOR_EACH_PTR_NOTAG(filelist, file) {
200 sparse(file);
201 examine_symbol_list(file_scope->symbols);
202 } END_FOR_EACH_PTR_NOTAG(file);
203 examine_symbol_list(global_scope->symbols);
204 sort_list((struct ptr_list **)&taglist, cmp_sym);
205 show_tags(taglist);
206 return 0;