diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -66,7 +66,8 @@ (Style.PPIndentWidth >= 0) ? Style.PPIndentWidth : Style.IndentWidth; Indent = Line.Level * IndentWidth + AdditionalIndent; } else { - IndentForLevel.resize(Line.Level + 1); + if (IndentForLevel.size() < Line.Level + 1) + IndentForLevel.resize(Line.Level + 1); Indent = getIndent(Line.Level); } if (static_cast(Indent) + Offset >= 0) @@ -91,6 +92,7 @@ unsigned LevelIndent = Line.First->OriginalColumn; if (static_cast(LevelIndent) - Offset >= 0) LevelIndent -= Offset; + assert(Line.Level < IndentForLevel.size()); if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) && !Line.InPPDirective) { IndentForLevel[Line.Level] = LevelIndent; diff --git a/clang/unittests/Format/FormatTestSelective.cpp b/clang/unittests/Format/FormatTestSelective.cpp --- a/clang/unittests/Format/FormatTestSelective.cpp +++ b/clang/unittests/Format/FormatTestSelective.cpp @@ -609,6 +609,14 @@ " return a == 8 ? 32 : 16;\n" "}\n"; EXPECT_EQ(Code, format(Code, 40, 0)); + + // https://llvm.org/PR56352 + Style.CompactNamespaces = true; + Style.NamespaceIndentation = FormatStyle::NI_All; + Code = "\n" + "namespace ns1 { namespace ns2 {\n" + "}}"; + EXPECT_EQ(Code, format(Code, 0, 0)); } } // end namespace