[clang-format] Fix a crash in AlignArrayOfStructures#167099
Merged
Conversation
Member
|
@llvm/pr-subscribers-clang-format Author: owenca (owenca) ChangesFixes #157405 2 Files Affected:
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index f24b8ab14bdce..d37834e2df902 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1215,7 +1215,10 @@ void WhitespaceManager::alignArrayInitializers() {
bool FoundComplete = false;
for (unsigned InsideIndex = ChangeIndex + 1; InsideIndex < ChangeEnd;
++InsideIndex) {
- if (Changes[InsideIndex].Tok == C.Tok->MatchingParen) {
+ const auto *Tok = Changes[InsideIndex].Tok;
+ if (Tok->is(tok::pp_define))
+ break;
+ if (Tok == C.Tok->MatchingParen) {
alignArrayInitializers(ChangeIndex, InsideIndex + 1);
ChangeIndex = InsideIndex + 1;
FoundComplete = true;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ca9e7925e5e95..36ce5ef35acb3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22265,6 +22265,19 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
"});",
Style);
+ verifyNoCrash(
+ "PANEL_Ic PANEL_ic[PANEL_IC_NUMBER] =\n"
+ " {\n"
+ " {PIC(0), PIC(0), PIC(99), PIC(81), 0}, // Backbox\n"
+ " {PIC(1), PIC(83), PIC(191), PIC(137), 0}, // AK47\n"
+ "\n"
+ "#define PICALL1(a, b, c, d) \\\n"
+ " { PIC(a), PIC(b), PIC(c), PIC(d), 1 }\n"
+ "\n"
+ " PICALL1(1, 1, 75, 50),\n"
+ "};",
+ Style);
+
Style.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign;
verifyFormat("#define FOO \\\n"
" int foo[][2] = { \\\n"
|
HazardyKnusperkeks
approved these changes
Nov 9, 2025
Contributor
HazardyKnusperkeks
left a comment
There was a problem hiding this comment.
What about other pp directives?
Contributor
Author
I wouldn't bother as brace tokens can only exist in macro bodies in practice. |
dyung
pushed a commit
to llvmbot/llvm-project
that referenced
this pull request
Dec 10, 2025
Fixes llvm#157405 (cherry picked from commit 836919b)
YukinoHayakawa
pushed a commit
to UsagiEngine/buildsystem-llvm-project
that referenced
this pull request
Mar 14, 2026
Fixes llvm#157405 (cherry picked from commit 836919b)
LumioseSil
pushed a commit
to LumioseSil/llvm-project
that referenced
this pull request
Apr 4, 2026
Fixes llvm#157405 (cherry picked from commit 836919b)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #157405