cheshirekow / cmake_format

Source code formatter for cmake listfiles.
GNU General Public License v3.0
949 stars 104 forks source link

cmake-format --check crashes with formatted code #262

Open armandas opened 3 years ago

armandas commented 3 years ago

I have a function that has bracket comments inside the declaration:

function(myFunction #[[target]] #[[linker_script]])
    if(${ARGC} GREATER 0)
        set(target ${ARGV0})
    else()
        set(target ${PROJECT_NAME})
    endif()
endfunction()

Doing a --check on the above code results in an error (expected):

ERROR __main__.py:618: Check failed: ./test.cmake

When formatting the above block of code, the result is as below:

function(myFunction #[[target]]
         #[[linker_script]]
)
    if(${ARGC} GREATER 0)
        set(target ${ARGV0})
    else()
        set(target ${PROJECT_NAME})
    endif()
endfunction()

Now, trying to check the above code, the cmake-format crashes with the following traceback:

WARNING __main__.py:530: While processing ./test.cmake
Traceback (most recent call last):
  File "/usr/bin/cmake-format", line 8, in <module>
    sys.exit(main())
  File "/usr/lib/python3.10/site-packages/cmakelang/format/__main__.py", line 633, in main
    return inner_main()
  File "/usr/lib/python3.10/site-packages/cmakelang/format/__main__.py", line 616, in inner_main
    onefile_main(infile_path, args, argparse_dict)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/__main__.py", line 526, in onefile_main
    outtext, reflow_valid = process_file(cfg, intext, args.dump)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/__main__.py", line 166, in process_file
    box_tree = formatter.layout_tree(parse_tree, config, first_token=first_token)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1778, in layout_tree
    root_box.reflow(stack_context, Cursor(0, 0))
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1594, in _reflow
    cursor = child.reflow(stack_context, column_cursor, passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1620, in _reflow
    cursor = child.reflow(stack_context, column_cursor, passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 806, in reflow
    return super(StatementNode, self).reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 892, in _reflow
    cursor = child.reflow(stack_context, cursor, passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1420, in _reflow
    cursor = child.reflow(stack_context, cursor, passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1274, in _reflow
    cursor = child.reflow(stack_context, input_cursor, passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 688, in _reflow
    cursor = child.reflow(stack_context, cursor + (0, 1), passno)
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 560, in reflow
    outcursor = self._reflow(
  File "/usr/lib/python3.10/site-packages/cmakelang/format/formatter.py", line 1699, in _reflow
    self._colextent = cursor[1] + max(len(line) for line in lines)
ValueError: max() arg is an empty sequence

cmake-format version: 0.6.13

cmake-format is run as below:

find -type f -name "*.cmake" | xargs cmake-format -i
armandas commented 2 years ago

It looks like the issue is caused by using bracket comments. I have updated the description.