标签: clang-format

如何以 clang 格式对齐函数参数名称?

是否可以将clang-format结构成员和函数参数名称格式化为列?

例如:

struct
{
   int           alpha; //aligned to 'b' of "beta"
   unsigned int  beta;
   MyObject     *gamma; //aligned with 'g' not '*'
};

void foobar (int           alpha, //aligned to 'b' of "beta"
             unsigned int  beta
             MyObject     *gamma) //aligned with 'g' not '*'
{
}
Run Code Online (Sandbox Code Playgroud)

如果不可能,我可以以某种方式扩展 clang-format 来实现这一点吗?

c clang clang-format

5
推荐指数
2
解决办法
5125
查看次数

在 clang 格式的控制语句后中断

BreakBeforeBraces: Allman在我的.clang-format文件中使用,但控制语句中的大括号(例如if, for, while, ...)没有放在自己的行上。

// Currently:
void foo()
{
    while(true) {
        bar();
    }
}

// What I want:
void foo()
{
    while(true) 
    {
        bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

我读过您可以在 中为大括号设置嵌套配置类BraceWrapping,但我无法找出正确的 YAML 语法(以及 sublime text 插件的 JSON 语法),并且找不到任何现有示例。

有没有办法做到这一点?

c++ formatting clang libclang clang-format

5
推荐指数
1
解决办法
2533
查看次数

clang-format在if()while()for()之后添加{}语句

我想知道,如果铛格式可以设置,以减少非compound_statement{ non-compound_statement; }iteration_statement.

statement
: labeled_statement
| compound_statement
| expression_statement
| selection_statement
| iteration_statement
| jump_statement
;
iteration_statement
: WHILE '(' expression ')' statement
| DO statement WHILE '(' expression ')' ';'
| FOR '(' expression_statement expression_statement ')' statement
| FOR '(' expression_statement expression_statement expression ')' statement
;
Run Code Online (Sandbox Code Playgroud)

输入:

if (exp) foo = 1;
Run Code Online (Sandbox Code Playgroud)

输出:

if (exp) { foo = 1; }
Run Code Online (Sandbox Code Playgroud)

然后美化师会根据需要缩进.

clang-format

5
推荐指数
1
解决办法
683
查看次数

clang-format不能在gVim下工作

clang-format-3.8通过apt-get 安装.现在我尝试在gVim中使用它,但它不起作用.我检查了文件夹中存在clang-format-3.8 /usr/share/vim/addons/syntax.

但是当我进入:pyf /usr/share/vim/addons/syntax/clang-format-3.8.py我的vim命令行时,它返回:

E319: Sorry, the command is not available in this version.

我在Ubuntu 16.04下使用gVim 7.4.

c++ vim clang clang-format

5
推荐指数
2
解决办法
2364
查看次数

SourceTree 中断路径变量

我开始git-hooks在我的项目中使用,其中一个 precommit-hooks 使用clang-format. 在通过 SourceTree 提交时,它无法找到 clang 格式。

Error: 'clang-format' not found.

但是提交有效,如果我通过终端或通过 SourceTree 内的终端执行此操作。将此行添加到 git-hook 后,一切都在 SourceTree 中工作。

export PATH=/usr/local/bin:$PATH
Run Code Online (Sandbox Code Playgroud)

所以看起来它在 SourceTree 中不起作用,但我没有找到任何关于当前版本(版本 2.3.2)的错误报告

我是否忘记在 SourceTree 的某处启用某些选项,或者它应该自己工作?

environment-variables githooks atlassian-sourcetree clang-format

5
推荐指数
1
解决办法
737
查看次数

用Clang-Format格式化CMakeLists.txt

有没有办法获取clang格式以正确格式化CMake文件?

我有一个.clang-format文件Language: CppBasedOnStyle: Google。没有指定其他语言。

理想情况下,我想自定义样式,但是现在最大的问题是,clang格式会使许多行缩进。文件越长,我获得的缩进级别越高。

问题:

  1. 有没有办法让clang格式将a识别CMakeLists.txt为不同于Cpp的语言?
  2. clang格式是否可以为我添加CMake语言规则?
  3. 在这种情况下是否存在替代clang格式的格式?

输入项

cmake_minimum_required (VERSION 3.2)
project(HELLO)

add_executable (helloDemo demo.cxx demo_b.cxx)
add_executable (goodByeDemo goodbye.cxx goodbye_b.cxx)
Run Code Online (Sandbox Code Playgroud)

实际产量

cmake_minimum_required(VERSION 3.2) project(HELLO)

    add_executable(helloDemo demo.cxx demo_b.cxx)
        add_executable(goodByeDemo goodbye.cxx goodbye_b.cxx)
Run Code Online (Sandbox Code Playgroud)

预期输出:与输入相同。或者命令和括号之间可能没有空格。

cmake clang-format cmake-format

5
推荐指数
1
解决办法
1583
查看次数

如何指示clang格式在文件末尾添加EOL字符?

也许我错过了一些东西,但是仍然没有找到这样的设置。从形式上讲,clang-format这不会产生正确的UNIX文本文件,因为最后几行始终缺少EOL字符。

clang-format

5
推荐指数
1
解决办法
237
查看次数

强制clang格式尊重doxygen块注释

我在C / C ++代码中使用长块C风格的Doxygen注释。这是http://www.doxygen.nl/manual/docblocks.html上列出的#4样式,看起来像这样(用完80个字符)...

/**************************************************************************//**
* \file
* \date   2017-01-02
* \author Alex Hogen
******************************************************************************/
Run Code Online (Sandbox Code Playgroud)

如果我对此运行clang-format,它将在两个正斜杠之间插入一个空格,因此看起来像是愚蠢的……。

/**************************************************************************/ /**
* \file
* \date   2017-01-02
* \author Alex Hogen
******************************************************************************/
Run Code Online (Sandbox Code Playgroud)
  • SpacesBeforeTrailingComments设置为2,所以那不是问题。
  • 尝试过CommentPragmas正则表达式\/\*+\/\/\*+
  • 尝试CommentPragmas正则表达式/\*(.+\n.+)+\*/
  • 我尝试设置ReflowCommentsfalse

...但是这些都不起作用。

我知道在此块中有两个注释,但是在同一行上找不到任何clang-format参数寻址块注释。如何停止clang格式插入此空间?

我不想通过为每个Doxygen注释块禁用clang格式来解决此问题。这似乎很荒谬。

有什么好的建议吗?:-)

clang-format

5
推荐指数
1
解决办法
1307
查看次数

Objective-C Clang-Format方法Brace Break

我正在为我的Objective-C项目使用clang-format,但是我在设置它时遇到了困难.

这是我想要的结果:

- (NSString *)someMethod
{
  return @"My String";
}
Run Code Online (Sandbox Code Playgroud)

这是格式化后的实际结果:

- (NSString *)someMethod {
  return @"My String";
}
Run Code Online (Sandbox Code Playgroud)

这是我的.clang-format档案:

BasedOnStyle: WebKit
AlignTrailingComments: true
ColumnLimit: 120
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: false
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PointerBindsToType: false
SpacesBeforeTrailingComments: 1
TabWidth: 2
UseTab: Never
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass: true
  AfterControlStatement: false
  AfterEnum: true
  AfterExternBlock: true
  AfterFunction: true
  AfterNamespace: true
  AfterObjCDeclaration: true
  AfterStruct: true
  AfterUnion: true
  BeforeCatch: false
  BeforeElse: false
  IndentBraces: false
  SplitEmptyFunction: true
  SplitEmptyRecord: true
  SplitEmptyNamespace: true
Run Code Online (Sandbox Code Playgroud)

我需要更改什么设置才能让格式化程序在Objective-C方法的开头括号之前放置换行符?

xcode lint objective-c clang-format

5
推荐指数
1
解决办法
261
查看次数

Clang-format用连续缩进缩进内部初始化器

我有一些C++ 11代码与clang格式规则使用正常的indent(IndentWidth)4和continuation indent(ContinuationIndentWidth)为8.所以一个长函数看起来像这样:

// All indented "correctly"
void this_function_has_a_very_long_name_and_lots_of_parameters(
        int parameter_a, int parameter_b, int parameter_c)
{
    this_function_has_a_very_long_name_and_lots_of_parameters(
            parameter_a, parameter_b, parameter_c);
}
Run Code Online (Sandbox Code Playgroud)

但是,我也有这样的数据(注意最后的逗号,以防止bin-packing并保持每行一项 - 在现实生活中这些数字不仅仅是1,2,3):

static std::vector<std::vector<int>> data{
    {
        1, // Comment
        2,
        3, // Comment
    },
};
Run Code Online (Sandbox Code Playgroud)

考虑到Cpp11BracedListStyle设置为false,上面是我期望它的外观,所以它应该使用块缩进(4),而不是连续缩进(8).从clang格式的文档中,如果是这样的话true:

"重要区别: - 支撑列表中没有空格. - 在右括号之前没有换行符. - 使用延续缩进缩进,而不是使用块缩进."

所以,我希望看到使用"块缩进"(4).但是,我实际得到的是:

static std::vector<std::vector<int>> data{
    {
           1, // Comment - indented by 4 + 8!
           2,
           3, // Comment
    }, …
Run Code Online (Sandbox Code Playgroud)

c++ code-formatting indentation c++11 clang-format

5
推荐指数
0
解决办法
220
查看次数