clang-tools  3.9.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/Refactoring.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/ADT/StringRef.h"
22 #include <string>
23 #include <system_error>
24 #include <vector>
25 
26 namespace clang {
27 
28 class DiagnosticsEngine;
29 class Rewriter;
30 
31 namespace format {
32 struct FormatStyle;
33 } // end namespace format
34 
35 namespace replace {
36 
37 /// \brief Collection of source ranges.
38 typedef std::vector<clang::tooling::Range> RangeVector;
39 
40 /// \brief Collection of TranslationUnitReplacements.
41 typedef std::vector<clang::tooling::TranslationUnitReplacements>
43 
44 /// \brief Collection of TranslationUnitReplacement files.
45 typedef std::vector<std::string> TUReplacementFiles;
46 
47 /// \brief Map mapping file name to Replacements targeting that file.
48 typedef llvm::DenseMap<const clang::FileEntry *,
49  std::vector<clang::tooling::Replacement>>
51 
52 /// \brief Recursively descends through a directory structure rooted at \p
53 /// Directory and attempts to deserialize *.yaml files as
54 /// TranslationUnitReplacements. All docs that successfully deserialize are
55 /// added to \p TUs.
56 ///
57 /// Directories starting with '.' are ignored during traversal.
58 ///
59 /// \param[in] Directory Directory to begin search for serialized
60 /// TranslationUnitReplacements.
61 /// \param[out] TUs Collection of all found and deserialized
62 /// TranslationUnitReplacements.
63 /// \param[out] TURFiles Collection of all TranslationUnitReplacement files
64 /// found in \c Directory.
65 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
66 ///
67 /// \returns An error_code indicating success or failure in navigating the
68 /// directory structure.
69 std::error_code
70 collectReplacementsFromDirectory(const llvm::StringRef Directory,
71  TUReplacements &TUs,
72  TUReplacementFiles &TURFiles,
73  clang::DiagnosticsEngine &Diagnostics);
74 
75 /// \brief Deduplicate, check for conflicts, and apply all Replacements stored
76 /// in \c TUs. If conflicts occur, no Replacements are applied.
77 ///
78 /// \post For all (key,value) in GroupedReplacements, value[i].getOffset() <=
79 /// value[i+1].getOffset().
80 ///
81 /// \param[in] TUs Collection of TranslationUnitReplacements to merge,
82 /// deduplicate, and test for conflicts.
83 /// \param[out] GroupedReplacements Container grouping all Replacements by the
84 /// file they target.
85 /// \param[in] SM SourceManager required for conflict reporting.
86 ///
87 /// \returns \parblock
88 /// \li true If all changes were applied successfully.
89 /// \li false If there were conflicts.
90 bool mergeAndDeduplicate(const TUReplacements &TUs,
91  FileToReplacementsMap &GroupedReplacements,
92  clang::SourceManager &SM);
93 
94 /// \brief Apply all replacements in \c GroupedReplacements.
95 ///
96 /// \param[in] GroupedReplacements Deduplicated and conflict free Replacements
97 /// to apply.
98 /// \param[out] Rewrites The results of applying replacements will be applied
99 /// to this Rewriter.
100 ///
101 /// \returns \parblock
102 /// \li true If all changes were applied successfully.
103 /// \li false If a replacement failed to apply.
104 bool applyReplacements(const FileToReplacementsMap &GroupedReplacements,
105  clang::Rewriter &Rewrites);
106 
107 /// \brief Given a collection of Replacements for a single file, produces a list
108 /// of source ranges that enclose those Replacements.
109 ///
110 /// \pre Replacements[i].getOffset() <= Replacements[i+1].getOffset().
111 ///
112 /// \param[in] Replacements Replacements from a single file.
113 ///
114 /// \returns Collection of source ranges that enclose all given Replacements.
115 /// One range is created for each replacement.
117  const std::vector<clang::tooling::Replacement> &Replacements);
118 
119 /// \brief Write the contents of \c FileContents to disk. Keys of the map are
120 /// filenames and values are the new contents for those files.
121 ///
122 /// \param[in] Rewrites Rewriter containing written files to write to disk.
123 bool writeFiles(const clang::Rewriter &Rewrites);
124 
125 /// \brief Delete the replacement files.
126 ///
127 /// \param[in] Files Replacement files to delete.
128 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
129 ///
130 /// \returns \parblock
131 /// \li true If all files have been deleted successfully.
132 /// \li false If at least one or more failures occur when deleting
133 /// files.
135  clang::DiagnosticsEngine &Diagnostics);
136 
137 } // end namespace replace
138 } // end namespace clang
139 
140 #endif // LLVM_CLANG_APPLYREPLACEMENTS_H
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::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
bool applyReplacements(const FileToReplacementsMap &GroupedReplacements, clang::Rewriter &Rewrites)
Apply all replacements in GroupedReplacements.
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TUReplacements &TUs, TUReplacementFiles &TURFiles, clang::DiagnosticsEngine &Diagnostics)
Recursively descends through a directory structure rooted at Directory and attempts to deserialize *...
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>"))
SourceManager & SM
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...
FileManager Files
Definition: ClangTidy.cpp:188
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
bool mergeAndDeduplicate(const TUReplacements &TUs, FileToReplacementsMap &GroupedReplacements, clang::SourceManager &SM)
Deduplicate, check for conflicts, and apply all Replacements stored in TUs.