Ninja
depfile_parser.cc
Go to the documentation of this file.
1 /* Generated by re2c 0.13.5 */
2 // Copyright 2011 Google Inc. All Rights Reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "depfile_parser.h"
17 
18 // A note on backslashes in Makefiles, from reading the docs:
19 // Backslash-newline is the line continuation character.
20 // Backslash-# escapes a # (otherwise meaningful as a comment start).
21 // Backslash-% escapes a % (otherwise meaningful as a special).
22 // Finally, quoting the GNU manual, "Backslashes that are not in danger
23 // of quoting ‘%’ characters go unmolested."
24 // How do you end a line with a backslash? The netbsd Make docs suggest
25 // reading the result of a shell command echoing a backslash!
26 //
27 // Rather than implement all of above, we do a simpler thing here:
28 // Backslashes escape a set of characters (see "escapes" defined below),
29 // otherwise they are passed through verbatim.
30 // If anyone actually has depfiles that rely on the more complicated
31 // behavior we can adjust this.
32 bool DepfileParser::Parse(string* content, string* err) {
33  // in: current parser input point.
34  // end: end of input.
35  // parsing_targets: whether we are parsing targets or dependencies.
36  char* in = &(*content)[0];
37  char* end = in + content->size();
38  bool parsing_targets = true;
39  while (in < end) {
40  // out: current output point (typically same as in, but can fall behind
41  // as we de-escape backslashes).
42  char* out = in;
43  // filename: start of the current parsed filename.
44  char* filename = out;
45  for (;;) {
46  // start: beginning of the current parsed span.
47  const char* start = in;
48 
49  {
50  char yych;
51  static const unsigned char yybm[] = {
52  0, 0, 0, 0, 0, 0, 0, 0,
53  0, 0, 0, 0, 0, 0, 0, 0,
54  0, 0, 0, 0, 0, 0, 0, 0,
55  0, 0, 0, 0, 0, 0, 0, 0,
56  0, 128, 128, 128, 128, 128, 128, 128,
57  128, 128, 128, 128, 128, 128, 128, 128,
58  128, 128, 128, 128, 128, 128, 128, 128,
59  128, 128, 128, 128, 128, 128, 0, 0,
60  128, 128, 128, 128, 128, 128, 128, 128,
61  128, 128, 128, 128, 128, 128, 128, 128,
62  128, 128, 128, 128, 128, 128, 128, 128,
63  128, 128, 128, 0, 0, 0, 0, 128,
64  0, 128, 128, 128, 128, 128, 128, 128,
65  128, 128, 128, 128, 128, 128, 128, 128,
66  128, 128, 128, 128, 128, 128, 128, 128,
67  128, 128, 128, 0, 0, 0, 128, 0,
68  0, 0, 0, 0, 0, 0, 0, 0,
69  0, 0, 0, 0, 0, 0, 0, 0,
70  0, 0, 0, 0, 0, 0, 0, 0,
71  0, 0, 0, 0, 0, 0, 0, 0,
72  0, 0, 0, 0, 0, 0, 0, 0,
73  0, 0, 0, 0, 0, 0, 0, 0,
74  0, 0, 0, 0, 0, 0, 0, 0,
75  0, 0, 0, 0, 0, 0, 0, 0,
76  0, 0, 0, 0, 0, 0, 0, 0,
77  0, 0, 0, 0, 0, 0, 0, 0,
78  0, 0, 0, 0, 0, 0, 0, 0,
79  0, 0, 0, 0, 0, 0, 0, 0,
80  0, 0, 0, 0, 0, 0, 0, 0,
81  0, 0, 0, 0, 0, 0, 0, 0,
82  0, 0, 0, 0, 0, 0, 0, 0,
83  0, 0, 0, 0, 0, 0, 0, 0,
84  };
85 
86  yych = *in;
87  if (yych <= '[') {
88  if (yych <= '$') {
89  if (yych <= 0x00) goto yy7;
90  if (yych <= ' ') goto yy9;
91  if (yych <= '#') goto yy6;
92  goto yy4;
93  } else {
94  if (yych <= '=') goto yy6;
95  if (yych <= '?') goto yy9;
96  if (yych <= 'Z') goto yy6;
97  goto yy9;
98  }
99  } else {
100  if (yych <= '`') {
101  if (yych <= '\\') goto yy2;
102  if (yych == '_') goto yy6;
103  goto yy9;
104  } else {
105  if (yych <= 'z') goto yy6;
106  if (yych == '~') goto yy6;
107  goto yy9;
108  }
109  }
110 yy2:
111  ++in;
112  if ((yych = *in) <= '#') {
113  if (yych <= '\n') {
114  if (yych <= 0x00) goto yy3;
115  if (yych <= '\t') goto yy14;
116  } else {
117  if (yych == ' ') goto yy16;
118  if (yych <= '"') goto yy14;
119  goto yy16;
120  }
121  } else {
122  if (yych <= 'Z') {
123  if (yych == '*') goto yy16;
124  goto yy14;
125  } else {
126  if (yych <= '\\') goto yy16;
127  if (yych == '|') goto yy16;
128  goto yy14;
129  }
130  }
131 yy3:
132  {
133  // For any other character (e.g. whitespace), swallow it here,
134  // allowing the outer logic to loop around again.
135  break;
136  }
137 yy4:
138  ++in;
139  if ((yych = *in) == '$') goto yy12;
140  goto yy11;
141 yy5:
142  {
143  // Got a span of plain text.
144  int len = (int)(in - start);
145  // Need to shift it over if we're overwriting backslashes.
146  if (out < start)
147  memmove(out, start, len);
148  out += len;
149  continue;
150  }
151 yy6:
152  yych = *++in;
153  goto yy11;
154 yy7:
155  ++in;
156  {
157  break;
158  }
159 yy9:
160  yych = *++in;
161  goto yy3;
162 yy10:
163  ++in;
164  yych = *in;
165 yy11:
166  if (yybm[0+yych] & 128) {
167  goto yy10;
168  }
169  goto yy5;
170 yy12:
171  ++in;
172  if (yybm[0+(yych = *in)] & 128) {
173  goto yy10;
174  }
175  {
176  // De-escape dollar character.
177  *out++ = '$';
178  continue;
179  }
180 yy14:
181  ++in;
182  {
183  // Let backslash before other characters through verbatim.
184  *out++ = '\\';
185  *out++ = yych;
186  continue;
187  }
188 yy16:
189  ++in;
190  {
191  // De-escape backslashed character.
192  *out++ = yych;
193  continue;
194  }
195  }
196 
197  }
198 
199  int len = (int)(out - filename);
200  const bool is_target = parsing_targets;
201  if (len > 0 && filename[len - 1] == ':') {
202  len--; // Strip off trailing colon, if any.
203  parsing_targets = false;
204  }
205 
206  if (len == 0)
207  continue;
208 
209  if (!is_target) {
210  ins_.push_back(StringPiece(filename, len));
211  } else if (!out_.str_) {
212  out_ = StringPiece(filename, len);
213  } else if (out_ != StringPiece(filename, len)) {
214  *err = "depfile has multiple output paths.";
215  return false;
216  }
217  }
218  return true;
219 }
const char * str_
Definition: string_piece.h:49
StringPiece represents a slice of a string whose memory is managed externally.
Definition: string_piece.h:27
bool Parse(string *content, string *err)
Parse an input file.
vector< StringPiece > ins_
StringPiece out_