Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:12

0001 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  Interfaces supporting refactorings that span multiple translation units.
0010 //  While single translation unit refactorings are supported via the Rewriter,
0011 //  when refactoring multiple translation units changes must be stored in a
0012 //  SourceManager independent form, duplicate changes need to be removed, and
0013 //  all changes must be applied at once at the end of the refactoring so that
0014 //  the code is always parseable.
0015 //
0016 //===----------------------------------------------------------------------===//
0017 
0018 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
0019 #define LLVM_CLANG_TOOLING_REFACTORING_H
0020 
0021 #include "clang/Tooling/Core/Replacement.h"
0022 #include "clang/Tooling/Tooling.h"
0023 #include <map>
0024 #include <string>
0025 
0026 namespace clang {
0027 
0028 class Rewriter;
0029 
0030 namespace tooling {
0031 
0032 /// A tool to run refactorings.
0033 ///
0034 /// This is a refactoring specific version of \see ClangTool. FrontendActions
0035 /// passed to run() and runAndSave() should add replacements to
0036 /// getReplacements().
0037 class RefactoringTool : public ClangTool {
0038 public:
0039   /// \see ClangTool::ClangTool.
0040   RefactoringTool(const CompilationDatabase &Compilations,
0041                   ArrayRef<std::string> SourcePaths,
0042                   std::shared_ptr<PCHContainerOperations> PCHContainerOps =
0043                       std::make_shared<PCHContainerOperations>());
0044 
0045   /// Returns the file path to replacements map to which replacements
0046   /// should be added during the run of the tool.
0047   std::map<std::string, Replacements> &getReplacements();
0048 
0049   /// Call run(), apply all generated replacements, and immediately save
0050   /// the results to disk.
0051   ///
0052   /// \returns 0 upon success. Non-zero upon failure.
0053   int runAndSave(FrontendActionFactory *ActionFactory);
0054 
0055   /// Apply all stored replacements to the given Rewriter.
0056   ///
0057   /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
0058   /// application.
0059   ///
0060   /// Replacement applications happen independently of the success of other
0061   /// applications.
0062   ///
0063   /// \returns true if all replacements apply. false otherwise.
0064   bool applyAllReplacements(Rewriter &Rewrite);
0065 
0066 private:
0067   /// Write all refactored files to disk.
0068   int saveRewrittenFiles(Rewriter &Rewrite);
0069 
0070 private:
0071   std::map<std::string, Replacements> FileToReplaces;
0072 };
0073 
0074 /// Groups \p Replaces by the file path and applies each group of
0075 /// Replacements on the related file in \p Rewriter. In addition to applying
0076 /// given Replacements, this function also formats the changed code.
0077 ///
0078 /// \pre Replacements must be conflict-free.
0079 ///
0080 /// FileToReplaces will be deduplicated with `groupReplacementsByFile` before
0081 /// application.
0082 ///
0083 /// Replacement applications happen independently of the success of other
0084 /// applications.
0085 ///
0086 /// \param[in] FileToReplaces Replacements (grouped by files) to apply.
0087 /// \param[in] Rewrite The `Rewritter` to apply replacements on.
0088 /// \param[in] Style The style name used for reformatting. See ```getStyle``` in
0089 /// "include/clang/Format/Format.h" for all possible style forms.
0090 ///
0091 /// \returns true if all replacements applied and formatted. false otherwise.
0092 bool formatAndApplyAllReplacements(
0093     const std::map<std::string, Replacements> &FileToReplaces,
0094     Rewriter &Rewrite, StringRef Style = "file");
0095 
0096 } // end namespace tooling
0097 } // end namespace clang
0098 
0099 #endif // LLVM_CLANG_TOOLING_REFACTORING_H