From c50d8344ea16706d34a8f5e63884afe21fd13f32 Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Sat, 24 Nov 2018 22:30:33 -0800 Subject: [PATCH] Make includeorder.py handle #ifndef --- wpiformat/wpiformat/includeorder.py | 5 +++-- wpiformat/wpiformat/test/test_includeorder.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/wpiformat/wpiformat/includeorder.py b/wpiformat/wpiformat/includeorder.py index 2204df77..cf8fd41f 100644 --- a/wpiformat/wpiformat/includeorder.py +++ b/wpiformat/wpiformat/includeorder.py @@ -228,10 +228,10 @@ def header_sort(self, config_file, lines_list, file_name, start, end, i = start while i < end: - if "#ifdef" in lines_list[i]: + if "#ifdef" in lines_list[i] or "#ifndef" in lines_list[i]: ifdef_count = 1 for j in range(i + 1, end): - if "#ifdef" in lines_list[j]: + if "#ifdef" in lines_list[j] or "#ifndef" in lines_list[j]: ifdef_count += 1 elif "#endif" in lines_list[j]: ifdef_count -= 1 @@ -371,6 +371,7 @@ def run_pipeline(self, config_file, name, lines): # Write lines from beginning of file to headers i = 0 while i < len(lines_list) and ("#ifdef" not in lines_list[i] and + "#ifndef" not in lines_list[i] and "#include" not in lines_list[i]): i += 1 output_list = lines_list[0:i] diff --git a/wpiformat/wpiformat/test/test_includeorder.py b/wpiformat/wpiformat/test/test_includeorder.py index 4cc8615a..77972eaa 100644 --- a/wpiformat/wpiformat/test/test_includeorder.py +++ b/wpiformat/wpiformat/test/test_includeorder.py @@ -546,4 +546,20 @@ def test_includeorder(): "#endif" + os.linesep) test.add_latest_input_as_output(True) + # Ensure #ifndef is handled properly + test.add_input("./Test.h", + "#ifndef __APPLE__" + os.linesep + \ + "#include " + os.linesep + \ + "#elif !defined(_WIN32)" + os.linesep + \ + "#include " + os.linesep + \ + "#endif" + os.linesep) + test.add_latest_input_as_output(True) + + # Ensure #ifndef is handled properly + test.add_input("./Test.h", + "#ifndef _WIN32" + os.linesep + \ + "#include " + os.linesep + \ + "#endif" + os.linesep) + test.add_latest_input_as_output(True) + test.run(OutputType.FILE)