directives-only.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* CPP Library - directive only preprocessing for distributed compilation.
  2. Copyright (C) 2007-2015 Free Software Foundation, Inc.
  3. Contributed by Ollie Wild <aaw@google.com>.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 3, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "cpplib.h"
  18. #include "internal.h"
  19. /* DO (Directive only) flags. */
  20. #define DO_BOL (1 << 0) /* At the beginning of a logical line. */
  21. #define DO_STRING (1 << 1) /* In a string constant. */
  22. #define DO_CHAR (1 << 2) /* In a character constant. */
  23. #define DO_BLOCK_COMMENT (1 << 3) /* In a block comment. */
  24. #define DO_LINE_COMMENT (1 << 4) /* In a single line "//-style" comment. */
  25. #define DO_LINE_SPECIAL (DO_STRING | DO_CHAR | DO_LINE_COMMENT)
  26. #define DO_SPECIAL (DO_LINE_SPECIAL | DO_BLOCK_COMMENT)
  27. /* Writes out the preprocessed file, handling spacing and paste
  28. avoidance issues. */
  29. void
  30. _cpp_preprocess_dir_only (cpp_reader *pfile,
  31. const struct _cpp_dir_only_callbacks *cb)
  32. {
  33. struct cpp_buffer *buffer;
  34. const unsigned char *cur, *base, *next_line, *rlimit;
  35. cppchar_t c, last_c;
  36. unsigned flags;
  37. linenum_type lines;
  38. int col;
  39. source_location loc;
  40. restart:
  41. /* Buffer initialization ala _cpp_clean_line(). */
  42. buffer = pfile->buffer;
  43. buffer->cur_note = buffer->notes_used = 0;
  44. buffer->cur = buffer->line_base = buffer->next_line;
  45. buffer->need_line = false;
  46. /* This isn't really needed. It prevents a compiler warning, though. */
  47. loc = pfile->line_table->highest_line;
  48. /* Scan initialization. */
  49. next_line = cur = base = buffer->cur;
  50. rlimit = buffer->rlimit;
  51. flags = DO_BOL;
  52. lines = 0;
  53. col = 1;
  54. for (last_c = '\n', c = *cur; cur < rlimit; last_c = c, c = *++cur, ++col)
  55. {
  56. /* Skip over escaped newlines. */
  57. if (__builtin_expect (c == '\\', false))
  58. {
  59. const unsigned char *tmp = cur + 1;
  60. while (is_nvspace (*tmp) && tmp < rlimit)
  61. tmp++;
  62. if (*tmp == '\r')
  63. tmp++;
  64. if (*tmp == '\n' && tmp < rlimit)
  65. {
  66. CPP_INCREMENT_LINE (pfile, 0);
  67. lines++;
  68. col = 0;
  69. cur = tmp;
  70. c = last_c;
  71. continue;
  72. }
  73. }
  74. if (__builtin_expect (last_c == '#', false) && !(flags & DO_SPECIAL))
  75. {
  76. if (c != '#' && (flags & DO_BOL))
  77. {
  78. struct line_maps *line_table;
  79. if (!pfile->state.skipping && next_line != base)
  80. cb->print_lines (lines, base, next_line - base);
  81. /* Prep things for directive handling. */
  82. buffer->next_line = cur;
  83. buffer->need_line = true;
  84. _cpp_get_fresh_line (pfile);
  85. /* Ensure proper column numbering for generated error messages. */
  86. buffer->line_base -= col - 1;
  87. _cpp_handle_directive (pfile, 0 /* ignore indented */);
  88. /* Sanitize the line settings. Duplicate #include's can mess
  89. things up. */
  90. line_table = pfile->line_table;
  91. line_table->highest_location = line_table->highest_line;
  92. /* The if block prevents us from outputing line information when
  93. the file ends with a directive and no newline. Note that we
  94. must use pfile->buffer, not buffer. */
  95. if (pfile->buffer->next_line < pfile->buffer->rlimit)
  96. cb->maybe_print_line (pfile->line_table->highest_line);
  97. goto restart;
  98. }
  99. flags &= ~DO_BOL;
  100. pfile->mi_valid = false;
  101. }
  102. else if (__builtin_expect (last_c == '/', false) \
  103. && !(flags & DO_SPECIAL) && c != '*' && c != '/')
  104. {
  105. /* If a previous slash is not starting a block comment, clear the
  106. DO_BOL flag. */
  107. flags &= ~DO_BOL;
  108. pfile->mi_valid = false;
  109. }
  110. switch (c)
  111. {
  112. case '/':
  113. if ((flags & DO_BLOCK_COMMENT) && last_c == '*')
  114. {
  115. flags &= ~DO_BLOCK_COMMENT;
  116. c = 0;
  117. }
  118. else if (!(flags & DO_SPECIAL) && last_c == '/')
  119. flags |= DO_LINE_COMMENT;
  120. else if (!(flags & DO_SPECIAL))
  121. /* Mark the position for possible error reporting. */
  122. loc = linemap_position_for_column (pfile->line_table, col);
  123. break;
  124. case '*':
  125. if (!(flags & DO_SPECIAL))
  126. {
  127. if (last_c == '/')
  128. flags |= DO_BLOCK_COMMENT;
  129. else
  130. {
  131. flags &= ~DO_BOL;
  132. pfile->mi_valid = false;
  133. }
  134. }
  135. break;
  136. case '\'':
  137. case '"':
  138. {
  139. unsigned state = (c == '"') ? DO_STRING : DO_CHAR;
  140. if (!(flags & DO_SPECIAL))
  141. {
  142. flags |= state;
  143. flags &= ~DO_BOL;
  144. pfile->mi_valid = false;
  145. }
  146. else if ((flags & state) && last_c != '\\')
  147. flags &= ~state;
  148. break;
  149. }
  150. case '\\':
  151. {
  152. if ((flags & (DO_STRING | DO_CHAR)) && last_c == '\\')
  153. c = 0;
  154. if (!(flags & DO_SPECIAL))
  155. {
  156. flags &= ~DO_BOL;
  157. pfile->mi_valid = false;
  158. }
  159. break;
  160. }
  161. case '\n':
  162. CPP_INCREMENT_LINE (pfile, 0);
  163. lines++;
  164. col = 0;
  165. flags &= ~DO_LINE_SPECIAL;
  166. if (!(flags & DO_SPECIAL))
  167. flags |= DO_BOL;
  168. break;
  169. case '#':
  170. next_line = cur;
  171. /* Don't update DO_BOL yet. */
  172. break;
  173. case ' ': case '\t': case '\f': case '\v': case '\0':
  174. break;
  175. default:
  176. if (!(flags & DO_SPECIAL))
  177. {
  178. flags &= ~DO_BOL;
  179. pfile->mi_valid = false;
  180. }
  181. break;
  182. }
  183. }
  184. if (flags & DO_BLOCK_COMMENT)
  185. cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0, "unterminated comment");
  186. if (!pfile->state.skipping && cur != base)
  187. {
  188. /* If the file was not newline terminated, add rlimit, which is
  189. guaranteed to point to a newline, to the end of our range. */
  190. if (cur[-1] != '\n')
  191. {
  192. cur++;
  193. CPP_INCREMENT_LINE (pfile, 0);
  194. lines++;
  195. }
  196. cb->print_lines (lines, base, cur - base);
  197. }
  198. _cpp_pop_buffer (pfile);
  199. if (pfile->buffer)
  200. goto restart;
  201. }