Skip to content

[Clang] Remove unreachable code in verilogGroupDecl (NFC) #95666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

xgupta
Copy link
Contributor

@xgupta xgupta commented Jun 15, 2024

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.

…achable 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.
@xgupta xgupta requested review from owenca and eywdck2l June 15, 2024 16:33
@llvmbot
Copy link
Member

llvmbot commented Jun 15, 2024

@llvm/pr-subscribers-clang-format

Author: Shivam Gupta (xgupta)

Changes

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 --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;
       }

@owenca owenca requested a review from sstwcw June 15, 2024 17:50
Copy link
Contributor

@HazardyKnusperkeks HazardyKnusperkeks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say if it's dead code, remove it. No tests are failing.

Or you have to provide a test case which fails without this change and passes with it.

Except the code is only recently dead, then we may have to wait for the bug reports.

@sstwcw
Copy link
Contributor

sstwcw commented Jun 16, 2024

I suggest removing the second block. The first block was intended for handling the backtick character. The second block was intended for handling the hash character. I messed things up when I made the code treat the backtick character like the hash character. I will figure out if the second block is really needed and add a test if it is needed.

Copy link

github-actions bot commented Jun 16, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@xgupta xgupta changed the title [Clang] Fix logical error in 'if else' condition that lead to an unreachable code [Clang] Remove unreachable code in verilogGroupDecl (NFC) Jun 16, 2024
@HazardyKnusperkeks
Copy link
Contributor

I think @sstwcw fixes this in #95703

@xgupta
Copy link
Contributor Author

xgupta commented Jun 16, 2024

I think @sstwcw fixes this in #95703

Oh nice then, I will close this PR.

@xgupta xgupta closed this Jun 16, 2024
@xgupta xgupta deleted the pvs-n1 branch June 16, 2024 18:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants