clang-tools  6.0.0
ApplyReplacements.h
Go to the documentation of this file.
1 //===-- ApplyReplacements.h - Deduplicate and apply replacements -- 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 /// \file
11 /// \brief This file provides the interface for deduplicating, detecting
12 /// conflicts in, and applying collections of Replacements.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_APPLYREPLACEMENTS_H
17 #define LLVM_CLANG_APPLYREPLACEMENTS_H
18 
19 #include "clang/Tooling/Core/Diagnostic.h"
20 #include "clang/Tooling/Refactoring.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include <string>
24 #include <system_error>
25 #include <vector>
26 
27 namespace clang {
28 
29 class DiagnosticsEngine;
30 class Rewriter;
31 
32 namespace format {
33 struct FormatStyle;
34 } // end namespace format
35 
36 namespace replace {
37 
38 /// \brief Collection of source ranges.
39 typedef std::vector<clang::tooling::Range> RangeVector;
40 
41 /// \brief Collection of TranslationUnitReplacements.
42 typedef std::vector<clang::tooling::TranslationUnitReplacements> TUReplacements;
43 
44 /// \brief Collection of TranslationUnitReplacement files.
45 typedef std::vector<std::string> TUReplacementFiles;
46 
47 /// \brief Collection of TranslationUniDiagnostics.
48 typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
49 
50 /// \brief Map mapping file name to Replacements targeting that file.
51 typedef llvm::DenseMap<const clang::FileEntry *,
52  std::vector<clang::tooling::Replacement>>
54 
55 /// \brief Recursively descends through a directory structure rooted at \p
56 /// Directory and attempts to deserialize *.yaml files as
57 /// TranslationUnitReplacements. All docs that successfully deserialize are
58 /// added to \p TUs.
59 ///
60 /// Directories starting with '.' are ignored during traversal.
61 ///
62 /// \param[in] Directory Directory to begin search for serialized
63 /// TranslationUnitReplacements.
64 /// \param[out] TUs Collection of all found and deserialized
65 /// TranslationUnitReplacements or TranslationUnitDiagnostics.
66 /// \param[out] TUFiles Collection of all TranslationUnitReplacement files
67 /// found in \c Directory.
68 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
69 ///
70 /// \returns An error_code indicating success or failure in navigating the
71 /// directory structure.
72 std::error_code collectReplacementsFromDirectory(
73  const llvm::StringRef Directory, TUReplacements &TUs,
74  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
75 
76 std::error_code collectReplacementsFromDirectory(
77  const llvm::StringRef Directory, TUDiagnostics &TUs,
78  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
79 
80 /// \brief Deduplicate, check for conflicts, and apply all Replacements stored
81 /// in \c TUs. If conflicts occur, no Replacements are applied.
82 ///
83 /// \post For all (key,value) in GroupedReplacements, value[i].getOffset() <=
84 /// value[i+1].getOffset().
85 ///
86 /// \param[in] TUs Collection of TranslationUnitReplacements or
87 /// TranslationUnitDiagnostics to merge,
88 /// deduplicate, and test for conflicts.
89 /// \param[out] GroupedReplacements Container grouping all Replacements by the
90 /// file they target.
91 /// \param[in] SM SourceManager required for conflict reporting.
92 ///
93 /// \returns \parblock
94 /// \li true If all changes were applied successfully.
95 /// \li false If there were conflicts.
96 bool mergeAndDeduplicate(const TUReplacements &TUs,
97  FileToReplacementsMap &GroupedReplacements,
98  clang::SourceManager &SM);
99 
100 bool mergeAndDeduplicate(const TUDiagnostics &TUs,
101  FileToReplacementsMap &GroupedReplacements,
102  clang::SourceManager &SM);
103 
104 // FIXME: Remove this function after changing clang-apply-replacements to use
105 // Replacements class.
106 bool applyAllReplacements(const std::vector<tooling::Replacement> &Replaces,
107  Rewriter &Rewrite);
108 
109 /// \brief Apply all replacements in \c GroupedReplacements.
110 ///
111 /// \param[in] GroupedReplacements Deduplicated and conflict free Replacements
112 /// to apply.
113 /// \param[out] Rewrites The results of applying replacements will be applied
114 /// to this Rewriter.
115 ///
116 /// \returns \parblock
117 /// \li true If all changes were applied successfully.
118 /// \li false If a replacement failed to apply.
119 bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
120  clang::Rewriter &Rewrites);
121 
122 /// \brief Given a collection of Replacements for a single file, produces a list
123 /// of source ranges that enclose those Replacements.
124 ///
125 /// \pre Replacements[i].getOffset() <= Replacements[i+1].getOffset().
126 ///
127 /// \param[in] Replacements Replacements from a single file.
128 ///
129 /// \returns Collection of source ranges that enclose all given Replacements.
130 /// One range is created for each replacement.
131 RangeVector calculateChangedRanges(
132  const std::vector<clang::tooling::Replacement> &Replacements);
133 
134 /// \brief Write the contents of \c FileContents to disk. Keys of the map are
135 /// filenames and values are the new contents for those files.
136 ///
137 /// \param[in] Rewrites Rewriter containing written files to write to disk.
138 bool writeFiles(const clang::Rewriter &Rewrites);
139 
140 /// \brief Delete the replacement files.
141 ///
142 /// \param[in] Files Replacement files to delete.
143 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
144 ///
145 /// \returns \parblock
146 /// \li true If all files have been deleted successfully.
147 /// \li false If at least one or more failures occur when deleting
148 /// files.
149 bool deleteReplacementFiles(const TUReplacementFiles &Files,
150  clang::DiagnosticsEngine &Diagnostics);
151 
152 } // end namespace replace
153 } // end namespace clang
154 
155 #endif // LLVM_CLANG_APPLYREPLACEMENTS_H
bool mergeAndDeduplicate(const TUDiagnostics &TUs, FileToReplacementsMap &GroupedReplacements, clang::SourceManager &SM)
llvm::DenseMap< const clang::FileEntry *, std::vector< clang::tooling::Replacement > > FileToReplacementsMap
Map mapping file name to Replacements targeting that file.
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TUDiagnostics &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics)
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
bool writeFiles(const clang::Rewriter &Rewrites)
Write the contents of FileContents to disk.
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
std::vector< clang::tooling::Range > RangeVector
Collection of source ranges.
RangeVector calculateChangedRanges(const std::vector< clang::tooling::Replacement > &Replacements)
Given a collection of Replacements for a single file, produces a list of source ranges that enclose t...
std::vector< clang::tooling::TranslationUnitDiagnostics > TUDiagnostics
Collection of TranslationUniDiagnostics.
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
bool applyAllReplacements(const std::vector< tooling::Replacement > &Replaces, Rewriter &Rewrite)
static bool applyReplacements(const std::vector< tooling::Replacement > &Replacements, std::string &Result, DiagnosticsEngine &Diagnostics)
Apply Replacements and return the new file contents.
static cl::opt< std::string > FormatStyle("format-style", cl::desc(R"( Style for formatting code around applied fixes: - 'none' (default) turns off formatting - 'file' (literally 'file', not a placeholder) uses .clang-format file in the closest parent directory - '{ <json> }' specifies options inline, e.g. -format-style='{BasedOnStyle: llvm, IndentWidth: 8}' - 'llvm', 'google', 'webkit', 'mozilla' See clang-format documentation for the up-to-date information about formatting styles and options. This option overrides the 'FormatStyle` option in .clang-tidy file, if any. )"), cl::init("none"), cl::cat(ClangTidyCategory))