1 //===-- ApplyReplacements.h - Deduplicate and apply replacements -- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file provides the interface for deduplicating, detecting
11 /// conflicts in, and applying collections of Replacements.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_APPLYREPLACEMENTS_H
16 #define LLVM_CLANG_APPLYREPLACEMENTS_H
17 
18 #include "clang/Tooling/Core/Diagnostic.h"
19 #include "clang/Tooling/Refactoring.h"
20 #include "clang/Tooling/Refactoring/AtomicChange.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 
31 namespace replace {
32 
33 /// Collection of TranslationUnitReplacements.
34 typedef std::vector<clang::tooling::TranslationUnitReplacements> TUReplacements;
35 
36 /// Collection of TranslationUnitReplacement files.
37 typedef std::vector<std::string> TUReplacementFiles;
38 
39 /// Collection of TranslationUniDiagnostics.
40 typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
41 
42 /// Map mapping file name to a set of AtomicChange targeting that file.
43 typedef llvm::DenseMap<const clang::FileEntry *,
44                        std::vector<tooling::AtomicChange>>
45     FileToChangesMap;
46 
47 /// Recursively descends through a directory structure rooted at \p
48 /// Directory and attempts to deserialize *.yaml files as
49 /// TranslationUnitReplacements. All docs that successfully deserialize are
50 /// added to \p TUs.
51 ///
52 /// Directories starting with '.' are ignored during traversal.
53 ///
54 /// \param[in] Directory Directory to begin search for serialized
55 /// TranslationUnitReplacements.
56 /// \param[out] TUs Collection of all found and deserialized
57 /// TranslationUnitReplacements or TranslationUnitDiagnostics.
58 /// \param[out] TUFiles Collection of all TranslationUnitReplacement files
59 /// found in \c Directory.
60 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
61 ///
62 /// \returns An error_code indicating success or failure in navigating the
63 /// directory structure.
64 std::error_code collectReplacementsFromDirectory(
65     const llvm::StringRef Directory, TUReplacements &TUs,
66     TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
67 
68 std::error_code collectReplacementsFromDirectory(
69     const llvm::StringRef Directory, TUDiagnostics &TUs,
70     TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
71 
72 /// Deduplicate, check for conflicts, and extract all Replacements stored
73 /// in \c TUs. Conflicting replacements are skipped.
74 ///
75 /// \post For all (key,value) in FileChanges, value[i].getOffset() <=
76 /// value[i+1].getOffset().
77 ///
78 /// \param[in] TUs Collection of TranslationUnitReplacements or
79 /// TranslationUnitDiagnostics to merge, deduplicate, and test for conflicts.
80 /// \param[out] FileChanges Container grouping all changes by the
81 /// file they target. Only non conflicting replacements are kept into
82 /// FileChanges.
83 /// \param[in] SM SourceManager required for conflict reporting.
84 ///
85 /// \returns \parblock
86 ///          \li true If all changes were converted successfully.
87 ///          \li false If there were conflicts.
88 bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
89                          FileToChangesMap &FileChanges,
90                          clang::SourceManager &SM,
91                          bool IgnoreInsertConflict = false);
92 
93 /// Apply \c AtomicChange on File and rewrite it.
94 ///
95 /// \param[in] File Path of the file where to apply AtomicChange.
96 /// \param[in] Changes to apply.
97 /// \param[in] Spec For code cleanup and formatting.
98 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
99 ///
100 /// \returns The changed code if all changes are applied successfully;
101 /// otherwise, an llvm::Error carrying llvm::StringError or an error_code.
102 llvm::Expected<std::string>
103 applyChanges(StringRef File, const std::vector<tooling::AtomicChange> &Changes,
104              const tooling::ApplyChangesSpec &Spec,
105              DiagnosticsEngine &Diagnostics);
106 
107 /// Delete the replacement files.
108 ///
109 /// \param[in] Files Replacement files to delete.
110 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
111 ///
112 /// \returns \parblock
113 ///          \li true If all files have been deleted successfully.
114 ///          \li false If at least one or more failures occur when deleting
115 /// files.
116 bool deleteReplacementFiles(const TUReplacementFiles &Files,
117                             clang::DiagnosticsEngine &Diagnostics);
118 
119 } // end namespace replace
120 } // end namespace clang
121 
122 #endif // LLVM_CLANG_APPLYREPLACEMENTS_H
123