Clang API解析析构函数

dev*_*low 1 c++ api clang

我使用clang API(版本3.1 - 主干153913)编译一些非常简单的代码,如下所示:

class MyClass
{
   ~MyClass() ;

};

MyClass::~MyClass()
{

}

int main()
{
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我收到错误消息:test.cpp:20:10:错误:析构函数不能有返回类型 MyClass::~MyClass()

如果有人可以指出我的方向很好.如果将析构函数定义为类内联,则编译正常.

注意我也可以使用clang ++编译:-bash-4.1 $ clang ++ test.cpp

因此,我的clang API使用中必须存在一个设置.任何人都可以表明可能是什么.我一直在寻找缺少的选项/配置.

这是我的clang API用法:

// Include appropriate headers. 
int main()
{
    clang::DiagnosticOptions diagnosticOptions;
    diagnosticOptions.ShowColors=1;
    diagnosticOptions.ShowOptionNames=1;
    clang::TextDiagnosticPrinter *pTextDiagnosticPrinter =
       new clang::TextDiagnosticPrinter(
           llvm::outs(),
           diagnosticOptions);
    llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
    clang::DiagnosticsEngine *pDiagnosticsEngine =
       new clang::DiagnosticsEngine(pDiagIDs, pTextDiagnosticPrinter);

    clang::LangOptions languageOptions;
    languageOptions.GNUMode = 1;
    languageOptions.CXXExceptions = 1;
    languageOptions.RTTI = 1;
    languageOptions.Bool = 1;
    languageOptions.CPlusPlus = 1;
    clang::FileSystemOptions fileSystemOptions;
    clang::FileManager fileManager(fileSystemOptions);

    clang::SourceManager sourceManager(
      *pDiagnosticsEngine,
      fileManager);


    clang::TargetOptions targetOptions;
     targetOptions.Triple = "x86_64-unknown-linux-gnu";
    targetOptions.CPU = "x86-64";

    clang::TargetInfo *pTargetInfo =
    clang::TargetInfo::CreateTargetInfo(
        *pDiagnosticsEngine,
        targetOptions);

    clang::HeaderSearch headerSearch(fileManager,
                                 *pDiagnosticsEngine,
                                 languageOptions,
                                 pTargetInfo);
    clang::CompilerInstance compInst;
    compInst.getTargetOpts() = targetOptions;
    compInst.getLangOpts().CPlusPlus = 1;
    compInst.getLangOpts().Bool = 1;

    clang::HeaderSearchOptions &headerSearchOpts = compInst.getHeaderSearchOpts();
    headerSearchOpts = headerSearchOptions;

    clang::CompilerInvocation &compInvocation = compInst.getInvocation();

    clang::FrontendOptions & frontendOpts = compInvocation.getFrontendOpts();
    frontendOpts.ProgramAction = clang::frontend::EmitObj;

    clang::CodeGenOptions & codeGenOpts = compInvocation.getCodeGenOpts ();

    codeGenOpts.RelaxAll = 1;
    codeGenOpts.DebugInfo = 1;
    codeGenOpts.RelocationModel = "static";
    codeGenOpts.DisableFPElim = 1;
    codeGenOpts.AsmVerbose = 1;
    codeGenOpts.CXXCtorDtorAliases= 1;
    codeGenOpts.UnwindTables = 1;
    codeGenOpts.OmitLeafFramePointer = 1;
    codeGenOpts.StackRealignment = 1;

    std::vector<std::string> res;
    compInvocation.toArgs (res);

    std::vector<std::string>::iterator it;

   std::cout << "Arguments: " << std::endl;
   for (it = res.begin(); it != res.end(); it++)
   {
      std::string arg = *it;
      std::cout << "Arg: " << arg << std::endl;
   }

   clang::Preprocessor preprocessor(
    *pDiagnosticsEngine,
    languageOptions,
    pTargetInfo,
    sourceManager,
    headerSearch,
    compInst);

   preprocessor.getBuiltinInfo().InitializeBuiltins
    (preprocessor.getIdentifierTable(),
     languageOptions);

   clang::PreprocessorOptions preprocessorOptions;

   clang::FrontendOptions frontendOptions;
   frontendOptions.DisableFree=1;
   clang::InitializePreprocessor(
    preprocessor,
    preprocessorOptions,
    headerSearchOptions,
    frontendOptions);

   const clang::FileEntry *pFile = fileManager.getFile(
    "test.cpp");
   sourceManager.createMainFileID(pFile);
   const clang::TargetInfo &targetInfo = *pTargetInfo;

   clang::IdentifierTable identifierTable(languageOptions);
   clang::SelectorTable selectorTable;

   clang::Builtin::Context builtinContext;
   builtinContext.InitializeTarget(targetInfo);
   builtinContext.InitializeBuiltins(identifierTable, languageOptions);

   clang::ASTContext astContext(
    languageOptions,
    sourceManager,
    pTargetInfo,
    identifierTable,
    selectorTable,
    builtinContext,
    0 /* size_reserve*/);
    MyASTConsumer astConsumer;

    clang::Sema sema(
    preprocessor,
    astContext,
    astConsumer);

    pTextDiagnosticPrinter->BeginSourceFile(languageOptions, &preprocessor);
    clang::ParseAST (preprocessor, &astConsumer, astContext);
    pTextDiagnosticPrinter->EndSourceFile();

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

小智 7

你错过了

languageOptions.ImplicitInt = 0;
Run Code Online (Sandbox Code Playgroud)

如果没有这种配置,任何没有显式返回类型的函数都将被设置为int返回类型,并在检查析构函数返回类型时引发错误,从而引发错误.使用上面的配置,Sema阶段不会引发错误,因为析构函数返回类型将保持未定义.