Home | History | Annotate | Download | only in Tooling
      1 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  Interfaces supporting refactorings that span multiple translation units.
     11 //  While single translation unit refactorings are supported via the Rewriter,
     12 //  when refactoring multiple translation units changes must be stored in a
     13 //  SourceManager independent form, duplicate changes need to be removed, and
     14 //  all changes must be applied at once at the end of the refactoring so that
     15 //  the code is always parseable.
     16 //
     17 //===----------------------------------------------------------------------===//
     18 
     19 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
     20 #define LLVM_CLANG_TOOLING_REFACTORING_H
     21 
     22 #include "clang/Tooling/Core/Replacement.h"
     23 #include "clang/Tooling/Tooling.h"
     24 #include <map>
     25 #include <string>
     26 
     27 namespace clang {
     28 
     29 class Rewriter;
     30 
     31 namespace tooling {
     32 
     33 /// \brief A tool to run refactorings.
     34 ///
     35 /// This is a refactoring specific version of \see ClangTool. FrontendActions
     36 /// passed to run() and runAndSave() should add replacements to
     37 /// getReplacements().
     38 class RefactoringTool : public ClangTool {
     39 public:
     40   /// \see ClangTool::ClangTool.
     41   RefactoringTool(const CompilationDatabase &Compilations,
     42                   ArrayRef<std::string> SourcePaths,
     43                   std::shared_ptr<PCHContainerOperations> PCHContainerOps =
     44                       std::make_shared<PCHContainerOperations>());
     45 
     46   /// \brief Returns the file path to replacements map to which replacements
     47   /// should be added during the run of the tool.
     48   std::map<std::string, Replacements> &getReplacements();
     49 
     50   /// \brief Call run(), apply all generated replacements, and immediately save
     51   /// the results to disk.
     52   ///
     53   /// \returns 0 upon success. Non-zero upon failure.
     54   int runAndSave(FrontendActionFactory *ActionFactory);
     55 
     56   /// \brief Apply all stored replacements to the given Rewriter.
     57   ///
     58   /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
     59   /// application.
     60   ///
     61   /// Replacement applications happen independently of the success of other
     62   /// applications.
     63   ///
     64   /// \returns true if all replacements apply. false otherwise.
     65   bool applyAllReplacements(Rewriter &Rewrite);
     66 
     67 private:
     68   /// \brief Write all refactored files to disk.
     69   int saveRewrittenFiles(Rewriter &Rewrite);
     70 
     71 private:
     72   std::map<std::string, Replacements> FileToReplaces;
     73 };
     74 
     75 /// \brief Groups \p Replaces by the file path and applies each group of
     76 /// Replacements on the related file in \p Rewriter. In addition to applying
     77 /// given Replacements, this function also formats the changed code.
     78 ///
     79 /// \pre Replacements must be conflict-free.
     80 ///
     81 /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
     82 /// application.
     83 ///
     84 /// Replacement applications happen independently of the success of other
     85 /// applications.
     86 ///
     87 /// \param[in] FileToReplaces Replacements (grouped by files) to apply.
     88 /// \param[in] Rewrite The `Rewritter` to apply replacements on.
     89 /// \param[in] Style The style name used for reformatting. See ```getStyle``` in
     90 /// "include/clang/Format/Format.h" for all possible style forms.
     91 ///
     92 /// \returns true if all replacements applied and formatted. false otherwise.
     93 bool formatAndApplyAllReplacements(
     94     const std::map<std::string, Replacements> &FileToReplaces,
     95     Rewriter &Rewrite, StringRef Style = "file");
     96 
     97 } // end namespace tooling
     98 } // end namespace clang
     99 
    100 #endif // LLVM_CLANG_TOOLING_REFACTORING_H
    101