From 39e6077d97238ec13c9ed1b9dbae1e6408e5aba3 Mon Sep 17 00:00:00 2001 From: Emilia Dreamer Date: Sun, 25 Sep 2022 20:30:10 +0300 Subject: [PATCH] [clang-format] Look ahead before consuming `bool` in requires clause. The comment handling the bool case says: "bool is only allowed if it is directly followed by a paren for a cast" This change more closely follows this directive by looking ahead for the paren before consuming the bool keyword itself. Without a following paren, the bool would be part of something else, such as a return type for a function declaration Fixes https://github.com/llvm/llvm-project/issues/57538 Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay Differential Revision: https://reviews.llvm.org/D134325 --- clang/lib/Format/UnwrappedLineParser.cpp | 4 ++-- clang/unittests/Format/TokenAnnotatorTest.cpp | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 87a0e43..3919bc0 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -3530,9 +3530,9 @@ void UnwrappedLineParser::parseConstraintExpression() { // concept C = bool(...); // and bool is the only type, all other types as cast must be inside a // cast to bool an thus are handled by the other cases. - nextToken(); - if (FormatTok->isNot(tok::l_paren)) + if (Tokens->peekNextToken()->isNot(tok::l_paren)) return; + nextToken(); parseParens(); break; diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 6bc295c..c8de8a2 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -450,6 +450,14 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresClausesAndConcepts) { EXPECT_TOKEN(Tokens[20], tok::arrow, TT_TrailingReturnArrow); Tokens = annotate("template \n" + "requires Bar\n" + "bool foo(T) { return false; }"); + ASSERT_EQ(Tokens.size(), 21u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::kw_requires, TT_RequiresClause); + EXPECT_TRUE(Tokens[9]->ClosesRequiresClause); + EXPECT_TOKEN(Tokens[11], tok::identifier, TT_FunctionDeclarationName); + + Tokens = annotate("template \n" "struct S {\n" " void foo() const requires Bar;\n" " void bar() const & requires Baz;\n" -- 2.7.4