From 9cf4b7266bbf238e4a5bd85c8d0011aac2f9181b Mon Sep 17 00:00:00 2001 From: Marek Kurdej Date: Fri, 17 Dec 2021 23:02:16 +0100 Subject: [PATCH] [clang-format] Refactor common handling of attributes. NFC. Reviewed By: MyDeveloperDay Differential Revision: https://reviews.llvm.org/D115968 --- clang/lib/Format/UnwrappedLineParser.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 0b1771b..9ea0db8 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2159,18 +2159,22 @@ void UnwrappedLineParser::parseSquare(bool LambdaIntroducer) { } void UnwrappedLineParser::parseIfThenElse() { + auto HandleAttributes = [this]() { + // Handle AttributeMacro, e.g. `if (x) UNLIKELY`. + if (FormatTok->is(TT_AttributeMacro)) + nextToken(); + // Handle [[likely]] / [[unlikely]] attributes. + if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) + parseSquare(); + }; + assert(FormatTok->Tok.is(tok::kw_if) && "'if' expected"); nextToken(); if (FormatTok->Tok.isOneOf(tok::kw_constexpr, tok::identifier)) nextToken(); if (FormatTok->Tok.is(tok::l_paren)) parseParens(); - // handle AttributeMacro if (x) UNLIKELY - if (FormatTok->is(TT_AttributeMacro)) - nextToken(); - // handle [[likely]] / [[unlikely]] - if (FormatTok->is(tok::l_square) && tryToParseSimpleAttribute()) - parseSquare(); + HandleAttributes(); bool NeedsUnwrappedLine = false; if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); @@ -2187,12 +2191,7 @@ void UnwrappedLineParser::parseIfThenElse() { } if (FormatTok->Tok.is(tok::kw_else)) { nextToken(); - // handle AttributeMacro else UNLIKELY - if (FormatTok->is(TT_AttributeMacro)) - nextToken(); - // handle [[likely]] / [[unlikely]] - if (FormatTok->Tok.is(tok::l_square) && tryToParseSimpleAttribute()) - parseSquare(); + HandleAttributes(); if (FormatTok->Tok.is(tok::l_brace)) { CompoundStatementIndenter Indenter(this, Style, Line->Level); parseBlock(); -- 2.7.4