Skip to content

Commit

Permalink
fix: update _contract_
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Nov 9, 2023
1 parent 9bf52c7 commit 2b577cb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
25 changes: 21 additions & 4 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,22 @@ class LineJoiner {
TheLine->First->is(TT_FunctionTypeLParen) &&
NextLine.First->isNot(tok::l_brace);
const AnnotatedLine *const ThirdLine = I + 2 != E ? I[2] : nullptr;
const auto EndsContract = [&](FormatToken *Tok) {
if (Tok->isNot(tok::r_paren) || !Tok->MatchingParen ||
!Tok->MatchingParen->Previous)
return false;
Tok = Tok->MatchingParen->Previous;
if (Tok->is(tok::greater)) {
if (!Tok->MatchingParen || !Tok->MatchingParen->Previous)
return false;
Tok = Tok->MatchingParen->Previous;
}
return Tok->isOneOf(Keywords.kw_pre, Keywords.kw_post,
Keywords.kw_assert);
};
const bool MergeSingleContract =
TheLine->Last->isNot(TT_AttributeSquare) &&
NextLine.First->is(TT_AttributeSquare) && NextLine.Children.empty() &&
ThirdLine && ThirdLine->First->isNot(TT_AttributeSquare);
!EndsContract(TheLine->Last) && EndsContract(NextLine.Last) &&
ThirdLine && !EndsContract(ThirdLine->Last);
if (MergeUnbracedParameterizedStatement || MergeSingleContract)
return tryMergeSimpleBlock(I, E, Limit);
}
Expand Down Expand Up @@ -899,7 +911,10 @@ class LineJoiner {
Line.First->is(TT_FunctionTypeLParen) &&
I[1]->First->isNot(tok::l_brace);
const bool MergesWithSingleContract =
I[1]->First->isOneOf(TT_AttributeSquare, tok::kw_requires,
(I[1]->First->isOneOf(Keywords.kw_pre, Keywords.kw_post,
Keywords.kw_assert) &&
I[1]->First->Next->isOneOf(tok::less, tok::l_paren)) ||
I[1]->First->isOneOf(tok::kw_requires,
TT_Cpp2DeclarationBinaryOperator);
if (!IsUnbracedParameterizedStatement && !MergesWithSingleContract)
return 0;
Expand Down Expand Up @@ -928,6 +943,8 @@ class LineJoiner {
// Count the merged the line.
if (MergedLines > 0)
++MergedLines;
else
return MergesWithSingleContract;
}
return MergedLines;
}
Expand Down
57 changes: 23 additions & 34 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ void printLine(llvm::raw_ostream &OS, const UnwrappedLine &Line,
OS << Prefix;
NewLine = false;
}
OS << I->Tok->Tok.getName() << "["
<< "T=" << (unsigned)I->Tok->getType()
OS << I->Tok->Tok.getName() << "[" << "T=" << (unsigned)I->Tok->getType()
<< ", OC=" << I->Tok->OriginalColumn << ", \"" << I->Tok->TokenText
<< "\"] ";
for (SmallVectorImpl<UnwrappedLine>::const_iterator
Expand Down Expand Up @@ -2280,7 +2279,8 @@ class UnwrappedLineParser::Cpp2FormatTokenSource : public FormatTokenSource {

static bool isReclaimedIdentifier(const FormatToken *const FormatTok) {
// Undo changes that can spill to Cpp1 code in the destructor.
return FormatTok->isOneOf(tok::kw_new, tok::kw_struct, tok::kw_class, tok::kw_enum, tok::kw_union) ||
return FormatTok->isOneOf(tok::kw_new, tok::kw_struct, tok::kw_class,
tok::kw_enum, tok::kw_union) ||
llvm::is_contained({"and", "and_eq", "bitand", "bitor", "compl",
"not", "not_eq", "or", "or_eq", "xor", "xor_eq"},
FormatTok->TokenText);
Expand Down Expand Up @@ -2675,7 +2675,8 @@ void UnwrappedLineParser::parseCpp2PrimaryExpression() {
}

bool UnwrappedLineParser::atCpp2PostfixOperator(const CurrentToken Tok) const {
if (Tok->isOneOf(tok::plusplus, tok::minusminus, tok::tilde, tok::dollar, tok::ellipsis))
if (Tok->isOneOf(tok::plusplus, tok::minusminus, tok::tilde, tok::dollar,
tok::ellipsis))
return true;
if (Tok->isOneOf(tok::star, tok::amp))
return !Tok->hasWhitespaceBefore();
Expand Down Expand Up @@ -2956,34 +2957,23 @@ void UnwrappedLineParser::parseCpp2ExpressionStatement() {
parseCpp2Semi();
}

void UnwrappedLineParser::parseCpp2ContractKind() {
parseCpp2Token({Keywords.kw_pre, Keywords.kw_post, Keywords.kw_assert});
bool UnwrappedLineParser::atCpp2Contract(const CurrentToken Tok) const {
return Tok->isOneOf(Keywords.kw_pre, Keywords.kw_post, Keywords.kw_assert) &&
Tokens->peekNextToken(/*SkipComment=*/true)
->isOneOf(tok::less, tok::l_paren);
}

void UnwrappedLineParser::parseCpp2ContractColon() {
parseCpp2Token(tok::colon, TT_AttributeColon);
}

void UnwrappedLineParser::parseCpp2ContractBody() {
parseCpp2ContractKind();
void UnwrappedLineParser::parseCpp2Contract() {
const auto _ = Cpp2Context.stackSomethingElse();
parseCpp2IdExpression();
parseCpp2ContractColon();
parseCpp2LogicalOrExpression();
parseCpp2Token(tok::comma);
parseCpp2Token(tok::string_literal);
}

template <UnwrappedLineParser::Cpp2ParserFunction Parser>
void UnwrappedLineParser::parseCpp2ContractBalancedSquares() {
parseCpp2BalancedPunctuators({tok::l_square, TT_AttributeSquare}, Parser);
parseCpp2PrimaryExpression();
if (FormatTok->isNot(tok::semi))
addUnwrappedLine();
}

void UnwrappedLineParser::parseCpp2Contract() {
const auto _ = Cpp2Context.stackSomethingElse();
parseCpp2ContractBalancedSquares<
&UnwrappedLineParser::parseCpp2ContractBalancedSquares<
&UnwrappedLineParser::parseCpp2ContractBody>>();
addUnwrappedLine();
void UnwrappedLineParser::parseCpp2ContractStatement() {
parseCpp2Contract();
parseCpp2Semi();
}

void UnwrappedLineParser::parseCpp2Statement() {
Expand All @@ -3004,6 +2994,8 @@ void UnwrappedLineParser::parseCpp2Statement() {
case tok::l_brace:
return parseCpp2CompoundStatement();
default:
if (atCpp2Contract(FormatTok))
return parseCpp2ContractStatement();
if (FormatTok->is(Keywords.kw_inspect))
return parseCpp2InspectExpression();
if (parseCpp2Label())
Expand All @@ -3015,8 +3007,6 @@ void UnwrappedLineParser::parseCpp2Statement() {
if (atCpp2ParameterizedStatement(FormatTok))
return parseCpp2ParameterizedStatement();
return parseCpp2ExpressionStatement();
case tok::l_square:
return parseCpp2Contract();
}
}

Expand Down Expand Up @@ -3092,9 +3082,8 @@ bool UnwrappedLineParser::skipBalancedTokens(CurrentToken &Tok) {
return true;
}

auto UnwrappedLineParser::skipDeclarationSignature(CurrentToken Tok,
const tok::TokenKind Closer)
-> CurrentToken {
auto UnwrappedLineParser::skipDeclarationSignature(
CurrentToken Tok, const tok::TokenKind Closer) -> CurrentToken {
while (!Tok->isOneOf(Closer, tok::eof, tok::comma)) {
if (skipBalancedTokens(Tok))
continue;
Expand Down Expand Up @@ -3221,10 +3210,10 @@ void UnwrappedLineParser::parseCpp2ReturnList() {
}

void UnwrappedLineParser::parseCpp2ContractSeq() {
if (FormatTok->isNot(tok::l_square))
if (!atCpp2Contract(FormatTok))
return;
if (parsingCpp2ImplicitBlock()) {
while (FormatTok->is(tok::l_square))
while (atCpp2Contract(FormatTok))
parseCpp2Contract();
return;
}
Expand Down
6 changes: 2 additions & 4 deletions clang/lib/Format/UnwrappedLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,9 @@ class UnwrappedLineParser {
bool parseCpp2Label();
void parseCpp2IterationStatementWithoutLabel();
void parseCpp2ExpressionStatement();
void parseCpp2ContractKind();
void parseCpp2ContractColon();
void parseCpp2ContractBody();
template <Cpp2ParserFunction> void parseCpp2ContractBalancedSquares();
bool atCpp2Contract(CurrentToken) const;
void parseCpp2Contract();
void parseCpp2ContractStatement();
void parseCpp2Statement();
void parseCpp2MetaFunctionsList();
bool precedesCpp2Identifier(CurrentToken) const;
Expand Down

0 comments on commit 2b577cb

Please sign in to comment.