Checker that validates system/platform headers are wrapped in IWYU pragma blocks.
| 55 | |
| 56 | |
| 57 | class IwyuPragmaBlockChecker(FileContentChecker): |
| 58 | """Checker that validates system/platform headers are wrapped in IWYU pragma blocks.""" |
| 59 | |
| 60 | def __init__(self, all_headers: frozenset[str] | None = None): |
| 61 | """Initialize the checker. |
| 62 | |
| 63 | Args: |
| 64 | all_headers: Optional frozenset of all header file paths in the project |
| 65 | (used for cross-file validation) |
| 66 | """ |
| 67 | self.violations = CheckerResults() |
| 68 | self.all_headers = all_headers or frozenset() |
| 69 | |
| 70 | def should_process_file(self, file_path: str) -> bool: |
| 71 | """Only process C++ header files (.h, .hpp).""" |
| 72 | path = Path(file_path) |
| 73 | |
| 74 | # Only check header files |
| 75 | if path.suffix not in {".h", ".hpp", ".hh", ".hxx"}: |
| 76 | return False |
| 77 | |
| 78 | # Skip third-party code |
| 79 | if "/third_party/" in str(path).replace("\\", "/"): |
| 80 | return False |
| 81 | |
| 82 | # Skip stub platform (test infrastructure) |
| 83 | if "/platforms/stub/" in str(path).replace("\\", "/"): |
| 84 | return False |
| 85 | |
| 86 | # Only process files in src/ directory |
| 87 | try: |
| 88 | path.relative_to(PROJECT_ROOT / "src") |
| 89 | return True |
| 90 | except ValueError: |
| 91 | return False |
| 92 | |
| 93 | def check_file_content(self, file_content: FileContent) -> list[str]: |
| 94 | """Check that system/platform headers are wrapped in IWYU pragma blocks.""" |
| 95 | violations: list[str] = [] |
| 96 | in_pragma_block = False |
| 97 | line_num = 0 |
| 98 | |
| 99 | # Determine if this file is in fl/ directory |
| 100 | file_path_normalized = str(Path(file_content.path)).replace("\\", "/") |
| 101 | is_fl_file = "/src/fl/" in file_path_normalized |
| 102 | |
| 103 | for line in file_content.lines: |
| 104 | line_num += 1 |
| 105 | |
| 106 | # Track IWYU pragma block state |
| 107 | if IWYU_PRAGMA_BEGIN.match(line): |
| 108 | in_pragma_block = True |
| 109 | continue |
| 110 | if IWYU_PRAGMA_END.match(line): |
| 111 | in_pragma_block = False |
| 112 | continue |
| 113 | |
| 114 | # Check #include statements |
no outgoing calls
no test coverage detected