【问题标题】:Why does clang-format wrap this long line the way it does?为什么 clang-format 会以这种方式包装这条长线?
【发布时间】:2020-09-02 10:47:35
【问题描述】:

在 clang-format 中,以下模板函数声明被包装成三行。我认为它应该只被包装成两个。是我的 .clang 格式文件中的问题还是工具中的错误?

template <class VEC3_T, class FLOAT_T>
FLOAT_T functionNamedBlahBlahhh(const VEC3_T blabla, const VEC3_T bla, FLOAT_T blah1,
                                FLOAT_T blah2) // foo
{
}

我使用的是 clang 格式版本 10.0.0。 我的 .clang 格式文件是:

BasedOnStyle: LLVM                          # (LLVM, Google, Chromium, Mozilla, WebKit)
AccessModifierOffset: -4
AlignEscapedNewlines: Left
AllowShortBlocksOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
BreakBeforeBraces: Custom                   # If this is "Custom" then use the BraceWrapping settings
BraceWrapping:
  AfterClass:            false
  AfterStruct:           false
  AfterUnion:            true
  AfterEnum:             true
  AfterNamespace:        false
  AfterControlStatement: false
  AfterFunction:         true
  AfterExternBlock:      false
  BeforeCatch:           true
  BeforeElse:            false
  IndentBraces:          false
  SplitEmptyFunction:    true
  SplitEmptyRecord:      true
  SplitEmptyNamespace:   true
BreakConstructorInitializers: AfterColon
ColumnLimit: 130                            # Drawback: AllowShort*OnASingleLine kind of goes crazy with it. Is there a Penalty property to adjust this?
IncludeBlocks: Regroup
IndentCaseLabels: false
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
PointerAlignment: Left
SpaceBeforeCpp11BracedList: true
UseTab: Never

我还看到了一些其他情况,它会将长行包装成 3 行以上。还有一个:

OCLShaderEnv::OCLShaderEnv(OCLEnv* oclEnv, SVMStruct* svm, std::string prog, std::string kernelName, int width, int height,
                           std::string buildOptions, Camera* camera, void* otherStuff, std::array<float, 4> fillColor) :
    m_device(oclEnv->getDevice()),
    m_context(oclEnv->getContext()), m_cmdQ(oclEnv->getCmdQ()), m_prog(prog), m_kernelName(kernelName), m_width(width),
    m_height(height), m_camera(camera), m_svmStructPtr(svm), m_oclEnv(oclEnv)
{
}

注意三行初始化器,它们很容易放在两行。

【问题讨论】:

    标签: clang-format


    【解决方案1】:

    在您的第一个示例中,1) 仅从一行中的一个参数开始,以及 2) 在该行上添加注释之间似乎存在交互。如果您删除评论,它的格式为:

    template <class VEC3_T, class FLOAT_T>
    FLOAT_T functionNamedBlahBlahhh(const VEC3_T blabla, const VEC3_T bla, FLOAT_T blah1, FLOAT_T blah2)
    {
    }
    

    如您所愿。如果您留下注释,但从一行开始所有参数,则如您所料,它会将它们全部留在一行。

    对我来说,这似乎是clang-format 中的一个错误。可能是预期的行为,认为注释必须仅适用于该参数,因此不应移动?但很难想象它会非常有用。

    解决方法:删除注释,或手动将所有参数放在一行中。


    在您的第二个示例中,这似乎是 1) 具有足够参数的构造函数以使参数占用多行,2) 具有该构造函数的多个初始值设定项和 3) 设置 BreakConstructorInitializers: AfterColon 之间的交互。在这种情况下,第一个初始化程序放在自己的行上,即使它可以与后续初始化程序组合。

    在我看来,这也是clang-format 中的一个错误。

    解决方法:减少构造函数的参数,使它们适合一行 - 我知道您不应该这样做,但无论如何可能会使您的代码更清晰。或者将BreakConstructorInitializers 设置为AfterColon 以外的其他值。或者只是使用次优格式,这可能是我会做的:(。

    【讨论】:

      猜你喜欢
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      • 2014-03-21
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多