[PATCH] D118924: [clang-format] Fix formatting of macro definitions with a leading comment.
Marek Kurdej via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 3 10:03:59 PST 2022
curdeius created this revision.
curdeius added reviewers: MyDeveloperDay, HazardyKnusperkeks, owenpan.
curdeius requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
Fixes https://github.com/llvm/llvm-project/issues/43206.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D118924
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
@@ -1797,6 +1797,7 @@
TEST_F(FormatTest, UnderstandsMacros) {
verifyFormat("#define A (parentheses)");
+ verifyFormat("/* comment */ #define A (parentheses)");
verifyFormat("#define true ((int)1)");
verifyFormat("#define and(x)");
verifyFormat("#define if(x) x");
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -3549,7 +3549,9 @@
void UnwrappedLineParser::readToken(int LevelDifference) {
SmallVector<FormatToken *, 1> Comments;
+ bool FirstNonCommentOnLine = true;
do {
+ FormatToken *Previous = FormatTok;
FormatTok = Tokens->getNextToken();
assert(FormatTok);
while (FormatTok->getType() == TT_ConflictStart ||
@@ -3565,8 +3567,17 @@
FormatTok->MustBreakBefore = true;
}
+ bool FirstOnLine = FormatTok->HasUnescapedNewline || FormatTok->IsFirst;
+ if (Previous && Previous->Tok.is(tok::comment)) {
+ // Consider preprocessor directives preceded by block comments as first on
+ // line.
+ FirstNonCommentOnLine |= FirstOnLine;
+ } else {
+ FirstNonCommentOnLine = FirstOnLine;
+ }
+
while (!Line->InPPDirective && FormatTok->Tok.is(tok::hash) &&
- (FormatTok->HasUnescapedNewline || FormatTok->IsFirst)) {
+ FirstNonCommentOnLine) {
distributeComments(Comments, FormatTok);
Comments.clear();
// If there is an unfinished unwrapped line, we flush the preprocessor
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118924.405699.patch
Type: text/x-patch
Size: 1760 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220203/b6cd653b/attachment-0001.bin>
More information about the cfe-commits
mailing list