diff options
Diffstat (limited to 'include/clang/Tooling')
-rw-r--r-- | include/clang/Tooling/ArgumentsAdjusters.h | 68 | ||||
-rw-r--r-- | include/clang/Tooling/CommonOptionsParser.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/CompilationDatabase.h | 22 | ||||
-rw-r--r-- | include/clang/Tooling/CompilationDatabasePluginRegistry.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/Core/Replacement.h | 229 | ||||
-rw-r--r-- | include/clang/Tooling/FileMatchTrie.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/JSONCompilationDatabase.h | 19 | ||||
-rw-r--r-- | include/clang/Tooling/Refactoring.h | 176 | ||||
-rw-r--r-- | include/clang/Tooling/RefactoringCallbacks.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/ReplacementsYaml.h | 6 | ||||
-rw-r--r-- | include/clang/Tooling/Tooling.h | 42 |
11 files changed, 325 insertions, 261 deletions
diff --git a/include/clang/Tooling/ArgumentsAdjusters.h b/include/clang/Tooling/ArgumentsAdjusters.h index 765e7d2e051d3..a92e021420059 100644 --- a/include/clang/Tooling/ArgumentsAdjusters.h +++ b/include/clang/Tooling/ArgumentsAdjusters.h @@ -7,59 +7,61 @@ // //===----------------------------------------------------------------------===// // -// This file declares base abstract class ArgumentsAdjuster and its descendants. -// These classes are intended to modify command line arguments obtained from -// a compilation database before they are used to run a frontend action. +// This file declares typedef ArgumentsAdjuster and functions to create several +// useful argument adjusters. +// ArgumentsAdjusters modify command line arguments obtained from a compilation +// database before they are used to run a frontend action. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H #define LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H +#include <functional> #include <string> #include <vector> namespace clang { - namespace tooling { /// \brief A sequence of command line arguments. typedef std::vector<std::string> CommandLineArguments; -/// \brief Abstract interface for a command line adjusters. +/// \brief A prototype of a command line adjuster. /// -/// This abstract interface describes a command line argument adjuster, -/// which is responsible for command line arguments modification before -/// the arguments are used to run a frontend action. -class ArgumentsAdjuster { - virtual void anchor(); -public: - /// \brief Returns adjusted command line arguments. - /// - /// \param Args Input sequence of command line arguments. - /// - /// \returns Modified sequence of command line arguments. - virtual CommandLineArguments Adjust(const CommandLineArguments &Args) = 0; - virtual ~ArgumentsAdjuster() { - } -}; +/// 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; -/// \brief Syntax check only command line adjuster. -/// -/// This class implements ArgumentsAdjuster interface and converts input -/// command line arguments to the "syntax check only" variant. -class ClangSyntaxOnlyAdjuster : public ArgumentsAdjuster { - CommandLineArguments Adjust(const CommandLineArguments &Args) override; -}; +/// \brief Gets an argument adjuster that converts input command line arguments +/// to the "syntax check only" variant. +ArgumentsAdjuster getClangSyntaxOnlyAdjuster(); -/// \brief An argument adjuster which removes output-related command line +/// \brief Gets an argument adjuster which removes output-related command line /// arguments. -class ClangStripOutputAdjuster : public ArgumentsAdjuster { - CommandLineArguments Adjust(const CommandLineArguments &Args) override; -}; +ArgumentsAdjuster getClangStripOutputAdjuster(); + +enum class ArgumentInsertPosition { BEGIN, END }; + +/// \brief Gets an argument adjuster which inserts \p Extra arguments in the +/// specified position. +ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra, + ArgumentInsertPosition Pos); + +/// \brief Gets an argument adjuster which inserts an \p Extra argument in the +/// specified position. +ArgumentsAdjuster getInsertArgumentAdjuster( + const char *Extra, + ArgumentInsertPosition Pos = ArgumentInsertPosition::END); + +/// \brief Gets an argument adjuster which adjusts the arguments in sequence +/// with the \p First adjuster and then with the \p Second one. +ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First, + ArgumentsAdjuster Second); -} // end namespace tooling -} // end namespace clang +} // namespace tooling +} // namespace clang #endif // LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h index 815ede80c2339..c23dc9211dc89 100644 --- a/include/clang/Tooling/CommonOptionsParser.h +++ b/include/clang/Tooling/CommonOptionsParser.h @@ -24,8 +24,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H -#define LLVM_TOOLS_CLANG_INCLUDE_CLANG_TOOLING_COMMONOPTIONSPARSER_H +#ifndef LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H +#define LLVM_CLANG_TOOLING_COMMONOPTIONSPARSER_H #include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/CommandLine.h" @@ -89,6 +89,8 @@ public: private: std::unique_ptr<CompilationDatabase> Compilations; std::vector<std::string> SourcePathList; + std::vector<std::string> ExtraArgsBefore; + std::vector<std::string> ExtraArgsAfter; }; } // namespace tooling diff --git a/include/clang/Tooling/CompilationDatabase.h b/include/clang/Tooling/CompilationDatabase.h index d1e729a88b27b..27c16524e62ee 100644 --- a/include/clang/Tooling/CompilationDatabase.h +++ b/include/clang/Tooling/CompilationDatabase.h @@ -25,8 +25,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_COMPILATION_DATABASE_H -#define LLVM_CLANG_TOOLING_COMPILATION_DATABASE_H +#ifndef LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H +#define LLVM_CLANG_TOOLING_COMPILATIONDATABASE_H #include "clang/Basic/LLVM.h" #include "llvm/ADT/ArrayRef.h" @@ -84,22 +84,22 @@ public: /// FIXME: Currently only supports JSON compilation databases, which /// are named 'compile_commands.json' in the given directory. Extend this /// for other build types (like ninja build files). - static CompilationDatabase *loadFromDirectory(StringRef BuildDirectory, - std::string &ErrorMessage); + static std::unique_ptr<CompilationDatabase> + loadFromDirectory(StringRef BuildDirectory, std::string &ErrorMessage); /// \brief Tries to detect a compilation database location and load it. /// /// Looks for a compilation database in all parent paths of file 'SourceFile' /// by calling loadFromDirectory. - static CompilationDatabase *autoDetectFromSource(StringRef SourceFile, - std::string &ErrorMessage); + static std::unique_ptr<CompilationDatabase> + autoDetectFromSource(StringRef SourceFile, std::string &ErrorMessage); /// \brief Tries to detect a compilation database location and load it. /// /// Looks for a compilation database in directory 'SourceDir' and all /// its parent paths by calling loadFromDirectory. - static CompilationDatabase *autoDetectFromDirectory(StringRef SourceDir, - std::string &ErrorMessage); + static std::unique_ptr<CompilationDatabase> + autoDetectFromDirectory(StringRef SourceDir, std::string &ErrorMessage); /// \brief Returns all compile commands in which the specified file was /// compiled. @@ -142,8 +142,8 @@ public: /// \brief Loads a compilation database from a build directory. /// /// \see CompilationDatabase::loadFromDirectory(). - virtual CompilationDatabase *loadFromDirectory(StringRef Directory, - std::string &ErrorMessage) = 0; + virtual std::unique_ptr<CompilationDatabase> + loadFromDirectory(StringRef Directory, std::string &ErrorMessage) = 0; }; /// \brief A compilation database that returns a single compile command line. @@ -213,4 +213,4 @@ private: } // end namespace tooling } // end namespace clang -#endif // LLVM_CLANG_TOOLING_COMPILATION_DATABASE_H +#endif diff --git a/include/clang/Tooling/CompilationDatabasePluginRegistry.h b/include/clang/Tooling/CompilationDatabasePluginRegistry.h index 84fcd24b4a12b..7323ec8974036 100644 --- a/include/clang/Tooling/CompilationDatabasePluginRegistry.h +++ b/include/clang/Tooling/CompilationDatabasePluginRegistry.h @@ -7,8 +7,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_COMPILATION_DATABASE_PLUGIN_REGISTRY_H -#define LLVM_CLANG_TOOLING_COMPILATION_DATABASE_PLUGIN_REGISTRY_H +#ifndef LLVM_CLANG_TOOLING_COMPILATIONDATABASEPLUGINREGISTRY_H +#define LLVM_CLANG_TOOLING_COMPILATIONDATABASEPLUGINREGISTRY_H #include "clang/Tooling/CompilationDatabase.h" #include "llvm/Support/Registry.h" @@ -24,4 +24,4 @@ typedef llvm::Registry<CompilationDatabasePlugin> } // end namespace tooling } // end namespace clang -#endif // LLVM_CLANG_TOOLING_COMPILATION_DATABASE_PLUGIN_REGISTRY_H +#endif diff --git a/include/clang/Tooling/Core/Replacement.h b/include/clang/Tooling/Core/Replacement.h new file mode 100644 index 0000000000000..30a7036c2bb53 --- /dev/null +++ b/include/clang/Tooling/Core/Replacement.h @@ -0,0 +1,229 @@ +//===--- Replacement.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. +// +//===----------------------------------------------------------------------===// +// +// Classes supporting refactorings that span multiple translation units. +// While single translation unit refactorings are supported via the Rewriter, +// when refactoring multiple translation units changes must be stored in a +// SourceManager independent form, duplicate changes need to be removed, and +// all changes must be applied at once at the end of the refactoring so that +// the code is always parseable. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CLANG_TOOLING_CORE_REPLACEMENT_H +#define LLVM_CLANG_TOOLING_CORE_REPLACEMENT_H + +#include "clang/Basic/SourceLocation.h" +#include "llvm/ADT/StringRef.h" +#include <set> +#include <string> +#include <vector> + +namespace clang { + +class Rewriter; + +namespace tooling { + +/// \brief A source range independent of the \c SourceManager. +class Range { +public: + Range() : Offset(0), Length(0) {} + Range(unsigned Offset, unsigned Length) : Offset(Offset), Length(Length) {} + + /// \brief Accessors. + /// @{ + unsigned getOffset() const { return Offset; } + unsigned getLength() const { return Length; } + /// @} + + /// \name Range Predicates + /// @{ + /// \brief Whether this range overlaps with \p RHS or not. + bool overlapsWith(Range RHS) const { + return Offset + Length > RHS.Offset && Offset < RHS.Offset + RHS.Length; + } + + /// \brief Whether this range contains \p RHS or not. + bool contains(Range RHS) const { + return RHS.Offset >= Offset && + (RHS.Offset + RHS.Length) <= (Offset + Length); + } + /// @} + +private: + unsigned Offset; + unsigned Length; +}; + +/// \brief A text replacement. +/// +/// Represents a SourceManager independent replacement of a range of text in a +/// specific file. +class Replacement { +public: + /// \brief Creates an invalid (not applicable) replacement. + Replacement(); + + /// \brief Creates a replacement of the range [Offset, Offset+Length) in + /// FilePath with ReplacementText. + /// + /// \param FilePath A source file accessible via a SourceManager. + /// \param Offset The byte offset of the start of the range in the file. + /// \param Length The length of the range in bytes. + Replacement(StringRef FilePath, unsigned Offset, + unsigned Length, StringRef ReplacementText); + + /// \brief Creates a Replacement of the range [Start, Start+Length) with + /// ReplacementText. + Replacement(const SourceManager &Sources, SourceLocation Start, unsigned Length, + StringRef ReplacementText); + + /// \brief Creates a Replacement of the given range with ReplacementText. + Replacement(const SourceManager &Sources, const CharSourceRange &Range, + StringRef ReplacementText); + + /// \brief Creates a Replacement of the node with ReplacementText. + template <typename Node> + Replacement(const SourceManager &Sources, const Node &NodeToReplace, + StringRef ReplacementText); + + /// \brief Returns whether this replacement can be applied to a file. + /// + /// Only replacements that are in a valid file can be applied. + bool isApplicable() const; + + /// \brief Accessors. + /// @{ + StringRef getFilePath() const { return FilePath; } + unsigned getOffset() const { return ReplacementRange.getOffset(); } + unsigned getLength() const { return ReplacementRange.getLength(); } + StringRef getReplacementText() const { return ReplacementText; } + /// @} + + /// \brief Applies the replacement on the Rewriter. + bool apply(Rewriter &Rewrite) const; + + /// \brief Returns a human readable string representation. + std::string toString() const; + + private: + void setFromSourceLocation(const SourceManager &Sources, SourceLocation Start, + unsigned Length, StringRef ReplacementText); + void setFromSourceRange(const SourceManager &Sources, + const CharSourceRange &Range, + StringRef ReplacementText); + + std::string FilePath; + Range ReplacementRange; + std::string ReplacementText; +}; + +/// \brief Less-than operator between two Replacements. +bool operator<(const Replacement &LHS, const Replacement &RHS); + +/// \brief Equal-to operator between two Replacements. +bool operator==(const Replacement &LHS, const Replacement &RHS); + +/// \brief A set of Replacements. +/// FIXME: Change to a vector and deduplicate in the RefactoringTool. +typedef std::set<Replacement> Replacements; + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply. false otherwise. +bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite); + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply. false otherwise. +bool applyAllReplacements(const std::vector<Replacement> &Replaces, + Rewriter &Rewrite); + +/// \brief Applies all replacements in \p Replaces to \p Code. +/// +/// This completely ignores the path stored in each replacement. If one or more +/// replacements cannot be applied, this returns an empty \c string. +std::string applyAllReplacements(StringRef Code, const Replacements &Replaces); + +/// \brief Calculates how a code \p Position is shifted when \p Replaces are +/// applied. +unsigned shiftedCodePosition(const Replacements& Replaces, unsigned Position); + +/// \brief Calculates how a code \p Position is shifted when \p Replaces are +/// applied. +/// +/// \pre Replaces[i].getOffset() <= Replaces[i+1].getOffset(). +unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces, + unsigned Position); + +/// \brief Removes duplicate Replacements and reports if Replacements conflict +/// with one another. All Replacements are assumed to be in the same file. +/// +/// \post Replaces[i].getOffset() <= Replaces[i+1].getOffset(). +/// +/// This function sorts \p Replaces so that conflicts can be reported simply by +/// offset into \p Replaces and number of elements in the conflict. +void deduplicate(std::vector<Replacement> &Replaces, + std::vector<Range> &Conflicts); + +/// \brief Collection of Replacements generated from a single translation unit. +struct TranslationUnitReplacements { + /// Name of the main source for the translation unit. + std::string MainSourceFile; + + /// A freeform chunk of text to describe the context of the replacements. + /// Will be printed, for example, when detecting conflicts during replacement + /// deduplication. + std::string Context; + + std::vector<Replacement> Replacements; +}; + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply. false otherwise. +bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite); + +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. +/// +/// \returns true if all replacements apply. false otherwise. +bool applyAllReplacements(const std::vector<Replacement> &Replaces, + Rewriter &Rewrite); + +/// \brief Applies all replacements in \p Replaces to \p Code. +/// +/// This completely ignores the path stored in each replacement. If one or more +/// replacements cannot be applied, this returns an empty \c string. +std::string applyAllReplacements(StringRef Code, const Replacements &Replaces); + +template <typename Node> +Replacement::Replacement(const SourceManager &Sources, + const Node &NodeToReplace, StringRef ReplacementText) { + const CharSourceRange Range = + CharSourceRange::getTokenRange(NodeToReplace->getSourceRange()); + setFromSourceRange(Sources, Range, ReplacementText); +} + +} // end namespace tooling +} // end namespace clang + +#endif // LLVM_CLANG_TOOLING_CORE_REPLACEMENT_H diff --git a/include/clang/Tooling/FileMatchTrie.h b/include/clang/Tooling/FileMatchTrie.h index be37baff2ffce..745c1645068a4 100644 --- a/include/clang/Tooling/FileMatchTrie.h +++ b/include/clang/Tooling/FileMatchTrie.h @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_FILE_MATCH_TRIE_H -#define LLVM_CLANG_TOOLING_FILE_MATCH_TRIE_H +#ifndef LLVM_CLANG_TOOLING_FILEMATCHTRIE_H +#define LLVM_CLANG_TOOLING_FILEMATCHTRIE_H #include "clang/Basic/LLVM.h" #include "llvm/ADT/StringRef.h" @@ -86,4 +86,4 @@ private: } // end namespace tooling } // end namespace clang -#endif // LLVM_CLANG_TOOLING_FILE_MATCH_TRIE_H +#endif diff --git a/include/clang/Tooling/JSONCompilationDatabase.h b/include/clang/Tooling/JSONCompilationDatabase.h index 1b3335968594e..b4edc31652f78 100644 --- a/include/clang/Tooling/JSONCompilationDatabase.h +++ b/include/clang/Tooling/JSONCompilationDatabase.h @@ -12,8 +12,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H -#define LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H +#ifndef LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H +#define LLVM_CLANG_TOOLING_JSONCOMPILATIONDATABASE_H #include "clang/Basic/LLVM.h" #include "clang/Tooling/CompilationDatabase.h" @@ -53,14 +53,14 @@ public: /// /// Returns NULL and sets ErrorMessage if the database could not be /// loaded from the given file. - static JSONCompilationDatabase *loadFromFile(StringRef FilePath, - std::string &ErrorMessage); + static std::unique_ptr<JSONCompilationDatabase> + loadFromFile(StringRef FilePath, std::string &ErrorMessage); /// \brief Loads a JSON compilation database from a data buffer. /// /// Returns NULL and sets ErrorMessage if the database could not be loaded. - static JSONCompilationDatabase *loadFromBuffer(StringRef DatabaseString, - std::string &ErrorMessage); + static std::unique_ptr<JSONCompilationDatabase> + loadFromBuffer(StringRef DatabaseString, std::string &ErrorMessage); /// \brief Returns all compile comamnds in which the specified file was /// compiled. @@ -81,8 +81,9 @@ public: private: /// \brief Constructs a JSON compilation database on a memory buffer. - JSONCompilationDatabase(llvm::MemoryBuffer *Database) - : Database(Database), YAMLStream(Database->getBuffer(), SM) {} + JSONCompilationDatabase(std::unique_ptr<llvm::MemoryBuffer> Database) + : Database(std::move(Database)), + YAMLStream(this->Database->getBuffer(), SM) {} /// \brief Parses the database file and creates the index. /// @@ -112,4 +113,4 @@ private: } // end namespace tooling } // end namespace clang -#endif // LLVM_CLANG_TOOLING_JSON_COMPILATION_DATABASE_H +#endif diff --git a/include/clang/Tooling/Refactoring.h b/include/clang/Tooling/Refactoring.h index cd2fb9f6c563c..e3e7f83c38c83 100644 --- a/include/clang/Tooling/Refactoring.h +++ b/include/clang/Tooling/Refactoring.h @@ -19,180 +19,16 @@ #ifndef LLVM_CLANG_TOOLING_REFACTORING_H #define LLVM_CLANG_TOOLING_REFACTORING_H -#include "clang/Basic/SourceLocation.h" +#include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Tooling.h" -#include "llvm/ADT/StringRef.h" -#include <set> #include <string> namespace clang { class Rewriter; -class SourceLocation; namespace tooling { -/// \brief A source range independent of the \c SourceManager. -class Range { -public: - Range() : Offset(0), Length(0) {} - Range(unsigned Offset, unsigned Length) : Offset(Offset), Length(Length) {} - - /// \brief Accessors. - /// @{ - unsigned getOffset() const { return Offset; } - unsigned getLength() const { return Length; } - /// @} - - /// \name Range Predicates - /// @{ - /// \brief Whether this range overlaps with \p RHS or not. - bool overlapsWith(Range RHS) const { - return Offset + Length > RHS.Offset && Offset < RHS.Offset + RHS.Length; - } - - /// \brief Whether this range contains \p RHS or not. - bool contains(Range RHS) const { - return RHS.Offset >= Offset && - (RHS.Offset + RHS.Length) <= (Offset + Length); - } - /// @} - -private: - unsigned Offset; - unsigned Length; -}; - -/// \brief A text replacement. -/// -/// Represents a SourceManager independent replacement of a range of text in a -/// specific file. -class Replacement { -public: - /// \brief Creates an invalid (not applicable) replacement. - Replacement(); - - /// \brief Creates a replacement of the range [Offset, Offset+Length) in - /// FilePath with ReplacementText. - /// - /// \param FilePath A source file accessible via a SourceManager. - /// \param Offset The byte offset of the start of the range in the file. - /// \param Length The length of the range in bytes. - Replacement(StringRef FilePath, unsigned Offset, - unsigned Length, StringRef ReplacementText); - - /// \brief Creates a Replacement of the range [Start, Start+Length) with - /// ReplacementText. - Replacement(const SourceManager &Sources, SourceLocation Start, unsigned Length, - StringRef ReplacementText); - - /// \brief Creates a Replacement of the given range with ReplacementText. - Replacement(const SourceManager &Sources, const CharSourceRange &Range, - StringRef ReplacementText); - - /// \brief Creates a Replacement of the node with ReplacementText. - template <typename Node> - Replacement(const SourceManager &Sources, const Node &NodeToReplace, - StringRef ReplacementText); - - /// \brief Returns whether this replacement can be applied to a file. - /// - /// Only replacements that are in a valid file can be applied. - bool isApplicable() const; - - /// \brief Accessors. - /// @{ - StringRef getFilePath() const { return FilePath; } - unsigned getOffset() const { return ReplacementRange.getOffset(); } - unsigned getLength() const { return ReplacementRange.getLength(); } - StringRef getReplacementText() const { return ReplacementText; } - /// @} - - /// \brief Applies the replacement on the Rewriter. - bool apply(Rewriter &Rewrite) const; - - /// \brief Returns a human readable string representation. - std::string toString() const; - - private: - void setFromSourceLocation(const SourceManager &Sources, SourceLocation Start, - unsigned Length, StringRef ReplacementText); - void setFromSourceRange(const SourceManager &Sources, - const CharSourceRange &Range, - StringRef ReplacementText); - - std::string FilePath; - Range ReplacementRange; - std::string ReplacementText; -}; - -/// \brief Less-than operator between two Replacements. -bool operator<(const Replacement &LHS, const Replacement &RHS); - -/// \brief Equal-to operator between two Replacements. -bool operator==(const Replacement &LHS, const Replacement &RHS); - -/// \brief A set of Replacements. -/// FIXME: Change to a vector and deduplicate in the RefactoringTool. -typedef std::set<Replacement> Replacements; - -/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. -/// -/// Replacement applications happen independently of the success of -/// other applications. -/// -/// \returns true if all replacements apply. false otherwise. -bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite); - -/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. -/// -/// Replacement applications happen independently of the success of -/// other applications. -/// -/// \returns true if all replacements apply. false otherwise. -bool applyAllReplacements(const std::vector<Replacement> &Replaces, - Rewriter &Rewrite); - -/// \brief Applies all replacements in \p Replaces to \p Code. -/// -/// This completely ignores the path stored in each replacement. If one or more -/// replacements cannot be applied, this returns an empty \c string. -std::string applyAllReplacements(StringRef Code, const Replacements &Replaces); - -/// \brief Calculates how a code \p Position is shifted when \p Replaces are -/// applied. -unsigned shiftedCodePosition(const Replacements& Replaces, unsigned Position); - -/// \brief Calculates how a code \p Position is shifted when \p Replaces are -/// applied. -/// -/// \pre Replaces[i].getOffset() <= Replaces[i+1].getOffset(). -unsigned shiftedCodePosition(const std::vector<Replacement> &Replaces, - unsigned Position); - -/// \brief Removes duplicate Replacements and reports if Replacements conflict -/// with one another. -/// -/// \post Replaces[i].getOffset() <= Replaces[i+1].getOffset(). -/// -/// This function sorts \p Replaces so that conflicts can be reported simply by -/// offset into \p Replaces and number of elements in the conflict. -void deduplicate(std::vector<Replacement> &Replaces, - std::vector<Range> &Conflicts); - -/// \brief Collection of Replacements generated from a single translation unit. -struct TranslationUnitReplacements { - /// Name of the main source for the translation unit. - std::string MainSourceFile; - - /// A freeform chunk of text to describe the context of the replacements. - /// Will be printed, for example, when detecting conflicts during replacement - /// deduplication. - std::string Context; - - std::vector<Replacement> Replacements; -}; - /// \brief A tool to run refactorings. /// /// This is a refactoring specific version of \see ClangTool. FrontendActions @@ -230,15 +66,7 @@ private: Replacements Replace; }; -template <typename Node> -Replacement::Replacement(const SourceManager &Sources, - const Node &NodeToReplace, StringRef ReplacementText) { - const CharSourceRange Range = - CharSourceRange::getTokenRange(NodeToReplace->getSourceRange()); - setFromSourceRange(Sources, Range, ReplacementText); -} - } // end namespace tooling } // end namespace clang -#endif // end namespace LLVM_CLANG_TOOLING_REFACTORING_H +#endif // LLVM_CLANG_TOOLING_REFACTORING_H diff --git a/include/clang/Tooling/RefactoringCallbacks.h b/include/clang/Tooling/RefactoringCallbacks.h index 19f277431a3e2..6ef9ea11f0aed 100644 --- a/include/clang/Tooling/RefactoringCallbacks.h +++ b/include/clang/Tooling/RefactoringCallbacks.h @@ -26,8 +26,8 @@ // //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H -#define LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H +#ifndef LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H +#define LLVM_CLANG_TOOLING_REFACTORINGCALLBACKS_H #include "clang/ASTMatchers/ASTMatchFinder.h" #include "clang/Tooling/Refactoring.h" @@ -87,4 +87,4 @@ private: } // end namespace tooling } // end namespace clang -#endif // LLVM_CLANG_TOOLING_REFACTORING_CALLBACKS_H +#endif diff --git a/include/clang/Tooling/ReplacementsYaml.h b/include/clang/Tooling/ReplacementsYaml.h index ac9f46908ffe5..4a7666d2c555c 100644 --- a/include/clang/Tooling/ReplacementsYaml.h +++ b/include/clang/Tooling/ReplacementsYaml.h @@ -13,8 +13,8 @@ /// //===----------------------------------------------------------------------===// -#ifndef LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H -#define LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H +#ifndef LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H +#define LLVM_CLANG_TOOLING_REPLACEMENTSYAML_H #include "clang/Tooling/Refactoring.h" #include "llvm/Support/YAMLTraits.h" @@ -73,4 +73,4 @@ template <> struct MappingTraits<clang::tooling::TranslationUnitReplacements> { } // end namespace yaml } // end namespace llvm -#endif // LLVM_CLANG_TOOLING_REPLACEMENTS_YAML_H +#endif diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 769acd325367a..393cc1deace52 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -30,6 +30,7 @@ #ifndef LLVM_CLANG_TOOLING_TOOLING_H #define LLVM_CLANG_TOOLING_TOOLING_H +#include "clang/AST/ASTConsumer.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/LLVM.h" @@ -142,6 +143,10 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName = "input.cc"); +/// The first part of the pair is the filename, the second part the +/// file-content. +typedef std::vector<std::pair<std::string, std::string>> FileContentMappings; + /// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and /// with additional other flags. /// @@ -151,9 +156,10 @@ bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, /// \param FileName The file name which 'Code' will be mapped as. /// /// \return - True if 'ToolAction' was successfully executed. -bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, - const std::vector<std::string> &Args, - const Twine &FileName = "input.cc"); +bool runToolOnCodeWithArgs( + clang::FrontendAction *ToolAction, const Twine &Code, + const std::vector<std::string> &Args, const Twine &FileName = "input.cc", + const FileContentMappings &VirtualMappedFiles = FileContentMappings()); /// \brief Builds an AST for 'Code'. /// @@ -202,7 +208,9 @@ class ToolInvocation { ~ToolInvocation(); /// \brief Set a \c DiagnosticConsumer to use during parsing. - void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer); + void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { + this->DiagConsumer = DiagConsumer; + } /// \brief Map a virtual file to be used while running the tool. /// @@ -249,10 +257,12 @@ class ClangTool { ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths); - virtual ~ClangTool() { clearArgumentsAdjusters(); } + ~ClangTool(); /// \brief Set a \c DiagnosticConsumer to use during parsing. - void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer); + void setDiagnosticConsumer(DiagnosticConsumer *DiagConsumer) { + this->DiagConsumer = DiagConsumer; + } /// \brief Map a virtual file to be used while running the tool. /// @@ -260,19 +270,11 @@ class ClangTool { /// \param Content A null terminated buffer of the file's content. void mapVirtualFile(StringRef FilePath, StringRef Content); - /// \brief Install command line arguments adjuster. - /// - /// \param Adjuster Command line arguments adjuster. - // - /// FIXME: Function is deprecated. Use (clear/append)ArgumentsAdjuster instead. - /// Remove it once all callers are gone. - void setArgumentsAdjuster(ArgumentsAdjuster *Adjuster); - /// \brief Append a command line arguments adjuster to the adjuster chain. /// /// \param Adjuster An argument adjuster, which will be run on the output of /// previous argument adjusters. - void appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster); + void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster); /// \brief Clear the command line arguments adjuster chain. void clearArgumentsAdjusters(); @@ -292,14 +294,14 @@ class ClangTool { FileManager &getFiles() { return *Files; } private: - // We store compile commands as pair (file name, compile command). - std::vector< std::pair<std::string, CompileCommand> > CompileCommands; + const CompilationDatabase &Compilations; + std::vector<std::string> SourcePaths; llvm::IntrusiveRefCntPtr<FileManager> Files; // Contains a list of pairs (<file name>, <file content>). std::vector< std::pair<StringRef, StringRef> > MappedFileContents; - SmallVector<ArgumentsAdjuster *, 2> ArgsAdjusters; + ArgumentsAdjuster ArgsAdjuster; DiagnosticConsumer *DiagConsumer; }; @@ -335,8 +337,8 @@ inline std::unique_ptr<FrontendActionFactory> newFrontendActionFactory( SourceFileCallbacks *Callbacks) : ConsumerFactory(ConsumerFactory), Callbacks(Callbacks) {} - clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, - StringRef) override { + std::unique_ptr<clang::ASTConsumer> + CreateASTConsumer(clang::CompilerInstance &, StringRef) override { return ConsumerFactory->newASTConsumer(); } |