Following up a comment from this question: how can I find move constructors in C++ codebase using Clang AST tools? (find definitions / declarations only)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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