标签: clang-format

如何告诉clang-format缩进可见性修饰符?

我希望我的可见性修饰符(public,protectedprivate)由clang格式缩进,这些格式当前将它们保留在与类声明相同的级别.我已经查找indentvisibility转储了默认格式选项,但找不到任何内容.

c++ clang c++11 clang-format

9
推荐指数
2
解决办法
3984
查看次数

有没有办法配置clang格式以保持嵌套的命名空间声明在同一行?

在我正在使用的代码库中,我们总是声明嵌套的命名空间:

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]

c++ clang-format

9
推荐指数
2
解决办法
1104
查看次数

如何使用clang格式控制三元运算符的缩进?

如何以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}' )

c++ clang-format

9
推荐指数
1
解决办法
513
查看次数

Xcode 8扩展执行NSTask

我的目标是创建一个执行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)

xcode nstask clang-format xcode8 xcode-extension

9
推荐指数
1
解决办法
509
查看次数

在冒号之前和在构造函数中的逗号之后放置

我正在尝试创建一个构造函数初始化程序格式,如:

Constructor()
  : initializer1(),
    initializer2()
Run Code Online (Sandbox Code Playgroud)

根据文档,我试图设置BreakConstructorInitializersBeforeColon(描述说:"在冒号之前和逗号之后打破构造函数初始化程序.",这正是我想要的),但它是在逗号之前放置,这不是我想要的是.我做错了什么,或者这是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)

c++ auto-indent clang-format

9
推荐指数
1
解决办法
1517
查看次数

如何以clang格式强制East const?

是否有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)

c++ clang-format

9
推荐指数
2
解决办法
475
查看次数

如何告诉 clang-format 在二元运算符之间保持空白

是否可以指示 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)

也就是说,将二元运算符之间的空格留给程序员。

c++ clang-format

8
推荐指数
1
解决办法
1398
查看次数

clang-format:宏的缩进

我正在尝试将 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_NORESULTMacroBlockEndMacroBlockBegin但这没有帮助。其结果如下:

   QUERY_BEGIN()
      a = 1;
      QUERY_NORESULT
         a = 2;
      QUERY_END()
Run Code Online (Sandbox Code Playgroud)

目前有没有办法实现如上所示的缩进?

c++ macros clang-format

8
推荐指数
1
解决办法
8195
查看次数

clang-format:如何对齐结构体初始化列表

我有一个结构数组:(
假设标识符在#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 c++ clang-format

8
推荐指数
2
解决办法
5165
查看次数

尽管手动格式化文件,预提交仍抱怨“隐藏的更改与挂钩自动修复冲突”?

我正在使用以下钩子为我的 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 文件)。如果所有文件都通过,为什么会出现问题?

git pre-commit-hook clang-format pre-commit.com

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