diff options
Diffstat (limited to 'include/clang/Tooling')
-rw-r--r-- | include/clang/Tooling/CommonOptionsParser.h | 10 | ||||
-rw-r--r-- | include/clang/Tooling/CompilationDatabase.h | 13 | ||||
-rw-r--r-- | include/clang/Tooling/FileMatchTrie.h | 3 | ||||
-rw-r--r-- | include/clang/Tooling/JSONCompilationDatabase.h | 10 | ||||
-rw-r--r-- | include/clang/Tooling/Refactoring.h | 62 | ||||
-rw-r--r-- | include/clang/Tooling/Tooling.h | 17 |
6 files changed, 72 insertions, 43 deletions
diff --git a/include/clang/Tooling/CommonOptionsParser.h b/include/clang/Tooling/CommonOptionsParser.h index a1bad12..6775934 100644 --- a/include/clang/Tooling/CommonOptionsParser.h +++ b/include/clang/Tooling/CommonOptionsParser.h @@ -53,8 +53,8 @@ namespace tooling { /// /// int main(int argc, const char **argv) { /// CommonOptionsParser OptionsParser(argc, argv); -/// ClangTool Tool(OptionsParser.GetCompilations(), -/// OptionsParser.GetSourcePathListi()); +/// ClangTool Tool(OptionsParser.getCompilations(), +/// OptionsParser.getSourcePathListi()); /// return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>()); /// } /// \endcode @@ -67,19 +67,19 @@ public: CommonOptionsParser(int &argc, const char **argv); /// Returns a reference to the loaded compilations database. - CompilationDatabase &GetCompilations() { + CompilationDatabase &getCompilations() { return *Compilations; } /// Returns a list of source file paths to process. - std::vector<std::string> GetSourcePathList() { + std::vector<std::string> getSourcePathList() { return SourcePathList; } static const char *const HelpMessage; private: - llvm::OwningPtr<CompilationDatabase> Compilations; + OwningPtr<CompilationDatabase> Compilations; std::vector<std::string> SourcePathList; }; diff --git a/include/clang/Tooling/CompilationDatabase.h b/include/clang/Tooling/CompilationDatabase.h index a40bffe..7a8054f 100644 --- a/include/clang/Tooling/CompilationDatabase.h +++ b/include/clang/Tooling/CompilationDatabase.h @@ -33,7 +33,6 @@ #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/Twine.h" - #include <string> #include <vector> @@ -106,6 +105,10 @@ public: /// \brief Returns the list of all files available in the compilation database. virtual std::vector<std::string> getAllFiles() const = 0; + + /// \brief Returns all compile commands for all the files in the compilation + /// database. + virtual std::vector<CompileCommand> getAllCompileCommands() const = 0; }; /// \brief Interface for compilation database plugins. @@ -149,7 +152,7 @@ public: /// The argument list is meant to be compatible with normal llvm command line /// parsing in main methods. /// int main(int argc, char **argv) { - /// llvm::OwningPtr<FixedCompilationDatabase> Compilations( + /// OwningPtr<FixedCompilationDatabase> Compilations( /// FixedCompilationDatabase::loadFromCommandLine(argc, argv)); /// cl::ParseCommandLineOptions(argc, argv); /// ... @@ -181,6 +184,12 @@ public: /// Note: This is always an empty list for the fixed compilation database. virtual std::vector<std::string> getAllFiles() const; + /// \brief Returns all compile commands for all the files in the compilation + /// database. + /// + /// Note: This is always an empty list for the fixed compilation database. + virtual std::vector<CompileCommand> getAllCompileCommands() const; + private: /// This is built up to contain a single entry vector to be returned from /// getCompileCommands after adding the positional argument. diff --git a/include/clang/Tooling/FileMatchTrie.h b/include/clang/Tooling/FileMatchTrie.h index ff988be..e531854 100644 --- a/include/clang/Tooling/FileMatchTrie.h +++ b/include/clang/Tooling/FileMatchTrie.h @@ -18,7 +18,6 @@ #include "clang/Basic/LLVM.h" #include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/StringRef.h" - #include <string> #include <vector> @@ -77,7 +76,7 @@ public: /// matches, an empty \c StringRef is returned and a corresponding message /// written to 'Error'. StringRef findEquivalent(StringRef FileName, - llvm::raw_ostream &Error) const; + raw_ostream &Error) const; private: FileMatchTrieNode *Root; OwningPtr<PathComparator> Comparator; diff --git a/include/clang/Tooling/JSONCompilationDatabase.h b/include/clang/Tooling/JSONCompilationDatabase.h index d62ab5c..e3f149b 100644 --- a/include/clang/Tooling/JSONCompilationDatabase.h +++ b/include/clang/Tooling/JSONCompilationDatabase.h @@ -75,6 +75,10 @@ public: /// These are the 'file' entries of the JSON objects. virtual std::vector<std::string> getAllFiles() const; + /// \brief Returns all compile commands for all the files in the compilation + /// database. + virtual std::vector<CompileCommand> getAllCompileCommands() const; + private: /// \brief Constructs a JSON compilation database on a memory buffer. JSONCompilationDatabase(llvm::MemoryBuffer *Database) @@ -91,12 +95,16 @@ private: typedef std::pair<llvm::yaml::ScalarNode*, 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; FileMatchTrie MatchTrie; - llvm::OwningPtr<llvm::MemoryBuffer> Database; + OwningPtr<llvm::MemoryBuffer> Database; llvm::SourceMgr SM; llvm::yaml::Stream YAMLStream; }; diff --git a/include/clang/Tooling/Refactoring.h b/include/clang/Tooling/Refactoring.h index aaffc1a..079ce74 100644 --- a/include/clang/Tooling/Refactoring.h +++ b/include/clang/Tooling/Refactoring.h @@ -19,9 +19,9 @@ #ifndef LLVM_CLANG_TOOLING_REFACTORING_H #define LLVM_CLANG_TOOLING_REFACTORING_H -#include "llvm/ADT/StringRef.h" #include "clang/Basic/SourceLocation.h" #include "clang/Tooling/Tooling.h" +#include "llvm/ADT/StringRef.h" #include <set> #include <string> @@ -47,22 +47,22 @@ public: /// \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(llvm::StringRef FilePath, unsigned Offset, - unsigned Length, llvm::StringRef ReplacementText); + Replacement(StringRef FilePath, unsigned Offset, + unsigned Length, StringRef ReplacementText); /// \brief Creates a Replacement of the range [Start, Start+Length) with /// ReplacementText. Replacement(SourceManager &Sources, SourceLocation Start, unsigned Length, - llvm::StringRef ReplacementText); + StringRef ReplacementText); /// \brief Creates a Replacement of the given range with ReplacementText. Replacement(SourceManager &Sources, const CharSourceRange &Range, - llvm::StringRef ReplacementText); + StringRef ReplacementText); /// \brief Creates a Replacement of the node with ReplacementText. template <typename Node> Replacement(SourceManager &Sources, const Node &NodeToReplace, - llvm::StringRef ReplacementText); + StringRef ReplacementText); /// \brief Returns whether this replacement can be applied to a file. /// @@ -91,9 +91,9 @@ public: private: void setFromSourceLocation(SourceManager &Sources, SourceLocation Start, - unsigned Length, llvm::StringRef ReplacementText); + unsigned Length, StringRef ReplacementText); void setFromSourceRange(SourceManager &Sources, const CharSourceRange &Range, - llvm::StringRef ReplacementText); + StringRef ReplacementText); std::string FilePath; unsigned Offset; @@ -105,41 +105,54 @@ public: /// FIXME: Change to a vector and deduplicate in the RefactoringTool. typedef std::set<Replacement, Replacement::Less> Replacements; -/// \brief Apply all replacements on the Rewriter. +/// \brief Apply all replacements in \p Replaces to the Rewriter \p Rewrite. +/// +/// Replacement applications happen independently of the success of +/// other applications. /// -/// If at least one Apply returns false, ApplyAll returns false. Every -/// Apply will be executed independently of the result of other -/// Apply operations. +/// \returns true if all replacements apply. false otherwise. bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite); /// \brief A tool to run refactorings. /// -/// This is a refactoring specific version of \see ClangTool. -/// All text replacements added to getReplacements() during the run of the -/// tool will be applied and saved after all translation units have been -/// processed. -class RefactoringTool { +/// This is a refactoring specific version of \see ClangTool. FrontendActions +/// passed to run() and runAndSave() should add replacements to +/// getReplacements(). +class RefactoringTool : public ClangTool { public: /// \see ClangTool::ClangTool. RefactoringTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths); - /// \brief Returns a set of replacements. All replacements added during the - /// run of the tool will be applied after all translation units have been - /// processed. + /// \brief Returns the set of replacements to which replacements should + /// be added during the run of the tool. Replacements &getReplacements(); - /// \see ClangTool::run. - int run(FrontendActionFactory *ActionFactory); + /// \brief Call run(), apply all generated replacements, and immediately save + /// the results to disk. + /// + /// \returns 0 upon success. Non-zero upon failure. + int runAndSave(FrontendActionFactory *ActionFactory); + + /// \brief Apply all stored replacements to the given Rewriter. + /// + /// Replacement applications happen independently of the success of other + /// applications. + /// + /// \returns true if all replacements apply. false otherwise. + bool applyAllReplacements(Rewriter &Rewrite); + +private: + /// \brief Write all refactored files to disk. + int saveRewrittenFiles(Rewriter &Rewrite); private: - ClangTool Tool; Replacements Replace; }; template <typename Node> Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace, - llvm::StringRef ReplacementText) { + StringRef ReplacementText) { const CharSourceRange Range = CharSourceRange::getTokenRange(NodeToReplace->getSourceRange()); setFromSourceRange(Sources, Range, ReplacementText); @@ -149,4 +162,3 @@ Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace, } // end namespace clang #endif // end namespace LLVM_CLANG_TOOLING_REFACTORING_H - diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index a03bcb1..27e5a0a 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -30,14 +30,14 @@ #ifndef LLVM_CLANG_TOOLING_TOOLING_H #define LLVM_CLANG_TOOLING_TOOLING_H -#include "llvm/ADT/StringMap.h" -#include "llvm/ADT/Twine.h" #include "clang/Basic/FileManager.h" #include "clang/Basic/LLVM.h" #include "clang/Driver/Util.h" #include "clang/Frontend/FrontendAction.h" #include "clang/Tooling/ArgumentsAdjusters.h" #include "clang/Tooling/CompilationDatabase.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/Twine.h" #include <string> #include <vector> @@ -151,11 +151,10 @@ class ToolInvocation { bool runInvocation(const char *BinaryName, clang::driver::Compilation *Compilation, - clang::CompilerInvocation *Invocation, - const clang::driver::ArgStringList &CC1Args); + clang::CompilerInvocation *Invocation); std::vector<std::string> CommandLine; - llvm::OwningPtr<FrontendAction> ToolAction; + OwningPtr<FrontendAction> ToolAction; FileManager *Files; // Maps <file name> -> <file content>. llvm::StringMap<StringRef> MappedFileContents; @@ -179,6 +178,8 @@ class ClangTool { ClangTool(const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths); + virtual ~ClangTool() {} + /// \brief Map a virtual file to be used while running the tool. /// /// \param FilePath The path at which the content will be mapped. @@ -195,7 +196,7 @@ class ClangTool { /// \param ActionFactory Factory generating the frontend actions. The function /// takes ownership of this parameter. A new action is generated for every /// processed translation unit. - int run(FrontendActionFactory *ActionFactory); + virtual int run(FrontendActionFactory *ActionFactory); /// \brief Returns the file manager used in the tool. /// @@ -210,7 +211,7 @@ class ClangTool { // Contains a list of pairs (<file name>, <file content>). std::vector< std::pair<StringRef, StringRef> > MappedFileContents; - llvm::OwningPtr<ArgumentsAdjuster> ArgsAdjuster; + OwningPtr<ArgumentsAdjuster> ArgsAdjuster; }; template <typename T> @@ -244,7 +245,7 @@ inline FrontendActionFactory *newFrontendActionFactory( : ConsumerFactory(ConsumerFactory), EndCallback(EndCallback) {} clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &, - llvm::StringRef) { + StringRef) { return ConsumerFactory->newASTConsumer(); } |