[PATCH] D136437: [clang-format] Insert closing braces after an unaffected line
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 23 01:46:53 PDT 2022
owenpan updated this revision to Diff 469969.
owenpan added a comment.
This revision is now accepted and ready to land.
Fixed assertion failures on the tests in https://github.com/llvm/llvm-project/issues/58161#issuecomment-1287904972.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D136437/new/
https://reviews.llvm.org/D136437
Files:
clang/lib/Format/Format.cpp
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -1874,26 +1874,37 @@
void insertBraces(SmallVectorImpl<AnnotatedLine *> &Lines,
tooling::Replacements &Result) {
const auto &SourceMgr = Env.getSourceManager();
+ int OpeningBraceSurplus = 0;
for (AnnotatedLine *Line : Lines) {
insertBraces(Line->Children, Result);
- if (!Line->Affected)
+ if (!Line->Affected && OpeningBraceSurplus == 0)
continue;
for (FormatToken *Token = Line->First; Token && !Token->Finalized;
Token = Token->Next) {
- if (Token->BraceCount == 0)
+ int BraceCount = Token->BraceCount;
+ if (BraceCount == 0)
continue;
std::string Brace;
- if (Token->BraceCount < 0) {
- assert(Token->BraceCount == -1);
+ if (BraceCount < 0) {
+ assert(BraceCount == -1);
+ if (!Line->Affected)
+ break;
Brace = Token->is(tok::comment) ? "\n{" : "{";
+ ++OpeningBraceSurplus;
} else {
- Brace = '\n' + std::string(Token->BraceCount, '}');
+ if (OpeningBraceSurplus == 0)
+ break;
+ if (OpeningBraceSurplus < BraceCount)
+ BraceCount = OpeningBraceSurplus;
+ Brace = '\n' + std::string(BraceCount, '}');
+ OpeningBraceSurplus -= BraceCount;
}
Token->BraceCount = 0;
const auto Start = Token->Tok.getEndLoc();
cantFail(Result.add(tooling::Replacement(SourceMgr, Start, 0, Brace)));
}
}
+ assert(OpeningBraceSurplus == 0);
}
};
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136437.469969.patch
Type: text/x-patch
Size: 1729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221023/4e082da0/attachment-0001.bin>
More information about the cfe-commits
mailing list