【问题标题】:Clang-format AlignConsecutiveDeclarations weird behaviourClang 格式的 AlignConsecutiveDeclarations 奇怪的行为
【发布时间】:2018-04-10 11:37:51
【问题描述】:

当 AlignConsecutiveDeclarations 设置为 true 时,一些函数参数会奇怪地对齐:

ErrorType Layer::Construct(AbstractManagedObject*                      a_parent,
                           container::Map<utf8::String, utf8::String>& a_configuration_attributess, uint8_t a_opacity)

我希望变量声明对齐,但不是参数列表。如果第二行只有一个参数,我不介意参数列表中的对齐方式。

这是预期的行为吗?有什么办法吗?

【问题讨论】:

    标签: c++ clang-format


    【解决方案1】:

    是的,AlignConsecutiveDeclarations 也会对齐函数参数。起初我并不确定,documentation 对此也不是很清楚,但是通过 clang-format 的source code 阅读,很明显函数参数对齐是设计使然:

    void WhitespaceManager::alignConsecutiveDeclarations() {
      ...
      AlignTokens(Style,
                  [](Change const &C) {
                    return C.Tok->is(TT_StartOfName) ||             // <-- variables
                           C.Tok->is(TT_FunctionDeclarationName) || // <-- parameters
                           C.Tok->is(tok::kw_operator);
                  },
      ...
    

    您的示例中的精确对齐方式,我无法重现(尝试过 clang-format 5.0、6.0) - 对我来说,它将每个函数参数放在一行中:

    ErrorType Layer::Construct(
        AbstractManagedObject *                     a_parent,
        container::Map<utf8::String, utf8::String> &a_configuration_attributess,
        uint8_t                                     a_opacity);
    

    AlignConsecutiveDeclarations: true 可能与您的 clang 格式配置中的某些其他参数发生冲突,从而产生您所看到的结果。确保 BinPackParameters 设置为 false - 如果超出列限制,这应该强制将函数参数拆分为单独的行:

    BinPackParameters(布尔型)

    如果为 false,则函数声明或函数定义的参数要么都在同一行,要么各占一行。

    如果您希望完全避免函数参数对齐,并愿意降级您的 clang-format 版本,您可以使用 clang-format 4.0。 AlignConsecutiveDeclarations 选项是在 3.8 版本中引入的,当时它没有对齐函数参数 - 只是连续的变量声明。将您的示例输入到 clang-format 3.8 会得到以下结果:

    ErrorType Layer::Construct(
        AbstractManagedObject *a_parent,
        container::Map<utf8::String, utf8::String> &a_configuration_attributess,
        uint8_t a_opacity);
    

    在后续版本 3.9 和 4.0 中,AlignConsecutiveDeclarations 参数的功能保持不变。从 clang-format 5.0 开始,功能发生了变化,它也开始对齐函数参数。

    【讨论】:

      猜你喜欢
      • 2013-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2019-06-18
      • 2023-04-03
      相关资源
      最近更新 更多