clang-tools  10.0.0svn
ApplyReplacements.h
Go to the documentation of this file.
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 class Rewriter;
31 
32 namespace replace {
33 
34 /// Collection of TranslationUnitReplacements.
35 typedef std::vector<clang::tooling::TranslationUnitReplacements> TUReplacements;
36 
37 /// Collection of TranslationUnitReplacement files.
38 typedef std::vector<std::string> TUReplacementFiles;
39 
40 /// Collection of TranslationUniDiagnostics.
41 typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
42 
43 /// Map mapping file name to a set of AtomicChange targeting that file.
44 typedef llvm::DenseMap<const clang::FileEntry *,
45  std::vector<tooling::AtomicChange>>
47 
48 /// Recursively descends through a directory structure rooted at \p
49 /// Directory and attempts to deserialize *.yaml files as
50 /// TranslationUnitReplacements. All docs that successfully deserialize are
51 /// added to \p TUs.
52 ///
53 /// Directories starting with '.' are ignored during traversal.
54 ///
55 /// \param[in] Directory Directory to begin search for serialized
56 /// TranslationUnitReplacements.
57 /// \param[out] TUs Collection of all found and deserialized
58 /// TranslationUnitReplacements or TranslationUnitDiagnostics.
59 /// \param[out] TUFiles Collection of all TranslationUnitReplacement files
60 /// found in \c Directory.
61 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
62 ///
63 /// \returns An error_code indicating success or failure in navigating the
64 /// directory structure.
65 std::error_code collectReplacementsFromDirectory(
66  const llvm::StringRef Directory, TUReplacements &TUs,
67  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
68 
69 std::error_code collectReplacementsFromDirectory(
70  const llvm::StringRef Directory, TUDiagnostics &TUs,
71  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
72 
73 /// Deduplicate, check for conflicts, and extract all Replacements stored
74 /// in \c TUs. Conflicting replacements are skipped.
75 ///
76 /// \post For all (key,value) in FileChanges, value[i].getOffset() <=
77 /// value[i+1].getOffset().
78 ///
79 /// \param[in] TUs Collection of TranslationUnitReplacements or
80 /// TranslationUnitDiagnostics to merge, deduplicate, and test for conflicts.
81 /// \param[out] FileChanges Container grouping all changes by the
82 /// file they target. Only non conflicting replacements are kept into
83 /// FileChanges.
84 /// \param[in] SM SourceManager required for conflict reporting.
85 ///
86 /// \returns \parblock
87 /// \li true If all changes were converted successfully.
88 /// \li false If there were conflicts.
89 bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
90  FileToChangesMap &FileChanges,
91  clang::SourceManager &SM);
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
tooling::Replacements Changes
Definition: Format.cpp:108
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
static std::string replace(llvm::StringRef Haystack, llvm::StringRef Needle, llvm::StringRef Repl)
Definition: TestIndex.cpp:30
llvm::Expected< std::string > applyChanges(StringRef File, const std::vector< tooling::AtomicChange > &Changes, const tooling::ApplyChangesSpec &Spec, DiagnosticsEngine &Diagnostics)
Apply AtomicChange on File and rewrite it.
llvm::StringRef Directory
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TUReplacements &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics)
Recursively descends through a directory structure rooted at Directory and attempts to deserialize *...
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs, FileToChangesMap &FileChanges, clang::SourceManager &SM)
Deduplicate, check for conflicts, and extract all Replacements stored in TUs.
std::vector< clang::tooling::TranslationUnitDiagnostics > TUDiagnostics
Collection of TranslationUniDiagnostics.
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
llvm::DenseMap< const clang::FileEntry *, std::vector< tooling::AtomicChange > > FileToChangesMap
Map mapping file name to a set of AtomicChange targeting that file.
llvm::StringMap< std::string > Files