标签: clang-format

如何使用clang格式格式化objective-c块?

我有以下代码,例如:

[cardRegistrationVC setCancelBlock:^{
  [weakSelf.navigationController popViewControllerAnimated:YES];
}];
Run Code Online (Sandbox Code Playgroud)

当我对它应用clang格式时,它变成:

[cardRegistrationVC setCancelBlock:^{ [weakSelf.navigationController popViewControllerAnimated:YES]; }];
Run Code Online (Sandbox Code Playgroud)

如您所见,块内的代码出现在同一行.但我应该总是换上新的路线.

如何设置clang格式正确?我的以下设置文件:

BasedOnStyle: LLVM
AllowShortIfStatementsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: false
IndentCaseLabels: true
ColumnLimit: 120
ObjCSpaceAfterProperty: true
KeepEmptyLinesAtTheStartOfBlocks: true
PenaltyBreakString: 1000000
SpacesInContainerLiterals: false
Run Code Online (Sandbox Code Playgroud)

xcode objective-c clang-format

11
推荐指数
2
解决办法
2586
查看次数

Git'预先接收'钩子和'git-clang-format'脚本可以可靠地拒绝违反代码样式约定的推送

让我们立刻从pre-receive我已经写过的一个钩子开始:

#!/bin/sh
##
  format_bold='\033[1m'
   format_red='\033[31m'
format_yellow='\033[33m'
format_normal='\033[0m'
##
  format_error="${format_bold}${format_red}%s${format_normal}"
format_warning="${format_bold}${format_yellow}%s${format_normal}"
##
stdout() {
  format="${1}"
  shift
  printf "${format}" "${@}"
}
##
stderr() {
  stdout "${@}" 1>&2
}
##
output() {
  format="${1}"
  shift
  stdout "${format}\n" "${@}"
}
##
error() {
  format="${1}"
  shift
  stderr "${format_error}: ${format}\n" 'error' "${@}"
}
##
warning() {
  format="${1}"
  shift
  stdout "${format_warning}: ${format}\n" 'warning' "${@}"
}
##
die() {
  error "${@}"
  exit 1
}
##
git() {
  command git --no-pager "${@}"
}
##
list() {
  git rev-list …
Run Code Online (Sandbox Code Playgroud)

git sh githooks git-daemon clang-format

11
推荐指数
1
解决办法
1820
查看次数

clang-format:将指针声明的星号(*)与变量名称对齐

我在我的.clang-format文件中使用以下选项:

AlignConsecutiveDeclarations: true
PointerAlignment: Right
Run Code Online (Sandbox Code Playgroud)

当前的格式化结果如下:

char *         var1;
SomeOtherType *var2;
int            var3;
Run Code Online (Sandbox Code Playgroud)

我期待的结果将是:

char          *var1; //note the changed position of * 
SomeOtherType *var2;
int            var3;
Run Code Online (Sandbox Code Playgroud)

如何配置clang-format将星号(*)与变量名称对齐,而不是在使用AlignConsecutiveDeclarations选项时使用类型?

clang clang-format

11
推荐指数
2
解决办法
3288
查看次数

如何使用视觉工作室进行clang-tidy现代化

我正在尝试从visual studio或命令行中对文件应用clang现代化,利用我的visual studio 2015 C++项目中的包含路径和设置.

我创建了一个铿锵有力的设置文件,如下所示:

clang-tidy -dump-config=.clang-tidy 
  -checks="modernize-loop-convert,modernize-deprecated-headers"
Run Code Online (Sandbox Code Playgroud)

并验证它可以在单个文件上运行,来自(cygwin)命令行:clang-tidy.exe -explain-config列表(以及其他内容)

'modernize-deprecated-headers' is enabled in the C:\abc\.clang-tidy.
Run Code Online (Sandbox Code Playgroud)

可以通过手动设置包括在单个文件上运行它: clang-tidy.exe someFile.cpp -- -Ic:/abc -I. -IIncludes

我安装了视觉工作室铛格式VS插件,和创建的示例.clang格式文件,{ BasedOnStyle: "LLVM", IndentWidth: 20 }由VS插件拾取.然后我天真地尝试用clang-tidy配置来提供clang-format,但不出所料,这根本不起作用.

总结一下:如何在文件和visual studio项目上运行clang-tidy?

也许解决方法是从visual studio中转储每个文件的包含?任何前进的方式都可以.

以供参考:

如何启用clang-tidy的"现代化"检查?

如何使用clang格式文件提供Visual Studio Clang-Format插件?

c++ clang visual-studio clang-format clang-tidy

11
推荐指数
1
解决办法
4590
查看次数

Clang格式.如何在交换机中获取单行对齐的case语句

我希望clang格式如下:

switch (x)
{
case long_name: return 1;
case sn:        return 2;
}
Run Code Online (Sandbox Code Playgroud)

AllowShortCaseLabelsOnASingleLine选项将它们放在同一行,
但我还没有找到一种方法来使语句对齐.

c++ clang-format

11
推荐指数
1
解决办法
458
查看次数

BracketAlignmentStyle:在关闭括号之前中断

结合AlignAfterOpenBracket(BracketAlignmentStyle含)选项BinPackArgumentsBinPackParameters设置false,有可能得到以下格式:

someShortFunction(argument);
someVeryVeryVeryLongFunction(
    argument1,
    argument2,
    argument3,
    argument4);
Run Code Online (Sandbox Code Playgroud)

但是,类似于BreakBeforeBraces,我想在右括号之前打破:

someShortFunction(argument);
someVeryVeryVeryLongFunction(
    argument1,
    argument2,
    argument3,
    argument4
);
Run Code Online (Sandbox Code Playgroud)

这可能与现有选项有关吗?

c++ clang-format

11
推荐指数
1
解决办法
506
查看次数

clang-format:总是打破所有参数,每行一个

我希望我的函数原型/定义总是将参数分成单独的行,而不管总长度如何.否则默认.clang-format选项

BasedOnStyle: Chromium
AlignAfterOpenBracket: 'AlwaysBreak'
BinPackArguments: 'false'
BinPackParameters: 'false'
ColumnLimit: '80'
Run Code Online (Sandbox Code Playgroud)

给出以下格式

  void foo(float a, float b);

  void foo(float a, float b, float c, float d, float e, float f, float g);

  void
  foo(float a, float b, float c, float d, float e, float f, float g, float h);

  void foo(
      float a,
      float b,
      float c,
      float d,
      float e,
      float f,
      float g,
      float h,
      float i);
Run Code Online (Sandbox Code Playgroud)

我想让他们一直被打破,每行一个参数,如下:

  void foo(
      float a,
      float b);

  void foo( …
Run Code Online (Sandbox Code Playgroud)

c++ formatting clang-format

11
推荐指数
1
解决办法
565
查看次数

如何为 clang 格式的外部标题创建类别?

我想配置 clang-format 以在 C++ 中对包含的标头进行排序,如下所示:

  • 主标头(与当前 cpp 文件关联),
  • 通过“”包含本地标头,
  • 通过 <> 包含的其他标头,
  • 来自特定外部库的头文件(例如 boost、catch2),
  • 系统/标准标头。

我在 macOS 上使用 clang-format 8.0.0。我当前的配置(仅与包含相关的片段)如下:

SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
  # Headers in <> without extension.
  - Regex:           '<([A-Za-z0-9\/-_])+>'
    Priority:        4
  # Headers in <> from specific external libraries.
  - Regex:           '<((\bboost\b)|(\bcatch2\b))\/([A-Za-z0-9.\/-_])+>'
    Priority:        3
  # Headers in <> with extension.
  - Regex:           '<([A-Za-z0-9.\/-_])+>'
    Priority:        2
  # Headers in "" with extension.
  - Regex:           '"([A-Za-z0-9.\/-_])+"'
    Priority:        1
Run Code Online (Sandbox Code Playgroud)

在此配置中,我假设系统/标准标头没有扩展名。它不适用于 UNIX/POSIX 标头。主标头会自动检测并分配优先级 0。到目前为止,除了外部库的类别之外,一切似乎都按预期工作。看起来 clang-format 正在将其分配给优先级 2。

预期结果:

#include …
Run Code Online (Sandbox Code Playgroud)

c++ regex clang llvm-clang clang-format

11
推荐指数
2
解决办法
4031
查看次数

如何为不同的 clang-format 版本维护 .clang-format 文件?

这个问题用不同的 clang 格式版本扩展了统一输出。问题是 clang-format 的默认行为因版本而异,即使对于内置样式也是如此。坦率地说,我想问一下为什么开发人员在这里不关心兼容性,但这无关紧要。情况就是这样,我必须处理它。我不允许需要某个版本的 clang-format(就像相关答案中建议的一位用户)并且需要配置 clang-format 以便它为不同版本提供相同的结果。如果可能,应涵盖 >= 4.0 的版本。如果这不可行,则可以接受适用于版本 >= minimum_version 的解决方案。

我想人们可以为每个 clang 格式版本找到一个配置,以提供所需的输出——繁琐的工作,但至少它是一个解决方案。但是对不同版本使用相同的 .clang 格式文件会产生一些问题,因为旧版本不知道新密钥。所以一个人需要

  • 如果键未知,则跳过键的 clang 格式开关
  • 一种将附加选项传递给 clang-format 的方法(除了文件中给出的选项)
  • 一种为给定的 clang 格式版本指定配置文件的方法
  • 执行上述操作之一的好方法(不确定此处提供什么 clang-format)

有任何想法吗?

compatibility coding-style clang-format

11
推荐指数
1
解决办法
1518
查看次数

如何使用制表符而不是空格使 clang 格式缩进?

使用以下内容进行测试

clang-format -style="{BasedOnStyle: Google, UseTab: Always}" -i /path/to/file.ino

结果以空格而不是制表符显示

clang-format

11
推荐指数
2
解决办法
1万
查看次数