| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * 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 |
|---|
| 18 | 4 | */ |
|---|
| 19 | 5 | |
|---|
| 20 | 6 | #define _GNU_SOURCE |
|---|
| .. | .. |
|---|
| 33 | 19 | /* This is the list of directories that we search for source files */ |
|---|
| 34 | 20 | static struct search_path *search_path_head, **search_path_tail; |
|---|
| 35 | 21 | |
|---|
| 22 | +/* Detect infinite include recursion. */ |
|---|
| 23 | +#define MAX_SRCFILE_DEPTH (100) |
|---|
| 24 | +static int srcfile_depth; /* = 0 */ |
|---|
| 36 | 25 | |
|---|
| 37 | 26 | static char *get_dirname(const char *path) |
|---|
| 38 | 27 | { |
|---|
| .. | .. |
|---|
| 51 | 40 | |
|---|
| 52 | 41 | FILE *depfile; /* = NULL */ |
|---|
| 53 | 42 | struct srcfile_state *current_srcfile; /* = NULL */ |
|---|
| 43 | +static char *initial_path; /* = NULL */ |
|---|
| 44 | +static int initial_pathlen; /* = 0 */ |
|---|
| 45 | +static bool initial_cpp = true; |
|---|
| 54 | 46 | |
|---|
| 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); |
|---|
| 58 | 50 | |
|---|
| 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 | +} |
|---|
| 59 | 88 | |
|---|
| 60 | 89 | /** |
|---|
| 61 | 90 | * Try to open a file in a given directory. |
|---|
| .. | .. |
|---|
| 157 | 186 | srcfile->colno = 1; |
|---|
| 158 | 187 | |
|---|
| 159 | 188 | current_srcfile = srcfile; |
|---|
| 189 | + |
|---|
| 190 | + if (srcfile_depth == 1) |
|---|
| 191 | + set_initial_path(srcfile->name); |
|---|
| 160 | 192 | } |
|---|
| 161 | 193 | |
|---|
| 162 | 194 | bool srcfile_pop(void) |
|---|
| .. | .. |
|---|
| 197 | 229 | search_path_tail = &node->next; |
|---|
| 198 | 230 | } |
|---|
| 199 | 231 | |
|---|
| 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 | | - |
|---|
| 212 | 232 | void srcpos_update(struct srcpos *pos, const char *text, int len) |
|---|
| 213 | 233 | { |
|---|
| 214 | 234 | int i; |
|---|
| .. | .. |
|---|
| 234 | 254 | srcpos_copy(struct srcpos *pos) |
|---|
| 235 | 255 | { |
|---|
| 236 | 256 | struct srcpos *pos_new; |
|---|
| 257 | + struct srcfile_state *srcfile_state; |
|---|
| 258 | + |
|---|
| 259 | + if (!pos) |
|---|
| 260 | + return NULL; |
|---|
| 237 | 261 | |
|---|
| 238 | 262 | pos_new = xmalloc(sizeof(struct srcpos)); |
|---|
| 263 | + assert(pos->next == NULL); |
|---|
| 239 | 264 | memcpy(pos_new, pos, sizeof(struct srcpos)); |
|---|
| 240 | 265 | |
|---|
| 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 | + |
|---|
| 241 | 271 | 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; |
|---|
| 242 | 284 | } |
|---|
| 243 | 285 | |
|---|
| 244 | 286 | char * |
|---|
| .. | .. |
|---|
| 264 | 306 | pos->first_line, pos->first_column); |
|---|
| 265 | 307 | |
|---|
| 266 | 308 | 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); |
|---|
| 267 | 371 | } |
|---|
| 268 | 372 | |
|---|
| 269 | 373 | void srcpos_verror(struct srcpos *pos, const char *prefix, |
|---|
| .. | .. |
|---|
| 294 | 398 | { |
|---|
| 295 | 399 | current_srcfile->name = f; |
|---|
| 296 | 400 | current_srcfile->lineno = l; |
|---|
| 401 | + |
|---|
| 402 | + if (initial_cpp) { |
|---|
| 403 | + initial_cpp = false; |
|---|
| 404 | + set_initial_path(f); |
|---|
| 405 | + } |
|---|
| 297 | 406 | } |
|---|