[clang] [Clang] Fix logical error in 'if else' condition that lead to an unreachable code (PR #95666)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Jun 15 09:33:57 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Shivam Gupta (xgupta)
<details>
<summary>Changes</summary>
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.
---
Full diff: https://github.com/llvm/llvm-project/pull/95666.diff
1 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+13-10)
``````````diff
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;
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/95666
More information about the cfe-commits
mailing list