[analyzer] Enhancement for modernize-make-unique check

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[analyzer] Enhancement for modernize-make-unique check

Richard Smith via cfe-dev
I did write simple matcher, which match case from example. But my
matcher is different and looks like this:

hasType(pointsTo(cxxRecordDecl(isDerivedFrom(hasName("Base"))))))

And this matches my example. Also I tried to write check like

hasType(pointsTo(cxxRecordDecl(isDerivedFrom(hasType(type(
equalsBoundNode(PointerType))))))))

But this code has compilation errors (I have no access to the logs right
now, but it's something with incorrect work with polymorphic types).

1/3/2019 3:07 PM, Jonas Toth пишет:

> Hi Alexander,
>
> from reading the matcher I can not find an issue right now, but did you
> try `clang-query`  and a minimal matcher that will just match you code?
> From there it is easier to integrate it into the already existing matcher.
>
> Best Regards
> Jonas
>
> Am 03.01.19 um 01:06 schrieb Alexander Zaitsev via cfe-dev:
>> Hello.
>>
>> Recently I found one interesting piece of code which isn't handled by
>> clang-tidy with -modernize-make_unique check:
>>
>>
>> #include <memory>
>>
>> struct Base{
>>     virtual ~Base() = default;
>>     virtual void onTest() = 0;
>> };
>>
>> struct Derived : Base {
>>     void onTest() override {
>>
>>     }
>> };
>>
>> class A {
>>     std::unique_ptr<Base> base_;
>> public:
>>     A() {
>>         base_ = std::unique_ptr<Base>(new Derived); // this line
>>     }
>> };
>>
>> void foo()
>> {
>>     A a;
>> }
>>
>>
>> As you see we can rewrite the marked line with std::make_unique.
>>
>> I decided to fix it but met one problem with Clang AST matchers.
>>
>> I tried to modify existent matcher in MakeSmartPtrCheck.cpp (I have
>> added lines with IsDerivedFrom):
>>
>> Finder->addMatcher(
>>       cxxBindTemporaryExpr(has(ignoringParenImpCasts(
>>           cxxConstructExpr(
>>               hasType(getSmartPointerTypeMatcher()), argumentCountIs(1),
>>               hasArgument(0,
>> cxxNewExpr(anyOf(hasType(pointsTo(qualType(hasCanonicalType(
>> equalsBoundNode(PointerType))))),
>> hasType(pointsTo(cxxRecordDecl(isDerivedFrom(
>> equalsBoundNode(PointerType)))))),
>>                                      CanCallCtor)
>>                               .bind(NewExpression)),
>>               unless(isInTemplateInstantiation()))
>>               .bind(ConstructorCall)))),
>>       this);
>>
>>
>> Unfortunately it doesn't work (but compilation is successful) - modified
>> matcher doesn't match provided above example.
>>
>> What I am doing wrong here? Thank you.
>>
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev