【发布时间】:2020-02-28 18:18:00
【问题描述】:
我在源文件上运行clang-formt,它不断切换评论的位置,从未确定正确的位置。
这是我的.clang-format 文件的副本:
BasedOnStyle: Microsoft
版本信息:
$ clang-format --version
clang-format version 9.0.0
下面的 sn-p 显示了当我运行 clang-format 和 -i 然后 --output-replacements-xml 时的结果:
$ clang-format -i MyFile.cpp
$ clang-format --output-replacements-xml MyFile.cpp
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='2702' length='7'> </replacement>
</replacements>
$ clang-format -i MyFile.cpp
$ clang-format MyFile.cpp
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='2702' length='13'> </replacement>
</replacements>
如您所见,替换偏移量的长度字段不断变化。如果我一遍又一遍地运行这个,你会看到它在上面列出的两个结果之间交替出现。本质上,它只是来回移动特定评论的位置。
这里是有问题的来源:
格式化版本 1:
if (mp) // if foo bar likes to foo, the foo
{ // but bar foo also is bar
// too bizz bazz buzz bizz
status = contains(mp->foobar());
格式化版本 2:
if (mp) // if foo bar likes to foo, the foo
{ // but bar foo also is bar
// too bizz bazz buzz bizz
status = contains(mp->foobar());
有问题的线路是// too bizz bazz buzz bizz,因为它的位置永远不会“确定”。有谁知道为什么这条评论不断来回移动?此外,关于如何防止这种情况的任何想法(更改格式文件、不同版本的格式化程序等)。
【问题讨论】:
标签: c++ clang code-formatting clang-format