[clang] [clang-format] NEVER allow && to be an unary operator. (PR #78852)

Emilia Kond via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 20 07:28:59 PST 2024


https://github.com/rymiel created https://github.com/llvm/llvm-project/pull/78852

Simply always return false if tok::ampamp is passed into determineUnaryOperatorByUsage.

I've done a fix earlier regarding ampamp becoming an unary operator, this would technically supersede that.

I'm not sure why this hasn't been done before, I don't see any downsides.

Fixes https://github.com/llvm/llvm-project/issues/78789

>From 4171fa9282330764a35b45ac08bde8b3bfc6f2c5 Mon Sep 17 00:00:00 2001
From: Emilia Kond <emilia at rymiel.space>
Date: Sat, 20 Jan 2024 17:21:40 +0200
Subject: [PATCH] [clang-format] NEVER allow && to be an unary operator.

Simply always return false if tok::ampamp is passed into
determineUnaryOperatorByUsage.

I've done a fix earlier regarding ampamp becoming an unary operator,
this would technically supersede that.

I'm not sure why this hasn't been done before, I don't see any
downsides.

Fixes https://github.com/llvm/llvm-project/issues/78789
---
 clang/lib/Format/TokenAnnotator.cpp           | 3 +++
 clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
 2 files changed, 7 insertions(+)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 25fcceb8786437..573e986b6939df 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2503,6 +2503,9 @@ class AnnotatingParser {
     if (!PrevToken)
       return true;
 
+    if (Tok.is(tok::ampamp))
+      return false;
+
     // These keywords are deliberately not included here because they may
     // precede only one of unary star/amp and plus/minus but not both.  They are
     // either included in determineStarAmpUsage or determinePlusMinusCaretUsage.
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 64b2abac5cce53..f8d2b264ee7480 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -309,6 +309,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
   EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
   EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
   EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
+
+  Tokens = annotate("if (delete && foo) {}");
+  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {



More information about the cfe-commits mailing list