This is an archive of the discontinued LLVM Phabricator instance.

[clang] Cleanup unneeded Function nullptr checks [NFC]
ClosedPublic

Authored by mikerice on Dec 15 2021, 11:50 AM.

Details

Summary

Add an assert and avoid unneeded nullptr checks of Fn in CodeGenFunction::GenerateCode.

Unless I am missing something, Fn is always non-null here. If Fn could be null then the code in StartFunction seems seriously broken since it is used unchecked there.

Once there is one "if (Fn)" in the code, static analyzers start complaining about any unchecked uses of Fn.

Diff Detail

Event Timeline

mikerice requested review of this revision.Dec 15 2021, 11:50 AM
mikerice created this revision.
aaron.ballman added inline comments.Dec 15 2021, 12:04 PM
clang/lib/CodeGen/CodeGenFunction.cpp
1299–1301

How obnoxious would it be if I asked to make Fn a llvm::Function & instead of a pointer? Then we don't need the assert at all and the interface will enforce the requirement for us.

mikerice added inline comments.Dec 15 2021, 1:51 PM
clang/lib/CodeGen/CodeGenFunction.cpp
1299–1301

Good idea, but there is an assignment to it (Fn = Clone) which would be a problem right?

This revision is now accepted and ready to land.Dec 15 2021, 6:58 PM
aaron.ballman accepted this revision.Dec 16 2021, 4:24 AM

LG as-is, but feel free to go with the reference approach if you think it has value.

clang/lib/CodeGen/CodeGenFunction.cpp
1299–1301

Good idea, but there is an assignment to it (Fn = Clone) which would be a problem right?

I was thinking we'd be cheap and do something like:

void CodeGenFunction::GenerateCode(GlobalDecl GD, llvm::Function &Func,
                                   const CGFunctionInfo &FnInfo) {
  llvm::Function *Fn = &Func;
  ....

under the assumption that the static analysis tool should be smart enough to see that the pointer comes from a reference and thus cannot be null. But I may be making a faulty assumption there, so I don't insist.

This revision was landed with ongoing or failed builds.Dec 16 2021, 8:28 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptDec 16 2021, 8:28 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript