[PATCH] D136437: [clang-format] Insert closing braces after an unaffected line
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 24 20:00:25 PDT 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG37e754e5801c: [clang-format] Insert closing braces after an unaffected line (authored by owenpan).
Repository:
rG LLVM Github Monorepo
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.470359.patch
Type: text/x-patch
Size: 1729 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221025/3b89ce0f/attachment.bin>
More information about the cfe-commits
mailing list