示例源码
Makefile
LLVM_CONFIG ?= llvm-config
#CXX := clang++
ifndef VERBOSE
QUIET :=@
endifSRC_DIR ?= $(PWD)
LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
COMMON_FLAGS = -Wall -Wextra
CXXFLAGS += $(COMMON_FLAGS) $(shell $(LLVM_CONFIG) --cxxflags)
LCXX :=$(shell $(LLVM_CONFIG) --cxxflags)
CPPFLAGS += $(shell $(LLVM_CONFIG) --cppflags) -I$(SRC_DIR)CLANGLIBS = \-Wl,--start-group \-lclang \-lclangFrontend \-lclangDriver \-lclangSerialization \-lclangParse \-lclangSema \-lclangAnalysis \-lclangEdit \-lclangAST \-lclangLex \-lclangBasic \-Wl,--end-groupLLVMLIBS = $(shell $(LLVM_CONFIG) --libs)PROJECT = frontEnd_ASTConsumer
PROJECT_OBJECTS = frontEnd_ASTConsumer.odefault: $(PROJECT)%.o : $(SRC_DIR)/%.cpp@echo Compiling $*.cpp$(QUIET)$(CXX) -Wall -Wextra -c $(CPFLAGS) $(CXXFLAGS) $<$(PROJECT) : $(PROJECT_OBJECTS) @echo Linking $@$(QUIET)$(CXX) -o $@ $(LDFLAGS) $^ $(CLANGLIBS) $(LLVMLIBS) -lncurses.PHONY: clean
clean:$(QUIET)rm -f $(PROJECT) $(PROJECT_OBJECTS).PHONY: echo
echo:@echo "CXX is $(CXX)"@echo "LDFLAGS is $(LDFLAGS)}"@echo "CXXFLAGS is $(CXXFLAGS)"@echo "CPPFLAGS is $(CPPFLAGS)"@echo "SRC_DIR is $(SRC_DIR)"
frontEnd_ASTConsumer.cpp
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#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/Frontend/TextDiagnosticPrinter.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/ParseAST.h"
#include <iostream>using namespace llvm;
using namespace clang;static cl::opt<std::string>
FileName(cl::Positional, cl::desc("Input file"), cl::Required);int main(int argc, char** argv){cl::ParseCommandLineOptions(argc, argv, "My simple front end\n");CompilerInstance CI;DiagnosticOptions diagnosticOptions;CI.createDiagnostics();IntrusiveRefCntPtr<TargetOptions> PTO(new TargetOptions());PTO->Triple = sys::getDefaultTargetTriple();TargetInfo *PTI = TargetInfo::CreateTargetInfo(CI.getDiagnostics(), PTO.getPtr());CI.setTarget(PTI);CI.createFileManager();CI.createSourceManager(CI.getFileManager());CI.createPreprocessor();CI.getPreprocessorOpts().UsePredefines = false;ASTConsumer *astConsumer = CreateASTPrinter(NULL, "");CI.setASTConsumer(astConsumer);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;}printf("\n99999\n");CI.getSourceManager().createMainFileID(pFile);printf("\n00000\n");CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0);printf("\n11111\n");ParseAST(CI.getSema());printf("\n22222\n");// print AST statisticsCI.getASTContext().PrintStats();printf("\n33333\n");CI.getASTContext().Idents.PrintStats();return 0;
}