[cfe-dev] [analyzer] Enhancement for modernize-make-unique check

Alexander Zaitsev via cfe-dev cfe-dev at lists.llvm.org
Wed Jan 2 16:06:40 PST 2019


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



More information about the cfe-dev mailing list