我一直在尝试使用clang(而不是libclang)进行一些代码生成,而且有一个问题困扰着我好几天了。
我有一个包含std::vector
个std::unique_ptr
的类,没有明确定义/删除/默认的复制或移动构造函数。它具有一个默认的默认构造函数和一个接受vector
的显式构造函数。 Clang会自动为该类生成一个副本构造函数,但不会将其标记为已删除(FunctionDecl::isDeleted
)。
我相信一定有我没有做的事情触发clang检查是否应该删除它。查看clang::Sema
的代码,似乎在Sema::DeclareImplicitCopyConstructor
(更具体地说,SpecialMemberDeletionInfo::shouldDeleteForClassSubobject
)中进行了某种检查。
我的假设正确吗?如果是这样,我应该怎么做才能手动触发这些检查?
这是我用来解析文件的代码:
clang::CompilerInstance _compiler;
std::unique_ptr<clang::Compilerinvocation> invocation = ...;
_compiler.createDiagnostics();
_compiler.setinvocation(std::move(invocation));
_compiler.setTarget(clang::TargetInfo::CreatetargetInfo(
_compiler.getDiagnostics(),_compiler.getinvocation().TargetOpts
));
_compiler.createFileManager();
_compiler.createSourceManager(_compiler.getFileManager());
_compiler.createPreprocessor(clang::TU_Complete);
_compiler.getPreprocessor().getBuiltinInfo().initializeBuiltins(
_compiler.getPreprocessor().getIdentifierTable(),*_compiler.getinvocation().getLangOpts()
); // this is necessary
_compiler.createASTContext();
assert_true(!_compiler.getFrontendOpts().Inputs.empty(),"no input file");
_ast_visitor visitor(reg);
_compiler.setaSTConsumer(llvm::make_unique<_ast_consumer>(visitor));
if (_compiler.getFrontendOpts().Inputs.size() > 1) {
std::cerr << "warning: main file not unique\n";
}
const clang::FileEntry *file =
_compiler.getFileManager().getFile(_compiler.getFrontendOpts().Inputs[0].getFile());
_compiler.getsourceManager().setMainFileID(_compiler.getsourceManager().createFileID(
file,clang::SourceLocation(),clang::SrcMgr::C_User
));
_compiler.getDiagnosticClient().BeginSourceFile(_compiler.getLangOpts(),&_compiler.getPreprocessor());
clang::ParseAST(_compiler.getPreprocessor(),&_compiler.getaSTConsumer(),_compiler.getaSTContext());
_compiler.getDiagnosticClient().EndSourceFile();
编辑:我的错误。向量包含unique_ptr
,而不是shared_ptr
。