Skip to content

Allow including the "main" header with < > #53013

Open
@reddwarf69

Description

@reddwarf69

IncludeCategories in https://clang.llvm.org/docs/ClangFormatStyleOptions.html explains that "The main header for a source file automatically gets category 0".
The first problem is that it fails to document it only considers a header "main" if it has been included with ".
The second issue is that it fails to consider a header that has been included with " as the "main" header.

This is not a problem for the LLVM project because it includes its own headers with ". This makes sense, the standard in "15.3 Source file inclusion" says

[Note: Although an implementation may provide a mechanism for making arbitrary source files available to
the < > search, in general programmers should use the < > form for headers provided with the implementation,
and the " " form for sources outside the control of the implementation.

and https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html says "This variant is used for header files of your own program".

But unfortunately, the C++ Core Guidelines say the usage of < or " should not be considering whether it's "outside the control of the implementation" or "header files of your own program" but considering whether the file is "relative to the including file" (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rs-incform).

A common thing to do is

src/thing.cpp
include/mylib/thing.hpp

to have the headers defining the public API separated from others. And since thing.hpp is not relative to thing.cpp (I don't think .. is defined in any standard, and it would be ugly anyway), the C++ Core Guidelines state that < should be used and clang-format fails to see #include mylib/thing.hpp is the main header of thing.cpp.

One way to solve this would be adding an option to specify the directory prefix used by the project. If I can say IncludeDirectoryOwnHeaders: mylib then clang-format could consider mylib/thing.hpp as the main header without getting confused with otherlib/thing.hpp.

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang-formatenhancementImproving things as opposed to bug fixing, e.g. new or missing feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions