首页 > 解决方案 > 如何暂时禁用 clang-format 包括排序?

问题描述

我正在将我的项目从使用 Artistic Style 更改为clang-format。我很高兴使用的一项功能是包含文件排序。但是,有一个问题我不知道如何解决。我在 CentOS 7 上使用clang-format 7.0.1(来自 SCLo 存储库中的llvm-toolset-7.0)。

我们的标准规定,在类头中,将首先包含基类。然后,我们包括“系统”(ish)标头,然后是本地目录,以及按耦合降序排列的库。我想更改标准以更符合llvm 指南

问题是我不知道如何强制我的基类保持在标题的首位。如果需要的话,我愿意修改每个类头(一次)。

这是一个示例输入文件,根据旧项目标准手动构建:

#pragma once

#include "util/Application.hh"
#include "ZMQmixin.hh"

#include <filesystem>
#include <json/json.h>

#include "ZMQconfig.hh"
#include "ZMQendpoint.hh"
#include "Platform.hh"

#include "Foo/bar.hh"

#include "icd/Published.hh"
#include "icd/NavPoint.hh"

class Sample : public Application
             , public ZMQmixin
{
    using Base = Application;
public:
    Sample(int num_io_threads = 1);
};

这是我用来对包含进行排序的最小.clang 格式文件:

Language:                                       Cpp
BreakBeforeBraces:                              Allman

AlwaysBreakAfterDefinitionReturnType:           None
AlwaysBreakAfterReturnType:                     TopLevelDefinitions

BreakInheritanceList:                           BeforeComma

SortIncludes:                                   true
IncludeBlocks:                                  Merge # Regroup # or Preserve ?
IncludeCategories:
  # include file order:
  #    1 current directory ("Foo.hh")
  #    2 parent directory -- only in unit tests ("../Foo.hh")
  #    5 other directory   ("bar/Baz.hh")
  #   15 osw|Common/*.hh
  #   20 icd/*.hh
  #   30 util/*.hh
  #   40 <pseudo/system.h> (boost, armadillo, zmq, etc.)
  #   45 <c++include>
  #   46 <cwrapper>  (cmath, etc.)
  #   50 <c-include.h>
  #   55 <other/include.h>

  ## Regexes must be ordered so that the most specific pattern matches first,
  ## when multiple patterns might match

  # current dir
  - Regex:           '^"[[:alnum:]]+\.hh'
    Priority:        1

  # parent dir (unit test)
  - Regex:           '^"\.\.\/[[:alnum:]_-]+'
    Priority:        2

  # project headers, in decreasing order of coupling
  - Regex:           '^"(osw|Common)/'
    Priority:        15
  - Regex:           '^"icd/'
    Priority:        20
  - Regex:           '^"util/'
    Priority:        30

  # any other "x/y.hh" header is assumed to be a project header, but very coupled
  - Regex:           '^"[[:alnum:]]+/'
    Priority:        5

  # pseudo system headers
  - Regex:           '^(<|")(boost|armadillo|json|zmq)/'
    Priority:        40

  # override for C++ headers that begin with 'c' and lack any special character
  - Regex:           '^<(chrono|compare|charconv|complex)>'
    Priority:        45
  # C-language headers
  - Regex:           '^<c[a-z]+>'
    Priority:        46
  # C++ language headers
  - Regex:           '^<([A-Za-z0-9-_]+)>'
    Priority:        45

  # system headers (top level)
  - Regex:           '^<[A-Za-z0-9-_]+\.h>'
    Priority:        50
  # system headers (sub-dirs)
  - Regex:           '^<.*>'
    Priority:        55

Standard:                                       Cpp11
...

只关注包含排序,clang-format会产生以下结果:

#include "Platform.hh"
#include "ZMQconfig.hh"
#include "ZMQendpoint.hh"
#include "ZMQmixin.hh"
#include "Foo/bar.hh"
#include "icd/NavPoint.hh"
#include "icd/Published.hh"
#include "util/Application.hh"
#include <json/json.h>
#include <filesystem>

如您所见,块已合并并应用了规则。现在,如果我尝试禁用 clang-format,就在标题块周围:

// clang-format off
#include "util/Application.hh"
#include "ZMQmixin.hh"
// clang-format on

#include <filesystem>
#include <json/json.h>

#include "ZMQconfig.hh"
#include "ZMQendpoint.hh"
#include "Platform.hh"

#include "Foo/bar.hh"

#include "icd/Published.hh"
#include "icd/NavPoint.hh"

包括排序被完全禁用。在玩这个时,我注意到块末尾的注释会禁止块合并,但不会阻止块排序:

#include "ZMQmixin.hh"
#include "util/Application.hh"
// clang-format on

#include "Platform.hh"
#include "ZMQconfig.hh"
#include "ZMQendpoint.hh"
#include "Foo/bar.hh"
#include "icd/NavPoint.hh"
#include "icd/Published.hh"
#include <json/json.h>
#include <filesystem>

这非常接近;我很少有多重继承的实例,其中基类的排序是有问题的。

有没有更好的办法?我错过了什么?

标签: c++clang-format

解决方案


推荐阅读