我希望我的可见性修饰符(public,protected和private)由clang格式缩进,这些格式当前将它们保留在与类声明相同的级别.我已经查找indent并visibility转储了默认格式选项,但找不到任何内容.
在我正在使用的代码库中,我们总是声明嵌套的命名空间:
namespace foo { namespace detail {
// stuff
} } // foo::detail namespace
Run Code Online (Sandbox Code Playgroud)
我还没有找到一种方法来配置clang-format 不将其分解为多行:
namespace foo {
namespace detail {
// stuff
}
} // foo::detail namespace
Run Code Online (Sandbox Code Playgroud)
我已经玩过BreakBeforeBraces配置了,我已经研究了BraceWrappingclang 3.8中的新配置,两者都没有成功.
是否可以在不注释代码的情况下执行此操作// clang-format [on/off]?
如何以clang格式控制三元运算符的缩进?我想要普通的延续,例如
int foobar = bar ? a
: b;
Run Code Online (Sandbox Code Playgroud)
相反,我得到运营商的一致性
int foobar = bar ? a
: b;
Run Code Online (Sandbox Code Playgroud)
我已经有了AlignOperands:false有什么想法吗?
(完整选项:-style ='{BasedOnStyle:LLVM,TabWidth:4,IndentWidth:4,ContinuationIndentWidth:8,UseTab:Always,AlignAfterOpenBracket:false,BreakBeforeBinaryOperators:All,AlwaysBreakTemplateDeclarations:true,AlignOperands:false,ColumnLimit:120}' )
我的目标是创建一个执行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) 我正在尝试创建一个构造函数初始化程序格式,如:
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) 是否有clang格式的标志将“ const west”更改为“ east const”,因此需要执行以下操作:
void fun(const std::string &s);
Run Code Online (Sandbox Code Playgroud)
将重新格式化为:
void fun(std::string const &s);
Run Code Online (Sandbox Code Playgroud) 是否可以指示 clang-format不改变
z = sqrt(x*x + y*y);
Run Code Online (Sandbox Code Playgroud)
进入
z = sqrt(x * x + y * y);
Run Code Online (Sandbox Code Playgroud)
也就是说,将二元运算符之间的空格留给程序员。
我正在尝试将 clang-format 应用到现有的代码库并遇到以下问题:
简化(并格式化)的示例代码:
#define QUERY_BEGIN()
#define QUERY_NORESULT()
#define QUERY_END()
void foo()
{
int a = 0;
QUERY_BEGIN()
a = 1;
QUERY_NORESULT()
a = 2;
QUERY_END()
}
Run Code Online (Sandbox Code Playgroud)
我设置了以下选项:
MacroBlockEnd: 'QUERY_END'
MacroBlockBegin: 'QUERY_BEGIN'
Run Code Online (Sandbox Code Playgroud)
我想要实现的是宏部分的以下格式:
QUERY_BEGIN()
a = 1;
QUERY_NORESULT()
a = 2;
QUERY_END()
Run Code Online (Sandbox Code Playgroud)
我的第一个猜测是设置QUERY_NORESULT为MacroBlockEnd和MacroBlockBegin但这没有帮助。其结果如下:
QUERY_BEGIN()
a = 1;
QUERY_NORESULT
a = 2;
QUERY_END()
Run Code Online (Sandbox Code Playgroud)
目前有没有办法实现如上所示的缩进?
我有一个结构数组:(
假设标识符在#define其他地方......)
typedef struct
{
int a;
char id[2+1];
} T;
const T T_list[] = {
{ PRIO_HOUSE, "HI" },
{ PRIO_SOMETHING_ELSE, "WO" }
// ...
}
Run Code Online (Sandbox Code Playgroud)
我希望 clang-format 将其格式化为这样:
const T T_list[] = {
{ PRIO_HOUSE , "HI" },
{ PRIO_SOMETHING_ELSE, "WO" }
// ...
}
Run Code Online (Sandbox Code Playgroud)
是否可以?
我已经阅读了文档,但在这方面我没有找到有用的东西。 https://clang.llvm.org/docs/ClangFormatStyleOptions.html
这是我的.clang-format
---
BasedOnStyle: WebKit
BreakBeforeBraces: Allman
BraceWrapping:
AfterEnum: false
IndentCaseLabels: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignEscapedNewlines: 'true'
AlignTrailingComments: 'true'
AllowShortFunctionsOnASingleLine: 'false'
#...
Run Code Online (Sandbox Code Playgroud) 我正在使用以下钩子为我的 C++ 项目使用预提交(版本 2.20.0):
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
Run Code Online (Sandbox Code Playgroud)
我只是从几个不同的 .cpp/.h/cmakelists 文件中分别暂存了几行。当我尝试提交这些更改时,我从预提交中收到以下错误:
[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
[INFO] Restored changes from C:\Users\tyler.shellberg\.cache\pre-commit\patch1665600217-21836.
Run Code Online (Sandbox Code Playgroud)
我对此感到困惑。预提交是否不允许我部分提交文件,或者在提交时根本不允许进行未暂存的更改?
所有文件,无论是暂存的还是未暂存的,都已按 clang-format 进行格式化。我已经手动仔细检查了这一点。
为了清楚起见编辑:
如果我pre-commit run --files [filename]在每个文件(暂存和未暂存)上运行,所有报告都会返回“已通过”或“已跳过”(对于非 cpp 文件)。如果所有文件都通过,为什么会出现问题?