c++ FixNamespaceComment在. clang格式中未按预期工作

beq87vna  于 2023-02-14  发布在  其他
关注(0)|答案(2)|浏览(169)

我有一个cpp代码如下:

#include<bits/stdc++.h>
using namespace std;

namespace a {
    const int b=1;
}

int main() {
    cout << "hello" << endl;
    return 0;
}

我尝试了.clang-format的以下配置

Language:        Cpp 
BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass:      false
  AfterStruct:     true
  BeforeCatch:     false
  BeforeElse:      false

FixNamespaceComments: true # add commend at end:
NamespaceIndentation: All #intend content of namespace

预期输出在命名空间右括号// namespace a的末尾包含注解。但如果命名空间中只有int a,则不显示注解。
当我尝试在命名空间中再放一个变量时,它工作得很好。
我使用的是clang-format-6.0

wwodge7n

wwodge7n1#

在clang格式中,命名空间结束注解没有被添加到只有一行的命名空间中,这看起来很随意,因为有1条、2条或3条语句的命名空间没有太大区别。
违规代码:

// The maximal number of unwrapped lines that a short namespace spans.
// Short namespaces don't need an end comment.
static const int kShortNamespaceMaxLines = 1;

https://github.com/llvm-mirror/clang/blob/release_70/lib/Format/NamespaceEndCommentsFixer.cpp

mklgxw1f

mklgxw1f2#

Clang-Format不会为“短”命名空间(即包含很少代码行的命名空间)添加命名空间结束注解。默认情况下,不会为一行的命名空间添加注解。行数是可配置的,请参阅https://clang.llvm.org/docs/ClangFormatStyleOptions.html#shortnamespacelines。将该选项设置为0应向所有命名空间添加命名空间结束注解。

相关问题