This is a retry of https://reviews.llvm.org/D114583, which was backed
authorGalen Elias <galenelias@gmail.com>
Tue, 23 May 2023 03:11:17 +0000 (20:11 -0700)
committerOwen Pan <owenpiano@gmail.com>
Tue, 23 May 2023 03:25:55 +0000 (20:25 -0700)
commit6dcde658b2380d7ca1451ea5d1099af3e294ea16
treede2ac21f56b1c2b2ac4b362547aa22715eb6d29f
parent4fafa39b76b8e11960a75d7b15d762840330ecbf
This is a retry of https://reviews.llvm.org/D114583, which was backed
out for regressions.

Clang Format is detecting a nested scope followed by another open brace
as a braced initializer list due to incorrectly thinking it's matching a
braced initializer at the end of a constructor initializer list which is
followed by the body open brace.

Unfortunately, UnwrappedLineParser isn't doing a very detailed parse, so
it's not super straightforward to distinguish these cases given the
current structure of calculateBraceTypes. My current hypothesis is that
these can be disambiguated by looking at the token preceding the
l_brace, as initializer list parameters will be preceded by an
identifier, but a scope block generally will not (barring the MACRO
wildcard).

To this end, I am adding tracking of the previous token to the LBraceStack
to help scope this particular case.

TokenAnnotatorTests cherry picked from https://reviews.llvm.org/D150452.

Fixes #33891.
Fixes #52911.

Differential Revision: https://reviews.llvm.org/D150403
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp