clang-format doesn't recognize matching header's name

Viewed 331

I have a .clang-format file configured, where I want to do the following sorting

#include <stdafx.h>

#include "the_header_of_this_source_file.h"

#include <includes_with_arrow>

#include "includes_with_quote.h"

So far, I have this configuration

IncludeCategories:
  - Regex:           'stdafx.h'
    Priority:        -2
  - Regex:           '^[<|"].*Prototypes.h'
    Priority:        2
  - Regex:           '^<.*>'
    Priority:        2
  - Regex:           '^".*"'
    Priority:        3
IncludeIsMainRegex: '$'
IncludeIsMainSourceRegex: ''

but when formatting the code, it moves the_header_of_this_source_file.h into the quote group (assuming the header is included with quotes), leading me to the following result

#include <stdafx.h>

#include <includes_with_arrow>

// Sorted in alphabetical order
#include "includes_with_quote.h"
#include "the_header_of_this_source_file.h"

How do I prevent this behavior?

Edit

As I understand, the reason comes from the fact, that the header of the file matches the pattern ^".*", therefore its priority is changed. So, maybe there is a way to ignore pattern matching for the main header?

1 Answers

In my project I have something like this:

IncludeBlocks: Regroup
IncludeCategories:
  - Regex:           'set_nod.h|stdafx.h'
    Priority:        -1
  - Regex:           '^"'
    Priority:        1
  - Regex:           '(projectDir1|projectDir2|projectDir3)'
    Priority:        2
  - Regex:           '(someDep1|someDep2)'
    Priority:        3
  - Regex:           '.*'
    Priority:        4

And it works for me as desired. Didn't had to investigate how to force matching header file - it just worked.

Also consider trashing IncludeIsMainRegex and IncludeIsMainSourceRegex. Default value should do the work. If I understand documentation correctly those properties are controlling matching source file with respective header file. They allow to handle suffixes in the filenames.

Doc says that '' means "arbitrary suffix" (I ready this as "any suffix") and I think IncludeIsMainSourceRegex is entry which is screwing your configuration. Drop this value or set it to '$'.

Related