clang API Documentation

Refactoring.h
Go to the documentation of this file.
00001 //===--- Refactoring.h - Framework for clang refactoring tools --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  Interfaces supporting refactorings that span multiple translation units.
00011 //  While single translation unit refactorings are supported via the Rewriter,
00012 //  when refactoring multiple translation units changes must be stored in a
00013 //  SourceManager independent form, duplicate changes need to be removed, and
00014 //  all changes must be applied at once at the end of the refactoring so that
00015 //  the code is always parseable.
00016 //
00017 //===----------------------------------------------------------------------===//
00018 
00019 #ifndef LLVM_CLANG_TOOLING_REFACTORING_H
00020 #define LLVM_CLANG_TOOLING_REFACTORING_H
00021 
00022 #include "clang/Tooling/Core/Replacement.h"
00023 #include "clang/Tooling/Tooling.h"
00024 #include <string>
00025 
00026 namespace clang {
00027 
00028 class Rewriter;
00029 
00030 namespace tooling {
00031 
00032 /// \brief A tool to run refactorings.
00033 ///
00034 /// This is a refactoring specific version of \see ClangTool. FrontendActions
00035 /// passed to run() and runAndSave() should add replacements to
00036 /// getReplacements().
00037 class RefactoringTool : public ClangTool {
00038 public:
00039   /// \see ClangTool::ClangTool.
00040   RefactoringTool(const CompilationDatabase &Compilations,
00041                   ArrayRef<std::string> SourcePaths);
00042 
00043   /// \brief Returns the set of replacements to which replacements should
00044   /// be added during the run of the tool.
00045   Replacements &getReplacements();
00046 
00047   /// \brief Call run(), apply all generated replacements, and immediately save
00048   /// the results to disk.
00049   ///
00050   /// \returns 0 upon success. Non-zero upon failure.
00051   int runAndSave(FrontendActionFactory *ActionFactory);
00052 
00053   /// \brief Apply all stored replacements to the given Rewriter.
00054   ///
00055   /// Replacement applications happen independently of the success of other
00056   /// applications.
00057   ///
00058   /// \returns true if all replacements apply. false otherwise.
00059   bool applyAllReplacements(Rewriter &Rewrite);
00060 
00061 private:
00062   /// \brief Write all refactored files to disk.
00063   int saveRewrittenFiles(Rewriter &Rewrite);
00064 
00065 private:
00066   Replacements Replace;
00067 };
00068 
00069 } // end namespace tooling
00070 } // end namespace clang
00071 
00072 #endif // LLVM_CLANG_TOOLING_REFACTORING_H