blob: 4318d7ad34d91d2ada1a5d7f92d2c84148fec366 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
David Gibsona4da2e32007-12-18 15:06:42 +11002/*
3 * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
David Gibsona4da2e32007-12-18 15:06:42 +11004 */
5
Rob Herring9130ba82018-02-27 17:40:38 -06006#ifndef SRCPOS_H
7#define SRCPOS_H
David Gibsona4da2e32007-12-18 15:06:42 +11008
David Gibsoned95d742008-08-07 12:24:17 +10009#include <stdio.h>
Rob Herring47605972015-04-29 16:00:05 -050010#include <stdbool.h>
Rob Herring89d12312017-03-21 09:01:08 -050011#include "util.h"
David Gibsoned95d742008-08-07 12:24:17 +100012
John Bonesio658f29a2010-11-17 15:28:20 -080013struct srcfile_state {
14 FILE *f;
15 char *name;
David Gibsoned95d742008-08-07 12:24:17 +100016 char *dir;
John Bonesio658f29a2010-11-17 15:28:20 -080017 int lineno, colno;
18 struct srcfile_state *prev;
David Gibsoned95d742008-08-07 12:24:17 +100019};
20
Stephen Warren136ec202012-01-10 17:27:52 -070021extern FILE *depfile; /* = NULL */
John Bonesio658f29a2010-11-17 15:28:20 -080022extern struct srcfile_state *current_srcfile; /* = NULL */
23
Stephen Warrencd296722012-09-28 21:25:59 +000024/**
25 * Open a source file.
26 *
27 * If the source file is a relative pathname, then it is searched for in the
28 * current directory (the directory of the last source file read) and after
29 * that in the search path.
30 *
31 * We work through the search path in order from the first path specified to
32 * the last.
33 *
34 * If the file is not found, then this function does not return, but calls
35 * die().
36 *
37 * @param fname Filename to search
38 * @param fullnamep If non-NULL, it is set to the allocated filename of the
39 * file that was opened. The caller is then responsible
40 * for freeing the pointer.
41 * @return pointer to opened FILE
42 */
John Bonesio658f29a2010-11-17 15:28:20 -080043FILE *srcfile_relative_open(const char *fname, char **fullnamep);
Stephen Warrencd296722012-09-28 21:25:59 +000044
John Bonesio658f29a2010-11-17 15:28:20 -080045void srcfile_push(const char *fname);
Rob Herring47605972015-04-29 16:00:05 -050046bool srcfile_pop(void);
John Bonesio658f29a2010-11-17 15:28:20 -080047
Stephen Warrencd296722012-09-28 21:25:59 +000048/**
49 * Add a new directory to the search path for input files
50 *
51 * The new path is added at the end of the list.
52 *
53 * @param dirname Directory to add
54 */
55void srcfile_add_search_path(const char *dirname);
56
John Bonesio658f29a2010-11-17 15:28:20 -080057struct srcpos {
David Gibsona4da2e32007-12-18 15:06:42 +110058 int first_line;
59 int first_column;
60 int last_line;
61 int last_column;
John Bonesio658f29a2010-11-17 15:28:20 -080062 struct srcfile_state *file;
Rob Herringc2e70752018-11-28 18:37:35 -060063 struct srcpos *next;
David Gibsoned95d742008-08-07 12:24:17 +100064};
David Gibsona4da2e32007-12-18 15:06:42 +110065
John Bonesio658f29a2010-11-17 15:28:20 -080066#define YYLTYPE struct srcpos
67
68#define YYLLOC_DEFAULT(Current, Rhs, N) \
69 do { \
70 if (N) { \
71 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
72 (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
73 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
74 (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
75 (Current).file = YYRHSLOC(Rhs, N).file; \
76 } else { \
77 (Current).first_line = (Current).last_line = \
78 YYRHSLOC(Rhs, 0).last_line; \
79 (Current).first_column = (Current).last_column = \
80 YYRHSLOC(Rhs, 0).last_column; \
81 (Current).file = YYRHSLOC (Rhs, 0).file; \
82 } \
Rob Herringc2e70752018-11-28 18:37:35 -060083 (Current).next = NULL; \
John Bonesio658f29a2010-11-17 15:28:20 -080084 } while (0)
85
86
John Bonesio658f29a2010-11-17 15:28:20 -080087extern void srcpos_update(struct srcpos *pos, const char *text, int len);
88extern struct srcpos *srcpos_copy(struct srcpos *pos);
Rob Herringc2e70752018-11-28 18:37:35 -060089extern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
90 struct srcpos *old_srcpos);
John Bonesio658f29a2010-11-17 15:28:20 -080091extern char *srcpos_string(struct srcpos *pos);
Rob Herringc2e70752018-11-28 18:37:35 -060092extern char *srcpos_string_first(struct srcpos *pos, int level);
93extern char *srcpos_string_last(struct srcpos *pos, int level);
94
John Bonesio658f29a2010-11-17 15:28:20 -080095
Rob Herring89d12312017-03-21 09:01:08 -050096extern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
97 const char *fmt, va_list va);
98extern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
99 const char *fmt, ...);
John Bonesio658f29a2010-11-17 15:28:20 -0800100
Stephen Warrencd296722012-09-28 21:25:59 +0000101extern void srcpos_set_line(char *f, int l);
102
Rob Herring9130ba82018-02-27 17:40:38 -0600103#endif /* SRCPOS_H */