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

c++ - How to keep clang-format include header at the top of the file? - Stack Overflow

programmeradmin1浏览0评论

I'm running into an issue with clang-format setup in my project.

As all my includes are in angled brackets <> I've got and issue with IncludeIsMainRegex.

I want to keep something like this:

For every MyFile.cpp:

#include <directory/MyFile.hpp>

#include <string>
#include <utility>

#include <directory/SecondFile.hpp>
#include <directory/ThirdFile.hpp>

But no matter what I do in IncludeIsMainRegex, it is always sorted like this when using IncludeBlocks: Regroup

#include <string>
#include <utility>

#include <directory/MyFile.hpp>
#include <directory/SecondFile.hpp>
#include <directory/ThirdFile.hpp>

I tried with setting IncludeIsMainRegex: '<.*$1>', but it doesn't work. It has to be something universal, that will dynamically assume file name in every file and based on that will set it at the top of the include list. Is it even possible?

I'm running into an issue with clang-format setup in my project.

As all my includes are in angled brackets <> I've got and issue with IncludeIsMainRegex.

I want to keep something like this:

For every MyFile.cpp:

#include <directory/MyFile.hpp>

#include <string>
#include <utility>

#include <directory/SecondFile.hpp>
#include <directory/ThirdFile.hpp>

But no matter what I do in IncludeIsMainRegex, it is always sorted like this when using IncludeBlocks: Regroup

#include <string>
#include <utility>

#include <directory/MyFile.hpp>
#include <directory/SecondFile.hpp>
#include <directory/ThirdFile.hpp>

I tried with setting IncludeIsMainRegex: '<.*$1>', but it doesn't work. It has to be something universal, that will dynamically assume file name in every file and based on that will set it at the top of the include list. Is it even possible?

Share Improve this question edited Jan 29 at 10:29 sweenish 5,2023 gold badges14 silver badges28 bronze badges asked Jan 29 at 10:08 lorakislorakis 234 bronze badges 7
  • Why don't you just use IncludeBlocks: Preserve, which sorts everything within each block, but preserves the order of blocks? – Jan Schultke Commented Jan 29 at 11:16
  • 2 For includes within your own project, you should be using "" and not <>. Maybe this is preventing clang-format from identifying your headers as the "main include", which would normally give it category zero. – Jan Schultke Commented Jan 29 at 11:22
  • 1 ¿Is there a reason to keep <directory/MyFile.hpp> at the top? Only the precompiled header (if any) is required to be included at the top, but it can be achieved with compiler arguments, without any file formatting tools. – user7860670 Commented Jan 29 at 11:33
  • @user7860670 • Could be the team's coding standard requirement. My team requires that the .cpp file's "identity" header file appear as the very first #include directive. – Eljay Commented Jan 29 at 12:18
  • 1 @user7860670 Concrete reason - it helps to verify that header is self-contained and you didn't fet #includes inside. Opinionated reason - it's the most important header for this source file, you are implementing this very header, so it should be on top. Also, in my head, the top line include matches #pragma once from header, so it's kinda an introductory line to the file. – Yksisarvinen Commented Jan 29 at 13:20
 |  Show 2 more comments

1 Answer 1

Reset to default 0

Usually, it is a nice to have the includes ordered. clang-format can also sort them in groups (separate system includes from workspace includes and even from the current file's corresponding header (e.g. for Foo.cpp , Foo.hpp will be placed at the top in a group of its own).

However, in some cases, sorting the includes has some unwanted affects. For this you could have:

  • Set the proper value in .clang-format (mainly the directives: SortIncludes and IncludeBlocks), see here for more details: https://clang.llvm./docs/ClangFormatStyleOptions.html#sortincludes

  • Manually inject comment in the code to instruct clang-format to ignore specific code:

#include <b.h>
#include <a.h>

// clang-format off
// .. content here will be untouched by clang-format
// clang-format on
  • clang-format allows to override the include sorting from the command line by passing: clang-format --sort-includes ...

HTH,

Eran

发布评论

评论列表(0)

  1. 暂无评论