我有以下代码,例如:
[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) 让我们立刻从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) 我在我的.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选项时使用类型?
我正在尝试从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格式如下:
switch (x)
{
case long_name: return 1;
case sn: return 2;
}
Run Code Online (Sandbox Code Playgroud)
该AllowShortCaseLabelsOnASingleLine选项将它们放在同一行,
但我还没有找到一种方法来使语句对齐.
结合AlignAfterOpenBracket(BracketAlignmentStyle含)选项BinPackArguments和BinPackParameters设置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)
这可能与现有选项有关吗?
我希望我的函数原型/定义总是将参数分成单独的行,而不管总长度如何.否则默认.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) 我想配置 clang-format 以在 C++ 中对包含的标头进行排序,如下所示:
我在 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) 这个问题用不同的 clang 格式版本扩展了统一输出。问题是 clang-format 的默认行为因版本而异,即使对于内置样式也是如此。坦率地说,我想问一下为什么开发人员在这里不关心兼容性,但这无关紧要。情况就是这样,我必须处理它。我不允许需要某个版本的 clang-format(就像相关答案中建议的一位用户)并且需要配置 clang-format 以便它为不同版本提供相同的结果。如果可能,应涵盖 >= 4.0 的版本。如果这不可行,则可以接受适用于版本 >= minimum_version 的解决方案。
我想人们可以为每个 clang 格式版本找到一个配置,以提供所需的输出——繁琐的工作,但至少它是一个解决方案。但是对不同版本使用相同的 .clang 格式文件会产生一些问题,因为旧版本不知道新密钥。所以一个人需要
有任何想法吗?
使用以下内容进行测试
clang-format -style="{BasedOnStyle: Google, UseTab: Always}" -i /path/to/file.ino
结果以空格而不是制表符显示
clang-format ×10
c++ ×5
clang ×3
clang-tidy ×1
coding-style ×1
formatting ×1
git ×1
git-daemon ×1
githooks ×1
llvm-clang ×1
objective-c ×1
regex ×1
sh ×1
xcode ×1