clang-formatIncludeIsMainRegex
来确定多个文件是否共享相同的主标头,以便将此#include
指令放在首位。足够整洁。
我几乎总是使用这种风格的前向声明标头:"entity_fwd.hpp"、"entity.hpp"和"entity.cpp"。此外,还有一个"entity_test.cpp",它应该使用"entity.hpp"标头。但是,"entity.hpp"应使用"entity_fwd.hpp"作为其主标题。
因此,我实际需要的是一种将其他标头指定为主文件的方法,而不是使用相同的主标头的其他源文件。
有没有人找到一种方法来使 clang 格式做到这一点?
编辑:好的,所以更多的背景。假设我有这个标题,称为entity_fwd.hpp
:
class Entity;
class Player;
class MOB;
bool collisionTest(Entity const & e1, Entity const & e2);
然后我有一个普通的定义标题,entity.hpp
:
#include "entity_fwd.hpp"
#include "world.hpp"
#include <vector>
class Entity {
public:
Entity(Entity const &) = delete;
virtual bool isAlive() const;
...
};
...
最后是一个实现文件,entity.cpp
:
#include "entity.hpp"
bool collisionTest(Entity const & e1, Entity const & e2) {
...
}
bool Entity::isAlive() const {
...
}
...
因为entity.cpp
clang-format知道entity.hpp
是该文件的主标头,基于标头文件名的词干;术语main header是clang-format的术语。主标题将放在包含列表中的第一个。
对于entity.hpp
,主标头应entity_fwd.hpp
。我可以将IncludeCategories
与正则表达式'_fwd.h(pp)?"$'
一起使用来首先对entity_fwd.hpp
进行排序,但这会将所有_fwd
个标头放在首位,只有entity_fwd.hpp
应该得到特殊处理。
最新的clang-format
文档显示了一个新的样式选项IncludeIsMainSourceRegex
,这似乎是您所需要的。这不是版本 10.0.0,因此您需要查找比该版本更新的clang-format
版本。