Check a file for #include directives containing ".." Returns: List of (line_number, line_content) tuples for offending includes
(file_path: Path)
| 30 | |
| 31 | |
| 32 | def check_relative_includes(file_path: Path) -> List[Tuple[int, str]]: |
| 33 | """ |
| 34 | Check a file for #include directives containing ".." |
| 35 | |
| 36 | Returns: |
| 37 | List of (line_number, line_content) tuples for offending includes |
| 38 | """ |
| 39 | violations: List[Tuple[int, str]] = [] |
| 40 | |
| 41 | # Regex to match #include directives with ".." in the path |
| 42 | # Matches both #include "path/with/../file.h" and #include <path/with/../file.h> |
| 43 | include_pattern = re.compile(r'^\s*#\s*include\s+[<"]([^>"]*\.\..*)[>"]') |
| 44 | |
| 45 | try: |
| 46 | with open(file_path, 'r', encoding='utf-8', errors='ignore') as f: |
| 47 | for line_num, line in enumerate(f, start=1): |
| 48 | match = include_pattern.match(line) |
| 49 | if match: |
| 50 | violations.append((line_num, line.rstrip())) |
| 51 | except Exception as e: |
| 52 | print(f"Warning: Could not read {file_path}: {e}", file=sys.stderr) |
| 53 | |
| 54 | return violations |
| 55 | |
| 56 | |
| 57 | def check_fastled_header_usage(file_path: Path) -> List[Tuple[int, str]]: |