Skip to content

Clang-format SortIncludes does not allow main headers in <> instead of "" #63619

Closed as not planned
@aristotelos

Description

@aristotelos

Clang-format has advanced detection of the "main header" of a C++ file to keep it as the first include. However, this only works if the main header file is referenced using double quotes ("myproject/MyClass.h"), not if the main header is referenced using angle brackets (<myproject/MyClass.h>). This is invalid because there is no strict rule to put main header includes into double quotes.

Root cause

Clang-format has a hard-coded check that the main header file should be included with double quotes.

Reproduction scenario

Project layout:

myproject
   include
      myproject
          MyClass.h
   src
       MyClass.cc

.clang-format file:

SortIncludes: true

MyClass.cc:

#include <myproject/MyClass.h>
#include <anotherproject/OtherClass.h>

Clang-format will reformat this to:

#include <anotherproject/OtherClass.h>
#include <myproject/MyClass.h>

because it doesn't recognize <myproject/MyClass.h> as the main header reference.

Workaround: change MyClass.cc to:

#include "myproject/MyClass.h"
#include <anotherproject/OtherClass.h>

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions