Skip to content
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

[clang][analyzer][NFC] Remove redundant code in StreamChecker #71394

Merged
merged 1 commit into from
Nov 7, 2023
Merged

[clang][analyzer][NFC] Remove redundant code in StreamChecker #71394

merged 1 commit into from
Nov 7, 2023

Conversation

benshi001
Copy link
Member

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Nov 6, 2023
Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

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

I find this alternative less idiomatic (dissimilar to how we implement checks in other checkers), thus I find this less readable.
I'm okay with that amount of redundancy as it was present.

/// Perform some common checks for all preXXX functions.
bool basicCheck(const FnDescription *Desc, const CallEvent &Call,
CheckerContext &C, ProgramStateRef &State,
SVal &StreamVal) const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function has a role like ensureStreamNonNullAndOpened, that would be a better name for it. And it should return the new State, like ensureStreamNonNull.

return;
State = ensureStreamOpened(StreamVal, C, State);
if (!State)
if (!basicCheck(Desc, Call, C, State, StreamVal))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if (!basicCheck(Desc, Call, C, State, StreamVal))
State = ensureStreamNonNullAndOpened(Desc, Call, C, State, StreamVal);
if (!State)
return;

Copy link
Collaborator

Choose a reason for hiding this comment

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

The function returns a state that is modified further by the following functions. Otherwise the state changes applied in basicCheck are lost. (ensureStreamNonNull does a state change, ensureStreamOpened does not, but all return a state to make the usage similar.)

@balazske
Copy link
Collaborator

balazske commented Nov 6, 2023

With the current code it is a corner case if this change makes the code more readable. Probably it can be useful if new functions are added to the checker. But the rule here is that there is one "ensure" function to check one aspect of the state, and the pre-callbacks call all of the ensure functions that are needed in that case. It would not much more readable if multiple combinations of ensure functions are made, for example ensureStreamNonNullAndOpenedAndNoFilePositionIndeterminate.

@benshi001 benshi001 changed the title [clang][Analyzer][NFC] Simplify preDefault/preFseek/preFreadFwrite of StreamChecker [clang][analyzer][NFC] Remove redundant code in StreamChecker Nov 7, 2023
@benshi001
Copy link
Member Author

It seems my previous change make little sense, so I reuse this PR for another change.

@balazske
Copy link
Collaborator

balazske commented Nov 7, 2023

This change looks not very useful to me. These removed return statements indicate that at the end of the if branch there is no more work to do in this function, and it reduces complexity (less execution paths). The code becomes a bit shorter but not necessarily more easy to understand. Optimizing code for execution speed or binary size is not important because I am sure that compiler optimizations can handle this case.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
@benshi001
Copy link
Member Author

How about only remove the second return State ? The removal of the first one may be less readable. But for the second one, it is very near to the final return State. It may looks strange for very near return States.

if () {
   return State;
}
return State;

@benshi001 benshi001 merged commit 1b45fe5 into llvm:main Nov 7, 2023
2 checks passed
@benshi001 benshi001 deleted the csa-nfc branch November 7, 2023 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants