gpt4 book ai didi

c++ - lang格式继续切换评论位置

转载 作者:行者123 更新时间:2023-12-01 14:58:39 25 4
gpt4 key购买 nike

我正在源文件上运行clang-formt,并且该代码会不断切换注释的位置,从不设置正确的位置。

这是我的.clang-format文件的副本:

BasedOnStyle: Microsoft

版本信息:
$ clang-format --version
clang-format version 9.0.0

下面的代码片段显示了当我先使用 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'>&#10; </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'>&#10; </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,因为它的位置永远不会“稳定”。有谁知道为什么这个评论不断来回移动?另外,关于如何防止这种情况的任何想法(更改格式文件,格式化程序的不同版本等)。

最佳答案

显然,这是clang-format中的错误。我发现的唯一解决方法是移动第三条注释行。

  • 如果您从最后一个注释行开始,并与随后的代码行对齐:
        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());
    然后它不会移动。但是,如果初始缩进是其他任何东西,它将像您发现的那样进行切换。
  • 如果设置ReflowComments: false,则第三个注释行的缩进不会切换,但是缩进确实需要两次往返才能解决。首次调用clang-format将注释与其他注释对齐。第二次调用将其与后续代码对齐。之后,它不会移动,就像上面的项目符号所描述的那样。总体而言,这是非常奇怪的行为,我认为它将在后续版本的clang-format中得到更改/修复。
  • 关于c++ - lang格式继续切换评论位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676367/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com