#include "llvm/Support/CompilerInstance.h" int main() { llvm::CompilerInstance CI; return 0; }
#include "llvm/Support/CompilerInstance.h" #include "llvm/ADT/Triple.h" #include "llvm/Support/SourceMgr.h" int main() { llvm::Triple triple("x86_64-pc-linux-gnu"); llvm::SourceMgr sourceMgr; llvm::SMDiagnostic err; llvm::CompilerInstance CI; CI.createDiagnostics(); CI.setTarget(llvm::TargetRegistry::lookupTarget(triple.getTriple(), err, CI.getDiagnostics())); CI.createFileManager(); CI.createSourceManager(CI.getFileManager()); CI.createPreprocessor(llvm::TU_Prefix); return 0; }
#include "llvm/Support/CompilerInstance.h" #include "llvm/Support/TargetSelect.h" #include "llvm/IR/Module.h" int main() { llvm::InitializeNativeTarget(); llvm::InitializeNativeTargetAsmPrinter(); llvm::InitializeNativeTargetAsmParser(); llvm::CompilerInstance CI; CI.createDiagnostics(); CI.createFileManager(); CI.createSourceManager(CI.getFileManager()); CI.createPreprocessor(llvm::TU_Prefix); CI.createASTContext(); CI.setASTConsumer(new llvm::ASTConsumer()); llvm::Module* module = CI.getASTContext().getModule(); module->setModuleIdentifier(llvm::MDString::get(CI.getLLVMContext(), "MyModule")); module->setTargetTriple(CI.getTarget().getTriple()); // ... add code to parse and handle command line arguments, input files, etc. CI.getFrontendOpts().Inputs.push_back({llvm::InputKind::IK_CXX, "MySource.cpp"}); CI.createFrontendActionFactory(); CI.runFrontend(); return 0; }In this example, we include the CompilerInstance header file as well as two other header files for using the TargetSelect and Module classes. We initialize the native target and asm printer and parser, create a new CompilerInstance object, set up its diagnostics system, create a FileManager object, a SourceManager object, a Preprocessor object, and an ASTContext object. We set the ASTConsumer object to a new, empty one. We then get the ASTContext's Module object and set its identifier and target triple. We add code to handle command line arguments and input files. We create a FrontendActionFactory object and run the frontend with the "runFrontend()" method. Package/Library: LLVM