简体   繁体   中英

How to find move constructors in codebase using Clang AST tools?

Following up a comment from this question : how can I find move constructors in C++ codebase using Clang AST tools? (find definitions / declarations only)

The Clang AST matcher now provides this functionality with the isMoveConstructor matcher. Here's an example program:

#include <iostream>

#include "clang/AST/AST.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Refactoring.h"

static llvm::cl::OptionCategory ToolingSampleCategory("move constructor finder");

struct MoveCtorHandler : public clang::ast_matchers::MatchFinder::MatchCallback {
public:
    virtual void run(clang::ast_matchers::MatchFinder::MatchResult const& result) override {
        using namespace clang;
        // reject things from include files
        ASTContext *ctx = result.Context;
        const CXXConstructorDecl *decl = result.Nodes.getStmtAs<CXXConstructorDecl>("moveCtor");
        auto loc = decl->getLocation();
        if (!ctx->getSourceManager().isInMainFile(loc)) {
            return;
        }

        std::cout << "found a move constructor at "
                  << loc.printToString(ctx->getSourceManager())
                  << std::endl;
    }
};

int main(int argc, char const **argv) {
    using namespace clang;
    using namespace clang::tooling;
    using namespace clang::ast_matchers;

    CommonOptionsParser opt(argc, argv, ToolingSampleCategory);
    RefactoringTool     tool(opt.getCompilations(), opt.getSourcePathList());

    MatchFinder  finder;

    // set up callbacks
    MoveCtorHandler       move_ctor_handler;
    finder.addMatcher(constructorDecl(isMoveConstructor()).bind("moveCtor"),
                      &move_ctor_handler);

    if (int result = tool.run(newFrontendActionFactory(&finder).get())) {
        return result;
    }

    return 0;
}

When applied to the following input:

#include <vector>

struct foo {
    foo() {}
    foo(foo && other) : v_(std::move(other.v_)) {}
private:
    std::vector<int>  v_;
};

int main() {
}

It produces the output:

found a move constructor at xxx.cpp:5:5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM