[analyzer] Enhancement for modernize-make-unique check

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[analyzer] Enhancement for modernize-make-unique check

Richard Smith 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.

--
Best regards,
Alexander Zaitsev

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [analyzer] Enhancement for modernize-make-unique check

Richard Smith via cfe-dev
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