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