clang-format 可以将每个函数完全排列在一行上吗?

问题描述 投票:0回答:1

我想格式化一组 C++ 文件,以便每个函数或函数模板完全在一行上定义。输入如下:

template <typename T>
void foo(int i)
{
  return;
}

template <int I, typename T>
struct Bar {
  Bar()
  {
  }

  static int testsmf(double d)
  {
    return 42;
  }

  template  <typename U>
  inline void testmft() const {
    int i;
    double longname1;
    double longname2;
    double longname3;
    double longname4;
    double longname5;
  }
};

int main()
{
  return 0;
}

我可能会大致这样寻找输出:

template <typename T>void foo(int i){  return;}
    
template <int I, typename T>
struct Bar {
    Bar()  {  }
    
    static int testsmf(double d)  {    return 42;  }
    
    template  <typename U>  inline void testmft() const {    int i;    double longname1;    double longname2;    double longname3;    double longname4;    double longname5;  }
};

int main(){  return 0;}

目前我的 clang 格式样式文件(名为

cf
)很小:

ColumnLimit: 0
AllowShortFunctionsOnASingleLine: All

我通过

clang-format -style=file:./cf test.cpp
进行测试,但输出中唯一明显的变化是每个函数的左大括号与其名称放在同一行。我之前尝试从
clang-format -style=llvm -dump-config
的输出开始,并如上所述更改两个样式选项,但这也不起作用。将
ColumnLimit
设置为较大的值(例如 1000)也没有什么区别。我使用的是 ClangFormat 版本 18.1.3。

c++ clang llvm clang-format
1个回答
0
投票

出于各种原因,您不应该使用您想要的格式,并且大量评论会列出它们。

但你没有问这是否是一个好主意。

clang-format
可以进行配置,让您顺利完成大部分任务。

我不确定你是否可以让它完全忽略分号,但这是你如何开始

首先你要获得一个 .clang 格式的文件:

clang-format -dump-config > .clang-format

然后您需要禁用文件中的列限制

ColumnLimit: 0
CompactNamespaces: true

以下选项围绕“短”块,不确定短的定义是什么。就个人而言,并不完全反对这些。

AllowShortBlocksOnASingleLine: Always
AllowShortCaseExpressionOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse 
AllowShortLambdasOnASingleLine: All

您想禁用各种“中断”选项

AlwaysBreakAfterDefinitionReturnType: None
BreakAfterReturnType: None
BreakBeforeBraces: Attach
Break****
PackConstructorInitializers: CurrentLine

如果你正在写东西,你可能想尽可能删除角色

RemoveBracesLLVM: true
RemoveParentheses: ReturnStatement 
SpaceBefore****

使用 https://clang.llvm.org/docs/ClangFormatStyleOptions.html 获取有关选项的更多信息。

© www.soinside.com 2019 - 2024. All rights reserved.