This revision was automatically updated to reflect the committed changes. Closed by commit rGf465a482caa9: [clang-format] Fix segmentation fault when formatting nested namespaces (authored by d0nc1h0t, committed by owenpan).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D158363/new/ https://reviews.llvm.org/D158363 Files: clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/FormatTest.cpp Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -4180,6 +4180,16 @@ "void foo() {}\n" "} // namespace ns", Style); + + FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle(); + LLVMWithCompactInnerNamespace.CompactNamespaces = true; + LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner; + verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n" + "// block for debug mode\n" + "#ifndef NDEBUG\n" + "#endif\n" + "}}} // namespace ns1::ns2::ns3", + LLVMWithCompactInnerNamespace); } TEST_F(FormatTest, NamespaceMacros) { Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -386,11 +386,14 @@ // Reduce indent level for bodies of namespaces which were compacted, // but only if their content was indented in the first place. auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1; - auto OutdentBy = I[J]->Level - TheLine->Level; + const int OutdentBy = I[J]->Level - TheLine->Level; + assert(OutdentBy >= 0); for (auto *CompactedLine = I + J; CompactedLine <= ClosingLine; ++CompactedLine) { - if (!(*CompactedLine)->InPPDirective) - (*CompactedLine)->Level -= OutdentBy; + if (!(*CompactedLine)->InPPDirective) { + const int Level = (*CompactedLine)->Level; + (*CompactedLine)->Level = std::max(Level - OutdentBy, 0); + } } } return J - 1;
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -4180,6 +4180,16 @@ "void foo() {}\n" "} // namespace ns", Style); + + FormatStyle LLVMWithCompactInnerNamespace = getLLVMStyle(); + LLVMWithCompactInnerNamespace.CompactNamespaces = true; + LLVMWithCompactInnerNamespace.NamespaceIndentation = FormatStyle::NI_Inner; + verifyFormat("namespace ns1 { namespace ns2 { namespace ns3 {\n" + "// block for debug mode\n" + "#ifndef NDEBUG\n" + "#endif\n" + "}}} // namespace ns1::ns2::ns3", + LLVMWithCompactInnerNamespace); } TEST_F(FormatTest, NamespaceMacros) { Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -386,11 +386,14 @@ // Reduce indent level for bodies of namespaces which were compacted, // but only if their content was indented in the first place. auto *ClosingLine = AnnotatedLines.begin() + ClosingLineIndex + 1; - auto OutdentBy = I[J]->Level - TheLine->Level; + const int OutdentBy = I[J]->Level - TheLine->Level; + assert(OutdentBy >= 0); for (auto *CompactedLine = I + J; CompactedLine <= ClosingLine; ++CompactedLine) { - if (!(*CompactedLine)->InPPDirective) - (*CompactedLine)->Level -= OutdentBy; + if (!(*CompactedLine)->InPPDirective) { + const int Level = (*CompactedLine)->Level; + (*CompactedLine)->Level = std::max(Level - OutdentBy, 0); + } } } return J - 1;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits