[PATCH] D158363: [clang-format] Fix segmentation fault when formatting nested namespaces

Arkadiy Yudintsev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Aug 20 07:17:22 PDT 2023


d0nc1h0t created this revision.
d0nc1h0t added a project: clang-format.
Herald added projects: All, clang.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
Herald added a comment.
d0nc1h0t requested review of this revision.

NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please try to ensure all code changes have a unit test (unless this is an `NFC` or refactoring, adding documentation etc..)

Add your unit tests in `clang/unittests/Format` and you can build with `ninja FormatTests`.  We recommend using the `verifyFormat(xxx)` format of unit tests rather than `EXPECT_EQ` as this will ensure you change is tolerant to random whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can happen if you are trying to improve the annotation phase to ensure we are correctly identifying the type of a token, please add a token annotator test in `TokenAnnotatorTest.cpp`


Fixes #64701 <https://github.com/llvm/llvm-project/issues/64701>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158363

Files:
  clang/lib/Format/UnwrappedLineFormatter.cpp


Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -390,7 +390,7 @@
           for (auto *CompactedLine = I + J; CompactedLine <= ClosingLine;
                ++CompactedLine) {
             if (!(*CompactedLine)->InPPDirective)
-              (*CompactedLine)->Level -= OutdentBy;
+              (*CompactedLine)->Level -= std::min(OutdentBy, (*CompactedLine)->Level);
           }
         }
         return J - 1;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158363.551828.patch
Type: text/x-patch
Size: 584 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230820/769ea87a/attachment.bin>


More information about the cfe-commits mailing list