diff --git a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp --- a/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp +++ b/clang/tools/clang-extdef-mapping/ClangExtDefMapGen.cpp @@ -1,4 +1,4 @@ -//===- ClangExtDefMapGen.cpp -----------------------------------------------===// +//===- ClangExtDefMapGen.cpp ---------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -13,10 +13,12 @@ #include "clang/AST/ASTConsumer.h" #include "clang/AST/ASTContext.h" +#include "clang/Basic/DiagnosticOptions.h" #include "clang/Basic/SourceManager.h" #include "clang/CrossTU/CrossTranslationUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendActions.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Tooling/CommonOptionsParser.h" #include "clang/Tooling/Tooling.h" #include "llvm/Support/CommandLine.h" @@ -29,12 +31,16 @@ using namespace clang::cross_tu; using namespace clang::tooling; -static cl::OptionCategory ClangExtDefMapGenCategory("clang-extdefmapgen options"); +static cl::OptionCategory + ClangExtDefMapGenCategory("clang-extdefmapgen options"); class MapExtDefNamesConsumer : public ASTConsumer { public: - MapExtDefNamesConsumer(ASTContext &Context) - : Ctx(Context), SM(Context.getSourceManager()) {} + MapExtDefNamesConsumer(ASTContext &Context, + StringRef astFilePath = StringRef()) + : Ctx(Context), SM(Context.getSourceManager()) { + CurrentFileName = astFilePath.str(); + } ~MapExtDefNamesConsumer() { // Flush results to standard output. @@ -111,6 +117,46 @@ static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); +void HandleAST(StringRef astPath) { + + CompilerInstance CI; + + IntrusiveRefCntPtr DiagOpts = new DiagnosticOptions(); + TextDiagnosticPrinter *DiagClient = + new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts); + IntrusiveRefCntPtr DiagID(new DiagnosticIDs()); + IntrusiveRefCntPtr Diags( + new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient)); + + std::unique_ptr unit = ASTUnit::LoadFromASTFile( + astPath.str(), CI.getPCHContainerOperations()->getRawReader(), + ASTUnit::LoadASTOnly, Diags, CI.getFileSystemOpts()); + + FileManager fm(CI.getFileSystemOpts()); + SmallString<128> absPath(astPath); + fm.makeAbsolutePath(absPath); + + std::unique_ptr consumer = + std::make_unique(unit->getASTContext(), absPath); + consumer->HandleTranslationUnit(unit->getASTContext()); +} + +void HandleFiles(ArrayRef sourceFiles, + CompilationDatabase &compilations) { + std::vector sourceToBeParsed; + for (StringRef src : sourceFiles) { + if (src.endswith(".ast")) + HandleAST(src); + else + sourceToBeParsed.push_back(src.str()); + } + + if (!sourceToBeParsed.empty()) { + ClangTool Tool(compilations, sourceToBeParsed); + Tool.run(newFrontendActionFactory().get()); + } +} + int main(int argc, const char **argv) { // Print a stack trace if we signal out. sys::PrintStackTraceOnErrorSignal(argv[0], false); @@ -127,8 +173,6 @@ } CommonOptionsParser &OptionsParser = ExpectedParser.get(); - ClangTool Tool(OptionsParser.getCompilations(), - OptionsParser.getSourcePathList()); - - return Tool.run(newFrontendActionFactory().get()); + HandleFiles(OptionsParser.getSourcePathList(), + OptionsParser.getCompilations()); }