1 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
19 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
20 #define LLVM_CLANG_TOOLING_REFACTORING_H
22 #include "llvm/ADT/StringRef.h"
23 #include "clang/Basic/SourceLocation.h"
24 #include "clang/Tooling/Tooling.h"
35 /// \brief A text replacement.
37 /// Represents a SourceManager independent replacement of a range of text in a
41 /// \brief Creates an invalid (not applicable) replacement.
44 /// \brief Creates a replacement of the range [Offset, Offset+Length) in
45 /// FilePath with ReplacementText.
47 /// \param FilePath A source file accessible via a SourceManager.
48 /// \param Offset The byte offset of the start of the range in the file.
49 /// \param Length The length of the range in bytes.
50 Replacement(llvm::StringRef FilePath, unsigned Offset,
51 unsigned Length, llvm::StringRef ReplacementText);
53 /// \brief Creates a Replacement of the range [Start, Start+Length) with
55 Replacement(SourceManager &Sources, SourceLocation Start, unsigned Length,
56 llvm::StringRef ReplacementText);
58 /// \brief Creates a Replacement of the given range with ReplacementText.
59 Replacement(SourceManager &Sources, const CharSourceRange &Range,
60 llvm::StringRef ReplacementText);
62 /// \brief Creates a Replacement of the node with ReplacementText.
63 template <typename Node>
64 Replacement(SourceManager &Sources, const Node &NodeToReplace,
65 llvm::StringRef ReplacementText);
67 /// \brief Returns whether this replacement can be applied to a file.
69 /// Only replacements that are in a valid file can be applied.
70 bool isApplicable() const;
74 StringRef getFilePath() const { return FilePath; }
75 unsigned getOffset() const { return Offset; }
76 unsigned getLength() const { return Length; }
77 StringRef getReplacementText() const { return ReplacementText; }
80 /// \brief Applies the replacement on the Rewriter.
81 bool apply(Rewriter &Rewrite) const;
83 /// \brief Returns a human readable string representation.
84 std::string toString() const;
86 /// \brief Comparator to be able to use Replacement in std::set for uniquing.
89 bool operator()(const Replacement &R1, const Replacement &R2) const;
93 void setFromSourceLocation(SourceManager &Sources, SourceLocation Start,
94 unsigned Length, llvm::StringRef ReplacementText);
95 void setFromSourceRange(SourceManager &Sources, const CharSourceRange &Range,
96 llvm::StringRef ReplacementText);
101 std::string ReplacementText;
104 /// \brief A set of Replacements.
105 /// FIXME: Change to a vector and deduplicate in the RefactoringTool.
106 typedef std::set<Replacement, Replacement::Less> Replacements;
108 /// \brief Apply all replacements on the Rewriter.
110 /// If at least one Apply returns false, ApplyAll returns false. Every
111 /// Apply will be executed independently of the result of other
112 /// Apply operations.
113 bool applyAllReplacements(Replacements &Replaces, Rewriter &Rewrite);
115 /// \brief A tool to run refactorings.
117 /// This is a refactoring specific version of \see ClangTool.
118 /// All text replacements added to getReplacements() during the run of the
119 /// tool will be applied and saved after all translation units have been
121 class RefactoringTool {
123 /// \see ClangTool::ClangTool.
124 RefactoringTool(const CompilationDatabase &Compilations,
125 ArrayRef<std::string> SourcePaths);
127 /// \brief Returns a set of replacements. All replacements added during the
128 /// run of the tool will be applied after all translation units have been
130 Replacements &getReplacements();
132 /// \see ClangTool::run.
133 int run(FrontendActionFactory *ActionFactory);
137 Replacements Replace;
140 template <typename Node>
141 Replacement::Replacement(SourceManager &Sources, const Node &NodeToReplace,
142 llvm::StringRef ReplacementText) {
143 const CharSourceRange Range =
144 CharSourceRange::getTokenRange(NodeToReplace->getSourceRange());
145 setFromSourceRange(Sources, Range, ReplacementText);
148 } // end namespace tooling
149 } // end namespace clang
151 #endif // end namespace LLVM_CLANG_TOOLING_REFACTORING_H