[clang] [Clang] Fix logical error in 'if else' condition that lead to an unreachable code (PR #95666)
Shivam Gupta via cfe-commits
cfe-commits at lists.llvm.org
Sun Jun 16 07:49:41 PDT 2024
https://github.com/xgupta updated https://github.com/llvm/llvm-project/pull/95666
>From 0e88700adf7add65f3eb8a2d4d2c2de72703a0f0 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Sat, 15 Jun 2024 21:56:09 +0530
Subject: [PATCH 1/3] [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;
}
>From 17e1c3561830866592fd3a55e7a296194b221a90 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Sun, 16 Jun 2024 20:00:48 +0530
Subject: [PATCH 2/3] remove dead code
---
clang/lib/Format/TokenAnnotator.cpp | 7 -------
1 file changed, 7 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 5a7029bda65f3..3bc7caaa95529 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3369,13 +3369,6 @@ class ExpressionParser {
FormatToken *Next = Tok->getNextNonComment();
if (Tok->is(tok::hash)) {
-
- 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;
>From 3928b8936f05562312e177443821f452829034db Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Sun, 16 Jun 2024 20:18:49 +0530
Subject: [PATCH 3/3] clang-format
---
clang/lib/Format/TokenAnnotator.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 3bc7caaa95529..4150c59ac5081 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3369,12 +3369,12 @@ 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();
- }
+ // Start of a macro expansion.
+ First = Tok;
+ Tok = Next;
+ if (Tok)
+ Tok = Tok->getNextNonComment();
+ }
} else if (Tok->is(tok::hashhash)) {
// Concatenation. Skip.
Tok = Next;
More information about the cfe-commits
mailing list