Fare9

my-front-end.cpp

Apr 26th, 2020
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.05 KB | None | 0 0
  1. #include "llvm/Support/CommandLine.h"
  2. #include "llvm/Support/Host.h"
  3. #include "clang/AST/ASTContext.h"
  4. #include "clang/AST/ASTConsumer.h"
  5. #include "clang/Basic/Diagnostic.h"
  6. #include "clang/Basic/DiagnosticOptions.h"
  7. #include "clang/Basic/FileManager.h"
  8. #include "clang/Basic/SourceManager.h"
  9. #include "clang/Basic/LangOptions.h"
  10. #include "clang/Basic/TargetInfo.h"
  11. #include "clang/Basic/TargetOptions.h"
  12. #include "clang/Frontend/ASTConsumers.h"
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/Lex/Preprocessor.h"
  15. #include "clang/Parse/Parser.h"
  16. #include "clang/Parse/ParseAST.h"
  17. #include <iostream>
  18.  
  19. using namespace llvm;
  20. using namespace clang;
  21.  
  22.  
  23. // option for the program
  24. // input file
  25. static cl::opt<std::string> FileName(cl::Positional, cl::desc("Input file"), cl::Required);
  26.  
  27. int
  28. main(int argc, char **argv)
  29. {
  30.     // necessary always to parse command line
  31.     cl::ParseCommandLineOptions(argc, argv, "My front end example\n");
  32.  
  33.     /*
  34.     *   this class will manage entire infrastructure to handle compilation
  35.     *   clang -cc1 commonly instantiate a specific FrontendAction, which
  36.     *   will perform all steps here covered, but we will do all the steps
  37.     *   by our own.
  38.     */
  39.     CompilerInstance CI;    
  40.     DiagnosticOptions diagnosticOptions; // code diagnostics
  41.     CI.createDiagnostics();
  42.  
  43.     std::shared_ptr<TargetOptions>  PTO(new TargetOptions());
  44.     PTO->Triple = sys::getDefaultTargetTriple();
  45.     TargetInfo *PTI = TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);
  46.     CI.setTarget(PTI);
  47.  
  48.     // initialization code of file manager necessary to read source files
  49.     CI.createFileManager();
  50.     // the source manager will be responsible for managing SourceLocation used in lexer and parser
  51.     CI.createSourceManager(CI.getFileManager());
  52.     // preprocessor
  53.     CI.createPreprocessor(TU_Complete);
  54.     CI.getPreprocessorOpts().UsePredefines = false;
  55.    
  56.     // AST Consumer to consume final AST (after parsing and semantic analysis)
  57.     // if we wanted to generate LLVM IR, we would have to provide specific code
  58.     // generation ASTConsumer instance (called BackendConsumer).
  59.     // this time we just create an AST consumer that prints AST to console
  60.     ASTConsumer *astConsumer = CreateASTPrinter(NULL,"");
  61.     // set AST Consumer
  62.     CI.setASTConsumer(astConsumer);
  63.     // AST Context (used by parser) and semantics (used by semantic analysis)
  64.     CI.createASTContext();
  65.     CI.createSema(TU_Complete, NULL);
  66.  
  67.     const FileEntry *pFile = CI.getFileManager().getFile(FileName);
  68.     if (!pFile)
  69.     {
  70.         std::cerr << "File not found: " << FileName << std::endl;
  71.         return 1;
  72.     }
  73.  
  74.     CI.getSourceManager().setMainFileID(
  75.         CI.getSourceManager().createFileID(
  76.             pFile, SourceLocation(), SrcMgr::C_User
  77.         )
  78.     );
  79.  
  80.     CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
  81.     // peform lexical and syntactic analysis
  82.     ParseAST(CI.getSema());
  83.     CI.getASTContext().PrintStats();
  84.     CI.getDiagnosticClient().EndSourceFile();
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment