diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Tooling')
7 files changed, 443 insertions, 118 deletions
diff --git a/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp b/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp index 1722ede..2f3d829 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/ArgumentsAdjusters.cpp @@ -13,15 +13,13 @@ //===----------------------------------------------------------------------===// #include "clang/Tooling/ArgumentsAdjusters.h" -#include "clang/Basic/LLVM.h" -#include "llvm/ADT/StringRef.h" namespace clang { namespace tooling { /// Add -fsyntax-only option to the commnand line arguments. ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { - return [](const CommandLineArguments &Args) { + return [](const CommandLineArguments &Args, StringRef /*unused*/) { CommandLineArguments AdjustedArgs; for (size_t i = 0, e = Args.size(); i != e; ++i) { StringRef Arg = Args[i]; @@ -36,7 +34,7 @@ ArgumentsAdjuster getClangSyntaxOnlyAdjuster() { } ArgumentsAdjuster getClangStripOutputAdjuster() { - return [](const CommandLineArguments &Args) { + return [](const CommandLineArguments &Args, StringRef /*unused*/) { CommandLineArguments AdjustedArgs; for (size_t i = 0, e = Args.size(); i < e; ++i) { StringRef Arg = Args[i]; @@ -55,7 +53,7 @@ ArgumentsAdjuster getClangStripOutputAdjuster() { ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, ArgumentInsertPosition Pos) { - return [Extra, Pos](const CommandLineArguments &Args) { + return [Extra, Pos](const CommandLineArguments &Args, StringRef /*unused*/) { CommandLineArguments Return(Args); CommandLineArguments::iterator I; @@ -78,8 +76,8 @@ ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra, ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, ArgumentsAdjuster Second) { - return [First, Second](const CommandLineArguments &Args) { - return Second(First(Args)); + return [First, Second](const CommandLineArguments &Args, StringRef File) { + return Second(First(Args, File), File); }; } diff --git a/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp b/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp index adae178..82f5601 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/CommonOptionsParser.cpp @@ -86,22 +86,22 @@ private: adjustCommands(std::vector<CompileCommand> Commands) const { for (CompileCommand &Command : Commands) for (const auto &Adjuster : Adjusters) - Command.CommandLine = Adjuster(Command.CommandLine); + Command.CommandLine = Adjuster(Command.CommandLine, Command.Filename); return Commands; } }; } // namespace -CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, - cl::OptionCategory &Category, - const char *Overview) { +CommonOptionsParser::CommonOptionsParser( + int &argc, const char **argv, cl::OptionCategory &Category, + llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview) { static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden); static cl::opt<std::string> BuildPath("p", cl::desc("Build path"), cl::Optional, cl::cat(Category)); static cl::list<std::string> SourcePaths( - cl::Positional, cl::desc("<source0> [... <sourceN>]"), cl::OneOrMore, + cl::Positional, cl::desc("<source0> [... <sourceN>]"), OccurrencesFlag, cl::cat(Category)); static cl::list<std::string> ArgsAfter( @@ -116,10 +116,12 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, cl::HideUnrelatedOptions(Category); - Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, - argv)); + Compilations.reset(FixedCompilationDatabase::loadFromCommandLine(argc, argv)); cl::ParseCommandLineOptions(argc, argv, Overview); SourcePathList = SourcePaths; + if ((OccurrencesFlag == cl::ZeroOrMore || OccurrencesFlag == cl::Optional) && + SourcePathList.empty()) + return; if (!Compilations) { std::string ErrorMessage; if (!BuildPath.empty()) { @@ -129,8 +131,12 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv, Compilations = CompilationDatabase::autoDetectFromSource(SourcePaths[0], ErrorMessage); } - if (!Compilations) - llvm::report_fatal_error(ErrorMessage); + if (!Compilations) { + llvm::errs() << "Error while trying to load a compilation database:\n" + << ErrorMessage << "Running without flags.\n"; + Compilations.reset( + new FixedCompilationDatabase(".", std::vector<std::string>())); + } } auto AdjustingCompilations = llvm::make_unique<ArgumentsAdjustingCompilations>( diff --git a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp index 2272be6..957e401 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp @@ -299,13 +299,15 @@ FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) { std::vector<std::string> ToolCommandLine(1, "clang-tool"); ToolCommandLine.insert(ToolCommandLine.end(), CommandLine.begin(), CommandLine.end()); - CompileCommands.emplace_back(Directory, std::move(ToolCommandLine)); + CompileCommands.emplace_back(Directory, StringRef(), + std::move(ToolCommandLine)); } std::vector<CompileCommand> FixedCompilationDatabase::getCompileCommands(StringRef FilePath) const { std::vector<CompileCommand> Result(CompileCommands); Result[0].CommandLine.push_back(FilePath); + Result[0].Filename = FilePath; return Result; } @@ -325,7 +327,7 @@ namespace tooling { // This anchor is used to force the linker to link in the generated object file // and thus register the JSONCompilationDatabasePlugin. extern volatile int JSONAnchorSource; -static int JSONAnchorDest = JSONAnchorSource; +static int LLVM_ATTRIBUTE_UNUSED JSONAnchorDest = JSONAnchorSource; } // end namespace tooling } // end namespace clang diff --git a/contrib/llvm/tools/clang/lib/Tooling/Core/Lookup.cpp b/contrib/llvm/tools/clang/lib/Tooling/Core/Lookup.cpp new file mode 100644 index 0000000..697eeb4 --- /dev/null +++ b/contrib/llvm/tools/clang/lib/Tooling/Core/Lookup.cpp @@ -0,0 +1,113 @@ +//===--- Lookup.cpp - Framework for clang refactoring tools ---------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines helper methods for clang tools performing name lookup. +// +//===----------------------------------------------------------------------===// + +#include "clang/Tooling/Core/Lookup.h" +#include "clang/AST/Decl.h" +using namespace clang; +using namespace clang::tooling; + +static bool isInsideDifferentNamespaceWithSameName(const DeclContext *DeclA, + const DeclContext *DeclB) { + while (true) { + // Look past non-namespaces on DeclA. + while (DeclA && !isa<NamespaceDecl>(DeclA)) + DeclA = DeclA->getParent(); + + // Look past non-namespaces on DeclB. + while (DeclB && !isa<NamespaceDecl>(DeclB)) + DeclB = DeclB->getParent(); + + // We hit the root, no namespace collision. + if (!DeclA || !DeclB) + return false; + + // Literally the same namespace, not a collision. + if (DeclA == DeclB) + return false; + + // Now check the names. If they match we have a different namespace with the + // same name. + if (cast<NamespaceDecl>(DeclA)->getDeclName() == + cast<NamespaceDecl>(DeclB)->getDeclName()) + return true; + + DeclA = DeclA->getParent(); + DeclB = DeclB->getParent(); + } +} + +static StringRef getBestNamespaceSubstr(const DeclContext *DeclA, + StringRef NewName, + bool HadLeadingColonColon) { + while (true) { + while (DeclA && !isa<NamespaceDecl>(DeclA)) + DeclA = DeclA->getParent(); + + // Fully qualified it is! Leave :: in place if it's there already. + if (!DeclA) + return HadLeadingColonColon ? NewName : NewName.substr(2); + + // Otherwise strip off redundant namespace qualifications from the new name. + // We use the fully qualified name of the namespace and remove that part + // from NewName if it has an identical prefix. + std::string NS = + "::" + cast<NamespaceDecl>(DeclA)->getQualifiedNameAsString() + "::"; + if (NewName.startswith(NS)) + return NewName.substr(NS.size()); + + // No match yet. Strip of a namespace from the end of the chain and try + // again. This allows to get optimal qualifications even if the old and new + // decl only share common namespaces at a higher level. + DeclA = DeclA->getParent(); + } +} + +/// Check if the name specifier begins with a written "::". +static bool isFullyQualified(const NestedNameSpecifier *NNS) { + while (NNS) { + if (NNS->getKind() == NestedNameSpecifier::Global) + return true; + NNS = NNS->getPrefix(); + } + return false; +} + +std::string tooling::replaceNestedName(const NestedNameSpecifier *Use, + const DeclContext *UseContext, + const NamedDecl *FromDecl, + StringRef ReplacementString) { + assert(ReplacementString.startswith("::") && + "Expected fully-qualified name!"); + + // We can do a raw name replacement when we are not inside the namespace for + // the original function and it is not in the global namespace. The + // assumption is that outside the original namespace we must have a using + // statement that makes this work out and that other parts of this refactor + // will automatically fix using statements to point to the new function + const bool class_name_only = !Use; + const bool in_global_namespace = + isa<TranslationUnitDecl>(FromDecl->getDeclContext()); + if (class_name_only && !in_global_namespace && + !isInsideDifferentNamespaceWithSameName(FromDecl->getDeclContext(), + UseContext)) { + auto Pos = ReplacementString.rfind("::"); + return Pos != StringRef::npos ? ReplacementString.substr(Pos + 2) + : ReplacementString; + } + // We did not match this because of a using statement, so we will need to + // figure out how good a namespace match we have with our destination type. + // We work backwards (from most specific possible namespace to least + // specific). + return getBestNamespaceSubstr(UseContext, ReplacementString, + isFullyQualified(Use)); +} diff --git a/contrib/llvm/tools/clang/lib/Tooling/Core/Replacement.cpp b/contrib/llvm/tools/clang/lib/Tooling/Core/Replacement.cpp index 6d37a49..47bbdeb 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/Core/Replacement.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/Core/Replacement.cpp @@ -113,15 +113,7 @@ void Replacement::setFromSourceLocation(const SourceManager &Sources, const std::pair<FileID, unsigned> DecomposedLocation = Sources.getDecomposedLoc(Start); const FileEntry *Entry = Sources.getFileEntryForID(DecomposedLocation.first); - if (Entry) { - // Make FilePath absolute so replacements can be applied correctly when - // relative paths for files are used. - llvm::SmallString<256> FilePath(Entry->getName()); - std::error_code EC = llvm::sys::fs::make_absolute(FilePath); - this->FilePath = EC ? FilePath.c_str() : Entry->getName(); - } else { - this->FilePath = InvalidLocation; - } + this->FilePath = Entry ? Entry->getName() : InvalidLocation; this->ReplacementRange = Range(DecomposedLocation.second, Length); this->ReplacementText = ReplacementText; } @@ -151,34 +143,32 @@ void Replacement::setFromSourceRange(const SourceManager &Sources, ReplacementText); } -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(); +template <typename T> +unsigned shiftedCodePositionInternal(const T &Replaces, unsigned Position) { + unsigned Offset = 0; + for (const auto& R : Replaces) { + if (R.getOffset() + R.getLength() <= Position) { + Offset += R.getReplacementText().size() - R.getLength(); + continue; + } + if (R.getOffset() < Position && + R.getOffset() + R.getReplacementText().size() <= Position) { + Position = R.getOffset() + R.getReplacementText().size() - 1; + } + break; } - return NewPosition; + return Position + Offset; +} + +unsigned shiftedCodePosition(const Replacements &Replaces, unsigned Position) { + return shiftedCodePositionInternal(Replaces, Position); } // 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; + return shiftedCodePositionInternal(Replaces, Position); } void deduplicate(std::vector<Replacement> &Replaces, @@ -265,19 +255,18 @@ bool applyAllReplacements(const std::vector<Replacement> &Replaces, } std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { - FileManager Files((FileSystemOptions())); + IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( + new vfs::InMemoryFileSystem); + FileManager Files(FileSystemOptions(), InMemoryFileSystem); DiagnosticsEngine Diagnostics( IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), new DiagnosticOptions); SourceManager SourceMgr(Diagnostics, Files); Rewriter Rewrite(SourceMgr, LangOptions()); - std::unique_ptr<llvm::MemoryBuffer> Buf = - llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>"); - const clang::FileEntry *Entry = - Files.getVirtualFile("<stdin>", Buf->getBufferSize(), 0); - SourceMgr.overrideFileContents(Entry, std::move(Buf)); - FileID ID = - SourceMgr.createFileID(Entry, SourceLocation(), clang::SrcMgr::C_User); + InMemoryFileSystem->addFile( + "<stdin>", 0, llvm::MemoryBuffer::getMemBuffer(Code, "<stdin>")); + FileID ID = SourceMgr.createFileID(Files.getFile("<stdin>"), 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(), @@ -292,6 +281,139 @@ std::string applyAllReplacements(StringRef Code, const Replacements &Replaces) { return Result; } +namespace { +// Represents a merged replacement, i.e. a replacement consisting of multiple +// overlapping replacements from 'First' and 'Second' in mergeReplacements. +// +// Position projection: +// Offsets and lengths of the replacements can generally refer to two different +// coordinate spaces. Replacements from 'First' refer to the original text +// whereas replacements from 'Second' refer to the text after applying 'First'. +// +// MergedReplacement always operates in the coordinate space of the original +// text, i.e. transforms elements from 'Second' to take into account what was +// changed based on the elements from 'First'. +// +// We can correctly calculate this projection as we look at the replacements in +// order of strictly increasing offsets. +// +// Invariants: +// * We always merge elements from 'First' into elements from 'Second' and vice +// versa. Within each set, the replacements are non-overlapping. +// * We only extend to the right, i.e. merge elements with strictly increasing +// offsets. +class MergedReplacement { +public: + MergedReplacement(const Replacement &R, bool MergeSecond, int D) + : MergeSecond(MergeSecond), Delta(D), FilePath(R.getFilePath()), + Offset(R.getOffset() + (MergeSecond ? 0 : Delta)), Length(R.getLength()), + Text(R.getReplacementText()) { + Delta += MergeSecond ? 0 : Text.size() - Length; + DeltaFirst = MergeSecond ? Text.size() - Length : 0; + } + + // Merges the next element 'R' into this merged element. As we always merge + // from 'First' into 'Second' or vice versa, the MergedReplacement knows what + // set the next element is coming from. + void merge(const Replacement &R) { + if (MergeSecond) { + unsigned REnd = R.getOffset() + Delta + R.getLength(); + unsigned End = Offset + Text.size(); + if (REnd > End) { + Length += REnd - End; + MergeSecond = false; + } + StringRef TextRef = Text; + StringRef Head = TextRef.substr(0, R.getOffset() + Delta - Offset); + StringRef Tail = TextRef.substr(REnd - Offset); + Text = (Head + R.getReplacementText() + Tail).str(); + Delta += R.getReplacementText().size() - R.getLength(); + } else { + unsigned End = Offset + Length; + StringRef RText = R.getReplacementText(); + StringRef Tail = RText.substr(End - R.getOffset()); + Text = (Text + Tail).str(); + if (R.getOffset() + RText.size() > End) { + Length = R.getOffset() + R.getLength() - Offset; + MergeSecond = true; + } else { + Length += R.getLength() - RText.size(); + } + DeltaFirst += RText.size() - R.getLength(); + } + } + + // Returns 'true' if 'R' starts strictly after the MergedReplacement and thus + // doesn't need to be merged. + bool endsBefore(const Replacement &R) const { + if (MergeSecond) + return Offset + Text.size() < R.getOffset() + Delta; + return Offset + Length < R.getOffset(); + } + + // Returns 'true' if an element from the second set should be merged next. + bool mergeSecond() const { return MergeSecond; } + int deltaFirst() const { return DeltaFirst; } + Replacement asReplacement() const { return {FilePath, Offset, Length, Text}; } + +private: + bool MergeSecond; + + // Amount of characters that elements from 'Second' need to be shifted by in + // order to refer to the original text. + int Delta; + + // Sum of all deltas (text-length - length) of elements from 'First' merged + // into this element. This is used to update 'Delta' once the + // MergedReplacement is completed. + int DeltaFirst; + + // Data of the actually merged replacement. FilePath and Offset aren't changed + // as the element is only extended to the right. + const StringRef FilePath; + const unsigned Offset; + unsigned Length; + std::string Text; +}; +} // namespace + +Replacements mergeReplacements(const Replacements &First, + const Replacements &Second) { + if (First.empty() || Second.empty()) + return First.empty() ? Second : First; + + // Delta is the amount of characters that replacements from 'Second' need to + // be shifted so that their offsets refer to the original text. + int Delta = 0; + Replacements Result; + + // Iterate over both sets and always add the next element (smallest total + // Offset) from either 'First' or 'Second'. Merge that element with + // subsequent replacements as long as they overlap. See more details in the + // comment on MergedReplacement. + for (auto FirstI = First.begin(), SecondI = Second.begin(); + FirstI != First.end() || SecondI != Second.end();) { + bool NextIsFirst = SecondI == Second.end() || + (FirstI != First.end() && + FirstI->getOffset() < SecondI->getOffset() + Delta); + MergedReplacement Merged(NextIsFirst ? *FirstI : *SecondI, NextIsFirst, + Delta); + ++(NextIsFirst ? FirstI : SecondI); + + while ((Merged.mergeSecond() && SecondI != Second.end()) || + (!Merged.mergeSecond() && FirstI != First.end())) { + auto &I = Merged.mergeSecond() ? SecondI : FirstI; + if (Merged.endsBefore(*I)) + break; + Merged.merge(*I); + ++I; + } + Delta -= Merged.deltaFirst(); + Result.insert(Merged.asReplacement()); + } + return Result; +} + } // end namespace tooling } // end namespace clang diff --git a/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp b/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp index 454a2ff..299fbdc 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/JSONCompilationDatabase.cpp @@ -206,24 +206,33 @@ JSONCompilationDatabase::getAllFiles() const { std::vector<CompileCommand> JSONCompilationDatabase::getAllCompileCommands() const { std::vector<CompileCommand> Commands; - for (llvm::StringMap< std::vector<CompileCommandRef> >::const_iterator - CommandsRefI = IndexByFile.begin(), CommandsRefEnd = IndexByFile.end(); - CommandsRefI != CommandsRefEnd; ++CommandsRefI) { - getCommands(CommandsRefI->getValue(), Commands); - } + getCommands(AllCommands, Commands); return Commands; } +static std::vector<std::string> +nodeToCommandLine(const std::vector<llvm::yaml::ScalarNode *> &Nodes) { + SmallString<1024> Storage; + if (Nodes.size() == 1) { + return unescapeCommandLine(Nodes[0]->getValue(Storage)); + } + std::vector<std::string> Arguments; + for (auto *Node : Nodes) { + Arguments.push_back(Node->getValue(Storage)); + } + return Arguments; +} + void JSONCompilationDatabase::getCommands( - ArrayRef<CompileCommandRef> CommandsRef, - std::vector<CompileCommand> &Commands) const { + ArrayRef<CompileCommandRef> CommandsRef, + std::vector<CompileCommand> &Commands) const { for (int I = 0, E = CommandsRef.size(); I != E; ++I) { SmallString<8> DirectoryStorage; - SmallString<1024> CommandStorage; + SmallString<32> FilenameStorage; Commands.emplace_back( - // FIXME: Escape correctly: - CommandsRef[I].first->getValue(DirectoryStorage), - unescapeCommandLine(CommandsRef[I].second->getValue(CommandStorage))); + std::get<0>(CommandsRef[I])->getValue(DirectoryStorage), + std::get<1>(CommandsRef[I])->getValue(FilenameStorage), + nodeToCommandLine(std::get<2>(CommandsRef[I]))); } } @@ -243,43 +252,56 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { ErrorMessage = "Expected array."; return false; } - for (llvm::yaml::SequenceNode::iterator AI = Array->begin(), - AE = Array->end(); - AI != AE; ++AI) { - llvm::yaml::MappingNode *Object = dyn_cast<llvm::yaml::MappingNode>(&*AI); + for (auto& NextObject : *Array) { + llvm::yaml::MappingNode *Object = dyn_cast<llvm::yaml::MappingNode>(&NextObject); if (!Object) { ErrorMessage = "Expected object."; return false; } llvm::yaml::ScalarNode *Directory = nullptr; - llvm::yaml::ScalarNode *Command = nullptr; + llvm::Optional<std::vector<llvm::yaml::ScalarNode *>> Command; llvm::yaml::ScalarNode *File = nullptr; - for (llvm::yaml::MappingNode::iterator KVI = Object->begin(), - KVE = Object->end(); - KVI != KVE; ++KVI) { - llvm::yaml::Node *Value = (*KVI).getValue(); + for (auto& NextKeyValue : *Object) { + llvm::yaml::ScalarNode *KeyString = + dyn_cast<llvm::yaml::ScalarNode>(NextKeyValue.getKey()); + if (!KeyString) { + ErrorMessage = "Expected strings as key."; + return false; + } + SmallString<10> KeyStorage; + StringRef KeyValue = KeyString->getValue(KeyStorage); + llvm::yaml::Node *Value = NextKeyValue.getValue(); if (!Value) { ErrorMessage = "Expected value."; return false; } llvm::yaml::ScalarNode *ValueString = dyn_cast<llvm::yaml::ScalarNode>(Value); - if (!ValueString) { - ErrorMessage = "Expected string as value."; + llvm::yaml::SequenceNode *SequenceString = + dyn_cast<llvm::yaml::SequenceNode>(Value); + if (KeyValue == "arguments" && !SequenceString) { + ErrorMessage = "Expected sequence as value."; return false; - } - llvm::yaml::ScalarNode *KeyString = - dyn_cast<llvm::yaml::ScalarNode>((*KVI).getKey()); - if (!KeyString) { - ErrorMessage = "Expected strings as key."; + } else if (KeyValue != "arguments" && !ValueString) { + ErrorMessage = "Expected string as value."; return false; } - SmallString<8> KeyStorage; - if (KeyString->getValue(KeyStorage) == "directory") { + if (KeyValue == "directory") { Directory = ValueString; - } else if (KeyString->getValue(KeyStorage) == "command") { - Command = ValueString; - } else if (KeyString->getValue(KeyStorage) == "file") { + } else if (KeyValue == "arguments") { + Command = std::vector<llvm::yaml::ScalarNode *>(); + for (auto &Argument : *SequenceString) { + auto Scalar = dyn_cast<llvm::yaml::ScalarNode>(&Argument); + if (!Scalar) { + ErrorMessage = "Only strings are allowed in 'arguments'."; + return false; + } + Command->push_back(Scalar); + } + } else if (KeyValue == "command") { + if (!Command) + Command = std::vector<llvm::yaml::ScalarNode *>(1, ValueString); + } else if (KeyValue == "file") { File = ValueString; } else { ErrorMessage = ("Unknown key: \"" + @@ -292,7 +314,7 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { return false; } if (!Command) { - ErrorMessage = "Missing key: \"command\"."; + ErrorMessage = "Missing key: \"command\" or \"arguments\"."; return false; } if (!Directory) { @@ -311,8 +333,9 @@ bool JSONCompilationDatabase::parse(std::string &ErrorMessage) { } else { llvm::sys::path::native(FileName, NativeFilePath); } - IndexByFile[NativeFilePath].push_back( - CompileCommandRef(Directory, Command)); + auto Cmd = CompileCommandRef(Directory, File, *Command); + IndexByFile[NativeFilePath].push_back(Cmd); + AllCommands.push_back(Cmd); MatchTrie.insert(NativeFilePath); } return true; diff --git a/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp b/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp index f9cb7c6..fd5596e 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/Tooling.cpp @@ -17,6 +17,7 @@ #include "clang/Driver/Compilation.h" #include "clang/Driver/Driver.h" #include "clang/Driver/Tool.h" +#include "clang/Driver/ToolChain.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendDiagnostic.h" @@ -31,13 +32,6 @@ #include "llvm/Support/Host.h" #include "llvm/Support/raw_ostream.h" -// For chdir, see the comment in ClangTool::run for more information. -#ifdef LLVM_ON_WIN32 -# include <direct.h> -#else -# include <unistd.h> -#endif - #define DEBUG_TYPE "clang-tooling" namespace clang { @@ -52,10 +46,11 @@ FrontendActionFactory::~FrontendActionFactory() {} // it to be based on the same framework. /// \brief Builds a clang driver initialized for running clang tools. -static clang::driver::Driver *newDriver(clang::DiagnosticsEngine *Diagnostics, - const char *BinaryName) { +static clang::driver::Driver *newDriver( + clang::DiagnosticsEngine *Diagnostics, const char *BinaryName, + IntrusiveRefCntPtr<vfs::FileSystem> VFS) { clang::driver::Driver *CompilerDriver = new clang::driver::Driver( - BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics); + BinaryName, llvm::sys::getDefaultTargetTriple(), *Diagnostics, VFS); CompilerDriver->setTitle("clang_based_tool"); return CompilerDriver; } @@ -130,18 +125,25 @@ bool runToolOnCodeWithArgs( SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); + llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( + new vfs::OverlayFileSystem(vfs::getRealFileSystem())); + llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( + new vfs::InMemoryFileSystem); + OverlayFileSystem->pushOverlay(InMemoryFileSystem); llvm::IntrusiveRefCntPtr<FileManager> Files( - new FileManager(FileSystemOptions())); + new FileManager(FileSystemOptions(), OverlayFileSystem)); ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), ToolAction, Files.get(), PCHContainerOps); SmallString<1024> CodeStorage; - Invocation.mapVirtualFile(FileNameRef, - Code.toNullTerminatedStringRef(CodeStorage)); + InMemoryFileSystem->addFile(FileNameRef, 0, + llvm::MemoryBuffer::getMemBuffer( + Code.toNullTerminatedStringRef(CodeStorage))); for (auto &FilenameWithContent : VirtualMappedFiles) { - Invocation.mapVirtualFile(FilenameWithContent.first, - FilenameWithContent.second); + InMemoryFileSystem->addFile( + FilenameWithContent.first, 0, + llvm::MemoryBuffer::getMemBuffer(FilenameWithContent.second)); } return Invocation.run(); @@ -162,6 +164,31 @@ std::string getAbsolutePath(StringRef File) { return AbsolutePath.str(); } +void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, + StringRef InvokedAs) { + if (!CommandLine.empty() && !InvokedAs.empty()) { + bool AlreadyHasTarget = false; + bool AlreadyHasMode = false; + // Skip CommandLine[0]. + for (auto Token = ++CommandLine.begin(); Token != CommandLine.end(); + ++Token) { + StringRef TokenRef(*Token); + AlreadyHasTarget |= + (TokenRef == "-target" || TokenRef.startswith("-target=")); + AlreadyHasMode |= (TokenRef == "--driver-mode" || + TokenRef.startswith("--driver-mode=")); + } + auto TargetMode = + clang::driver::ToolChain::getTargetAndModeFromProgramName(InvokedAs); + if (!AlreadyHasMode && !TargetMode.second.empty()) { + CommandLine.insert(++CommandLine.begin(), TargetMode.second); + } + if (!AlreadyHasTarget && !TargetMode.first.empty()) { + CommandLine.insert(++CommandLine.begin(), {"-target", TargetMode.first}); + } + } +} + namespace { class SingleFrontendActionFactory : public FrontendActionFactory { @@ -212,7 +239,7 @@ bool ToolInvocation::run() { DiagConsumer ? DiagConsumer : &DiagnosticPrinter, false); const std::unique_ptr<clang::driver::Driver> Driver( - newDriver(&Diagnostics, BinaryName)); + newDriver(&Diagnostics, BinaryName, Files->getVirtualFileSystem())); // Since the input might only be virtual, don't check whether it exists. Driver->setCheckInputsExist(false); const std::unique_ptr<clang::driver::Compilation> Compilation( @@ -224,6 +251,7 @@ bool ToolInvocation::run() { } std::unique_ptr<clang::CompilerInvocation> Invocation( newInvocation(&Diagnostics, *CC1Args)); + // FIXME: remove this when all users have migrated! for (const auto &It : MappedFileContents) { // Inject the code as the given file name into the preprocessor options. std::unique_ptr<llvm::MemoryBuffer> Input = @@ -282,7 +310,11 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations, std::shared_ptr<PCHContainerOperations> PCHContainerOps) : Compilations(Compilations), SourcePaths(SourcePaths), PCHContainerOps(PCHContainerOps), - Files(new FileManager(FileSystemOptions())), DiagConsumer(nullptr) { + OverlayFileSystem(new vfs::OverlayFileSystem(vfs::getRealFileSystem())), + InMemoryFileSystem(new vfs::InMemoryFileSystem), + Files(new FileManager(FileSystemOptions(), OverlayFileSystem)), + DiagConsumer(nullptr) { + OverlayFileSystem->pushOverlay(InMemoryFileSystem); appendArgumentsAdjuster(getClangStripOutputAdjuster()); appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster()); } @@ -320,6 +352,16 @@ int ClangTool::run(ToolAction *Action) { if (std::error_code EC = llvm::sys::fs::current_path(InitialDirectory)) llvm::report_fatal_error("Cannot detect current path: " + Twine(EC.message())); + + // First insert all absolute paths into the in-memory VFS. These are global + // for all compile commands. + if (SeenWorkingDirectories.insert("/").second) + for (const auto &MappedFile : MappedFileContents) + if (llvm::sys::path::is_absolute(MappedFile.first)) + InMemoryFileSystem->addFile( + MappedFile.first, 0, + llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); + bool ProcessingFailed = false; for (const auto &SourcePath : SourcePaths) { std::string File(getAbsolutePath(SourcePath)); @@ -350,12 +392,24 @@ int ClangTool::run(ToolAction *Action) { // difference for example on network filesystems, where symlinks might be // switched during runtime of the tool. Fixing this depends on having a // file system abstraction that allows openat() style interactions. - if (chdir(CompileCommand.Directory.c_str())) + if (OverlayFileSystem->setCurrentWorkingDirectory( + CompileCommand.Directory)) llvm::report_fatal_error("Cannot chdir into \"" + Twine(CompileCommand.Directory) + "\n!"); + + // Now fill the in-memory VFS with the relative file mappings so it will + // have the correct relative paths. We never remove mappings but that + // should be fine. + if (SeenWorkingDirectories.insert(CompileCommand.Directory).second) + for (const auto &MappedFile : MappedFileContents) + if (!llvm::sys::path::is_absolute(MappedFile.first)) + InMemoryFileSystem->addFile( + MappedFile.first, 0, + llvm::MemoryBuffer::getMemBuffer(MappedFile.second)); + std::vector<std::string> CommandLine = CompileCommand.CommandLine; if (ArgsAdjuster) - CommandLine = ArgsAdjuster(CommandLine); + CommandLine = ArgsAdjuster(CommandLine, CompileCommand.Filename); assert(!CommandLine.empty()); CommandLine[0] = MainExecutable; // FIXME: We need a callback mechanism for the tool writer to output a @@ -364,8 +418,7 @@ int ClangTool::run(ToolAction *Action) { ToolInvocation Invocation(std::move(CommandLine), Action, Files.get(), PCHContainerOps); Invocation.setDiagnosticConsumer(DiagConsumer); - for (const auto &MappedFile : MappedFileContents) - Invocation.mapVirtualFile(MappedFile.first, MappedFile.second); + if (!Invocation.run()) { // FIXME: Diagnostics should be used instead. llvm::errs() << "Error while processing " << File << ".\n"; @@ -373,7 +426,7 @@ int ClangTool::run(ToolAction *Action) { } // Return to the initial directory to correctly resolve next file by // relative path. - if (chdir(InitialDirectory.c_str())) + if (OverlayFileSystem->setCurrentWorkingDirectory(InitialDirectory.c_str())) llvm::report_fatal_error("Cannot chdir into \"" + Twine(InitialDirectory) + "\n!"); } @@ -392,12 +445,12 @@ public: bool runInvocation(CompilerInvocation *Invocation, FileManager *Files, std::shared_ptr<PCHContainerOperations> PCHContainerOps, DiagnosticConsumer *DiagConsumer) override { - // FIXME: This should use the provided FileManager. std::unique_ptr<ASTUnit> AST = ASTUnit::LoadFromCompilerInvocation( Invocation, PCHContainerOps, CompilerInstance::createDiagnostics(&Invocation->getDiagnosticOpts(), DiagConsumer, - /*ShouldOwnClient=*/false)); + /*ShouldOwnClient=*/false), + Files); if (!AST) return false; @@ -429,12 +482,20 @@ std::unique_ptr<ASTUnit> buildASTFromCodeWithArgs( std::vector<std::unique_ptr<ASTUnit>> ASTs; ASTBuilderAction Action(ASTs); + llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem( + new vfs::OverlayFileSystem(vfs::getRealFileSystem())); + llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem( + new vfs::InMemoryFileSystem); + OverlayFileSystem->pushOverlay(InMemoryFileSystem); + llvm::IntrusiveRefCntPtr<FileManager> Files( + new FileManager(FileSystemOptions(), OverlayFileSystem)); ToolInvocation Invocation(getSyntaxOnlyToolArgs(Args, FileNameRef), &Action, - nullptr, PCHContainerOps); + Files.get(), PCHContainerOps); SmallString<1024> CodeStorage; - Invocation.mapVirtualFile(FileNameRef, - Code.toNullTerminatedStringRef(CodeStorage)); + InMemoryFileSystem->addFile(FileNameRef, 0, + llvm::MemoryBuffer::getMemBuffer( + Code.toNullTerminatedStringRef(CodeStorage))); if (!Invocation.run()) return nullptr; |