[PATCH] D104774: [clang-format] Fix a bug that indents else-comment-if incorrectly
Owen Pan via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Jun 23 03:23:58 PDT 2021
owenpan created this revision.
owenpan added reviewers: djasper, klimek, MyDeveloperDay.
owenpan added a project: clang-format.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
If there is a comment between "else" and "if", the "if" statement including its body is indented incorrectly.
See https://bugs.llvm.org/show_bug.cgi?id=50809 for an example.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D104774
Files:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -1181,6 +1181,13 @@
" g();\n"
"else\n"
" h();");
+ verifyFormat("if (a)\n"
+ " f();\n"
+ "else // comment\n"
+ " if (b) {\n"
+ " g();\n"
+ " h()\n"
+ " }");
verifyFormat("if constexpr (a)\n"
" f();\n"
"else if constexpr (b)\n"
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -2021,7 +2021,15 @@
parseBlock(/*MustBeDeclaration=*/false);
addUnwrappedLine();
} else if (FormatTok->Tok.is(tok::kw_if)) {
+ FormatToken *Previous = AllTokens[Tokens->getPosition() - 1];
+ bool PrecededByComment = Previous->is(tok::comment);
+ if (PrecededByComment) {
+ addUnwrappedLine();
+ ++Line->Level;
+ }
parseIfThenElse();
+ if (PrecededByComment)
+ --Line->Level;
} else {
addUnwrappedLine();
++Line->Level;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104774.353909.patch
Type: text/x-patch
Size: 1326 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210623/06cb8b81/attachment.bin>
More information about the cfe-commits
mailing list