diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Tooling')
7 files changed, 540 insertions, 107 deletions
diff --git a/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp b/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp index 31dd4659..a69971e 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp @@ -13,6 +13,8 @@ //===----------------------------------------------------------------------===// #include "clang/Tooling/ArgumentsAdjusters.h" +#include "clang/Basic/LLVM.h" +#include "llvm/ADT/StringRef.h" namespace clang { namespace tooling { @@ -23,12 +25,35 @@ void ArgumentsAdjuster::anchor() { /// Add -fsyntax-only option to the commnand line arguments. CommandLineArguments ClangSyntaxOnlyAdjuster::Adjust(const CommandLineArguments &Args) { - CommandLineArguments AdjustedArgs = Args; - // FIXME: Remove options that generate output. + CommandLineArguments AdjustedArgs; + for (size_t i = 0, e = Args.size(); i != e; ++i) { + StringRef Arg = Args[i]; + // FIXME: Remove options that generate output. + if (!Arg.startswith("-fcolor-diagnostics") && + !Arg.startswith("-fdiagnostics-color")) + AdjustedArgs.push_back(Args[i]); + } AdjustedArgs.push_back("-fsyntax-only"); return AdjustedArgs; } +CommandLineArguments +ClangStripOutputAdjuster::Adjust(const CommandLineArguments &Args) { + CommandLineArguments AdjustedArgs; + for (size_t i = 0, e = Args.size(); i < e; ++i) { + StringRef Arg = Args[i]; + if(!Arg.startswith("-o")) + AdjustedArgs.push_back(Args[i]); + + if(Arg == "-o") { + // Output is specified as -o foo. Skip the next argument also. + ++i; + } + // Else, the output is specified as -ofoo. Just do nothing. + } + return AdjustedArgs; +} + } // end namespace tooling } // end namespace clang diff --git a/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp b/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp index 99aff9f..cce4816 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp @@ -53,7 +53,8 @@ const char *const CommonOptionsParser::HelpMessage = "\tsuffix of a path in the compile command database.\n" "\n"; -CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv) { +CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, + const char *Overview) { static cl::opt<std::string> BuildPath( "p", cl::desc("Build path"), cl::Optional); @@ -62,7 +63,7 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv) { Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, argv)); - cl::ParseCommandLineOptions(argc, argv); + cl::ParseCommandLineOptions(argc, argv, Overview); SourcePathList = SourcePaths; if (!Compilations) { std::string ErrorMessage; diff --git a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp index b5b99cb..c962055 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp @@ -20,6 +20,16 @@ #include "llvm/Support/system_error.h" #include <sstream> +#include "clang/Basic/Diagnostic.h" +#include "clang/Driver/Action.h" +#include "clang/Driver/Driver.h" +#include "clang/Driver/DriverDiagnostic.h" +#include "clang/Driver/Job.h" +#include "clang/Driver/Compilation.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" +#include "llvm/Support/Host.h" +#include "llvm/Option/Arg.h" + namespace clang { namespace tooling { @@ -100,6 +110,173 @@ CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, CompilationDatabasePlugin::~CompilationDatabasePlugin() {} +// Helper for recursively searching through a chain of actions and collecting +// all inputs, direct and indirect, of compile jobs. +struct CompileJobAnalyzer { + void run(const driver::Action *A) { + runImpl(A, false); + } + + SmallVector<std::string, 2> Inputs; + +private: + + void runImpl(const driver::Action *A, bool Collect) { + bool CollectChildren = Collect; + switch (A->getKind()) { + case driver::Action::CompileJobClass: + CollectChildren = true; + break; + + case driver::Action::InputClass: { + if (Collect) { + const driver::InputAction *IA = cast<driver::InputAction>(A); + Inputs.push_back(IA->getInputArg().getSpelling()); + } + } break; + + default: + // Don't care about others + ; + } + + for (driver::ActionList::const_iterator I = A->begin(), E = A->end(); + I != E; ++I) + runImpl(*I, CollectChildren); + } +}; + +// Special DiagnosticConsumer that looks for warn_drv_input_file_unused +// diagnostics from the driver and collects the option strings for those unused +// options. +class UnusedInputDiagConsumer : public DiagnosticConsumer { +public: + UnusedInputDiagConsumer() : Other(0) {} + + // Useful for debugging, chain diagnostics to another consumer after + // recording for our own purposes. + UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {} + + virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const Diagnostic &Info) LLVM_OVERRIDE { + if (Info.getID() == clang::diag::warn_drv_input_file_unused) { + // Arg 1 for this diagnostic is the option that didn't get used. + UnusedInputs.push_back(Info.getArgStdStr(0)); + } + if (Other) + Other->HandleDiagnostic(DiagLevel, Info); + } + + DiagnosticConsumer *Other; + SmallVector<std::string, 2> UnusedInputs; +}; + +// Unary functor for asking "Given a StringRef S1, does there exist a string +// S2 in Arr where S1 == S2?" +struct MatchesAny { + MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {} + bool operator() (StringRef S) { + for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I) + if (*I == S) + return true; + return false; + } +private: + ArrayRef<std::string> Arr; +}; + +/// \brief Strips any positional args and possible argv[0] from a command-line +/// provided by the user to construct a FixedCompilationDatabase. +/// +/// FixedCompilationDatabase requires a command line to be in this format as it +/// constructs the command line for each file by appending the name of the file +/// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the +/// start of the command line although its value is not important as it's just +/// ignored by the Driver invoked by the ClangTool using the +/// FixedCompilationDatabase. +/// +/// FIXME: This functionality should probably be made available by +/// clang::driver::Driver although what the interface should look like is not +/// clear. +/// +/// \param[in] Args Args as provided by the user. +/// \return Resulting stripped command line. +/// \li true if successful. +/// \li false if \c Args cannot be used for compilation jobs (e.g. +/// contains an option like -E or -version). +bool stripPositionalArgs(std::vector<const char *> Args, + std::vector<std::string> &Result) { + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + UnusedInputDiagConsumer DiagClient; + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), + &*DiagOpts, &DiagClient, false); + + // Neither clang executable nor default image name are required since the + // jobs the driver builds will not be executed. + OwningPtr<driver::Driver> NewDriver(new driver::Driver( + /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(), + /* DefaultImageName= */ "", Diagnostics)); + NewDriver->setCheckInputsExist(false); + + // This becomes the new argv[0]. The value is actually not important as it + // isn't used for invoking Tools. + Args.insert(Args.begin(), "clang-tool"); + + // By adding -c, we force the driver to treat compilation as the last phase. + // It will then issue warnings via Diagnostics about un-used options that + // would have been used for linking. If the user provided a compiler name as + // the original argv[0], this will be treated as a linker input thanks to + // insertng a new argv[0] above. All un-used options get collected by + // UnusedInputdiagConsumer and get stripped out later. + Args.push_back("-c"); + + // Put a dummy C++ file on to ensure there's at least one compile job for the + // driver to construct. If the user specified some other argument that + // prevents compilation, e.g. -E or something like -version, we may still end + // up with no jobs but then this is the user's fault. + Args.push_back("placeholder.cpp"); + + const OwningPtr<driver::Compilation> Compilation( + NewDriver->BuildCompilation(Args)); + + const driver::JobList &Jobs = Compilation->getJobs(); + + CompileJobAnalyzer CompileAnalyzer; + + for (driver::JobList::const_iterator I = Jobs.begin(), E = Jobs.end(); I != E; + ++I) { + if ((*I)->getKind() == driver::Job::CommandClass) { + const driver::Command *Cmd = cast<driver::Command>(*I); + // Collect only for Assemble jobs. If we do all jobs we get duplicates + // since Link jobs point to Assemble jobs as inputs. + if (Cmd->getSource().getKind() == driver::Action::AssembleJobClass) + CompileAnalyzer.run(&Cmd->getSource()); + } + } + + if (CompileAnalyzer.Inputs.empty()) { + // No compile jobs found. + // FIXME: Emit a warning of some kind? + return false; + } + + // Remove all compilation input files from the command line. This is + // necessary so that getCompileCommands() can construct a command line for + // each file. + std::vector<const char *>::iterator End = std::remove_if( + Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); + + // Remove all inputs deemed unused for compilation. + End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); + + // Remove the -c add above as well. It will be at the end right now. + --End; + + Result = std::vector<std::string>(Args.begin() + 1, End); + return true; +} + FixedCompilationDatabase * FixedCompilationDatabase::loadFromCommandLine(int &Argc, const char **Argv, @@ -107,9 +284,13 @@ FixedCompilationDatabase::loadFromCommandLine(int &Argc, const char **DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); if (DoubleDash == Argv + Argc) return NULL; - std::vector<std::string> CommandLine(DoubleDash + 1, Argv + Argc); + std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc); Argc = DoubleDash - Argv; - return new FixedCompilationDatabase(Directory, CommandLine); + + std::vector<std::string> StrippedArgs; + if (!stripPositionalArgs(CommandLine, StrippedArgs)) + return 0; + return new FixedCompilationDatabase(Directory, StrippedArgs); } FixedCompilationDatabase:: diff --git a/contrib/llvm/tools/clang/lib/Tooling/FileMatchTrie.cpp b/contrib/llvm/tools/clang/lib/Tooling/FileMatchTrie.cpp index 5eb4bb9..89979a8 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/FileMatchTrie.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/FileMatchTrie.cpp @@ -14,7 +14,7 @@ #include "clang/Tooling/FileMatchTrie.h" #include "llvm/ADT/StringMap.h" #include "llvm/Support/FileSystem.h" -#include "llvm/Support/PathV2.h" +#include "llvm/Support/Path.h" #include "llvm/Support/raw_ostream.h" #include <sstream> diff --git a/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp b/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp index 254b069..1e33b41 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp @@ -117,8 +117,6 @@ std::vector<std::string> unescapeCommandLine( return parser.parse(); } -} // end namespace - class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { virtual CompilationDatabase *loadFromDirectory( StringRef Directory, std::string &ErrorMessage) { @@ -132,6 +130,8 @@ class JSONCompilationDatabasePlugin : public CompilationDatabasePlugin { } }; +} // end namespace + // Register the JSONCompilationDatabasePlugin with the // CompilationDatabasePluginRegistry using this statically initialized variable. static CompilationDatabasePluginRegistry::Add<JSONCompilationDatabasePlugin> diff --git a/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp b/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp index d8440d6..e165c12 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/Refactoring.cpp @@ -19,6 +19,8 @@ #include "clang/Rewrite/Core/Rewriter.h" #include "clang/Tooling/Refactoring.h" #include "llvm/Support/raw_os_ostream.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" namespace clang { namespace tooling { @@ -26,12 +28,12 @@ namespace tooling { static const char * const InvalidLocation = ""; Replacement::Replacement() - : FilePath(InvalidLocation), Offset(0), Length(0) {} + : FilePath(InvalidLocation) {} -Replacement::Replacement(StringRef FilePath, unsigned Offset, - unsigned Length, StringRef ReplacementText) - : FilePath(FilePath), Offset(Offset), - Length(Length), ReplacementText(ReplacementText) {} +Replacement::Replacement(StringRef FilePath, unsigned Offset, unsigned Length, + StringRef ReplacementText) + : FilePath(FilePath), ReplacementRange(Offset, Length), + ReplacementText(ReplacementText) {} Replacement::Replacement(SourceManager &Sources, SourceLocation Start, unsigned Length, StringRef ReplacementText) { @@ -62,11 +64,12 @@ bool Replacement::apply(Rewriter &Rewrite) const { // the remapping API is not public in the RewriteBuffer. const SourceLocation Start = SM.getLocForStartOfFile(ID). - getLocWithOffset(Offset); + getLocWithOffset(ReplacementRange.getOffset()); // ReplaceText returns false on success. // ReplaceText only fails if the source location is not a file location, in // which case we already returned false earlier. - bool RewriteSucceeded = !Rewrite.ReplaceText(Start, Length, ReplacementText); + bool RewriteSucceeded = !Rewrite.ReplaceText( + Start, ReplacementRange.getLength(), ReplacementText); assert(RewriteSucceeded); return RewriteSucceeded; } @@ -74,17 +77,26 @@ bool Replacement::apply(Rewriter &Rewrite) const { std::string Replacement::toString() const { std::string result; llvm::raw_string_ostream stream(result); - stream << FilePath << ": " << Offset << ":+" << Length - << ":\"" << ReplacementText << "\""; + stream << FilePath << ": " << ReplacementRange.getOffset() << ":+" + << ReplacementRange.getLength() << ":\"" << ReplacementText << "\""; return result; } -bool Replacement::Less::operator()(const Replacement &R1, - const Replacement &R2) const { - if (R1.FilePath != R2.FilePath) return R1.FilePath < R2.FilePath; - if (R1.Offset != R2.Offset) return R1.Offset < R2.Offset; - if (R1.Length != R2.Length) return R1.Length < R2.Length; - return R1.ReplacementText < R2.ReplacementText; +bool operator<(const Replacement &LHS, const Replacement &RHS) { + if (LHS.getOffset() != RHS.getOffset()) + return LHS.getOffset() < RHS.getOffset(); + if (LHS.getLength() != RHS.getLength()) + return LHS.getLength() < RHS.getLength(); + if (LHS.getFilePath() != RHS.getFilePath()) + return LHS.getFilePath() < RHS.getFilePath(); + return LHS.getReplacementText() < RHS.getReplacementText(); +} + +bool operator==(const Replacement &LHS, const Replacement &RHS) { + return LHS.getOffset() == RHS.getOffset() && + LHS.getLength() == RHS.getLength() && + LHS.getFilePath() == RHS.getFilePath() && + LHS.getReplacementText() == RHS.getReplacementText(); } void Replacement::setFromSourceLocation(SourceManager &Sources, @@ -93,9 +105,16 @@ void Replacement::setFromSourceLocation(SourceManager &Sources, const std::pair<FileID, unsigned> DecomposedLocation = Sources.getDecomposedLoc(Start); const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first); - this->FilePath = Entry != NULL ? Entry->getName() : InvalidLocation; - this->Offset = DecomposedLocation.second; - this->Length = Length; + if (Entry != NULL) { + // Make FilePath absolute so replacements can be applied correctly when + // relative paths for files are used. + llvm::SmallString<256> FilePath(Entry->getName()); + llvm::error_code EC = llvm::sys::fs::make_absolute(FilePath); + this->FilePath = EC ? FilePath.c_str() : Entry->getName(); + } else { + this->FilePath = InvalidLocation; + } + this->ReplacementRange = Range(DecomposedLocation.second, Length); this->ReplacementText = ReplacementText; } @@ -121,7 +140,7 @@ void Replacement::setFromSourceRange(SourceManager &Sources, getRangeSize(Sources, Range), ReplacementText); } -bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite) { +bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite) { bool Result = true; for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); @@ -135,6 +154,121 @@ bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite) { return Result; } +// FIXME: Remove this function when Replacements is implemented as std::vector +// instead of std::set. +bool applyAllReplacements(const std::vector<Replacement> &Replaces, + Rewriter &Rewrite) { + bool Result = true; + for (std::vector<Replacement>::const_iterator I = Replaces.begin(), + E = Replaces.end(); + I != E; ++I) { + if (I->isApplicable()) { + Result = I->apply(Rewrite) && Result; + } else { + Result = false; + } + } + return Result; +} + +std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { + FileManager Files((FileSystemOptions())); + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), + new DiagnosticOptions); + Diagnostics.setClient(new TextDiagnosticPrinter( + llvm::outs(), &Diagnostics.getDiagnosticOptions())); + SourceManager SourceMgr(Diagnostics, Files); + Rewriter Rewrite(SourceMgr, LangOptions()); + llvm::MemoryBuffer *Buf = llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"); + const clang::FileEntry *Entry = + Files.getVirtualFile("<stdin>", Buf->getBufferSize(), 0); + SourceMgr.overrideFileContents(Entry, Buf); + FileID ID = + SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); + for (Replacements::const_iterator I = Replaces.begin(), E = Replaces.end(); + I != E; ++I) { + Replacement Replace("<stdin>", I->getOffset(), I->getLength(), + I->getReplacementText()); + if (!Replace.apply(Rewrite)) + return ""; + } + std::string Result; + llvm::raw_string_ostream OS(Result); + Rewrite.getEditBuffer(ID).write(OS); + OS.flush(); + return Result; +} + +unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) { + unsigned NewPosition = Position; + for (Replacements::iterator I = Replaces.begin(), E = Replaces.end(); I != E; + ++I) { + if (I->getOffset() >= Position) + break; + if (I->getOffset() + I->getLength() > Position) + NewPosition += I->getOffset() + I->getLength() - Position; + NewPosition += I->getReplacementText().size() - I->getLength(); + } + return NewPosition; +} + +// FIXME: Remove this function when Replacements is implemented as std::vector +// instead of std::set. +unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces, + unsigned Position) { + unsigned NewPosition = Position; + for (std::vector<Replacement>::const_iterator I = Replaces.begin(), + E = Replaces.end(); + I != E; ++I) { + if (I->getOffset() >= Position) + break; + if (I->getOffset() + I->getLength() > Position) + NewPosition += I->getOffset() + I->getLength() - Position; + NewPosition += I->getReplacementText().size() - I->getLength(); + } + return NewPosition; +} + +void deduplicate(std::vector<Replacement> &Replaces, + std::vector<Range> &Conflicts) { + if (Replaces.empty()) + return; + + // Deduplicate + std::sort(Replaces.begin(), Replaces.end()); + std::vector<Replacement>::iterator End = + std::unique(Replaces.begin(), Replaces.end()); + Replaces.erase(End, Replaces.end()); + + // Detect conflicts + Range ConflictRange(Replaces.front().getOffset(), + Replaces.front().getLength()); + unsigned ConflictStart = 0; + unsigned ConflictLength = 1; + for (unsigned i = 1; i < Replaces.size(); ++i) { + Range Current(Replaces[i].getOffset(), Replaces[i].getLength()); + if (ConflictRange.overlapsWith(Current)) { + // Extend conflicted range + ConflictRange = Range(ConflictRange.getOffset(), + std::max(ConflictRange.getLength(), + Current.getOffset() + Current.getLength() - + ConflictRange.getOffset())); + ++ConflictLength; + } else { + if (ConflictLength > 1) + Conflicts.push_back(Range(ConflictStart, ConflictLength)); + ConflictRange = Current; + ConflictStart = i; + ConflictLength = 1; + } + } + + if (ConflictLength > 1) + Conflicts.push_back(Range(ConflictStart, ConflictLength)); +} + + RefactoringTool::RefactoringTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths) : ClangTool(Compilations, SourcePaths) {} @@ -167,23 +301,7 @@ bool RefactoringTool::applyAllReplacements(Rewriter &Rewrite) { } int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) { - for (Rewriter::buffer_iterator I = Rewrite.buffer_begin(), - E = Rewrite.buffer_end(); - I != E; ++I) { - // FIXME: This code is copied from the FixItRewriter.cpp - I think it should - // go into directly into Rewriter (there we also have the Diagnostics to - // handle the error cases better). - const FileEntry *Entry = - Rewrite.getSourceMgr().getFileEntryForID(I->first); - std::string ErrorInfo; - llvm::raw_fd_ostream FileStream( - Entry->getName(), ErrorInfo, llvm::raw_fd_ostream::F_Binary); - if (!ErrorInfo.empty()) - return 1; - I->second.write(FileStream); - FileStream.flush(); - } - return 0; + return Rewrite.overwriteChangedFiles() ? 1 : 0; } } // end namespace tooling diff --git a/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp b/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp index 52855f6..a58854d 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp @@ -13,15 +13,18 @@ //===----------------------------------------------------------------------===// #include "clang/Tooling/Tooling.h" +#include "clang/AST/ASTConsumer.h" #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" +#include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "clang/Frontend/TextDiagnosticPrinter.h" #include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CompilationDatabase.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/Option/Option.h" #include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Host.h" @@ -37,6 +40,8 @@ namespace clang { namespace tooling { +ToolAction::~ToolAction() {} + FrontendActionFactory::~FrontendActionFactory() {} // FIXME: This file contains structural duplication with other parts of the @@ -57,7 +62,7 @@ static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, /// \brief Retrieves the clang CC1 specific flags out of the compilation's jobs. /// /// Returns NULL on error. -static const clang::driver::ArgStringList *getCC1Arguments( +static const llvm::opt::ArgStringList *getCC1Arguments( clang::DiagnosticsEngine *Diagnostics, clang::driver::Compilation *Compilation) { // We expect to get back exactly one Command job, if we didn't something @@ -66,7 +71,7 @@ static const clang::driver::ArgStringList *getCC1Arguments( if (Jobs.size() != 1 || !isa<clang::driver::Command>(*Jobs.begin())) { SmallString<256> error_msg; llvm::raw_svector_ostream error_stream(error_msg); - Compilation->PrintJob(error_stream, Compilation->getJobs(), "; ", true); + Jobs.Print(error_stream, "; ", true); Diagnostics->Report(clang::diag::err_fe_expected_compiler_job) << error_stream.str(); return NULL; @@ -86,13 +91,14 @@ static const clang::driver::ArgStringList *getCC1Arguments( /// \brief Returns a clang build invocation initialized from the CC1 flags. static clang::CompilerInvocation *newInvocation( clang::DiagnosticsEngine *Diagnostics, - const clang::driver::ArgStringList &CC1Args) { + const llvm::opt::ArgStringList &CC1Args) { assert(!CC1Args.empty() && "Must at least contain the program name!"); clang::CompilerInvocation *Invocation = new clang::CompilerInvocation; clang::CompilerInvocation::CreateFromArgs( *Invocation, CC1Args.data() + 1, CC1Args.data() + CC1Args.size(), *Diagnostics); Invocation->getFrontendOpts().DisableFree = false; + Invocation->getCodeGenOpts().DisableFree = false; return Invocation; } @@ -102,18 +108,26 @@ bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, ToolAction, Code, std::vector<std::string>(), FileName); } +static std::vector<std::string> +getSyntaxOnlyToolArgs(const std::vector<std::string> &ExtraArgs, + StringRef FileName) { + std::vector<std::string> Args; + Args.push_back("clang-tool"); + Args.push_back("-fsyntax-only"); + Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end()); + Args.push_back(FileName.str()); + return Args; +} + bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, const std::vector<std::string> &Args, const Twine &FileName) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - std::vector<std::string> Commands; - Commands.push_back("clang-tool"); - Commands.push_back("-fsyntax-only"); - Commands.insert(Commands.end(), Args.begin(), Args.end()); - Commands.push_back(FileNameRef.data()); - FileManager Files((FileSystemOptions())); - ToolInvocation Invocation(Commands, ToolAction, &Files); + llvm::IntrusiveRefCntPtr<FileManager> Files( + new FileManager(FileSystemOptions())); + ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction, + Files.getPtr()); SmallString<1024> CodeStorage; Invocation.mapVirtualFile(FileNameRef, @@ -122,31 +136,56 @@ bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, } std::string getAbsolutePath(StringRef File) { - SmallString<1024> BaseDirectory; - if (const char *PWD = ::getenv("PWD")) - BaseDirectory = PWD; - else - llvm::sys::fs::current_path(BaseDirectory); - SmallString<1024> PathStorage; - if (llvm::sys::path::is_absolute(File)) { - llvm::sys::path::native(File, PathStorage); - return PathStorage.str(); - } StringRef RelativePath(File); // FIXME: Should '.\\' be accepted on Win32? if (RelativePath.startswith("./")) { RelativePath = RelativePath.substr(strlen("./")); } - SmallString<1024> AbsolutePath(BaseDirectory); - llvm::sys::path::append(AbsolutePath, RelativePath); - llvm::sys::path::native(Twine(AbsolutePath), PathStorage); - return PathStorage.str(); + + SmallString<1024> AbsolutePath = RelativePath; + llvm::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); + assert(!EC); + (void)EC; + llvm::sys::path::native(AbsolutePath); + return AbsolutePath.str(); } -ToolInvocation::ToolInvocation( - ArrayRef<std::string> CommandLine, FrontendAction *ToolAction, - FileManager *Files) - : CommandLine(CommandLine.vec()), ToolAction(ToolAction), Files(Files) { +namespace { + +class SingleFrontendActionFactory : public FrontendActionFactory { + FrontendAction *Action; + +public: + SingleFrontendActionFactory(FrontendAction *Action) : Action(Action) {} + + FrontendAction *create() { return Action; } +}; + +} + +ToolInvocation::ToolInvocation(ArrayRef<std::string> CommandLine, + ToolAction *Action, FileManager *Files) + : CommandLine(CommandLine.vec()), + Action(Action), + OwnsAction(false), + Files(Files), + DiagConsumer(NULL) {} + +ToolInvocation::ToolInvocation(ArrayRef<std::string> CommandLine, + FrontendAction *FAction, FileManager *Files) + : CommandLine(CommandLine.vec()), + Action(new SingleFrontendActionFactory(FAction)), + OwnsAction(true), + Files(Files), + DiagConsumer(NULL) {} + +ToolInvocation::~ToolInvocation() { + if (OwnsAction) + delete Action; +} + +void ToolInvocation::setDiagnosticConsumer(DiagnosticConsumer *D) { + DiagConsumer = D; } void ToolInvocation::mapVirtualFile(StringRef FilePath, StringRef Content) { @@ -164,8 +203,8 @@ bool ToolInvocation::run() { TextDiagnosticPrinter DiagnosticPrinter( llvm::errs(), &*DiagOpts); DiagnosticsEngine Diagnostics( - IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), - &*DiagOpts, &DiagnosticPrinter, false); + IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, + DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); const OwningPtr<clang::driver::Driver> Driver( newDriver(&Diagnostics, BinaryName)); @@ -173,13 +212,21 @@ bool ToolInvocation::run() { Driver->setCheckInputsExist(false); const OwningPtr<clang::driver::Compilation> Compilation( Driver->BuildCompilation(llvm::makeArrayRef(Argv))); - const clang::driver::ArgStringList *const CC1Args = getCC1Arguments( + const llvm::opt::ArgStringList *const CC1Args = getCC1Arguments( &Diagnostics, Compilation.get()); if (CC1Args == NULL) { return false; } OwningPtr<clang::CompilerInvocation> Invocation( newInvocation(&Diagnostics, *CC1Args)); + for (llvm::StringMap<StringRef>::const_iterator + It = MappedFileContents.begin(), End = MappedFileContents.end(); + It != End; ++It) { + // Inject the code as the given file name into the preprocessor options. + const llvm::MemoryBuffer *Input = + llvm::MemoryBuffer::getMemBuffer(It->getValue()); + Invocation->getPreprocessorOpts().addRemappedFile(It->getKey(), Input); + } return runInvocation(BinaryName, Compilation.get(), Invocation.take()); } @@ -190,54 +237,44 @@ bool ToolInvocation::runInvocation( // Show the invocation, with -v. if (Invocation->getHeaderSearchOpts().Verbose) { llvm::errs() << "clang Invocation:\n"; - Compilation->PrintJob(llvm::errs(), Compilation->getJobs(), "\n", true); + Compilation->getJobs().Print(llvm::errs(), "\n", true); llvm::errs() << "\n"; } + return Action->runInvocation(Invocation, Files, DiagConsumer); +} + +bool FrontendActionFactory::runInvocation(CompilerInvocation *Invocation, + FileManager *Files, + DiagnosticConsumer *DiagConsumer) { // Create a compiler instance to handle the actual work. clang::CompilerInstance Compiler; Compiler.setInvocation(Invocation); Compiler.setFileManager(Files); - // FIXME: What about LangOpts? - // ToolAction can have lifetime requirements for Compiler or its members, and - // we need to ensure it's deleted earlier than Compiler. So we pass it to an - // OwningPtr declared after the Compiler variable. - OwningPtr<FrontendAction> ScopedToolAction(ToolAction.take()); + // The FrontendAction can have lifetime requirements for Compiler or its + // members, and we need to ensure it's deleted earlier than Compiler. So we + // pass it to an OwningPtr declared after the Compiler variable. + OwningPtr<FrontendAction> ScopedToolAction(create()); // Create the compilers actual diagnostics engine. - Compiler.createDiagnostics(); + Compiler.createDiagnostics(DiagConsumer, /*ShouldOwnClient=*/false); if (!Compiler.hasDiagnostics()) return false; Compiler.createSourceManager(*Files); - addFileMappingsTo(Compiler.getSourceManager()); const bool Success = Compiler.ExecuteAction(*ScopedToolAction); - Compiler.resetAndLeakFileManager(); Files->clearStatCaches(); return Success; } -void ToolInvocation::addFileMappingsTo(SourceManager &Sources) { - for (llvm::StringMap<StringRef>::const_iterator - It = MappedFileContents.begin(), End = MappedFileContents.end(); - It != End; ++It) { - // Inject the code as the given file name into the preprocessor options. - const llvm::MemoryBuffer *Input = - llvm::MemoryBuffer::getMemBuffer(It->getValue()); - // FIXME: figure out what '0' stands for. - const FileEntry *FromFile = Files->getVirtualFile( - It->getKey(), Input->getBufferSize(), 0); - Sources.overrideFileContents(FromFile, Input); - } -} - ClangTool::ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths) - : Files((FileSystemOptions())), - ArgsAdjuster(new ClangSyntaxOnlyAdjuster()) { + : Files(new FileManager(FileSystemOptions())), DiagConsumer(NULL) { + ArgsAdjusters.push_back(new ClangStripOutputAdjuster()); + ArgsAdjusters.push_back(new ClangSyntaxOnlyAdjuster()); for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) { SmallString<1024> File(getAbsolutePath(SourcePaths[I])); @@ -259,15 +296,30 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations, } } +void ClangTool::setDiagnosticConsumer(DiagnosticConsumer *D) { + DiagConsumer = D; +} + void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) { MappedFileContents.push_back(std::make_pair(FilePath, Content)); } void ClangTool::setArgumentsAdjuster(ArgumentsAdjuster *Adjuster) { - ArgsAdjuster.reset(Adjuster); + clearArgumentsAdjusters(); + appendArgumentsAdjuster(Adjuster); +} + +void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster) { + ArgsAdjusters.push_back(Adjuster); +} + +void ClangTool::clearArgumentsAdjusters() { + for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I) + delete ArgsAdjusters[I]; + ArgsAdjusters.clear(); } -int ClangTool::run(FrontendActionFactory *ActionFactory) { +int ClangTool::run(ToolAction *Action) { // Exists solely for the purpose of lookup of the resource path. // This just needs to be some symbol in the binary. static int StaticSymbol; @@ -277,7 +329,7 @@ int ClangTool::run(FrontendActionFactory *ActionFactory) { // first argument, thus allowing ClangTool and runToolOnCode to just // pass in made-up names here. Make sure this works on other platforms. std::string MainExecutable = - llvm::sys::Path::GetMainExecutable("clang_tool", &StaticSymbol).str(); + llvm::sys::fs::getMainExecutable("clang_tool", &StaticSymbol); bool ProcessingFailed = false; for (unsigned I = 0; I < CompileCommands.size(); ++I) { @@ -292,8 +344,9 @@ int ClangTool::run(FrontendActionFactory *ActionFactory) { if (chdir(CompileCommands[I].second.Directory.c_str())) llvm::report_fatal_error("Cannot chdir into \"" + CompileCommands[I].second.Directory + "\n!"); - std::vector<std::string> CommandLine = - ArgsAdjuster->Adjust(CompileCommands[I].second.CommandLine); + std::vector<std::string> CommandLine = CompileCommands[I].second.CommandLine; + for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I) + CommandLine = ArgsAdjusters[I]->Adjust(CommandLine); assert(!CommandLine.empty()); CommandLine[0] = MainExecutable; // FIXME: We need a callback mechanism for the tool writer to output a @@ -301,7 +354,8 @@ int ClangTool::run(FrontendActionFactory *ActionFactory) { DEBUG({ llvm::dbgs() << "Processing: " << File << ".\n"; }); - ToolInvocation Invocation(CommandLine, ActionFactory->create(), &Files); + ToolInvocation Invocation(CommandLine, Action, Files.getPtr()); + Invocation.setDiagnosticConsumer(DiagConsumer); for (int I = 0, E = MappedFileContents.size(); I != E; ++I) { Invocation.mapVirtualFile(MappedFileContents[I].first, MappedFileContents[I].second); @@ -315,5 +369,59 @@ int ClangTool::run(FrontendActionFactory *ActionFactory) { return ProcessingFailed ? 1 : 0; } +namespace { + +class ASTBuilderAction : public ToolAction { + std::vector<ASTUnit *> &ASTs; + +public: + ASTBuilderAction(std::vector<ASTUnit *> &ASTs) : ASTs(ASTs) {} + + bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, + DiagnosticConsumer *DiagConsumer) { + // FIXME: This should use the provided FileManager. + ASTUnit *AST = ASTUnit::LoadFromCompilerInvocation( + Invocation, CompilerInstance::createDiagnostics( + &Invocation->getDiagnosticOpts(), DiagConsumer, + /*ShouldOwnClient=*/false)); + if (!AST) + return false; + + ASTs.push_back(AST); + return true; + } +}; + +} + +int ClangTool::buildASTs(std::vector<ASTUnit *> &ASTs) { + ASTBuilderAction Action(ASTs); + return run(&Action); +} + +ASTUnit *buildASTFromCode(const Twine &Code, const Twine &FileName) { + return buildASTFromCodeWithArgs(Code, std::vector<std::string>(), FileName); +} + +ASTUnit *buildASTFromCodeWithArgs(const Twine &Code, + const std::vector<std::string> &Args, + const Twine &FileName) { + SmallString<16> FileNameStorage; + StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); + + std::vector<ASTUnit *> ASTs; + ASTBuilderAction Action(ASTs); + ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, 0); + + SmallString<1024> CodeStorage; + Invocation.mapVirtualFile(FileNameRef, + Code.toNullTerminatedStringRef(CodeStorage)); + if (!Invocation.run()) + return 0; + + assert(ASTs.size() == 1); + return ASTs[0]; +} + } // end namespace tooling } // end namespace clang |