hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/scripts/dtc/srcpos.c
....@@ -1,20 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
3
- *
4
- * This program is free software; you can redistribute it and/or
5
- * modify it under the terms of the GNU General Public License as
6
- * published by the Free Software Foundation; either version 2 of the
7
- * License, or (at your option) any later version.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
- * General Public License for more details.
13
- *
14
- * You should have received a copy of the GNU General Public License
15
- * along with this program; if not, write to the Free Software
16
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17
- * USA
184 */
195
206 #define _GNU_SOURCE
....@@ -33,6 +19,9 @@
3319 /* This is the list of directories that we search for source files */
3420 static struct search_path *search_path_head, **search_path_tail;
3521
22
+/* Detect infinite include recursion. */
23
+#define MAX_SRCFILE_DEPTH (100)
24
+static int srcfile_depth; /* = 0 */
3625
3726 static char *get_dirname(const char *path)
3827 {
....@@ -51,11 +40,51 @@
5140
5241 FILE *depfile; /* = NULL */
5342 struct srcfile_state *current_srcfile; /* = NULL */
43
+static char *initial_path; /* = NULL */
44
+static int initial_pathlen; /* = 0 */
45
+static bool initial_cpp = true;
5446
55
-/* Detect infinite include recursion. */
56
-#define MAX_SRCFILE_DEPTH (100)
57
-static int srcfile_depth; /* = 0 */
47
+static void set_initial_path(char *fname)
48
+{
49
+ int i, len = strlen(fname);
5850
51
+ xasprintf(&initial_path, "%s", fname);
52
+ initial_pathlen = 0;
53
+ for (i = 0; i != len; i++)
54
+ if (initial_path[i] == '/')
55
+ initial_pathlen++;
56
+}
57
+
58
+static char *shorten_to_initial_path(char *fname)
59
+{
60
+ char *p1, *p2, *prevslash1 = NULL;
61
+ int slashes = 0;
62
+
63
+ for (p1 = fname, p2 = initial_path; *p1 && *p2; p1++, p2++) {
64
+ if (*p1 != *p2)
65
+ break;
66
+ if (*p1 == '/') {
67
+ prevslash1 = p1;
68
+ slashes++;
69
+ }
70
+ }
71
+ p1 = prevslash1 + 1;
72
+ if (prevslash1) {
73
+ int diff = initial_pathlen - slashes, i, j;
74
+ int restlen = strlen(fname) - (p1 - fname);
75
+ char *res;
76
+
77
+ res = xmalloc((3 * diff) + restlen + 1);
78
+ for (i = 0, j = 0; i != diff; i++) {
79
+ res[j++] = '.';
80
+ res[j++] = '.';
81
+ res[j++] = '/';
82
+ }
83
+ strcpy(res + j, p1);
84
+ return res;
85
+ }
86
+ return NULL;
87
+}
5988
6089 /**
6190 * Try to open a file in a given directory.
....@@ -157,6 +186,9 @@
157186 srcfile->colno = 1;
158187
159188 current_srcfile = srcfile;
189
+
190
+ if (srcfile_depth == 1)
191
+ set_initial_path(srcfile->name);
160192 }
161193
162194 bool srcfile_pop(void)
....@@ -197,18 +229,6 @@
197229 search_path_tail = &node->next;
198230 }
199231
200
-/*
201
- * The empty source position.
202
- */
203
-
204
-struct srcpos srcpos_empty = {
205
- .first_line = 0,
206
- .first_column = 0,
207
- .last_line = 0,
208
- .last_column = 0,
209
- .file = NULL,
210
-};
211
-
212232 void srcpos_update(struct srcpos *pos, const char *text, int len)
213233 {
214234 int i;
....@@ -234,11 +254,33 @@
234254 srcpos_copy(struct srcpos *pos)
235255 {
236256 struct srcpos *pos_new;
257
+ struct srcfile_state *srcfile_state;
258
+
259
+ if (!pos)
260
+ return NULL;
237261
238262 pos_new = xmalloc(sizeof(struct srcpos));
263
+ assert(pos->next == NULL);
239264 memcpy(pos_new, pos, sizeof(struct srcpos));
240265
266
+ /* allocate without free */
267
+ srcfile_state = xmalloc(sizeof(struct srcfile_state));
268
+ memcpy(srcfile_state, pos->file, sizeof(struct srcfile_state));
269
+ pos_new->file = srcfile_state;
270
+
241271 return pos_new;
272
+}
273
+
274
+struct srcpos *srcpos_extend(struct srcpos *pos, struct srcpos *newtail)
275
+{
276
+ struct srcpos *p;
277
+
278
+ if (!pos)
279
+ return newtail;
280
+
281
+ for (p = pos; p->next != NULL; p = p->next);
282
+ p->next = newtail;
283
+ return pos;
242284 }
243285
244286 char *
....@@ -264,6 +306,68 @@
264306 pos->first_line, pos->first_column);
265307
266308 return pos_str;
309
+}
310
+
311
+static char *
312
+srcpos_string_comment(struct srcpos *pos, bool first_line, int level)
313
+{
314
+ char *pos_str, *fname, *first, *rest;
315
+ bool fresh_fname = false;
316
+
317
+ if (!pos) {
318
+ if (level > 1) {
319
+ xasprintf(&pos_str, "<no-file>:<no-line>");
320
+ return pos_str;
321
+ } else {
322
+ return NULL;
323
+ }
324
+ }
325
+
326
+ if (!pos->file)
327
+ fname = "<no-file>";
328
+ else if (!pos->file->name)
329
+ fname = "<no-filename>";
330
+ else if (level > 1)
331
+ fname = pos->file->name;
332
+ else {
333
+ fname = shorten_to_initial_path(pos->file->name);
334
+ if (fname)
335
+ fresh_fname = true;
336
+ else
337
+ fname = pos->file->name;
338
+ }
339
+
340
+ if (level > 1)
341
+ xasprintf(&first, "%s:%d:%d-%d:%d", fname,
342
+ pos->first_line, pos->first_column,
343
+ pos->last_line, pos->last_column);
344
+ else
345
+ xasprintf(&first, "%s:%d", fname,
346
+ first_line ? pos->first_line : pos->last_line);
347
+
348
+ if (fresh_fname)
349
+ free(fname);
350
+
351
+ if (pos->next != NULL) {
352
+ rest = srcpos_string_comment(pos->next, first_line, level);
353
+ xasprintf(&pos_str, "%s, %s", first, rest);
354
+ free(first);
355
+ free(rest);
356
+ } else {
357
+ pos_str = first;
358
+ }
359
+
360
+ return pos_str;
361
+}
362
+
363
+char *srcpos_string_first(struct srcpos *pos, int level)
364
+{
365
+ return srcpos_string_comment(pos, true, level);
366
+}
367
+
368
+char *srcpos_string_last(struct srcpos *pos, int level)
369
+{
370
+ return srcpos_string_comment(pos, false, level);
267371 }
268372
269373 void srcpos_verror(struct srcpos *pos, const char *prefix,
....@@ -294,4 +398,9 @@
294398 {
295399 current_srcfile->name = f;
296400 current_srcfile->lineno = l;
401
+
402
+ if (initial_cpp) {
403
+ initial_cpp = false;
404
+ set_initial_path(f);
405
+ }
297406 }