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 | Show 2 more comments1 Answer
Reset to default 0Usually, 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
andIncludeBlocks
), see here for more details: https://clang.llvm./docs/ClangFormatStyleOptions.html#sortincludesManually 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
IncludeBlocks: Preserve
, which sorts everything within each block, but preserves the order of blocks? – Jan Schultke Commented Jan 29 at 11:16""
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<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.cpp
file's "identity" header file appear as the very first#include
directive. – Eljay Commented Jan 29 at 12:18#include
s 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