Tracks line numbers for includes, and the order in which includes appear. As a dict, an _IncludeState object serves as a mapping between include filename and line number on which that file was included. Call CheckNextIncludeOrder() once for each header in the file, passing in the type cons
| 548 | |
| 549 | |
| 550 | class _IncludeState(dict): |
| 551 | """Tracks line numbers for includes, and the order in which includes appear. |
| 552 | |
| 553 | As a dict, an _IncludeState object serves as a mapping between include |
| 554 | filename and line number on which that file was included. |
| 555 | |
| 556 | Call CheckNextIncludeOrder() once for each header in the file, passing |
| 557 | in the type constants defined above. Calls in an illegal order will |
| 558 | raise an _IncludeError with an appropriate error message. |
| 559 | |
| 560 | """ |
| 561 | # self._section will move monotonically through this set. If it ever |
| 562 | # needs to move backwards, CheckNextIncludeOrder will raise an error. |
| 563 | _INITIAL_SECTION = 0 |
| 564 | _MY_H_SECTION = 1 |
| 565 | _C_SECTION = 2 |
| 566 | _CPP_SECTION = 3 |
| 567 | _OTHER_H_SECTION = 4 |
| 568 | |
| 569 | _TYPE_NAMES = { |
| 570 | _C_SYS_HEADER: 'C system header', |
| 571 | _CPP_SYS_HEADER: 'C++ system header', |
| 572 | _LIKELY_MY_HEADER: 'header this file implements', |
| 573 | _POSSIBLE_MY_HEADER: 'header this file may implement', |
| 574 | _OTHER_HEADER: 'other header', |
| 575 | } |
| 576 | _SECTION_NAMES = { |
| 577 | _INITIAL_SECTION: "... nothing. (This can't be an error.)", |
| 578 | _MY_H_SECTION: 'a header this file implements', |
| 579 | _C_SECTION: 'C system header', |
| 580 | _CPP_SECTION: 'C++ system header', |
| 581 | _OTHER_H_SECTION: 'other header', |
| 582 | } |
| 583 | |
| 584 | def __init__(self): |
| 585 | dict.__init__(self) |
| 586 | self.ResetSection() |
| 587 | |
| 588 | def ResetSection(self): |
| 589 | # The name of the current section. |
| 590 | self._section = self._INITIAL_SECTION |
| 591 | # The path of last found header. |
| 592 | self._last_header = '' |
| 593 | |
| 594 | def SetLastHeader(self, header_path): |
| 595 | self._last_header = header_path |
| 596 | |
| 597 | def CanonicalizeAlphabeticalOrder(self, header_path): |
| 598 | """Returns a path canonicalized for alphabetical comparison. |
| 599 | |
| 600 | - replaces "-" with "_" so they both cmp the same. |
| 601 | - removes '-inl' since we don't require them to be after the main header. |
| 602 | - lowercase everything, just in case. |
| 603 | |
| 604 | Args: |
| 605 | header_path: Path to be canonicalized. |
| 606 | |
| 607 | Returns: |