[clang] 4f7086c - [clang-format] Fix misannotation of && before noexcept (#65526)

via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 7 03:44:31 PDT 2023


Author: Björn Schäpers
Date: 2023-09-07T12:44:26+02:00
New Revision: 4f7086ce33fccd046dbd265debaf658d3dbf2e7c

URL: https://github.com/llvm/llvm-project/commit/4f7086ce33fccd046dbd265debaf658d3dbf2e7c
DIFF: https://github.com/llvm/llvm-project/commit/4f7086ce33fccd046dbd265debaf658d3dbf2e7c.diff

LOG: [clang-format] Fix misannotation of && before noexcept (#65526)

When we are in an expression, it has to be a binary operator and not
pointer or reference.

Added: 
    

Modified: 
    clang/lib/Format/TokenAnnotator.cpp
    clang/unittests/Format/TokenAnnotatorTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index ea811414b73c506..3e0599eb540a95f 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2535,8 +2535,9 @@ class AnnotatingParser {
       return TT_BinaryOperator;
 
     if (!NextToken ||
-        NextToken->isOneOf(tok::arrow, tok::equal, tok::kw_noexcept, tok::comma,
-                           tok::r_paren, TT_RequiresClause) ||
+        NextToken->isOneOf(tok::arrow, tok::equal, tok::comma, tok::r_paren,
+                           TT_RequiresClause) ||
+        (NextToken->is(tok::kw_noexcept) && !IsExpression) ||
         NextToken->canBePointerOrReferenceQualifier() ||
         (NextToken->is(tok::l_brace) && !NextToken->getNextNonComment())) {
       return TT_PointerOrReference;

diff  --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 467ade965d7c86a..66bfd7f0ef0d282 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -273,6 +273,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   ASSERT_EQ(Tokens.size(), 19u) << Tokens;
   EXPECT_TOKEN(Tokens[5], tok::ampamp, TT_BinaryOperator);
 
+  Tokens =
+      annotate("auto foo() noexcept(noexcept(bar()) && "
+               "trait<std::decay_t<decltype(bar())>> && noexcept(baz())) {}");
+  EXPECT_EQ(Tokens.size(), 38u) << Tokens;
+  EXPECT_TOKEN(Tokens[12], tok::ampamp, TT_BinaryOperator);
+  EXPECT_TOKEN(Tokens[27], tok::ampamp, TT_BinaryOperator);
+
   FormatStyle Style = getLLVMStyle();
   Style.TypeNames.push_back("MYI");
   Tokens = annotate("if (MYI *p{nullptr})", Style);


        


More information about the cfe-commits mailing list