2
votes

I am trying to get clang-format to recognize the main include for foo.cpp in the following setup:

project/my_lib
├── CMakeLists.txt
├── include
│   └── project
│       └── my_lib
│           └── foo.hpp
├── src
│   └── foo.cpp
└── tests
    └── foo_test.cpp

And foo.cpp has the following content:

#include <project/another_lib/bar.hpp>
#include <project/my_lib/foo.hpp>
#include <vector>

// ...

The problem I think I have identified is the following: clang tries to identify the 'main' include based on the file name, in this case foo.cpp. And when it looks at #include <project/my_lib/foo.hpp> it doesn't recognize this as the 'main' include for this cpp file. I know it is possible to specify suffixes for the "main include file" logic, but what would be needed here is a prefix regex.

Any help as to how I could get clang to set the right include to priority 0 in the example above?

Reference: Clang format options, section "IncludeCategories" and following

Edit: I am using clang-format as integrated in Visual Studio 2019, which means I do not think I have access to the command line options. Moreover, this being a shared project, I'd like to work off of the default behavior.

1
You did not show the command line you are using to compile. I wonder what argument you provided for clang++ -isystem or -cxx-isystem option. - Eljay
Good point. I am not compiling with clang, just using the Visual Studio integration for formatting. I added that information to the question. - Belgorath

1 Answers

1
votes

Just as a quick update, which kind of solved my issue: if the include uses regular quotes (") then clang-format bundled with Visual Studio seems to pick up the "main" header and order it accordingly.

Not ideal, but still a working solution.