Search code examples
include-pathclang-format

Running clang-format with command line: specify IncludeCategories


I want to run clang-format (version 3.8) with the command line. With the "-style=" option one can specify the ClangFormatStyleOptions. However, I was not able to find out how to specify the "IncludeCategories". In the Documentation it is only described how to do it with a style configuration file. So I was wondering, whether it is possible to specify it with the command line and if yes, how I can do it.


Solution

  • The format is as follows:

    -style="{IncludeCategories: [{Regex: 'bla', Priority: 100}, {Regex: 'blubb', Priority: 101}]}"
    

    I found this through experimentation and cannot cite any sources.


    I am using clang-format-6.0 and I observed the following unexpected behavior. Say I start with some default style and use the -dump-config flag and get the follow IncludeCategories:

    IncludeCategories: 
      - Regex:           '^<ext/.*\.h>'
        Priority:        2
      - Regex:           '^<.*\.h>'
        Priority:        1
      - Regex:           '^<.*'
        Priority:        2
      - Regex:           '.*'
        Priority:        3
    

    And then I add IncludeCategories: [{Regex: 'bla', Priority: 100}] to -style and dump the result of that, I get:

    IncludeCategories: 
      - Regex:           'bla'
        Priority:        100
      - Regex:           '^<.*\.h>'
        Priority:        1
      - Regex:           '^<.*'
        Priority:        2
      - Regex:           '.*'
        Priority:        3
    

    So it is overwriting entries of the default map, but it does not delete the map when the IncludeCategories is added to -style. I had to overwrite it with dummy entries and then append my actual entries to get the desired result.