首页 > 解决方案 > clang-tooling:如何检查一个字段是否是一个不完整的构造类型?

问题描述

我想编写一个匹配器,它排除包含不完整类型的类,例如: std::unique_ptr<ForwardDeclared>,但它没有被标识为一个,因为它是一个模板类型(我认为是这样)。有谁知道如何规避这个?

最小代码:

// ...
class MoveDefaultToDecl: public MatchFinder::MatchCallback {
// ...

public:
    void run(const MatchFinder::MatchResult& r) override {
        // ...
        auto const* class_decl = r.Nodes.getNodeAs<CXXRecordDecl>("method-class");
        auto iter = std::find_if(class_decl->field_begin(), class_decl->field_end(),
                                 [](auto const& field) { return field->getType()->isIncompleteType(); });
        if (iter != class_decl->field_end()) {
            return;
        }
        // ...
    }
// ...
}

test.h

#pragma once

#include <memory>

class H {
    H();
    ~H() = delete;
};

class MyType;

class K {
    K();
    ~K();
    std::unique_ptr<MyType> ptr_;
};

test.cpp

#include "test.h"

class MyType {
    // But not here:
    MyType() = default;
    // But not here:
    ~MyType() = default;
    int i;
};

// CHECK: :[[@LINE+1]]:3: note: "xyz" binds here
H::H() = default;

// But not here
K::K() = default; // Actually binds here

// But not here
K::~K() = default; // Actually binds here

标签: c++clangabstract-syntax-treeclang-ast-matchers

解决方案


推荐阅读