From 521bf9c529e653ab28896d027352d3e16e2672d5 Mon Sep 17 00:00:00 2001 From: Daniel Dunbar Date: Tue, 1 Dec 2009 09:51:01 +0000 Subject: Add ASTUnit::LoadFromCompilerInvocation, which does what it says. Also, add an -ast-from-source option to index-test which allows index-test to run on source files directly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@90223 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/index-test/index-test.cpp | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'tools/index-test/index-test.cpp') diff --git a/tools/index-test/index-test.cpp b/tools/index-test/index-test.cpp index fce48edf26..dd7cbb2164 100644 --- a/tools/index-test/index-test.cpp +++ b/tools/index-test/index-test.cpp @@ -43,6 +43,10 @@ #include "clang/Index/Analyzer.h" #include "clang/Index/Utils.h" #include "clang/Frontend/ASTUnit.h" +#include "clang/Frontend/CompilerInstance.h" +#include "clang/Frontend/CompilerInvocation.h" +#include "clang/Frontend/DiagnosticOptions.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Frontend/CommandLineSourceLoc.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/ExprObjC.h" @@ -202,9 +206,24 @@ static void ProcessASTLocation(ASTLocation ASTLoc, Indexer &Idxer) { } } +static llvm::cl::opt +ASTFromSource("ast-from-source", + llvm::cl::desc("Treat the inputs as source files to parse.")); + static llvm::cl::list InputFilenames(llvm::cl::Positional, llvm::cl::desc("")); +void CreateCompilerInvocation(const std::string &Filename, + CompilerInvocation &CI, Diagnostic &Diags, + const char *argv0) { + llvm::SmallVector Args; + Args.push_back(Filename.c_str()); + + void *MainAddr = (void*) (intptr_t) CreateCompilerInvocation; + CompilerInvocation::CreateFromArgs(CI, Args.data(), Args.data() + Args.size(), + argv0, MainAddr, Diags); +} + int main(int argc, char **argv) { llvm::sys::PrintStackTraceOnErrorSignal(); llvm::PrettyStackTraceProgram X(argc, argv); @@ -215,6 +234,10 @@ int main(int argc, char **argv) { Indexer Idxer(Prog); llvm::SmallVector TUnits; + TextDiagnosticPrinter DiagClient(llvm::errs(), DiagnosticOptions(), false); + llvm::OwningPtr Diags( + CompilerInstance::createDiagnostics(DiagnosticOptions(), argc, argv)); + // If no input was specified, read from stdin. if (InputFilenames.empty()) InputFilenames.push_back("-"); @@ -225,7 +248,15 @@ int main(int argc, char **argv) { std::string ErrMsg; llvm::OwningPtr AST; - AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg)); + if (ASTFromSource) { + CompilerInvocation CI; + CreateCompilerInvocation(InFile, CI, *Diags, argv[0]); + AST.reset(ASTUnit::LoadFromCompilerInvocation(CI, *Diags)); + if (!AST) + ErrMsg = "unable to create AST"; + } else + AST.reset(ASTUnit::LoadFromPCHFile(InFile, &ErrMsg)); + if (!AST) { llvm::errs() << "[" << InFile << "] Error: " << ErrMsg << '\n'; return 1; -- cgit v1.2.3-18-g5258