[clang-format] Fix indentation for selective formatting
authorSedenion <39583823+Sedeniono@users.noreply.github.com>
Wed, 24 May 2023 02:10:14 +0000 (19:10 -0700)
committerOwen Pan <owenpiano@gmail.com>
Wed, 24 May 2023 02:40:24 +0000 (19:40 -0700)
commit72ab89e3197cc1bee3b9774edb504690e3e43ed0
treece82abef820f098fc4b673ada62252b03697118d
parentc0d6f85e3ae8bcfdb7217d165314f01c1a4af9ae
[clang-format] Fix indentation for selective formatting

The problem was that the LevelIndentTracker remembered
the indentation level of previous deeper levels when
leaving a scope. Afterwards, when it entered again a
deeper level, it blindly reused the the previous
indentation level. In case of the --lines option
configured such that the previous deeper level was not
formatted, that previous level was whatever happened
to be there in the source code. The formatter simply
believed it.

This is fixed by letting the LevelIndentTracker forget
the previous deeper levels when stepping out of them
(=> change in LevelIndentTracker::nextLine()).
Note that this used to be the case until LLVM 14.0.6,
but was changed in
https://github.com/llvm/llvm-project/issues/56352 to
fix a crash. Our commit here essentially reverts that
crash fix. It seemed to have been incorrect. The proper
fix is to set the AnnotedLine::Level of joined lines
correctly (=> change in LineJoiner::join()).

See
https://github.com/llvm/llvm-project/issues/59178#issuecomment-1542637781
for some more details.

Fixes #58464.
Fixes #59178.

Differential Revision: https://reviews.llvm.org/D151047
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTestSelective.cpp