0

我正在针对 LLVM-CLANG 编译一个程序。这是主要

#include <iostream>

#include "CompilerFactory.h"

#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Basic/Diagnostic.h"

int main(int argc, char const *argv[])
{
using clang::CompilerInstance;
using clang::TargetOptions;
using clang::TargetInfo;
using clang::FileEntry;
using clang::Token;
using clang::DiagnosticOptions;
using clang::TextDiagnosticPrinter;

CompilerInstance ci;
CSFV::CompilerFactory::GetCompilerInstance(ci);
const FileEntry *pFile = ci.getFileManager().getFile("test.c");
ci.getSourceManager().createMainFileID(pFile);
ci.getPreprocessor().EnterMainSourceFile();
ci.getDiagnosticClient().BeginSourceFile(ci.getLangOpts(),
                                        &ci.getPreprocessor());
Token tok;
do
{
  ci.getPreprocessor().Lex(tok);
  if (ci.getDiagnostics().hasErrorOccurred())
    break;
  ci.getPreprocessor().DumpToken(tok);
  std::cerr << std::endl;
} while (tok.isNot(clang::tok::eof));
ci.getDiagnosticClient().EndSourceFile();
return 0;
}

这是包含的课程

//If they are not defined we have an error at compile time
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS

#include "llvm/Support/Host.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"

#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"

using namespace clang;

namespace CSFV{

class CompilerFactory
{
public:

  CompilerFactory();

  ~CompilerFactory();

  /// \brief Generate and returns a compiler instance object
  static void GetCompilerInstance(CompilerInstance &ci){
    DiagnosticOptions diagOpts;
    TextDiagnosticPrinter* diagPrinter =
      new TextDiagnosticPrinter(llvm::outs(), &diagOpts, true);
    ci.createDiagnostics(diagPrinter);

    llvm::IntrusiveRefCntPtr<TargetOptions> pto (new TargetOptions());
    pto->Triple = llvm::sys::getDefaultTargetTriple();
    TargetInfo *pti =
      TargetInfo::CreateTargetInfo(ci.getDiagnostics(), pto.getPtr());
    ci.setTarget(pti);

    ci.createFileManager();
    ci.createSourceManager(ci.getFileManager());
    ci.createPreprocessor();
    return;
  }
};
} //end of namespace CSFV

出于某种原因,我在 main 执行结束时遇到了段错误。我错过了什么?

4

1 回答 1

0

我不知道这是否与我遇到的问题相同,但我所做的是将 targetoptions 声明为新指针。

例如:

clang::TargetOptions *targetOpts=new clang::TargetOptions;

我有一种感觉 targetinfo 在自身被破坏时会清理它。

于 2013-09-22T13:28:28.310 回答