@@ -139,7 +139,8 @@ def fix_include_guards(filepath: Path, src_root: Path, project_name: str = "", d
139139
140140 if not ifndef_line :
141141 print (f"⚠️ No include guard found in { filepath } " )
142- return False
142+ print (f" Add incluide guards in { filepath } " )
143+ return add_include_guards (filepath , src_root , project_name , dry_run )
143144
144145 # Extract current guard name
145146 current_guard = re .search (r'#\s*ifndef\s+([A-Za-z0-9_]+)' , ifndef_line ).group (1 )
@@ -193,6 +194,37 @@ def fix_include_guards(filepath: Path, src_root: Path, project_name: str = "", d
193194 print (f"❌ Error writing { filepath } : { e } " )
194195 return False
195196
197+ def add_include_guards (filepath : Path , src_root : Path , project_name : str = "" , dry_run : bool = False ) -> bool :
198+ """
199+ Add include guards to a file that doesn't have them.
200+
201+ Returns:
202+ True if the file was modified, False otherwise
203+ """
204+ try :
205+ with open (filepath , 'r' , encoding = 'utf-8' ) as f :
206+ content = f .read ()
207+ except Exception as e :
208+ print (f"❌ Error reading { filepath } : { e } " )
209+ return False
210+
211+ guard_name = generate_guard_name (filepath , src_root , project_name )
212+
213+ print (f" { filepath .relative_to (src_root .parent )} :" )
214+ print (f" Adding guard: { guard_name } " )
215+
216+ if dry_run :
217+ return True
218+
219+ new_content = f"#ifndef { guard_name } \n #define { guard_name } \n \n { content .rstrip ()} \n \n #endif // { guard_name } \n "
220+
221+ try :
222+ with open (filepath , 'w' , encoding = 'utf-8' ) as f :
223+ f .write (new_content )
224+ return True
225+ except Exception as e :
226+ print (f"❌ Error writing { filepath } : { e } " )
227+ return False
196228
197229def main ():
198230 import argparse
@@ -228,6 +260,8 @@ def main():
228260
229261 extensions = [f".{ ext .strip ()} " for ext in args .extensions .split (',' )]
230262 exclude_dirs = args .exclude
263+
264+ exclude_dirs = [d .strip ("/" ) for d in args .exclude ]
231265
232266 print (f"🔍 Searching for header files in { src_root } " )
233267 print (f" Extensions: { ', ' .join (extensions )} " )
0 commit comments