标签: clang-format

告诉clang-format忽略编译指示

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)

c++ clang-format

10
推荐指数
1
解决办法
552
查看次数

clang-format:禁用宏的格式?

我使用 clang-format 作为我的代码库的自动格式化工具。但它的一些功能让我很烦恼。

例如,我不希望它格式化我的宏定义,因为在大多数情况下,手动格式化它们更清晰。但我不知道如何以 clang 格式禁用它。

另一个小问题是指针对齐。有时,很明显让它左对齐,有时是右对齐。所以我宁愿自己动手。但是从 clang-format 中禁用它似乎是不可能的?

对这些问题有什么帮助吗?

c++ clang-format

10
推荐指数
1
解决办法
2065
查看次数

如何在 Visual Studio 中使用 clang-format

自 15.7 prev1 起, Visual Studio 就对 clang-format 进行了本机集成

我使用的是 15.9.9,可以很好地看到选项中的配置。 Visual Studio 配置 C++ 格式

根据我的理解,由于在格式样式中选择了“Google”,因此它应该使用位于结账根目录中的 .clang 格式文件,或者回退到名为“Google”的硬编码配置。

但是,如果我格式化文件:Ctrl+K、Ctrl+D,我的文件将使用 Whitesmiths 进行格式化;这是我的 Visual Studio 格式的大括号配置。

我是否忘记配置一些允许我使用 clang-format 的东西,或者出现了严重的错误?如果是最后一个,我很欣赏一些调试技巧。

c++ visual-studio clang-format visual-studio-2017

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

有没有一种方法可以使用 .clang-format 在一行函数之前中断?

我在文档中找不到任何内容,甚至BreakBeforeBraces: Allman格式化了我已经拆分成的单行函数

void foo() { bar(); }

我想要类似的东西

void foo()
{
    bar();
}
Run Code Online (Sandbox Code Playgroud)

我希望这样做是为了代码的组织和统一,因为这就是每个多行函数的样子。

你能帮我么?

format clang clang-format

10
推荐指数
2
解决办法
6290
查看次数

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

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

c++ clang c++11 clang-format

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

clang-format BinPackArguments没有按预期工作

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 clang-format

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

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
查看次数

如何允许 clang-format 在一行上格式化空类/结构?

具有以下结构定义:

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(或类似)。

c++ formatting clang-format

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

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

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

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
查看次数

How to ignore files or directories with clang-format 3.9

我目前正在使用 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 travis-ci clang-format

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