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

Jonas Toth via cfe-dev cfe-dev at lists.llvm.org
Thu Jan 3 04:07:23 PST 2019


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


More information about the cfe-dev mailing list