clang-format目前将所有pragma移动到第一列.clang格式之前的一个例子:
for (int i = 0; i < 4; ++i) {
#pragma UNROLL
// ...some code...
}
Run Code Online (Sandbox Code Playgroud)
clang格式后的相同代码:
for (int i = 0; i < 4; ++i) {
#pragma UNROLL
// ...some code...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让clang-format完全忽略pragma行而不改变源代码(即不会混淆源代码// clang-format off)?例如使用正则表达式?
这与此问题有关(我希望避免安装第三方工具),并希望通过此错误报告解决.
另外,虽然clang-format off对于带有pragma的行是受到尊重的,但是注释行本身将缩进到pragma 将缩进到的内容(使用clang-format 6.0.0):
for (int i = 0; i < 4; ++i) {
// clang-format off
#pragma UNROLL
// clang-format on
// ...some code...
}
Run Code Online (Sandbox Code Playgroud) 我使用 clang-format 作为我的代码库的自动格式化工具。但它的一些功能让我很烦恼。
例如,我不希望它格式化我的宏定义,因为在大多数情况下,手动格式化它们更清晰。但我不知道如何以 clang 格式禁用它。
另一个小问题是指针对齐。有时,很明显让它左对齐,有时是右对齐。所以我宁愿自己动手。但是从 clang-format 中禁用它似乎是不可能的?
对这些问题有什么帮助吗?
自 15.7 prev1 起, Visual Studio 就对 clang-format 进行了本机集成。
根据我的理解,由于在格式样式中选择了“Google”,因此它应该使用位于结账根目录中的 .clang 格式文件,或者回退到名为“Google”的硬编码配置。
但是,如果我格式化文件:Ctrl+K、Ctrl+D,我的文件将使用 Whitesmiths 进行格式化;这是我的 Visual Studio 格式的大括号配置。
我是否忘记配置一些允许我使用 clang-format 的东西,或者出现了严重的错误?如果是最后一个,我很欣赏一些调试技巧。
我在文档中找不到任何内容,甚至BreakBeforeBraces: Allman格式化了我已经拆分成的单行函数
void foo() { bar(); }
我想要类似的东西
void foo()
{
bar();
}
Run Code Online (Sandbox Code Playgroud)
我希望这样做是为了代码的组织和统一,因为这就是每个多行函数的样子。
你能帮我么?
我希望我的可见性修饰符(public,protected和private)由clang格式缩进,这些格式当前将它们保留在与类声明相同的级别.我已经查找indent并visibility转储了默认格式选项,但找不到任何内容.
clang-format有2个叫做BinPackParameters和的
选项BinPackArguments.它们似乎控制着函数声明和函数调用的缩进方式.
BinPackParameters似乎提供了函数声明的预期结果,但BinPackArguments似乎没有像函数调用那样工作.
这是一个简单的测试文件:
#include <stdbool.h>
void function_with_a_huge_name_that_should_just_not_be(unsigned int a, char *b, unsigned int c, unsigned int d, unsigned int e)
{
return;
}
int main()
{
function_with_a_huge_name_that_should_just_not_be(13, "bb", 1234234, 4324324, 2355345);
}
Run Code Online (Sandbox Code Playgroud)
这就是它的格式:
#include <stdbool.h>
void function_with_a_huge_name_that_should_just_not_be(unsigned int a,
char *b,
unsigned int c,
unsigned int d,
unsigned int e)
{
return;
}
int main()
{
function_with_a_huge_name_that_should_just_not_be(
13, "bb", 1234234, 4324324, 2355345);
}
Run Code Online (Sandbox Code Playgroud)
我的.clang-format文件如下:
---
AccessModifierOffset: -2
AlignAfterOpenBracket: false
AlignEscapedNewlinesLeft: …Run Code Online (Sandbox Code Playgroud) 我的目标是创建一个执行clang格式的扩展.我的代码看起来像这样:
- (void)performCommandWithInvocation:(XCSourceEditorCommandInvocation *)invocation completionHandler:(void (^)(NSError * _Nullable nilOrError))completionHandler
{
NSError *error = nil;
NSURL *executableURL = [[self class] executableURL];
if (!executableURL)
{
NSString *errorDescription = [NSString stringWithFormat:@"Failed to find clang-format. Ensure it is installed at any of these locations\n%@", [[self class] clangFormatUrls]];
completionHandler([NSError errorWithDomain:SourceEditorCommandErrorDomain
code:1
userInfo:@{NSLocalizedDescriptionKey: errorDescription}]);
return;
}
NSMutableArray *args = [NSMutableArray array];
[args addObject:@"-style=LLVM"];
[args addObject:@"someFile.m"];
NSPipe *outputPipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.launchPath = executableURL.path;
task.arguments = …Run Code Online (Sandbox Code Playgroud) 具有以下结构定义:
struct city
{
};
struct country
{
};
Run Code Online (Sandbox Code Playgroud)
我想要clang-format为我格式化
struct city {};
struct country {};
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我可以看到很多选项,例如AllowShortBlocksOnASingleLine, AllowShortFunctionsOnASingleLineorAllowShortIfStatementsOnASingleLine但没有AllowShortClassDefinitionsOnASingleLine(或类似)。
我正在尝试创建一个构造函数初始化程序格式,如:
Constructor()
: initializer1(),
initializer2()
Run Code Online (Sandbox Code Playgroud)
根据文档,我试图设置BreakConstructorInitializers为BeforeColon(描述说:"在冒号之前和逗号之后打破构造函数初始化程序.",这正是我想要的),但它是在逗号之前放置,这不是我想要的是.我做错了什么,或者这是clang格式的错误?我尝试了clang-format-5.0和clang-format-6.0,它们的行为方式相同.
这是我完整的.clang格式文件:
---
Language: Cpp
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: All
AlwaysBreakAfterReturnType: TopLevel
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
BeforeCatch: true
BeforeElse: true
IndentBraces: true
SplitEmptyFunction: true
SplitEmptyRecord: …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 travis ci 在补丁进入 github 时检查补丁,并试图找出 clang-format 3.9(因为 travis ci 目前仅支持最新的 ubuntu 14.04)以在扫描时忽略整个目录或文件变化。
我的 .travis.yml 文件:
language: c++
sudo: required
dist: trusty
install:
- sudo apt-get update
- sudo apt-get install clang-format-3.9 python3
- ./travisci/check_patch.py
Run Code Online (Sandbox Code Playgroud)
我的 travisci/check_patch.py 文件:
#!/usr/bin/env python3
from subprocess import Popen, PIPE, STDOUT
# Run clang to check if code changes cause a diff output and return 1 if so.
cmd = "git show origin/master..@ | clang-format-diff-3.9 -p 1 -style=file"
diff = Popen(cmd, stdout=PIPE, shell=True).communicate()[0]
if diff: …Run Code Online (Sandbox Code Playgroud) clang-format ×10
c++ ×6
clang ×4
auto-indent ×1
c++11 ×1
format ×1
formatting ×1
nstask ×1
travis-ci ×1
xcode ×1
xcode8 ×1