https://github.com/xgupta created 
https://github.com/llvm/llvm-project/pull/95666

This is described in https://pvs-studio.com/en/blog/posts/cpp/1126/ so caught 
by the PVS Studio analyzer.

Warning message  -
The use of 'if (A) {...} else if (A) {...}' pattern was detected

There were two same 'if' conditions (Tok->is(tok::hash) but different execution 
blocks leading to unreachable code for the second 'if-else' condition.

>From 0e88700adf7add65f3eb8a2d4d2c2de72703a0f0 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98....@gmail.com>
Date: Sat, 15 Jun 2024 21:56:09 +0530
Subject: [PATCH] [Clang] Fix logical error in 'if else' condition that lead to
 an unreachable code

This is describe in https://pvs-studio.com/en/blog/posts/cpp/1126/ so caught by 
PVS
Studio analyzer.

Warning message  -
The use of 'if (A) {...} else if (A) {...}' pattern was detected

There were two same 'if' condition (Tok->is(tok::hash) but different execution 
blocks that was leading to
unnreachable code for second 'if else' condition.
---
 clang/lib/Format/TokenAnnotator.cpp | 23 +++++++++++++----------
 1 file changed, 13 insertions(+), 10 deletions(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp 
b/clang/lib/Format/TokenAnnotator.cpp
index 1fe3b61a5a81f..5a7029bda65f3 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3369,11 +3369,19 @@ class ExpressionParser {
       FormatToken *Next = Tok->getNextNonComment();
 
       if (Tok->is(tok::hash)) {
-        // Start of a macro expansion.
-        First = Tok;
-        Tok = Next;
-        if (Tok)
-          Tok = Tok->getNextNonComment();
+
+        if (Next && Next->is(tok::l_paren)) {
+          // Handle parameterized macro.
+          Next = Next->MatchingParen;
+          if (Next)
+            Tok = Next->getNextNonComment();
+        } else {
+          // Start of a macro expansion.
+          First = Tok;
+          Tok = Next;
+          if (Tok)
+            Tok = Tok->getNextNonComment();
+        }
       } else if (Tok->is(tok::hashhash)) {
         // Concatenation. Skip.
         Tok = Next;
@@ -3410,11 +3418,6 @@ class ExpressionParser {
         } else {
           break;
         }
-      } else if (Tok->is(tok::hash)) {
-        if (Next->is(tok::l_paren))
-          Next = Next->MatchingParen;
-        if (Next)
-          Tok = Next->getNextNonComment();
       } else {
         break;
       }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to