Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aristotelos opened this issue Jun 30, 2023 · 2 comments
Closed
Labels
clang-format duplicate Resolved as duplicate

Comments

@aristotelos
Copy link

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>
@aristotelos
Copy link
Author

Oops, duplicates found: #53013, #39735, #27008

@EugeneZelenko EugeneZelenko added duplicate Resolved as duplicate clang-format and removed new issue labels Jun 30, 2023
@EugeneZelenko EugeneZelenko closed this as not planned Won't fix, can't repro, duplicate, stale Jun 30, 2023
@llvmbot
Copy link
Member

llvmbot commented Jun 30, 2023

@llvm/issue-subscribers-clang-format

j-jorge added a commit to j-jorge/llvm-project that referenced this issue Jan 19, 2024
j-jorge added a commit to j-jorge/llvm-project that referenced this issue Jan 21, 2024
j-jorge added a commit to j-jorge/llvm-project that referenced this issue Jan 22, 2024
owenca pushed a commit that referenced this issue Feb 6, 2024
Resolves #27008, #39735, #53013, #63619.

Hello, this PR adds the MainIncludeChar option to clang-format, allowing
to select which include syntax must be considered when searching for the
main header: quotes (`#include "foo.hpp"`, the default), brackets
(`#include <foo.hpp>`), or both.

The lack of support for brackets has been reported many times, see the
linked issues, so I am pretty sure there is a need for it :)

A short note about why I did not implement a regex approach as discussed
in #53013: while a regex would have allowed many extra ways to describe
the main header, the bug descriptions listed above suggest a very simple
need: support brackets for the main header. This PR answers this needs
in a quite simple way, with a very simple style option. IMHO the feature
space covered by the regex (again, for which there is no demand :)) can
be implemented latter, in addition to the proposed option.

The PR also includes tests for the option with and without grouped
includes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

3 participants