srcpos.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 2007 Jon Loeliger, Freescale Semiconductor, Inc.
  4. */
  5. #ifndef SRCPOS_H
  6. #define SRCPOS_H
  7. #include <stdio.h>
  8. #include <stdbool.h>
  9. #include "util.h"
  10. struct srcfile_state {
  11. FILE *f;
  12. char *name;
  13. char *dir;
  14. int lineno, colno;
  15. struct srcfile_state *prev;
  16. };
  17. extern FILE *depfile; /* = NULL */
  18. extern struct srcfile_state *current_srcfile; /* = NULL */
  19. /**
  20. * Open a source file.
  21. *
  22. * If the source file is a relative pathname, then it is searched for in the
  23. * current directory (the directory of the last source file read) and after
  24. * that in the search path.
  25. *
  26. * We work through the search path in order from the first path specified to
  27. * the last.
  28. *
  29. * If the file is not found, then this function does not return, but calls
  30. * die().
  31. *
  32. * @param fname Filename to search
  33. * @param fullnamep If non-NULL, it is set to the allocated filename of the
  34. * file that was opened. The caller is then responsible
  35. * for freeing the pointer.
  36. * @return pointer to opened FILE
  37. */
  38. FILE *srcfile_relative_open(const char *fname, char **fullnamep);
  39. void srcfile_push(const char *fname);
  40. bool srcfile_pop(void);
  41. /**
  42. * Add a new directory to the search path for input files
  43. *
  44. * The new path is added at the end of the list.
  45. *
  46. * @param dirname Directory to add
  47. */
  48. void srcfile_add_search_path(const char *dirname);
  49. struct srcpos {
  50. int first_line;
  51. int first_column;
  52. int last_line;
  53. int last_column;
  54. struct srcfile_state *file;
  55. struct srcpos *next;
  56. };
  57. #define YYLTYPE struct srcpos
  58. #define YYLLOC_DEFAULT(Current, Rhs, N) \
  59. do { \
  60. if (N) { \
  61. (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
  62. (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \
  63. (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
  64. (Current).last_column = YYRHSLOC (Rhs, N).last_column; \
  65. (Current).file = YYRHSLOC(Rhs, N).file; \
  66. } else { \
  67. (Current).first_line = (Current).last_line = \
  68. YYRHSLOC(Rhs, 0).last_line; \
  69. (Current).first_column = (Current).last_column = \
  70. YYRHSLOC(Rhs, 0).last_column; \
  71. (Current).file = YYRHSLOC (Rhs, 0).file; \
  72. } \
  73. (Current).next = NULL; \
  74. } while (0)
  75. extern void srcpos_update(struct srcpos *pos, const char *text, int len);
  76. extern struct srcpos *srcpos_copy(struct srcpos *pos);
  77. extern struct srcpos *srcpos_extend(struct srcpos *new_srcpos,
  78. struct srcpos *old_srcpos);
  79. extern char *srcpos_string(struct srcpos *pos);
  80. extern char *srcpos_string_first(struct srcpos *pos, int level);
  81. extern char *srcpos_string_last(struct srcpos *pos, int level);
  82. extern void PRINTF(3, 0) srcpos_verror(struct srcpos *pos, const char *prefix,
  83. const char *fmt, va_list va);
  84. extern void PRINTF(3, 4) srcpos_error(struct srcpos *pos, const char *prefix,
  85. const char *fmt, ...);
  86. extern void srcpos_set_line(char *f, int l);
  87. #endif /* SRCPOS_H */