Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clang-format crash with --lines and CompactNamespaces: true NamespaceIndentation: All #56352

Closed
Frydac opened this issue Jul 2, 2022 · 6 comments
Labels
clang-format crash Prefer [crash-on-valid] or [crash-on-invalid]

Comments

@Frydac
Copy link

Frydac commented Jul 2, 2022

Hi,

I've had this issue for several versions of clang-format.
Currently using

> clang-format --version           
clang-format version 13.0.1

> clang --version                  
clang version 13.0.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

I tried to make a minimal example where I have the issue:
Given the source code in attached
test.txt (I can't attach something with .hpp or something more cpp like)
and using a .clang-format file with following settings:

CompactNamespaces: true
NamespaceIndentation: All

if I then run
clang-format --lines=1:1 test.txt

then I get the following report:

/tmp   
> clang-format --lines=1:1 test.txt
/usr/include/c++/11.2.0/bits/stl_vector.h:1045: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](std::vector<_Tp, _Alloc>::size_type) [with _Tp = int; _Alloc = std::allocator<int>; std::vector<_Tp, _Alloc>::reference = int&; std::vector<_Tp, _Alloc>::size_type = long unsigned int]: Assertion '__n < this->size()' failed.
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.	Program arguments: clang-format --lines=1:1 test.txt
 #0 0x00007f99660cdfda (/usr/lib/libLLVM-13.so+0xb97fda)
 #1 0x00007f99660cb65f llvm::sys::RunSignalHandlers() (/usr/lib/libLLVM-13.so+0xb9565f)
 #2 0x00007f99660cb7ad (/usr/lib/libLLVM-13.so+0xb957ad)
 #3 0x00007f99651318e0 (/usr/lib/libc.so.6+0x3e8e0)
 #4 0x00007f996518136c (/usr/lib/libc.so.6+0x8e36c)
 #5 0x00007f9965131838 gsignal (/usr/lib/libc.so.6+0x3e838)
 #6 0x00007f996511b535 abort (/usr/lib/libc.so.6+0x28535)
 #7 0x00007f996c6fa23a (/usr/lib/libclang-cpp.so.13+0x8ea23a)
 #8 0x00007f996e387b6a clang::format::UnwrappedLineFormatter::format(llvm::SmallVectorImpl<clang::format::AnnotatedLine*> const&, bool, int, bool, unsigned int, unsigned int, unsigned int) (/usr/lib/libclang-cpp.so.13+0x2577b6a)
 #9 0x00007f996e389512 (/usr/lib/libclang-cpp.so.13+0x2579512)
#10 0x00007f996e37fd68 clang::format::TokenAnalyzer::process() (/usr/lib/libclang-cpp.so.13+0x256fd68)
#11 0x00007f996e5274e8 (/usr/lib/libclang-cpp.so.13+0x27174e8)
#12 0x00007f996e33b36c (/usr/lib/libclang-cpp.so.13+0x252b36c)
#13 0x00007f996e380b01 clang::format::internal::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, unsigned int, unsigned int, unsigned int, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/usr/lib/libclang-cpp.so.13+0x2570b01)
#14 0x00007f996e38189d clang::format::reformat(clang::format::FormatStyle const&, llvm::StringRef, llvm::ArrayRef<clang::tooling::Range>, llvm::StringRef, clang::format::FormattingAttemptStatus*) (/usr/lib/libclang-cpp.so.13+0x257189d)
#15 0x000055a84f036c4d (/usr/bin/clang-format+0xcc4d)
#16 0x000055a84f030d48 (/usr/bin/clang-format+0x6d48)
#17 0x00007f996511c290 (/usr/lib/libc.so.6+0x29290)
#18 0x00007f996511c34a __libc_start_main (/usr/lib/libc.so.6+0x2934a)
#19 0x000055a84f031245 (/usr/bin/clang-format+0x7245)
[1]    28064 IOT instruction (core dumped)  clang-format --lines=1:1 test.txt
@Frydac Frydac changed the title crash with lines and CompactNamespaces: true NamespaceIndentation: All clang-format crash with --lines and CompactNamespaces: true NamespaceIndentation: All Jul 2, 2022
@EugeneZelenko EugeneZelenko added clang-format crash Prefer [crash-on-valid] or [crash-on-invalid] and removed new issue labels Jul 2, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 2, 2022

@llvm/issue-subscribers-clang-format

@panukulv
Copy link

panukulv commented Jul 2, 2022

Clang-format

@panukulv panukulv mentioned this issue Jul 2, 2022
@panukulv
Copy link

panukulv commented Jul 2, 2022

@panukulv
Copy link

panukulv commented Jul 2, 2022

issue-subscribers-clang-format

@panukulv panukulv mentioned this issue Jul 2, 2022
@EugeneZelenko
Copy link
Contributor

@panukulv: Please use preview for sandbox purposes. There is no need to spam project with test issues and comments.

@mkurdej mkurdej added the awaiting-review Has pending Phabricator review label Jul 4, 2022
@mkurdej
Copy link
Member

mkurdej commented Jul 4, 2022

Revision URI: https://reviews.llvm.org/D129064

@mkurdej mkurdej closed this as completed in 14c30c7 Jul 7, 2022
@github-actions github-actions bot removed the awaiting-review Has pending Phabricator review label Jul 7, 2022
mem-frob pushed a commit to draperlaboratory/hope-llvm-project that referenced this issue Oct 7, 2022
Fixes llvm/llvm-project#56352.

Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay

Differential Revision: https://reviews.llvm.org/D129064
owenca pushed a commit that referenced this issue May 24, 2023
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
#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
#59178 (comment)
for some more details.

Fixes #58464.
Fixes #59178.

Differential Revision: https://reviews.llvm.org/D151047
llvmbot pushed a commit to llvm/llvm-project-release-prs that referenced this issue May 24, 2023
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
llvm/llvm-project#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
llvm/llvm-project#59178 (comment)
for some more details.

Fixes #58464.
Fixes #59178.

Differential Revision: https://reviews.llvm.org/D151047

(cherry picked from commit 72ab89e)
owenca pushed a commit that referenced this issue Jul 18, 2023
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 previous
indentation level. In case the --lines option was used
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://reviews.llvm.org/D129064
(#56352) to fix a crash. Our commit here essentially
reverts that crash fix. It was incorrect/incomplete.

Fixes #58464.
Fixes #59178.
Fixes #62799.

Differential Revision: https://reviews.llvm.org/D151047
jdoerfert pushed a commit to jdoerfert/llvm-project that referenced this issue Jul 24, 2023
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 previous
indentation level. In case the --lines option was used
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://reviews.llvm.org/D129064
(llvm#56352) to fix a crash. Our commit here essentially
reverts that crash fix. It was incorrect/incomplete.

Fixes llvm#58464.
Fixes llvm#59178.
Fixes llvm#62799.

Differential Revision: https://reviews.llvm.org/D151047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-format crash Prefer [crash-on-valid] or [crash-on-invalid]
Projects
None yet
Development

No branches or pull requests

5 participants