标签: clang-format

VSCode 无法识别所有 .clang-format 选项

我是那种会系统地检查每个可切换选项的人,这次是clang-format在 VSCode 中。我发现了这个,我假设它列出了所有可以塞进.clang-format文件供 VSCode 使用的东西。不幸的是,VSCode 似乎无法识别其中的一些:

  1. BitFieldColonSpacing
  2. BreakBeforeConceptDeclarations
  3. EmptyLineBeforeAccessModifier
  4. IndentAccessModifier
  5. IndentRequires
  6. SortIncludes
  7. SpaceAroundPointerQualifiers
  8. SpaceBeforeCaseColon

在 VSCode 无法加载的少数选项中,仅能SortIncludes完全识别,但它只接受 bool,而不接受参考文献所说的可用选项。其余选项完全无法识别:

YAML:xxx:xxx: error: unknown key 'xxx'
Error reading /path/to/.clang-format: Invalid argument
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了某些内容,或者 VSCode 不支持这些选项?我正在使用 Microsoft 的 C/C++ 扩展,在 Linux 上使用 VSCode。

感谢您的时间。

clang-format visual-studio-code

2
推荐指数
1
解决办法
5116
查看次数

clang-format 不会修改磁盘上的文件

我有以下格式错误的文件,test.cpp

#include "maininclude.h"

int main() {
             class SIMPLE smpl;
  for (int i = 1; i < 100; 
  i++) {
      printf("Printing %d", i); // Trying out different stuff...
getchar();
  }
}
Run Code Online (Sandbox Code Playgroud)

我想在上面运行clang-format。在运行此命令之前,为了直观地查看要修改的位置,我运行clang-format -n test.cpp. 这可以正确识别由于终端上的格式和输出错误而将更改的位置:

test.cpp:3:13: warning: code should be clang-formatted [-Wclang-format-violations]
int main() {
            ^
test.cpp:5:27: warning: code should be clang-formatted [-Wclang-format-violations]
  for (int i = 1; i < 100; 
                          ^
test.cpp:6:9: warning: code should be clang-formatted [-Wclang-format-violations]
  i++) {
        ^
test.cpp:7:64: warning: code should …
Run Code Online (Sandbox Code Playgroud)

c++ clang-format

2
推荐指数
1
解决办法
2151
查看次数

如何避免 clang-format 将两个单独的“&gt;”格式化为一个班次?

当我使用 clang 来格式化我的 C++ 代码时,我遇到了一个问题:

priority_queue<int, vector<int>, greater<int> > q;
Run Code Online (Sandbox Code Playgroud)

将自动格式化为:

priority_queue<int, vector<int>, greater<int>> q;
Run Code Online (Sandbox Code Playgroud)

两个单独的 '>' 将被格式化为一个移位 >>。

那么我应该如何配置.clang-format文件来避免这种情况呢?

c++ c++03 clang-format

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

C++ 垂直对齐 Visual Studio Code 中的声明

我尝试在 Visual Studio Code 中进行垂直声明对齐。

这段代码:

struct A {
  double a;
  int b;
}
Run Code Online (Sandbox Code Playgroud)

必须转换成这样:

struct A {
  double a;
  int    b;
}
Run Code Online (Sandbox Code Playgroud)

请注意,这里不是赋值,这只是具有对齐字段的结构声明。

是否有任何 Visual Studio Code 扩展可以执行此操作?

c++ alignment clang-format visual-studio-code

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

clang-format 仍然使用 `DisableFormat: true` 进行格式化

正如文档所述:

DisableFormat (bool):完全禁用格式化。

C++但是检查仅具有此选项的源文件.clang-format仍然会报告该文件需要格式化。

clang-format

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

为什么 PointerAlignment 选项不起作用?

我正在使用version 8.0.0 (tags/google/stable/2019-01-18)带有样式文件的clang-format ( ),我在其中设置了

…
PointerAlignment: Left
…
Run Code Online (Sandbox Code Playgroud)

这成功地转换了这样的声明

const string &foo = "lorem ipsum";
Run Code Online (Sandbox Code Playgroud)

进入

const string& foo = "lorem ipsum";
Run Code Online (Sandbox Code Playgroud)

但是,当我还包含在我的样式文件中时

BasedOnStyle: Google
Run Code Online (Sandbox Code Playgroud)

选项不做任何事情。出于某种原因,它们被基本样式覆盖。这对我来说似乎是荒谬的——显式选项应该覆盖基本样式,不是吗?有人可以解释问题是什么以及如何同时使用BasedOnStylePointerAlignment: Left吗?

c++ formatting clang clang-format

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

以 clang 格式在长函数签名后包裹大括号(和/或括号)

我正在尝试为短函数实现以下格式:

void shortFunctionDecl(int x, int y) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但如果行太长,我希望它像这样溢出(而不是对参数进行装箱)

void longerFunctionDecl(
    int longName, 
    int longName2,
    int longName3
) { // I would also be ok if the paren were on the previous line and the brace on this one
    // ...
}
Run Code Online (Sandbox Code Playgroud)

目前代码最终是这样的,读起来很烦人

void longerFunctionDecl(
    int longName, 
    int longName2,
    int longName3) { 
    // code is at the same indentation level as the args, hard to read.
}
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用一些 BraceWrapping 设置,但看起来控制语句只有多行设置。可以做我想做的事吗clang-format?FWIW,似乎括号中断是不可能的,但我想知道括号中断是否可能?真的不确定。

这是我的.clang-format文件。我已经标记了相关部分,但为了完整性而包含了整个内容。

BasedOnStyle: …
Run Code Online (Sandbox Code Playgroud)

c++ clang-format

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