(lines: List[str], expected_header_guard: str, comment_style: str)
| 82 | |
| 83 | |
| 84 | def fix_header_guard(lines: List[str], expected_header_guard: str, comment_style: str) -> Tuple[List[str], bool]: |
| 85 | start_line, next_line, last_line = "", "", "" |
| 86 | start_index, last_index = find_code_boundaries(lines) |
| 87 | guards_updated: bool = True |
| 88 | |
| 89 | if start_index < len(lines): |
| 90 | # if not, the file is full of comments |
| 91 | start_line = lines[start_index] |
| 92 | |
| 93 | if start_index + 1 < len(lines): |
| 94 | # if not, the file has only one line of code |
| 95 | next_line = lines[start_index + 1] |
| 96 | |
| 97 | if last_index < len(lines) and last_index > start_index + 1: |
| 98 | # if not, either the file is full of comments OR it has less than three code lines |
| 99 | last_line = lines[last_index] |
| 100 | |
| 101 | expected_start_line = f"#ifndef {expected_header_guard}\n" |
| 102 | expected_next_line = f"#define {expected_header_guard}\n" |
| 103 | |
| 104 | if comment_style == 'double_slash': |
| 105 | expected_last_line = f"#endif // {expected_header_guard}\n" |
| 106 | elif comment_style == 'slash_asterix': |
| 107 | expected_last_line = f"#endif /* {expected_header_guard} */\n" |
| 108 | |
| 109 | empty_line = "\n" |
| 110 | |
| 111 | if looks_like_header_guard(start_line) and is_define(next_line) and is_endif(last_line): |
| 112 | # modify the current header guard if necessary |
| 113 | lines = lines[:start_index] + [expected_start_line, expected_next_line] + \ |
| 114 | lines[start_index+2:last_index] + [expected_last_line] + lines[last_index+1:] |
| 115 | |
| 116 | guards_updated = (start_line != expected_start_line) or (next_line != expected_next_line) \ |
| 117 | or (last_line != expected_last_line) |
| 118 | else: |
| 119 | # header guard could not be detected, add header guards |
| 120 | lines = lines[:start_index] + [empty_line, expected_start_line, expected_next_line] + \ |
| 121 | [empty_line] + lines[start_index:] + [empty_line, expected_last_line] |
| 122 | |
| 123 | |
| 124 | return lines, guards_updated |
| 125 | |
| 126 | |
| 127 | def find_expected_header_guard(filepath: str, prefix: str, add_extension: str, drop_outermost_subdir: str) -> str: |
no test coverage detected