llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] Failed to format nested blocks

Open llvmbot opened this issue 9 years ago • 4 comments

Bugzilla Link 34543
Version unspecified
OS All
Reporter LLVM Bugzilla Contributor

Extended Description

It works fine if there is only one nested block:

int main()
{
    {
        {
            int a = 0;
        }
    }
}

But if we copy the code in the main function again, like this

int main()
{
    {
        {
            int a = 0;
        }
    }
    {
        {
            int a = 0;
        }
    }
}

clang-format will format the code above like this:

int main()
{
    {{int a = 0;
}
}
{
    {
        int a = 0;
    }
}
}

Configuration for clang-format is:

{
    Language: Cpp,
    BasedOnStyle: Google,

    DerivePointerAlignment: false,
    PointerAlignment: Left,

    BreakBeforeBraces: Allman,

    AlignOperands: true,
    AlignAfterOpenBracket: Align,

    AccessModifierOffset: -4,

    Cpp11BracedListStyle: true,
    NamespaceIndentation: None,
    UseTab: Never,
    TabWidth: 4,
    Standard: Cpp11,
    SpaceBeforeParens: ControlStatements,
    AlwaysBreakTemplateDeclarations: true,

    BinPackArguments: false,
    BinPackParameters: false,

    AlignAfterOpenBracket: true,
    AlignConsecutiveAssignments: true,
    IndentWidth: 4,
    ColumnLimit: 120,
    AllowShortFunctionsOnASingleLine: Inline,
    AllowShortBlocksOnASingleLine: false,

    BreakConstructorInitializersBeforeComma: true,
    BreakBeforeBinaryOperators: NonAssignment,
    BreakBeforeTernaryOperators: true,

    BreakStringLiterals: false,

    SortIncludes: true,
}

llvmbot avatar Sep 11 '17 00:09 llvmbot

I've collected most nested scope-related clang-format bugs to this issue, since it was one of the first and most clear. (I did this simply because browsing the issue list, I kept seeing reports related to nested scopes, so I wanted to find them all)

Notable variations of the original report given the examples from other issues:

This only seems to occur with at least two compound blocks in a row, with nothing in between. Having only one seems to work fine, even if that one block is nested.

The second block can be completely empty, such as:

int main()
{
    {
        {
            int a = 0;
        }
    }
    {}
}

Which becomes

int main() { {{int a = 0; }
}
{}
}

Also notable is the result becomes even worse if the function declaration is followed by a (redundant) semicolon:

int main(){{{int a = 0;
}
}
{}
}
;

As can probably be guessed, this is a mis-annotation of the block kind. Specifically, most of the braces are set with the block kind BK_BracedInit.

rymiel avatar Jan 01 '23 18:01 rymiel

@mydeveloperday thoughts on how hard it would be to fix this?

tiagomacarios avatar May 04 '23 19:05 tiagomacarios

https://reviews.llvm.org/D150452

ghost avatar May 12 '23 15:05 ghost

I also took a try at fixing it, although I went with a bit of a different approach: https://reviews.llvm.org/D150403

galenelias avatar May 12 '23 17:05 galenelias