最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - SortIncludes to honor groups? - Stack Overflow

programmeradmin2浏览0评论

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?

Share Improve this question edited 18 hours ago rzickler 7211 gold badge5 silver badges21 bronze badges asked Feb 8 at 2:29 soumeng78soumeng78 88810 silver badges22 bronze badges 1
  • 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
Add a comment  | 

1 Answer 1

Reset to default 2

Yes, 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.

发布评论

评论列表(0)

  1. 暂无评论