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