包括前向 headers 和 clang-format 的顺序
Include order of forward headers with clang-format
clang-format有IncludeIsMainRegex
判断几个文件是否共享同一个main header,为了放置这个#include
指令第一。够整洁了。
我几乎总是使用这种风格的前向声明 header:"entity_fwd.hpp"、"entity.hpp" 和 "entity.cpp"。另外还有一个"entity_test.cpp",应该使用"entity.hpp" header。然而,"entity.hpp" 应该使用 "entity_fwd.hpp" 作为它的 main header.
所以,我真正需要的是一种将额外的 headers 指定为 main 的方法,而不是使用相同的 main 的额外源文件header.
有没有人找到让 clang-format 做到这一点的方法?
编辑:
好的,所以多一点上下文。假设我有这个 header,叫做 entity_fwd.hpp
:
class Entity;
class Player;
class MOB;
bool collisionTest(Entity const & e1, Entity const & e2);
然后我有一个正常的定义header,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 对于该文件,基于 header 的文件名的主干;术语 main header 是 clang-format 的术语。 main header 将放在包含列表的第一位。
对于 entity.hpp
,main header 应该是 entity_fwd.hpp
。我可以使用 IncludeCategories
和正则表达式 '_fwd.h(pp)?"$'
首先对 entity_fwd.hpp
进行排序,但这将放置 all _fwd
header首先,只有 entity_fwd.hpp
应该得到特殊待遇。
clang-format
的最新 documentation 显示了一个新样式选项 IncludeIsMainSourceRegex
,这似乎正是您所需要的。这不是 10.0.0 版本,因此您需要找到比该版本更新的 clang-format
版本。
clang-format有IncludeIsMainRegex
判断几个文件是否共享同一个main header,为了放置这个#include
指令第一。够整洁了。
我几乎总是使用这种风格的前向声明 header:"entity_fwd.hpp"、"entity.hpp" 和 "entity.cpp"。另外还有一个"entity_test.cpp",应该使用"entity.hpp" header。然而,"entity.hpp" 应该使用 "entity_fwd.hpp" 作为它的 main header.
所以,我真正需要的是一种将额外的 headers 指定为 main 的方法,而不是使用相同的 main 的额外源文件header.
有没有人找到让 clang-format 做到这一点的方法?
编辑:
好的,所以多一点上下文。假设我有这个 header,叫做 entity_fwd.hpp
:
class Entity;
class Player;
class MOB;
bool collisionTest(Entity const & e1, Entity const & e2);
然后我有一个正常的定义header,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 对于该文件,基于 header 的文件名的主干;术语 main header 是 clang-format 的术语。 main header 将放在包含列表的第一位。
对于 entity.hpp
,main header 应该是 entity_fwd.hpp
。我可以使用 IncludeCategories
和正则表达式 '_fwd.h(pp)?"$'
首先对 entity_fwd.hpp
进行排序,但这将放置 all _fwd
header首先,只有 entity_fwd.hpp
应该得到特殊待遇。
clang-format
的最新 documentation 显示了一个新样式选项 IncludeIsMainSourceRegex
,这似乎正是您所需要的。这不是 10.0.0 版本,因此您需要找到比该版本更新的 clang-format
版本。