diff options
Diffstat (limited to 'include/clang/Tooling')
-rw-r--r-- | include/clang/Tooling/ArgumentsAdjusters.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/CommonOptionsParser.h | 22 | ||||
-rw-r--r-- | include/clang/Tooling/CompilationDatabase.h | 10 | ||||
-rw-r--r-- | include/clang/Tooling/Core/Lookup.h | 48 | ||||
-rw-r--r-- | include/clang/Tooling/Core/Replacement.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/JSONCompilationDatabase.h | 31 | ||||
-rw-r--r-- | include/clang/Tooling/Tooling.h | 27 |
7 files changed, 137 insertions, 13 deletions
diff --git a/include/clang/Tooling/ArgumentsAdjusters.h b/include/clang/Tooling/ArgumentsAdjusters.h index a92e02142005..1fd7be688761 100644 --- a/include/clang/Tooling/ArgumentsAdjusters.h +++ b/include/clang/Tooling/ArgumentsAdjusters.h @@ -17,6 +17,8 @@ #ifndef LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H #define LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H +#include "clang/Basic/LLVM.h" +#include "llvm/ADT/StringRef.h" #include <functional> #include <string> #include <vector> @@ -31,8 +33,8 @@ typedef std::vector<std::string> CommandLineArguments; /// /// Command line argument adjuster is responsible for command line arguments /// modification before the arguments are used to run a frontend action. -typedef std::function<CommandLineArguments(const CommandLineArguments &)> - ArgumentsAdjuster; +typedef std::function<CommandLineArguments( + const CommandLineArguments &, StringRef Filename)> ArgumentsAdjuster; /// \brief Gets an argument adjuster that converts input command line arguments /// to the "syntax check only" variant. diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h index c23dc9211dc8..1e8462c631c3 100644 --- a/include/clang/Tooling/CommonOptionsParser.h +++ b/include/clang/Tooling/CommonOptionsParser.h @@ -42,6 +42,7 @@ namespace tooling { /// \code /// #include "clang/Frontend/FrontendActions.h" /// #include "clang/Tooling/CommonOptionsParser.h" +/// #include "clang/Tooling/Tooling.h" /// #include "llvm/Support/CommandLine.h" /// /// using namespace clang::tooling; @@ -56,8 +57,8 @@ namespace tooling { /// int main(int argc, const char **argv) { /// CommonOptionsParser OptionsParser(argc, argv, MyToolCategory); /// ClangTool Tool(OptionsParser.getCompilations(), -/// OptionsParser.getSourcePathListi()); -/// return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); +/// OptionsParser.getSourcePathList()); +/// return Tool.run(newFrontendActionFactory<SyntaxOnlyAction>().get()); /// } /// \endcode class CommonOptionsParser { @@ -72,6 +73,23 @@ public: /// This constructor exits program in case of error. CommonOptionsParser(int &argc, const char **argv, llvm::cl::OptionCategory &Category, + const char *Overview = nullptr) + : CommonOptionsParser(argc, argv, Category, llvm::cl::OneOrMore, + Overview) {} + + /// \brief Parses command-line, initializes a compilation database. + /// + /// This constructor can change argc and argv contents, e.g. consume + /// command-line options used for creating FixedCompilationDatabase. + /// + /// All options not belonging to \p Category become hidden. + /// + /// I also allows calls to set the required number of positional parameters. + /// + /// This constructor exits program in case of error. + CommonOptionsParser(int &argc, const char **argv, + llvm::cl::OptionCategory &Category, + llvm::cl::NumOccurrencesFlag OccurrencesFlag, const char *Overview = nullptr); /// Returns a reference to the loaded compilations database. diff --git a/include/clang/Tooling/CompilationDatabase.h b/include/clang/Tooling/CompilationDatabase.h index e5b95af3aef4..08a0ffec9d63 100644 --- a/include/clang/Tooling/CompilationDatabase.h +++ b/include/clang/Tooling/CompilationDatabase.h @@ -42,12 +42,18 @@ namespace tooling { /// \brief Specifies the working directory and command of a compilation. struct CompileCommand { CompileCommand() {} - CompileCommand(Twine Directory, std::vector<std::string> CommandLine) - : Directory(Directory.str()), CommandLine(std::move(CommandLine)) {} + CompileCommand(Twine Directory, Twine Filename, + std::vector<std::string> CommandLine) + : Directory(Directory.str()), + Filename(Filename.str()), + CommandLine(std::move(CommandLine)) {} /// \brief The working directory the command was executed from. std::string Directory; + /// The source file associated with the command. + std::string Filename; + /// \brief The command line that was executed. std::vector<std::string> CommandLine; diff --git a/include/clang/Tooling/Core/Lookup.h b/include/clang/Tooling/Core/Lookup.h new file mode 100644 index 000000000000..bc2b4db383cf --- /dev/null +++ b/include/clang/Tooling/Core/Lookup.h @@ -0,0 +1,48 @@ +//===--- Lookup.h - Framework for clang refactoring tools --*- C++ -*------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLING_CORE_LOOKUP_H +#define LLVM_CLANG_TOOLING_CORE_LOOKUP_H + +#include "clang/Basic/LLVM.h" +#include <string> + +namespace clang { + +class DeclContext; +class NamedDecl; +class NestedNameSpecifier; + +namespace tooling { + +/// Emulate a lookup to replace one nested name specifier with another using as +/// few additional namespace qualifications as possible. +/// +/// This does not perform a full C++ lookup so ADL will not work. +/// +/// \param Use The nested name to be replaced. +/// \param UseContext The context in which the nested name is contained. This +/// will be used to minimize namespace qualifications. +/// \param FromDecl The declaration to which the nested name points. +/// \param ReplacementString The replacement nested name. Must be fully +/// qualified including a leading "::". +/// \returns The new name to be inserted in place of the current nested name. +std::string replaceNestedName(const NestedNameSpecifier *Use, + const DeclContext *UseContext, + const NamedDecl *FromDecl, + StringRef ReplacementString); + +} // end namespace tooling +} // end namespace clang + +#endif // LLVM_CLANG_TOOLING_CORE_LOOKUP_H diff --git a/include/clang/Tooling/Core/Replacement.h b/include/clang/Tooling/Core/Replacement.h index f189e1250121..37389ac91566 100644 --- a/include/clang/Tooling/Core/Replacement.h +++ b/include/clang/Tooling/Core/Replacement.h @@ -220,6 +220,12 @@ bool applyAllReplacements(const std::vector<Replacement> &Replaces, /// replacements cannot be applied, this returns an empty \c string. std::string applyAllReplacements(StringRef Code, const Replacements &Replaces); +/// \brief Merges two sets of replacements with the second set referring to the +/// code after applying the first set. Within both 'First' and 'Second', +/// replacements must not overlap. +Replacements mergeReplacements(const Replacements &First, + const Replacements &Second); + template <typename Node> Replacement::Replacement(const SourceManager &Sources, const Node &NodeToReplace, StringRef ReplacementText, diff --git a/include/clang/Tooling/JSONCompilationDatabase.h b/include/clang/Tooling/JSONCompilationDatabase.h index b4edc31652f7..2a13fc155cea 100644 --- a/include/clang/Tooling/JSONCompilationDatabase.h +++ b/include/clang/Tooling/JSONCompilationDatabase.h @@ -33,18 +33,26 @@ namespace tooling { /// \brief A JSON based compilation database. /// /// JSON compilation database files must contain a list of JSON objects which -/// provide the command lines in the attributes 'directory', 'command' and -/// 'file': +/// provide the command lines in the attributes 'directory', 'command', +/// 'arguments' and 'file': /// [ /// { "directory": "<working directory of the compile>", /// "command": "<compile command line>", /// "file": "<path to source file>" /// }, +/// { "directory": "<working directory of the compile>", +/// "arguments": ["<raw>", "<command>" "<line>" "<parameters>"], +/// "file": "<path to source file>" +/// }, /// ... /// ] /// Each object entry defines one compile action. The specified file is /// considered to be the main source file for the translation unit. /// +/// 'command' is a full command line that will be unescaped. +/// +/// 'arguments' is a list of command line arguments that will not be unescaped. +/// /// JSON compilation databases can for example be generated in CMake projects /// by setting the flag -DCMAKE_EXPORT_COMPILE_COMMANDS. class JSONCompilationDatabase : public CompilationDatabase { @@ -91,17 +99,26 @@ private: /// failed. bool parse(std::string &ErrorMessage); - // Tuple (directory, commandline) where 'commandline' pointing to the - // corresponding nodes in the YAML stream. - typedef std::pair<llvm::yaml::ScalarNode*, - llvm::yaml::ScalarNode*> CompileCommandRef; + // Tuple (directory, filename, commandline) where 'commandline' points to the + // corresponding scalar nodes in the YAML stream. + // If the command line contains a single argument, it is a shell-escaped + // command line. + // Otherwise, each entry in the command line vector is a literal + // argument to the compiler. + typedef std::tuple<llvm::yaml::ScalarNode *, + llvm::yaml::ScalarNode *, + std::vector<llvm::yaml::ScalarNode *>> CompileCommandRef; /// \brief Converts the given array of CompileCommandRefs to CompileCommands. void getCommands(ArrayRef<CompileCommandRef> CommandsRef, std::vector<CompileCommand> &Commands) const; // Maps file paths to the compile command lines for that file. - llvm::StringMap< std::vector<CompileCommandRef> > IndexByFile; + llvm::StringMap<std::vector<CompileCommandRef>> IndexByFile; + + /// All the compile commands in the order that they were provided in the + /// JSON stream. + std::vector<CompileCommandRef> AllCommands; FileMatchTrie MatchTrie; diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 92e9065c6a8a..b7a9b25acd0e 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -243,6 +243,7 @@ public: /// /// \param FilePath The path at which the content will be mapped. /// \param Content A null terminated buffer of the file's content. + // FIXME: remove this when all users have migrated! void mapVirtualFile(StringRef FilePath, StringRef Content); /// \brief Run the clang invocation. @@ -331,9 +332,12 @@ class ClangTool { std::vector<std::string> SourcePaths; std::shared_ptr<PCHContainerOperations> PCHContainerOps; + llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> OverlayFileSystem; + llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem; llvm::IntrusiveRefCntPtr<FileManager> Files; // Contains a list of pairs (<file name>, <file content>). std::vector< std::pair<StringRef, StringRef> > MappedFileContents; + llvm::StringSet<> SeenWorkingDirectories; ArgumentsAdjuster ArgsAdjuster; @@ -417,6 +421,29 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( /// \param File Either an absolute or relative path. std::string getAbsolutePath(StringRef File); +/// \brief Changes CommandLine to contain implicit flags that would have been +/// defined had the compiler driver been invoked through the path InvokedAs. +/// +/// For example, when called with \c InvokedAs set to `i686-linux-android-g++`, +/// the arguments '-target', 'i686-linux-android`, `--driver-mode=g++` will +/// be inserted after the first argument in \c CommandLine. +/// +/// This function will not add new `-target` or `--driver-mode` flags if they +/// are already present in `CommandLine` (even if they have different settings +/// than would have been inserted). +/// +/// \pre `llvm::InitializeAllTargets()` has been called. +/// +/// \param CommandLine the command line used to invoke the compiler driver or +/// Clang tool, including the path to the executable as \c CommandLine[0]. +/// \param InvokedAs the path to the driver used to infer implicit flags. +/// +/// \note This will not set \c CommandLine[0] to \c InvokedAs. The tooling +/// infrastructure expects that CommandLine[0] is a tool path relative to which +/// the builtin headers can be found. +void addTargetAndModeForProgramName(std::vector<std::string> &CommandLine, + StringRef InvokedAs); + /// \brief Creates a \c CompilerInvocation. clang::CompilerInvocation *newInvocation( clang::DiagnosticsEngine *Diagnostics, |