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