]>
Commit | Line | Data |
---|---|---|
30fdf114 LG |
1 | ## @file\r |
2 | # preprocess source file\r | |
3 | #\r | |
231fdbb2 | 4 | # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r |
30fdf114 | 5 | #\r |
40d841f6 | 6 | # This program and the accompanying materials\r |
30fdf114 LG |
7 | # are licensed and made available under the terms and conditions of the BSD License\r |
8 | # which accompanies this distribution. The full text of the license may be found at\r | |
9 | # http://opensource.org/licenses/bsd-license.php\r | |
10 | #\r | |
11 | # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | #\r | |
14 | \r | |
15 | ##\r | |
16 | # Import Modules\r | |
17 | #\r | |
18 | \r | |
72443dd2 | 19 | from __future__ import print_function\r |
30fdf114 | 20 | import re\r |
1be2ed90 | 21 | import Common.LongFilePathOs as os\r |
30fdf114 LG |
22 | import sys\r |
23 | \r | |
24 | import antlr3\r | |
25 | from CLexer import CLexer\r | |
26 | from CParser import CParser\r | |
27 | \r | |
28 | import FileProfile\r | |
29 | from CodeFragment import Comment\r | |
30 | from CodeFragment import PP_Directive\r | |
31 | from ParserWarning import Warning\r | |
32 | \r | |
33 | \r | |
34 | ##define T_CHAR_SPACE ' '\r | |
35 | ##define T_CHAR_NULL '\0'\r | |
36 | ##define T_CHAR_CR '\r'\r | |
37 | ##define T_CHAR_TAB '\t'\r | |
38 | ##define T_CHAR_LF '\n'\r | |
39 | ##define T_CHAR_SLASH '/'\r | |
40 | ##define T_CHAR_BACKSLASH '\\'\r | |
41 | ##define T_CHAR_DOUBLE_QUOTE '\"'\r | |
42 | ##define T_CHAR_SINGLE_QUOTE '\''\r | |
43 | ##define T_CHAR_STAR '*'\r | |
44 | ##define T_CHAR_HASH '#'\r | |
45 | \r | |
46 | (T_CHAR_SPACE, T_CHAR_NULL, T_CHAR_CR, T_CHAR_TAB, T_CHAR_LF, T_CHAR_SLASH, \\r | |
47 | T_CHAR_BACKSLASH, T_CHAR_DOUBLE_QUOTE, T_CHAR_SINGLE_QUOTE, T_CHAR_STAR, T_CHAR_HASH) = \\r | |
48 | (' ', '\0', '\r', '\t', '\n', '/', '\\', '\"', '\'', '*', '#')\r | |
49 | \r | |
50 | SEPERATOR_TUPLE = ('=', '|', ',', '{', '}') \r | |
51 | \r | |
52 | (T_COMMENT_TWO_SLASH, T_COMMENT_SLASH_STAR) = (0, 1)\r | |
53 | \r | |
54 | (T_PP_INCLUDE, T_PP_DEFINE, T_PP_OTHERS) = (0, 1, 2)\r | |
55 | \r | |
56 | ## The collector for source code fragments.\r | |
57 | #\r | |
58 | # PreprocessFile method should be called prior to ParseFile\r | |
59 | #\r | |
60 | # GetNext*** procedures mean these procedures will get next token first, then make judgement.\r | |
61 | # Get*** procedures mean these procedures will make judgement on current token only.\r | |
62 | # \r | |
63 | class CodeFragmentCollector:\r | |
64 | ## The constructor\r | |
65 | #\r | |
66 | # @param self The object pointer\r | |
67 | # @param FileName The file that to be parsed\r | |
68 | #\r | |
69 | def __init__(self, FileName):\r | |
70 | self.Profile = FileProfile.FileProfile(FileName)\r | |
71 | self.Profile.FileLinesList.append(T_CHAR_LF)\r | |
72 | self.FileName = FileName\r | |
73 | self.CurrentLineNumber = 1\r | |
74 | self.CurrentOffsetWithinLine = 0\r | |
75 | \r | |
76 | self.__Token = ""\r | |
77 | self.__SkippedChars = ""\r | |
78 | \r | |
30fdf114 LG |
79 | ## __EndOfFile() method\r |
80 | #\r | |
81 | # Judge current buffer pos is at file end\r | |
82 | #\r | |
83 | # @param self The object pointer\r | |
84 | # @retval True Current File buffer position is at file end\r | |
85 | # @retval False Current File buffer position is NOT at file end\r | |
86 | #\r | |
87 | def __EndOfFile(self):\r | |
88 | NumberOfLines = len(self.Profile.FileLinesList)\r | |
89 | SizeOfLastLine = NumberOfLines\r | |
90 | if NumberOfLines > 0:\r | |
91 | SizeOfLastLine = len(self.Profile.FileLinesList[-1])\r | |
92 | \r | |
93 | if self.CurrentLineNumber == NumberOfLines and self.CurrentOffsetWithinLine >= SizeOfLastLine - 1:\r | |
94 | return True\r | |
95 | elif self.CurrentLineNumber > NumberOfLines:\r | |
96 | return True\r | |
97 | else:\r | |
98 | return False\r | |
99 | \r | |
100 | ## __EndOfLine() method\r | |
101 | #\r | |
102 | # Judge current buffer pos is at line end\r | |
103 | #\r | |
104 | # @param self The object pointer\r | |
105 | # @retval True Current File buffer position is at line end\r | |
106 | # @retval False Current File buffer position is NOT at line end\r | |
107 | #\r | |
108 | def __EndOfLine(self):\r | |
109 | SizeOfCurrentLine = len(self.Profile.FileLinesList[self.CurrentLineNumber - 1])\r | |
110 | if self.CurrentOffsetWithinLine >= SizeOfCurrentLine - 1:\r | |
111 | return True\r | |
112 | else:\r | |
113 | return False\r | |
114 | \r | |
115 | ## Rewind() method\r | |
116 | #\r | |
117 | # Reset file data buffer to the initial state\r | |
118 | #\r | |
119 | # @param self The object pointer\r | |
120 | #\r | |
121 | def Rewind(self):\r | |
122 | self.CurrentLineNumber = 1\r | |
123 | self.CurrentOffsetWithinLine = 0\r | |
124 | \r | |
125 | ## __UndoOneChar() method\r | |
126 | #\r | |
127 | # Go back one char in the file buffer\r | |
128 | #\r | |
129 | # @param self The object pointer\r | |
130 | # @retval True Successfully go back one char\r | |
131 | # @retval False Not able to go back one char as file beginning reached\r | |
132 | # \r | |
133 | def __UndoOneChar(self):\r | |
134 | \r | |
135 | if self.CurrentLineNumber == 1 and self.CurrentOffsetWithinLine == 0:\r | |
136 | return False\r | |
137 | elif self.CurrentOffsetWithinLine == 0:\r | |
138 | self.CurrentLineNumber -= 1\r | |
139 | self.CurrentOffsetWithinLine = len(self.__CurrentLine()) - 1\r | |
140 | else:\r | |
141 | self.CurrentOffsetWithinLine -= 1\r | |
142 | return True\r | |
143 | \r | |
144 | ## __GetOneChar() method\r | |
145 | #\r | |
146 | # Move forward one char in the file buffer\r | |
147 | #\r | |
148 | # @param self The object pointer\r | |
149 | # \r | |
150 | def __GetOneChar(self):\r | |
151 | if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r | |
152 | self.CurrentLineNumber += 1\r | |
153 | self.CurrentOffsetWithinLine = 0\r | |
154 | else:\r | |
155 | self.CurrentOffsetWithinLine += 1\r | |
156 | \r | |
157 | ## __CurrentChar() method\r | |
158 | #\r | |
159 | # Get the char pointed to by the file buffer pointer\r | |
160 | #\r | |
161 | # @param self The object pointer\r | |
162 | # @retval Char Current char\r | |
163 | # \r | |
164 | def __CurrentChar(self):\r | |
165 | CurrentChar = self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine]\r | |
166 | # if CurrentChar > 255:\r | |
167 | # raise Warning("Non-Ascii char found At Line %d, offset %d" % (self.CurrentLineNumber, self.CurrentOffsetWithinLine), self.FileName, self.CurrentLineNumber)\r | |
168 | return CurrentChar\r | |
169 | \r | |
170 | ## __NextChar() method\r | |
171 | #\r | |
172 | # Get the one char pass the char pointed to by the file buffer pointer\r | |
173 | #\r | |
174 | # @param self The object pointer\r | |
175 | # @retval Char Next char\r | |
176 | #\r | |
177 | def __NextChar(self):\r | |
178 | if self.CurrentOffsetWithinLine == len(self.Profile.FileLinesList[self.CurrentLineNumber - 1]) - 1:\r | |
179 | return self.Profile.FileLinesList[self.CurrentLineNumber][0]\r | |
180 | else:\r | |
181 | return self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine + 1]\r | |
182 | \r | |
183 | ## __SetCurrentCharValue() method\r | |
184 | #\r | |
185 | # Modify the value of current char\r | |
186 | #\r | |
187 | # @param self The object pointer\r | |
188 | # @param Value The new value of current char\r | |
189 | #\r | |
190 | def __SetCurrentCharValue(self, Value):\r | |
191 | self.Profile.FileLinesList[self.CurrentLineNumber - 1][self.CurrentOffsetWithinLine] = Value\r | |
192 | \r | |
193 | ## __SetCharValue() method\r | |
194 | #\r | |
195 | # Modify the value of current char\r | |
196 | #\r | |
197 | # @param self The object pointer\r | |
198 | # @param Value The new value of current char\r | |
199 | #\r | |
200 | def __SetCharValue(self, Line, Offset, Value):\r | |
201 | self.Profile.FileLinesList[Line - 1][Offset] = Value\r | |
202 | \r | |
203 | ## __CurrentLine() method\r | |
204 | #\r | |
205 | # Get the list that contains current line contents\r | |
206 | #\r | |
207 | # @param self The object pointer\r | |
208 | # @retval List current line contents\r | |
209 | #\r | |
210 | def __CurrentLine(self):\r | |
211 | return self.Profile.FileLinesList[self.CurrentLineNumber - 1]\r | |
212 | \r | |
213 | ## __InsertComma() method\r | |
214 | #\r | |
215 | # Insert ',' to replace PP\r | |
216 | #\r | |
217 | # @param self The object pointer\r | |
218 | # @retval List current line contents\r | |
219 | #\r | |
220 | def __InsertComma(self, Line):\r | |
221 | \r | |
222 | \r | |
223 | if self.Profile.FileLinesList[Line - 1][0] != T_CHAR_HASH:\r | |
224 | BeforeHashPart = str(self.Profile.FileLinesList[Line - 1]).split(T_CHAR_HASH)[0]\r | |
225 | if BeforeHashPart.rstrip().endswith(T_CHAR_COMMA) or BeforeHashPart.rstrip().endswith(';'):\r | |
226 | return\r | |
227 | \r | |
228 | if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(','):\r | |
229 | return\r | |
230 | \r | |
231 | if Line - 2 >= 0 and str(self.Profile.FileLinesList[Line - 2]).rstrip().endswith(';'):\r | |
232 | return\r | |
233 | \r | |
234 | if str(self.Profile.FileLinesList[Line]).lstrip().startswith(',') or str(self.Profile.FileLinesList[Line]).lstrip().startswith(';'):\r | |
235 | return\r | |
236 | \r | |
237 | self.Profile.FileLinesList[Line - 1].insert(self.CurrentOffsetWithinLine, ',')\r | |
238 | \r | |
239 | ## PreprocessFile() method\r | |
240 | #\r | |
241 | # Preprocess file contents, replace comments with spaces.\r | |
242 | # In the end, rewind the file buffer pointer to the beginning\r | |
243 | # BUGBUG: No !include statement processing contained in this procedure\r | |
244 | # !include statement should be expanded at the same FileLinesList[CurrentLineNumber - 1]\r | |
245 | #\r | |
246 | # @param self The object pointer\r | |
247 | # \r | |
248 | def PreprocessFile(self):\r | |
249 | \r | |
250 | self.Rewind()\r | |
251 | InComment = False\r | |
252 | DoubleSlashComment = False\r | |
253 | HashComment = False\r | |
254 | PPExtend = False\r | |
255 | CommentObj = None\r | |
256 | PPDirectiveObj = None\r | |
257 | # HashComment in quoted string " " is ignored.\r | |
258 | InString = False\r | |
259 | InCharLiteral = False \r | |
260 | \r | |
261 | self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile]\r | |
262 | while not self.__EndOfFile():\r | |
263 | \r | |
264 | if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE:\r | |
265 | InString = not InString\r | |
266 | \r | |
267 | if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE:\r | |
268 | InCharLiteral = not InCharLiteral\r | |
269 | # meet new line, then no longer in a comment for // and '#'\r | |
270 | if self.__CurrentChar() == T_CHAR_LF:\r | |
4231a819 | 271 | if HashComment and PPDirectiveObj is not None:\r |
30fdf114 LG |
272 | if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r |
273 | PPDirectiveObj.Content += T_CHAR_LF\r | |
274 | PPExtend = True\r | |
275 | else:\r | |
276 | PPExtend = False\r | |
277 | \r | |
278 | EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
279 | \r | |
280 | if InComment and DoubleSlashComment:\r | |
281 | InComment = False\r | |
282 | DoubleSlashComment = False\r | |
283 | CommentObj.Content += T_CHAR_LF\r | |
284 | CommentObj.EndPos = EndLinePos\r | |
285 | FileProfile.CommentList.append(CommentObj)\r | |
286 | CommentObj = None\r | |
287 | if InComment and HashComment and not PPExtend:\r | |
288 | InComment = False\r | |
289 | HashComment = False\r | |
290 | PPDirectiveObj.Content += T_CHAR_LF\r | |
291 | PPDirectiveObj.EndPos = EndLinePos\r | |
292 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
293 | PPDirectiveObj = None\r | |
294 | \r | |
295 | if InString or InCharLiteral:\r | |
296 | CurrentLine = "".join(self.__CurrentLine())\r | |
297 | if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r | |
298 | SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH)\r | |
299 | self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE)\r | |
300 | \r | |
301 | if InComment and not DoubleSlashComment and not HashComment:\r | |
302 | CommentObj.Content += T_CHAR_LF\r | |
303 | self.CurrentLineNumber += 1\r | |
304 | self.CurrentOffsetWithinLine = 0 \r | |
305 | # check for */ comment end\r | |
306 | elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r | |
307 | CommentObj.Content += self.__CurrentChar()\r | |
308 | # self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
309 | self.__GetOneChar()\r | |
310 | CommentObj.Content += self.__CurrentChar()\r | |
311 | # self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
312 | CommentObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
313 | FileProfile.CommentList.append(CommentObj)\r | |
314 | CommentObj = None\r | |
315 | self.__GetOneChar()\r | |
316 | InComment = False\r | |
317 | # set comments to spaces\r | |
318 | elif InComment: \r | |
319 | if HashComment:\r | |
320 | # // follows hash PP directive\r | |
321 | if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r | |
322 | InComment = False\r | |
323 | HashComment = False\r | |
324 | PPDirectiveObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine - 1)\r | |
325 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
326 | PPDirectiveObj = None\r | |
327 | continue\r | |
328 | else:\r | |
329 | PPDirectiveObj.Content += self.__CurrentChar()\r | |
330 | if PPExtend:\r | |
331 | self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
332 | else:\r | |
333 | CommentObj.Content += self.__CurrentChar()\r | |
334 | # self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
335 | self.__GetOneChar()\r | |
336 | # check for // comment\r | |
337 | elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r | |
338 | InComment = True\r | |
339 | DoubleSlashComment = True\r | |
340 | CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)\r | |
341 | # check for '#' comment\r | |
342 | elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral:\r | |
343 | InComment = True\r | |
344 | HashComment = True \r | |
345 | PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None)\r | |
346 | # check for /* comment start\r | |
347 | elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r | |
348 | CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)\r | |
349 | CommentObj.Content += self.__CurrentChar()\r | |
350 | # self.__SetCurrentCharValue( T_CHAR_SPACE)\r | |
351 | self.__GetOneChar()\r | |
352 | CommentObj.Content += self.__CurrentChar()\r | |
353 | # self.__SetCurrentCharValue( T_CHAR_SPACE)\r | |
354 | self.__GetOneChar()\r | |
355 | InComment = True\r | |
356 | else:\r | |
357 | self.__GetOneChar()\r | |
358 | \r | |
359 | EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
360 | \r | |
361 | if InComment and DoubleSlashComment:\r | |
362 | CommentObj.EndPos = EndLinePos\r | |
363 | FileProfile.CommentList.append(CommentObj)\r | |
364 | if InComment and HashComment and not PPExtend:\r | |
365 | PPDirectiveObj.EndPos = EndLinePos\r | |
366 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
367 | \r | |
368 | self.Rewind()\r | |
369 | \r | |
370 | def PreprocessFileWithClear(self):\r | |
371 | \r | |
372 | self.Rewind()\r | |
373 | InComment = False\r | |
374 | DoubleSlashComment = False\r | |
375 | HashComment = False\r | |
376 | PPExtend = False\r | |
377 | CommentObj = None\r | |
378 | PPDirectiveObj = None\r | |
379 | # HashComment in quoted string " " is ignored.\r | |
380 | InString = False\r | |
381 | InCharLiteral = False \r | |
382 | \r | |
383 | self.Profile.FileLinesList = [list(s) for s in self.Profile.FileLinesListFromFile]\r | |
384 | while not self.__EndOfFile():\r | |
385 | \r | |
386 | if not InComment and self.__CurrentChar() == T_CHAR_DOUBLE_QUOTE:\r | |
387 | InString = not InString\r | |
388 | \r | |
389 | if not InComment and self.__CurrentChar() == T_CHAR_SINGLE_QUOTE:\r | |
390 | InCharLiteral = not InCharLiteral\r | |
391 | # meet new line, then no longer in a comment for // and '#'\r | |
392 | if self.__CurrentChar() == T_CHAR_LF:\r | |
4231a819 | 393 | if HashComment and PPDirectiveObj is not None:\r |
30fdf114 LG |
394 | if PPDirectiveObj.Content.rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r |
395 | PPDirectiveObj.Content += T_CHAR_LF\r | |
396 | PPExtend = True\r | |
397 | else:\r | |
398 | PPExtend = False\r | |
399 | \r | |
400 | EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
401 | \r | |
402 | if InComment and DoubleSlashComment:\r | |
403 | InComment = False\r | |
404 | DoubleSlashComment = False\r | |
405 | CommentObj.Content += T_CHAR_LF\r | |
406 | CommentObj.EndPos = EndLinePos\r | |
407 | FileProfile.CommentList.append(CommentObj)\r | |
408 | CommentObj = None\r | |
409 | if InComment and HashComment and not PPExtend:\r | |
410 | InComment = False\r | |
411 | HashComment = False\r | |
412 | PPDirectiveObj.Content += T_CHAR_LF\r | |
413 | PPDirectiveObj.EndPos = EndLinePos\r | |
414 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
415 | PPDirectiveObj = None\r | |
416 | \r | |
417 | if InString or InCharLiteral:\r | |
418 | CurrentLine = "".join(self.__CurrentLine())\r | |
419 | if CurrentLine.rstrip(T_CHAR_LF).rstrip(T_CHAR_CR).endswith(T_CHAR_BACKSLASH):\r | |
420 | SlashIndex = CurrentLine.rindex(T_CHAR_BACKSLASH)\r | |
421 | self.__SetCharValue(self.CurrentLineNumber, SlashIndex, T_CHAR_SPACE)\r | |
422 | \r | |
423 | if InComment and not DoubleSlashComment and not HashComment:\r | |
424 | CommentObj.Content += T_CHAR_LF\r | |
425 | self.CurrentLineNumber += 1\r | |
426 | self.CurrentOffsetWithinLine = 0 \r | |
427 | # check for */ comment end\r | |
428 | elif InComment and not DoubleSlashComment and not HashComment and self.__CurrentChar() == T_CHAR_STAR and self.__NextChar() == T_CHAR_SLASH:\r | |
429 | CommentObj.Content += self.__CurrentChar()\r | |
430 | self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
431 | self.__GetOneChar()\r | |
432 | CommentObj.Content += self.__CurrentChar()\r | |
433 | self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
434 | CommentObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
435 | FileProfile.CommentList.append(CommentObj)\r | |
436 | CommentObj = None\r | |
437 | self.__GetOneChar()\r | |
438 | InComment = False\r | |
439 | # set comments to spaces\r | |
440 | elif InComment: \r | |
441 | if HashComment:\r | |
442 | # // follows hash PP directive\r | |
443 | if self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r | |
444 | InComment = False\r | |
445 | HashComment = False\r | |
446 | PPDirectiveObj.EndPos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine - 1)\r | |
447 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
448 | PPDirectiveObj = None\r | |
449 | continue\r | |
450 | else:\r | |
451 | PPDirectiveObj.Content += self.__CurrentChar()\r | |
452 | # if PPExtend:\r | |
453 | # self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
454 | else:\r | |
455 | CommentObj.Content += self.__CurrentChar()\r | |
456 | self.__SetCurrentCharValue(T_CHAR_SPACE)\r | |
457 | self.__GetOneChar()\r | |
458 | # check for // comment\r | |
459 | elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_SLASH:\r | |
460 | InComment = True\r | |
461 | DoubleSlashComment = True\r | |
462 | CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_TWO_SLASH)\r | |
463 | # check for '#' comment\r | |
464 | elif self.__CurrentChar() == T_CHAR_HASH and not InString and not InCharLiteral:\r | |
465 | InComment = True\r | |
466 | HashComment = True \r | |
467 | PPDirectiveObj = PP_Directive('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None)\r | |
468 | # check for /* comment start\r | |
469 | elif self.__CurrentChar() == T_CHAR_SLASH and self.__NextChar() == T_CHAR_STAR:\r | |
470 | CommentObj = Comment('', (self.CurrentLineNumber, self.CurrentOffsetWithinLine), None, T_COMMENT_SLASH_STAR)\r | |
471 | CommentObj.Content += self.__CurrentChar()\r | |
472 | self.__SetCurrentCharValue( T_CHAR_SPACE)\r | |
473 | self.__GetOneChar()\r | |
474 | CommentObj.Content += self.__CurrentChar()\r | |
475 | self.__SetCurrentCharValue( T_CHAR_SPACE)\r | |
476 | self.__GetOneChar()\r | |
477 | InComment = True\r | |
478 | else:\r | |
479 | self.__GetOneChar()\r | |
480 | \r | |
481 | EndLinePos = (self.CurrentLineNumber, self.CurrentOffsetWithinLine)\r | |
482 | \r | |
483 | if InComment and DoubleSlashComment:\r | |
484 | CommentObj.EndPos = EndLinePos\r | |
485 | FileProfile.CommentList.append(CommentObj)\r | |
486 | if InComment and HashComment and not PPExtend:\r | |
487 | PPDirectiveObj.EndPos = EndLinePos\r | |
488 | FileProfile.PPDirectiveList.append(PPDirectiveObj)\r | |
489 | self.Rewind()\r | |
490 | \r | |
491 | ## ParseFile() method\r | |
492 | #\r | |
493 | # Parse the file profile buffer to extract fd, fv ... information\r | |
494 | # Exception will be raised if syntax error found\r | |
495 | #\r | |
496 | # @param self The object pointer\r | |
497 | #\r | |
498 | def ParseFile(self):\r | |
499 | self.PreprocessFile()\r | |
500 | # restore from ListOfList to ListOfString\r | |
501 | self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r | |
502 | FileStringContents = ''\r | |
503 | for fileLine in self.Profile.FileLinesList:\r | |
504 | FileStringContents += fileLine\r | |
505 | cStream = antlr3.StringStream(FileStringContents)\r | |
506 | lexer = CLexer(cStream)\r | |
507 | tStream = antlr3.CommonTokenStream(lexer)\r | |
508 | parser = CParser(tStream)\r | |
509 | parser.translation_unit()\r | |
510 | \r | |
511 | def ParseFileWithClearedPPDirective(self):\r | |
512 | self.PreprocessFileWithClear()\r | |
513 | # restore from ListOfList to ListOfString\r | |
514 | self.Profile.FileLinesList = ["".join(list) for list in self.Profile.FileLinesList]\r | |
515 | FileStringContents = ''\r | |
516 | for fileLine in self.Profile.FileLinesList:\r | |
517 | FileStringContents += fileLine\r | |
518 | cStream = antlr3.StringStream(FileStringContents)\r | |
519 | lexer = CLexer(cStream)\r | |
520 | tStream = antlr3.CommonTokenStream(lexer)\r | |
521 | parser = CParser(tStream)\r | |
522 | parser.translation_unit()\r | |
523 | \r | |
524 | def CleanFileProfileBuffer(self):\r | |
525 | FileProfile.CommentList = []\r | |
526 | FileProfile.PPDirectiveList = []\r | |
527 | FileProfile.PredicateExpressionList = []\r | |
528 | FileProfile.FunctionDefinitionList = []\r | |
529 | FileProfile.VariableDeclarationList = []\r | |
530 | FileProfile.EnumerationDefinitionList = []\r | |
531 | FileProfile.StructUnionDefinitionList = []\r | |
532 | FileProfile.TypedefDefinitionList = []\r | |
533 | FileProfile.FunctionCallingList = []\r | |
534 | \r | |
535 | def PrintFragments(self):\r | |
536 | \r | |
72443dd2 | 537 | print('################# ' + self.FileName + '#####################')\r |
30fdf114 | 538 | \r |
72443dd2 GL |
539 | print('/****************************************/')\r |
540 | print('/*************** COMMENTS ***************/')\r | |
541 | print('/****************************************/')\r | |
30fdf114 | 542 | for comment in FileProfile.CommentList:\r |
72443dd2 | 543 | print(str(comment.StartPos) + comment.Content)\r |
30fdf114 | 544 | \r |
72443dd2 GL |
545 | print('/****************************************/')\r |
546 | print('/********* PREPROCESS DIRECTIVES ********/')\r | |
547 | print('/****************************************/')\r | |
30fdf114 | 548 | for pp in FileProfile.PPDirectiveList:\r |
72443dd2 | 549 | print(str(pp.StartPos) + pp.Content)\r |
30fdf114 | 550 | \r |
72443dd2 GL |
551 | print('/****************************************/')\r |
552 | print('/********* VARIABLE DECLARATIONS ********/')\r | |
553 | print('/****************************************/')\r | |
30fdf114 | 554 | for var in FileProfile.VariableDeclarationList:\r |
72443dd2 | 555 | print(str(var.StartPos) + var.Modifier + ' '+ var.Declarator)\r |
30fdf114 | 556 | \r |
72443dd2 GL |
557 | print('/****************************************/')\r |
558 | print('/********* FUNCTION DEFINITIONS *********/')\r | |
559 | print('/****************************************/')\r | |
30fdf114 | 560 | for func in FileProfile.FunctionDefinitionList:\r |
72443dd2 | 561 | print(str(func.StartPos) + func.Modifier + ' '+ func.Declarator + ' ' + str(func.NamePos))\r |
30fdf114 | 562 | \r |
72443dd2 GL |
563 | print('/****************************************/')\r |
564 | print('/************ ENUMERATIONS **************/')\r | |
565 | print('/****************************************/')\r | |
30fdf114 | 566 | for enum in FileProfile.EnumerationDefinitionList:\r |
72443dd2 | 567 | print(str(enum.StartPos) + enum.Content)\r |
30fdf114 | 568 | \r |
72443dd2 GL |
569 | print('/****************************************/')\r |
570 | print('/*********** STRUCTS/UNIONS *************/')\r | |
571 | print('/****************************************/')\r | |
30fdf114 | 572 | for su in FileProfile.StructUnionDefinitionList:\r |
72443dd2 | 573 | print(str(su.StartPos) + su.Content)\r |
30fdf114 | 574 | \r |
72443dd2 GL |
575 | print('/****************************************/')\r |
576 | print('/********* PREDICATE EXPRESSIONS ********/')\r | |
577 | print('/****************************************/')\r | |
30fdf114 | 578 | for predexp in FileProfile.PredicateExpressionList:\r |
72443dd2 | 579 | print(str(predexp.StartPos) + predexp.Content)\r |
30fdf114 | 580 | \r |
72443dd2 GL |
581 | print('/****************************************/')\r |
582 | print('/************** TYPEDEFS ****************/')\r | |
583 | print('/****************************************/')\r | |
30fdf114 | 584 | for typedef in FileProfile.TypedefDefinitionList:\r |
72443dd2 | 585 | print(str(typedef.StartPos) + typedef.ToType)\r |
30fdf114 LG |
586 | \r |
587 | if __name__ == "__main__":\r | |
588 | \r | |
589 | collector = CodeFragmentCollector(sys.argv[1])\r | |
590 | collector.PreprocessFile()\r | |
72443dd2 | 591 | print("For Test.")\r |