My .clang-format file has followings:
IncludeBlocks: Regroup
SortIncludes: CaseInsensitive
I do want to sort the includes, but I don't want includes for standard or 3rd-party headers (#include <>
) to get mixed up with local headers (#include ""
) in the sorted output.
I.e. the output should be like the following:
#include <memory>
#include <vector>
#include <boost/circular_buffer.hpp>
#include <tbb/tbb.h>
#include "another_local_header.h"
#include "local_header.h"
How can I specify this in .clang-format? Will IncludeCategories
be of any help here?
My .clang-format file has followings:
IncludeBlocks: Regroup
SortIncludes: CaseInsensitive
I do want to sort the includes, but I don't want includes for standard or 3rd-party headers (#include <>
) to get mixed up with local headers (#include ""
) in the sorted output.
I.e. the output should be like the following:
#include <memory>
#include <vector>
#include <boost/circular_buffer.hpp>
#include <tbb/tbb.h>
#include "another_local_header.h"
#include "local_header.h"
How can I specify this in .clang-format? Will IncludeCategories
be of any help here?
- 3 Tip: Do it the other way around. You want all the standard headers last to expose the least standard ones for lack of other inclusions. It'll save you time later. Do not do other headers favours. – Ted Lyngmo Commented Feb 8 at 2:40
1 Answer
Reset to default 2Yes, you are correct. IncludeCategories
will do that for you. For your request, it could look like:
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '<[A-Za-z0-9-_]+>'
Priority: 1
- Regex: '<(boost\/){1}[A-Za-z0-9.\Q/-_\E]+>'
Priority: 2
- Regex: '<[A-Za-z0-9-_]+\/+[A-Za-z0-9.\Q/-_\E]+>'
Priority: 3
- Regex: '"[A-Za-z0-9.\Q/-_\E]+"'
Priority: 4
- Priority 1 matches
<abc_def-ghi>
- Priority 2 matches
<boost/abc.def>
- Priority 3 matches
<a/b/c.d>
- Priority 4 matches
"abc_def-ghi.jkl"
You may find better Regex solutions. E.g. you could hard code .h
as extension.
And I second @Ted Lyngmo. Start local and go to global/system for your includes. If you want to have a good read about that, I can recommend this answer and this discussion (note: it's http). So better invert the priorities.