Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "llvm/Support/CommandLine.h"
- #include "llvm/Support/Host.h"
- #include "clang/AST/ASTContext.h"
- #include "clang/AST/ASTConsumer.h"
- #include "clang/Basic/Diagnostic.h"
- #include "clang/Basic/DiagnosticOptions.h"
- #include "clang/Basic/FileManager.h"
- #include "clang/Basic/SourceManager.h"
- #include "clang/Basic/LangOptions.h"
- #include "clang/Basic/TargetInfo.h"
- #include "clang/Basic/TargetOptions.h"
- #include "clang/Frontend/ASTConsumers.h"
- #include "clang/Frontend/CompilerInstance.h"
- #include "clang/Lex/Preprocessor.h"
- #include "clang/Parse/Parser.h"
- #include "clang/Parse/ParseAST.h"
- #include <iostream>
- using namespace llvm;
- using namespace clang;
- // option for the program
- // input file
- static cl::opt<std::string> FileName(cl::Positional, cl::desc("Input file"), cl::Required);
- int
- main(int argc, char **argv)
- {
- // necessary always to parse command line
- cl::ParseCommandLineOptions(argc, argv, "My front end example\n");
- /*
- * this class will manage entire infrastructure to handle compilation
- * clang -cc1 commonly instantiate a specific FrontendAction, which
- * will perform all steps here covered, but we will do all the steps
- * by our own.
- */
- CompilerInstance CI;
- DiagnosticOptions diagnosticOptions; // code diagnostics
- CI.createDiagnostics();
- std::shared_ptr<TargetOptions> PTO(new TargetOptions());
- PTO->Triple = sys::getDefaultTargetTriple();
- TargetInfo *PTI = TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO);
- CI.setTarget(PTI);
- // initialization code of file manager necessary to read source files
- CI.createFileManager();
- // the source manager will be responsible for managing SourceLocation used in lexer and parser
- CI.createSourceManager(CI.getFileManager());
- // preprocessor
- CI.createPreprocessor(TU_Complete);
- CI.getPreprocessorOpts().UsePredefines = false;
- // AST Consumer to consume final AST (after parsing and semantic analysis)
- // if we wanted to generate LLVM IR, we would have to provide specific code
- // generation ASTConsumer instance (called BackendConsumer).
- // this time we just create an AST consumer that prints AST to console
- ASTConsumer *astConsumer = CreateASTPrinter(NULL,"");
- // set AST Consumer
- CI.setASTConsumer(astConsumer);
- // AST Context (used by parser) and semantics (used by semantic analysis)
- CI.createASTContext();
- CI.createSema(TU_Complete, NULL);
- const FileEntry *pFile = CI.getFileManager().getFile(FileName);
- if (!pFile)
- {
- std::cerr << "File not found: " << FileName << std::endl;
- return 1;
- }
- CI.getSourceManager().setMainFileID(
- CI.getSourceManager().createFileID(
- pFile, SourceLocation(), SrcMgr::C_User
- )
- );
- CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);
- // peform lexical and syntactic analysis
- ParseAST(CI.getSema());
- CI.getASTContext().PrintStats();
- CI.getDiagnosticClient().EndSourceFile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment