This is an archive of the discontinued LLVM Phabricator instance.

[analyzer][NFC] Refactor llvm::isa<> usages in the StaticAnalyzer
ClosedPublic

Authored by steakhal on Oct 18 2021, 3:30 AM.

Details

Summary

It turns out llvm::isa<> is variadic, and we could have used this at a
lot of places.

The following patterns:

x && isa<T1>(x) || isa<T2>(x) ...

Will be replaced by:

isa_and_non_null<T1, T2, ...>(x)

Sometimes it caused further simplifications when it would cause even
more code smell.

Aside from this, keep in mind that within assert() or any macro
functions, we need to wrap the isa<> expression within a parenthesis,
due to the parsing of the comma.

Note: This is an NFC change!
Please, review accordingly. I hope I did not mess up anything.

Diff Detail

Event Timeline

steakhal created this revision.Oct 18 2021, 3:30 AM
steakhal requested review of this revision.Oct 18 2021, 3:30 AM

isa<> became a variadic template a year or so ago, only!

I think in general you did everything right!

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
939

Hrm, I didn't know about isa_and_nonnull either...

952–953

I think keeping the (not null) Stmt check here would keep the code more readable. I had to stop and think about why you would be checking for Stmt being null while you go and dereference something else.

clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
14

What required this header / change?

clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
18

Ditto.

steakhal marked an inline comment as done.Oct 18 2021, 5:00 AM

Thanks for the review @whisperity !
I really wanted to doublecheck this stuff, since I did this by hand.
Although, one can probably easily extend the existing llvm convention tidy checker to recognize this pattern.
That would make this refactoring really suitable for the whole llvm project family - even for downstream users.
However, I'm not the one implementing it :D

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
952–953

I like the new form.
And, just imagine if we would extend the llvm-prefer-isa-or-dyn-cast-in-conditionals checker with this pattern.
By simply applying the fixits we would reach this as its fixedpoint.
First, by merging the two distinct isa<> into a single one, then removing the redundant parenthesis.
Second, merging the Stmt && isa<> into an isa_and_non_null<>.

clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
14

Uh, I'm not sure. clang must have done something :D
Let me investigate!

clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
18

Yea, yea. I'm on it.

whisperity added inline comments.Oct 18 2021, 5:09 AM
clang/lib/StaticAnalyzer/Checkers/UnixAPIChecker.cpp
14

I think in general your editor just decided including this header would be better than including llvm/Support/Casting.h.

whisperity added inline comments.Oct 18 2021, 5:12 AM
clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
952–953

It seems that's already implemented and always has been since the introduction of the check (rG28413dd87aa21):

  } else if (const auto *MatchedDecl =
                 Result.Nodes.getNodeAs<BinaryOperator>("and")) {

/* ... */

    std::string Replacement("isa_and_nonnull");
    Replacement += RHSString.substr(Func->getName().size());

    diag(MatchedDecl->getBeginLoc(),
         "isa_and_nonnull<> is preferred over an explicit test for null "
         "followed by calling isa<>")
        << FixItHint::CreateReplacement(SourceRange(MatchedDecl->getBeginLoc(),
                                                    MatchedDecl->getEndLoc()),
                                        Replacement);
steakhal added inline comments.Oct 18 2021, 5:26 AM
clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
952–953

I meant the disjunction of isa<>s - which is probably not implemented yet.

martong accepted this revision.Oct 20 2021, 2:56 AM

Looks good, but please take care of the header inclusion problem.

clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
939

Ahh, this is very awesome.

clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
15

This header inclusion now wreaks havoc the alphabetical ordering.

clang/lib/StaticAnalyzer/Core/RegionStore.cpp
23

One more.

clang/lib/StaticAnalyzer/Core/SymbolManager.cpp
428

Nice!

This revision is now accepted and ready to land.Oct 20 2021, 2:56 AM

And @whisperity , thanks for the review!

Herald added a project: Restricted Project. · View Herald TranscriptOct 20 2021, 8:44 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript