Skip to content

[LLDB] Remove the redundent increment of 'properties' variable #95675

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

Merged
merged 2 commits into from
Jul 27, 2024

Conversation

xgupta
Copy link
Contributor

@xgupta xgupta commented Jun 15, 2024

This is described in (N3) https://pvs-studio.com/en/blog/posts/cpp/1126/

Warning message -
V547 Expression 'properties ++ > 0' is always false. CommandObjectTarget.cpp:100

I could not understand it properly but the properties++ operation is performed twice when the target architecture is valid.
First increment seems unnecessary since it is always false '0>0'.

This is described in (N3) https://pvs-studio.com/en/blog/posts/cpp/1126/ so caught by the PVS Studio analyzer.
Warning message -
V547 Expression 'properties ++ > 0' is always false. CommandObjectTarget.cpp:100

I could not understand it properly but the properties++ operation is performed
twice when the target architecture is valid.
First increment seems unnecessary since it is always false '0>0'.
@xgupta xgupta requested a review from JDevlieghere as a code owner June 15, 2024 18:07
@llvmbot llvmbot added the lldb label Jun 15, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 15, 2024

@llvm/pr-subscribers-lldb

Author: Shivam Gupta (xgupta)

Changes

This is described in (N3) https://pvs-studio.com/en/blog/posts/cpp/1126/ so caught by the PVS Studio analyzer. Warning message -
V547 Expression 'properties ++ > 0' is always false. CommandObjectTarget.cpp:100

I could not understand it properly but the properties++ operation is performed twice when the target architecture is valid.
First increment seems unnecessary since it is always false '0>0'.


Full diff: https://github.com/llvm/llvm-project/pull/95675.diff

1 Files Affected:

  • (modified) lldb/source/Commands/CommandObjectTarget.cpp (+1-1)
diff --git a/lldb/source/Commands/CommandObjectTarget.cpp b/lldb/source/Commands/CommandObjectTarget.cpp
index ae6c6d5479a19..c3d4307267a1b 100644
--- a/lldb/source/Commands/CommandObjectTarget.cpp
+++ b/lldb/source/Commands/CommandObjectTarget.cpp
@@ -97,7 +97,7 @@ static void DumpTargetInfo(uint32_t target_idx, Target *target,
 
   uint32_t properties = 0;
   if (target_arch.IsValid()) {
-    strm.Printf("%sarch=", properties++ > 0 ? ", " : " ( ");
+    strm.Printf("%sarch=", properties > 0 ? ", " : " ( ");
     target_arch.DumpTriple(strm.AsRawOstream());
     properties++;
   }

Copy link
Member

@bulbazord bulbazord left a comment

Choose a reason for hiding this comment

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

I think you can probably just remove the check for properties value entirely. This is the first property that may be printed, so it will never be greater than 0. I think this will work:

stream.Printf(" ( arch=");

The other ones will have to stay because if the target architecture isn't valid, properties may still be 0.

Alternatively, if you feel motivated, you can rewrite the target dumping code altogether. My suggestion would be to gather all of the information up front (triple, platform, pid, state, etc) and then print it all at the end in one group so we don't have to do all these inline ternary conditions in the first place.

@xgupta
Copy link
Contributor Author

xgupta commented Jul 26, 2024

I think you can probably just remove the check for properties value entirely. This is the first property that may be printed, so it will never be greater than 0. I think this will work:

stream.Printf(" ( arch=");

The other ones will have to stay because if the target architecture isn't valid, properties may still be 0.

Sorry have taken very long time to update the PR.

Alternatively, if you feel motivated, you can rewrite the target dumping code altogether. My suggestion would be to gather all of the information up front (triple, platform, pid, state, etc) and then print it all at the end in one group so we don't have to do all these inline ternary conditions in the first place.

I would be interested to do that, but don't know I have time and energy to do that right now. I will probably create a github issue, someone or me later can try that.

@xgupta xgupta merged commit 558315a into llvm:main Jul 27, 2024
5 of 6 checks passed
@xgupta xgupta deleted the pvs-n3 branch July 27, 2024 01:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants