[PATCH] D136437: [clang-format] Insert closing braces of unaffected lines
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 21 03:32:36 PDT 2022
owenpan created this revision.
owenpan added reviewers: MyDeveloperDay, HazardyKnusperkeks, rymiel.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The closing braces to be inserted are on unaffected lines by definition as those lines don't exist yet. Extra work is required in order for them to be inserted after the matching opening braces are inserted.
Fixes https://github.com/llvm/llvm-project/issues/58161.
Repository:
rG LLVM Github Monorepo
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,32 @@
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)
+ const 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);
Brace = Token->is(tok::comment) ? "\n{" : "{";
+ ++OpeningBraceSurplus;
} else {
- Brace = '\n' + std::string(Token->BraceCount, '}');
+ Brace = '\n' + std::string(BraceCount, '}');
+ OpeningBraceSurplus -= BraceCount;
+ assert(OpeningBraceSurplus >= 0);
}
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.469532.patch
Type: text/x-patch
Size: 1571 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221021/b9b59386/attachment.bin>
More information about the cfe-commits
mailing list