Debian Codegraph @

libdebugger/file2line.c

raw symbols SCIP
1 /*
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2011, 2020 R. Bernstein
3 <[email protected]>
4 This file is part of GNU Make (remake variant).
5
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20 /* Create filename to line number table used by breakpoints */
21 #include "../src/make.h"
22 #include <assert.h>
23 #include "../src/filedef.h"
24 #include "../src/file.h"
25 #include "../src/read.h"
26 #include "../src/rule.h"
27 #include "./file2line.h"
28
29 struct hash_table file2lines;
30
31 unsigned long
32 file2lines_hash_1 (const void *key)
33 {
34 return_ISTRING_HASH_1 (((const lineno_array_t *) key)->hname);
35 }
36
37 unsigned long
38 file2lines_hash_2 (const void *key)
39 {
40 return_ISTRING_HASH_2 (((const lineno_array_t *) key)->hname);
41 }
42
43 static int
44 file2lines_hash_cmp (const void *x, const void *y)
45 {
46 return strcmp(((const lineno_array_t *) x)->hname,
47 ((const lineno_array_t *) y)->hname);
48 }
49
50 lineno_array_t *
51 lookup_file2lines (const char *psz_filename)
52 {
53 assert (*psz_filename != '\0');
54 return hash_find_item (&file2lines, &psz_filename);
55 }
56
57 /* FIXME return a status code when we fail. */
58 file_t *
59 target_for_file_and_line (const char *psz_filename, unsigned int lineno,
60 /*out*/ f2l_entry_t *entry_type)
61 {
62 lineno_array_t **pp_linenos;
63 lineno_array_t lookup_linenos;
64 assert (*psz_filename != '\0');
65 lookup_linenos.hname = psz_filename;
66 if (0 == file2lines.ht_size) file2lines_init();
67 pp_linenos = (lineno_array_t **)
68 hash_find_slot(&file2lines, &lookup_linenos);
69
70 if (NULL == *pp_linenos) return NULL;
71 if (lineno > (*pp_linenos)->size) return NULL;
72 *entry_type = (*pp_linenos)->type[lineno];
73 return (*pp_linenos)->array[lineno];
74 }
75
76 void
77 enter_target_lineno (const char *psz_filename, unsigned int lineno,
78 file_t *p_target)
79 {
80 lineno_array_t lookup_linenos;
81 lineno_array_t **pp_linenos;
82 file_t *p_file;
83
84 lookup_linenos.hname = psz_filename;
85 pp_linenos = (lineno_array_t **)
86 hash_find_slot(&file2lines, &lookup_linenos);
87 p_file = lookup_file(psz_filename);
88
89 if (p_file == NULL) {
90 printf("Could not find file %s\n", psz_filename);
91 return;
92 }
93 if (p_file->nlines == 0) {
94 printf("Warning: %s shows no lines\n", psz_filename);
95 }
96
97 if (HASH_VACANT(*pp_linenos)) {
98 const unsigned int nlines = p_file->nlines+1;
99 void **new_array = calloc (sizeof(void *), nlines);
100 f2l_entry_t *new_type = calloc (sizeof(f2l_entry_t *), nlines);
101 lineno_array_t *p_new_linenos = calloc (sizeof(lineno_array_t), 1);
102 *pp_linenos = p_new_linenos;
103 (*pp_linenos)->hname = psz_filename;
104 (*pp_linenos)->type = new_type;
105 (*pp_linenos)->array = new_array;
106 (*pp_linenos)->size = nlines;
107 }
108 (*pp_linenos)->type[lineno] = F2L_TARGET;
109 (*pp_linenos)->array[lineno] = p_target;
110 }
111
112
113 static void
114 file2line_init (const void *item)
115 {
116 file_t *p_target = (file_t *) item;
117 const gmk_floc *p_floc = &p_target->floc;
118 if (p_floc && p_floc->filenm) {
119 enter_target_lineno(p_floc->filenm, p_floc->lineno, p_target);
120 }
121 }
122
123 static void
124 enter_rule_lineno (rule_t *r)
125 {
126 lineno_array_t lookup_linenos;
127 lineno_array_t **pp_linenos;
128 const char *psz_filename = r->floc.filenm;
129 const unsigned int lineno = r->floc.lineno;
130 file_t *p_file;
131
132 if (!psz_filename) return;
133 lookup_linenos.hname = psz_filename;
134 pp_linenos = (lineno_array_t **)
135 hash_find_slot(&file2lines, &lookup_linenos);
136 p_file = lookup_file(psz_filename);
137
138 if (p_file == NULL) {
139 printf("Could not find file %s\n", psz_filename);
140 return;
141 }
142 if (p_file->nlines == 0) {
143 printf("Warning: %s shows no lines\n", psz_filename);
144 }
145
146 if (HASH_VACANT(*pp_linenos)) {
147 const unsigned int nlines = p_file->nlines+1;
148 void **new_array = calloc (sizeof(void *), nlines);
149 f2l_entry_t *new_type = calloc (sizeof(f2l_entry_t *), nlines);
150 lineno_array_t *p_new_linenos = calloc (sizeof(lineno_array_t), 1);
151 *pp_linenos = p_new_linenos;
152 (*pp_linenos)->hname = psz_filename;
153 (*pp_linenos)->type = new_type;
154 (*pp_linenos)->array = new_array;
155 (*pp_linenos)->size = nlines;
156 }
157 (*pp_linenos)->type[lineno] = F2L_PATTERN;
158 (*pp_linenos)->array[lineno] = r;
159 }
160
161
162 /*!
163 Initializes hash table file2lines. file2lines is used in breakpoints
164 only. So we do this on demand.
165 */
166 bool file2lines_init(void)
167 {
168 if (!read_makefiles) return false;
169 hash_init (&file2lines, files.ht_size, file2lines_hash_1, file2lines_hash_2,
170 file2lines_hash_cmp);
171 hash_map (&files, file2line_init);
172
173 {
174 rule_t *r;
175 for (r = pattern_rules; r != 0; r = r->next) {
176 enter_rule_lineno(r);
177 }
178 }
179
180 return true;
181 }
182
183 void file2lines_print_entry(const void *item)
184 {
185 const lineno_array_t *p_linenos = (lineno_array_t *) item;
186 unsigned int i;
187 file_t *p_target;
188 printf("%s:\n", p_linenos->hname);
189 for (i=0; i<p_linenos->size; i++)
190 {
191 p_target = p_linenos->array[i];
192 if (p_target) {
193 if (p_linenos->type[i] == F2L_TARGET) {
194 printf("%8lu: %s\n",
195 p_target->floc.lineno, p_target->name);
196 } else {
197 rule_t *p_rule = (rule_t *) p_target;
198 printf("%8lu: %s (pattern)\n",
199 p_rule->floc.lineno, p_rule->targets[0]);
200 }
201 }
202 }
203 }
204
205 void file2lines_dump(void)
206 {
207 file2lines_init();
208 hash_map (&file2lines, file2lines_print_entry);
209 }
210
211 /*
212 * Local variables:
213 * eval: (c-set-style "gnu")
214 * indent-tabs-mode: nil
215 * End:
216 */