首页 > 解决方案 > FixNamespaceComment 在 .clang 格式中未按预期工作

问题描述

我有一个 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

标签: c++clang-format

解决方案


它以 clang 格式硬编码,命名空间结束注释不会添加到只有 1 行的命名空间,这似乎很随意,因为具有 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


推荐阅读