curdeius updated this revision to Diff 405702. curdeius added a comment. Add a test case. Use bool instead of Previous token.
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D118924/new/ 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,8 @@ TEST_F(FormatTest, UnderstandsMacros) { verifyFormat("#define A (parentheses)"); + verifyFormat("/* comment */ #define A (parentheses)"); + verifyFormat("/* comment */ /* another 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,6 +3549,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) { SmallVector<FormatToken *, 1> Comments; + bool PreviousWasComment = false; + bool FirstNonCommentOnLine = false; do { FormatTok = Tokens->getNextToken(); assert(FormatTok); @@ -3565,8 +3567,17 @@ FormatTok->MustBreakBefore = true; } + bool FirstOnLine = FormatTok->HasUnescapedNewline || FormatTok->IsFirst; + if (PreviousWasComment) { + // 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 @@ -3587,6 +3598,8 @@ parsePPDirective(); } + PreviousWasComment = FormatTok->Tok.is(tok::comment); + if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && !Line->InPPDirective) continue;
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -1797,6 +1797,8 @@ TEST_F(FormatTest, UnderstandsMacros) { verifyFormat("#define A (parentheses)"); + verifyFormat("/* comment */ #define A (parentheses)"); + verifyFormat("/* comment */ /* another 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,6 +3549,8 @@ void UnwrappedLineParser::readToken(int LevelDifference) { SmallVector<FormatToken *, 1> Comments; + bool PreviousWasComment = false; + bool FirstNonCommentOnLine = false; do { FormatTok = Tokens->getNextToken(); assert(FormatTok); @@ -3565,8 +3567,17 @@ FormatTok->MustBreakBefore = true; } + bool FirstOnLine = FormatTok->HasUnescapedNewline || FormatTok->IsFirst; + if (PreviousWasComment) { + // 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 @@ -3587,6 +3598,8 @@ parsePPDirective(); } + PreviousWasComment = FormatTok->Tok.is(tok::comment); + if (!PPStack.empty() && (PPStack.back().Kind == PP_Unreachable) && !Line->InPPDirective) continue;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits