|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert decimal integer literals to hexadecimal format with 'u' suffix. |
| 4 | +Processes all .c and .h files in src/ and drivers/ directories. |
| 5 | +This version properly handles identifiers and only converts actual numeric literals. |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import re |
| 10 | +import sys |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | + |
| 14 | +def is_identifier_char(c): |
| 15 | + """Check if character can be part of an identifier.""" |
| 16 | + return c.isalnum() or c == '_' |
| 17 | + |
| 18 | + |
| 19 | +def process_line(line): |
| 20 | + """ |
| 21 | + Process a single line and convert decimal literals to hex. |
| 22 | + Only converts standalone numeric literals, not numbers within identifiers. |
| 23 | + """ |
| 24 | + result = [] |
| 25 | + i = 0 |
| 26 | + in_string = False |
| 27 | + in_char = False |
| 28 | + escape_next = False |
| 29 | + |
| 30 | + while i < len(line): |
| 31 | + if escape_next: |
| 32 | + result.append(line[i]) |
| 33 | + escape_next = False |
| 34 | + i += 1 |
| 35 | + continue |
| 36 | + |
| 37 | + char = line[i] |
| 38 | + |
| 39 | + if char == '\\': |
| 40 | + escape_next = True |
| 41 | + result.append(char) |
| 42 | + i += 1 |
| 43 | + continue |
| 44 | + |
| 45 | + if char == '"' and not in_char: |
| 46 | + in_string = not in_string |
| 47 | + result.append(char) |
| 48 | + i += 1 |
| 49 | + continue |
| 50 | + |
| 51 | + if char == "'" and not in_string: |
| 52 | + in_char = not in_char |
| 53 | + result.append(char) |
| 54 | + i += 1 |
| 55 | + continue |
| 56 | + |
| 57 | + if in_string or in_char: |
| 58 | + result.append(char) |
| 59 | + i += 1 |
| 60 | + continue |
| 61 | + |
| 62 | + if len(result) > 0 and is_identifier_char(result[-1]): |
| 63 | + result.append(char) |
| 64 | + i += 1 |
| 65 | + continue |
| 66 | + |
| 67 | + if i + 1 < len(line) and char == '0' and line[i+1] in 'xX': |
| 68 | + result.append(char) |
| 69 | + result.append(line[i+1]) |
| 70 | + i += 2 |
| 71 | + while i < len(line) and (line[i] in '0123456789ABCDEFabcdef' or line[i] in 'uUlL'): |
| 72 | + result.append(line[i]) |
| 73 | + i += 1 |
| 74 | + continue |
| 75 | + |
| 76 | + if char.isdigit(): |
| 77 | + num_str = '' |
| 78 | + j = i |
| 79 | + |
| 80 | + while j < len(line) and line[j].isdigit(): |
| 81 | + num_str += line[j] |
| 82 | + j += 1 |
| 83 | + |
| 84 | + suffix_chars = '' |
| 85 | + while j < len(line) and line[j] in 'uUlL': |
| 86 | + suffix_chars += line[j] |
| 87 | + j += 1 |
| 88 | + |
| 89 | + next_char = line[j] if j < len(line) else '' |
| 90 | + |
| 91 | + if is_identifier_char(next_char): |
| 92 | + result.append(num_str) |
| 93 | + result.append(suffix_chars) |
| 94 | + i = j |
| 95 | + continue |
| 96 | + |
| 97 | + value = int(num_str) |
| 98 | + hex_str = f"0x{value:X}u" |
| 99 | + result.append(hex_str) |
| 100 | + i = j |
| 101 | + continue |
| 102 | + |
| 103 | + result.append(char) |
| 104 | + i += 1 |
| 105 | + |
| 106 | + return ''.join(result) |
| 107 | + |
| 108 | + |
| 109 | +def process_file(filepath): |
| 110 | + """ |
| 111 | + Process a single file and convert decimal literals to hex. |
| 112 | + """ |
| 113 | + try: |
| 114 | + with open(filepath, 'r', encoding='utf-8', errors='ignore') as f: |
| 115 | + lines = f.readlines() |
| 116 | + |
| 117 | + modified = False |
| 118 | + new_lines = [] |
| 119 | + |
| 120 | + for line in lines: |
| 121 | + original = line.rstrip('\n') |
| 122 | + processed_line = process_line(original) |
| 123 | + new_lines.append(processed_line + '\n' if line.endswith('\n') else processed_line) |
| 124 | + |
| 125 | + if processed_line != original: |
| 126 | + modified = True |
| 127 | + |
| 128 | + if modified: |
| 129 | + with open(filepath, 'w', encoding='utf-8') as f: |
| 130 | + f.writelines(new_lines) |
| 131 | + return True |
| 132 | + |
| 133 | + return False |
| 134 | + |
| 135 | + except Exception as e: |
| 136 | + print(f"Error processing {filepath}: {e}", file=sys.stderr) |
| 137 | + return False |
| 138 | + |
| 139 | + |
| 140 | +def should_skip_file(filepath): |
| 141 | + """ |
| 142 | + Determine if a file should be skipped. |
| 143 | + """ |
| 144 | + filename = os.path.basename(filepath) |
| 145 | + return filename in ['HeliOS.h', 'config.h'] |
| 146 | + |
| 147 | + |
| 148 | +def process_directory(directory): |
| 149 | + """ |
| 150 | + Process all C source files in the directory. |
| 151 | + """ |
| 152 | + directory = Path(directory) |
| 153 | + |
| 154 | + if not directory.exists(): |
| 155 | + print(f"Warning: Directory {directory} does not exist") |
| 156 | + return 0, 0 |
| 157 | + |
| 158 | + processed_count = 0 |
| 159 | + skipped_count = 0 |
| 160 | + |
| 161 | + for root, dirs, files in os.walk(directory): |
| 162 | + for filename in files: |
| 163 | + if not filename.endswith(('.c', '.h')): |
| 164 | + continue |
| 165 | + |
| 166 | + filepath = os.path.join(root, filename) |
| 167 | + |
| 168 | + if should_skip_file(filepath): |
| 169 | + print(f"Skipping: {filepath}") |
| 170 | + skipped_count += 1 |
| 171 | + continue |
| 172 | + |
| 173 | + if process_file(filepath): |
| 174 | + print(f"Modified: {filepath}") |
| 175 | + processed_count += 1 |
| 176 | + else: |
| 177 | + print(f"No changes: {filepath}") |
| 178 | + |
| 179 | + return processed_count, skipped_count |
| 180 | + |
| 181 | + |
| 182 | +def main(): |
| 183 | + """ |
| 184 | + Main entry point. |
| 185 | + """ |
| 186 | + print("Converting decimal integer literals to hexadecimal...") |
| 187 | + print("=" * 60) |
| 188 | + |
| 189 | + total_processed = 0 |
| 190 | + total_skipped = 0 |
| 191 | + |
| 192 | + for directory in ['src', 'drivers']: |
| 193 | + print(f"\nProcessing {directory}/ directory:") |
| 194 | + print("-" * 60) |
| 195 | + processed, skipped = process_directory(directory) |
| 196 | + total_processed += processed |
| 197 | + total_skipped += skipped |
| 198 | + |
| 199 | + print("\n" + "=" * 60) |
| 200 | + print(f"Summary: {total_processed} files modified, {total_skipped} files skipped") |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == '__main__': |
| 204 | + main() |
0 commit comments