加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – Clang前端API的所有权问题

发布时间:2020-12-16 07:02:49 所属栏目:百科 来源:网络整理
导读:我正在使用Clang C API.由于API没有正确使用智能指针,我一直在努力争取所有权.我踩到了我自己迄今为止发现的所有问题,但是这个问题令我感到烦恼.代码执行时,我得到访问冲突.我相当肯定这是一个双重删除,但由于文档不存在,我几乎不知道在哪里看.幸运的是,复制
我正在使用Clang C API.由于API没有正确使用智能指针,我一直在努力争取所有权.我踩到了我自己迄今为止发现的所有问题,但是这个问题令我感到烦恼.代码执行时,我得到访问冲突.我相当肯定这是一个双重删除,但由于文档不存在,我几乎不知道在哪里看.幸运的是,复制程序相当短.有什么建议?

#define _SCL_SECURE_NO_WARNINGS

#pragma warning(push,0)

#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Sema/Lookup.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/Mangle.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Sema/CodeCompleteConsumer.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/DataTypes.h>
#include <llvm/Module.h>
#include <llvm/Support/Host.h>

#pragma warning(pop)

#include <string>
#include <iostream>

int main(int argc,char* argv[])
{
    llvm::LLVMContext c;
    llvm::Module m("",c);
    clang::EmitLLVMOnlyAction emit(&c);
    emit.setLinkModule(&m);
    clang::CompilerInstance CI;

    std::string errors;
    llvm::raw_string_ostream error_stream(errors);
    clang::DiagnosticOptions diagopts;
    clang::TextDiagnosticPrinter printer(error_stream,&diagopts);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
    clang::DiagnosticsEngine engine(diagids,&diagopts,&printer,false);
    CI.setDiagnostics(&engine);

    CI.createFileManager();
    CI.createSourceManager(CI.getFileManager());

    llvm::raw_null_ostream empty;
    clang::PrintingCodeCompleteConsumer print(CodeCompleteOptions(),empty);        

    clang::TargetOptions target;
    target.Triple = llvm::sys::getDefaultTargetTriple();
    CI.setTarget(clang::TargetInfo::CreateTargetInfo(engine,&target));

    CI.createPreprocessor();
    CI.createASTContext();

    clang::SemaConsumer* cons = new clang::SemaConsumer();

    CI.setASTConsumer(cons);    
    CI.createSema(clang::TranslationUnitKind::TU_Complete,&print);
    cons->InitializeSema(CI.getSema());

    clang::FrontendInputFile f("header",clang::InputKind::IK_CXX,true);
    emit.BeginSourceFile(CI,f);
    emit.Execute();
    emit.EndSourceFile();
    emit.takeModule();
    clang::LookupResult lr(CI.getSema(),clang::DeclarationName(CI.getPreprocessor().getIdentifierInfo("function")),clang::SourceLocation(),clang::Sema::LookupNameKind::LookupOrdinaryName);
    auto result = CI.getSema().LookupName(lr,CI.getSema().TUScope);

    std::string temp;
    llvm::raw_string_ostream out(temp);
    CI.getASTContext().createMangleContext()->mangleName(lr.getFoundDecl(),out);
    auto fun = m.getFunction(temp);

    std::cout << fun->getName().str();

    return 0;
}

编辑:另外,我是否提到如果我更改它以便文件实际存在,即使它是一个简单的程序,Clang在执行操作时失败并发生访问冲突,甚至在EndSourceFile中的前一个之前. Whyyyyyy.

解决方法

我最终通过跳过整个前端来解决这个问题,因为这主要是所有狡猾的所有权所在,并简单地调用我自己需要的功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读