clang  3.7.0
Refactoring.cpp
Go to the documentation of this file.
1 //===--- Refactoring.cpp - Framework for clang refactoring tools ----------===//
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 // Implements tools to support refactorings.
11 //
12 //===----------------------------------------------------------------------===//
13 
18 #include "clang/Lex/Lexer.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/Path.h"
23 #include "llvm/Support/raw_os_ostream.h"
24 
25 namespace clang {
26 namespace tooling {
27 
29  const CompilationDatabase &Compilations, ArrayRef<std::string> SourcePaths,
30  std::shared_ptr<PCHContainerOperations> PCHContainerOps)
31  : ClangTool(Compilations, SourcePaths, PCHContainerOps) {}
32 
34 
36  if (int Result = run(ActionFactory)) {
37  return Result;
38  }
39 
40  LangOptions DefaultLangOptions;
42  TextDiagnosticPrinter DiagnosticPrinter(llvm::errs(), &*DiagOpts);
43  DiagnosticsEngine Diagnostics(
45  &*DiagOpts, &DiagnosticPrinter, false);
46  SourceManager Sources(Diagnostics, getFiles());
47  Rewriter Rewrite(Sources, DefaultLangOptions);
48 
49  if (!applyAllReplacements(Rewrite)) {
50  llvm::errs() << "Skipped some replacements.\n";
51  }
52 
53  return saveRewrittenFiles(Rewrite);
54 }
55 
57  return tooling::applyAllReplacements(Replace, Rewrite);
58 }
59 
60 int RefactoringTool::saveRewrittenFiles(Rewriter &Rewrite) {
61  return Rewrite.overwriteChangedFiles() ? 1 : 0;
62 }
63 
64 } // end namespace tooling
65 } // end namespace clang
Defines the clang::FileManager interface and associated types.
Replacements & getReplacements()
Returns the set of replacements to which replacements should be added during the run of the tool...
Definition: Refactoring.cpp:33
Defines the SourceManager interface.
std::set< Replacement > Replacements
A set of Replacements. FIXME: Change to a vector and deduplicate in the RefactoringTool.
Definition: Replacement.h:141
Utility to run a FrontendAction over a set of files.
Definition: Tooling.h:278
bool applyAllReplacements(const Replacements &Replaces, Rewriter &Rewrite)
Apply all replacements in Replaces to the Rewriter Rewrite.
Interface to generate clang::FrontendActions.
Definition: Tooling.h:83
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
FileManager & getFiles()
Returns the file manager used in the tool.
Definition: Tooling.h:327
The result type of a method or function.
bool overwriteChangedFiles()
Definition: Rewriter.cpp:452
Interface for compilation databases.
int runAndSave(FrontendActionFactory *ActionFactory)
Call run(), apply all generated replacements, and immediately save the results to disk...
Definition: Refactoring.cpp:35
Options for controlling the compiler diagnostics engine.
Used for handling and querying diagnostic IDs.
bool applyAllReplacements(Rewriter &Rewrite)
Apply all stored replacements to the given Rewriter.
Definition: Refactoring.cpp:56
int run(ToolAction *Action)
Definition: Tooling.cpp:307
RefactoringTool(const CompilationDatabase &Compilations, ArrayRef< std::string > SourcePaths, std::shared_ptr< PCHContainerOperations > PCHContainerOps=std::make_shared< PCHContainerOperations >())
Definition: Refactoring.cpp:28
This class handles loading and caching of source files into memory.