clang-tools  7.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 "clang/Tooling/Refactoring/AtomicChange.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include <string>
25 #include <system_error>
26 #include <vector>
27 
28 namespace clang {
29 
30 class DiagnosticsEngine;
31 class Rewriter;
32 
33 namespace replace {
34 
35 /// \brief Collection of TranslationUnitReplacements.
36 typedef std::vector<clang::tooling::TranslationUnitReplacements> TUReplacements;
37 
38 /// \brief Collection of TranslationUnitReplacement files.
39 typedef std::vector<std::string> TUReplacementFiles;
40 
41 /// \brief Collection of TranslationUniDiagnostics.
42 typedef std::vector<clang::tooling::TranslationUnitDiagnostics> TUDiagnostics;
43 
44 /// \brief Map mapping file name to a set of AtomicChange targeting that file.
45 typedef llvm::DenseMap<const clang::FileEntry *,
46  std::vector<tooling::AtomicChange>>
48 
49 /// \brief Recursively descends through a directory structure rooted at \p
50 /// Directory and attempts to deserialize *.yaml files as
51 /// TranslationUnitReplacements. All docs that successfully deserialize are
52 /// added to \p TUs.
53 ///
54 /// Directories starting with '.' are ignored during traversal.
55 ///
56 /// \param[in] Directory Directory to begin search for serialized
57 /// TranslationUnitReplacements.
58 /// \param[out] TUs Collection of all found and deserialized
59 /// TranslationUnitReplacements or TranslationUnitDiagnostics.
60 /// \param[out] TUFiles Collection of all TranslationUnitReplacement files
61 /// found in \c Directory.
62 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
63 ///
64 /// \returns An error_code indicating success or failure in navigating the
65 /// directory structure.
66 std::error_code collectReplacementsFromDirectory(
67  const llvm::StringRef Directory, TUReplacements &TUs,
68  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
69 
70 std::error_code collectReplacementsFromDirectory(
71  const llvm::StringRef Directory, TUDiagnostics &TUs,
72  TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics);
73 
74 /// \brief Deduplicate, check for conflicts, and extract all Replacements stored
75 /// in \c TUs. Conflicting replacements are skipped.
76 ///
77 /// \post For all (key,value) in FileChanges, value[i].getOffset() <=
78 /// value[i+1].getOffset().
79 ///
80 /// \param[in] TUs Collection of TranslationUnitReplacements or
81 /// TranslationUnitDiagnostics to merge, deduplicate, and test for conflicts.
82 /// \param[out] FileChanges Container grouping all changes by the
83 /// file they target. Only non conflicting replacements are kept into
84 /// FileChanges.
85 /// \param[in] SM SourceManager required for conflict reporting.
86 ///
87 /// \returns \parblock
88 /// \li true If all changes were converted successfully.
89 /// \li false If there were conflicts.
90 bool mergeAndDeduplicate(const TUReplacements &TUs, const TUDiagnostics &TUDs,
91  FileToChangesMap &FileChanges,
92  clang::SourceManager &SM);
93 
94 /// \brief Apply \c AtomicChange on File and rewrite it.
95 ///
96 /// \param[in] File Path of the file where to apply AtomicChange.
97 /// \param[in] Changes to apply.
98 /// \param[in] Spec For code cleanup and formatting.
99 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
100 ///
101 /// \returns The changed code if all changes are applied successfully;
102 /// otherwise, an llvm::Error carrying llvm::StringError or an error_code.
103 llvm::Expected<std::string>
104 applyChanges(StringRef File, const std::vector<tooling::AtomicChange> &Changes,
105  const tooling::ApplyChangesSpec &Spec,
106  DiagnosticsEngine &Diagnostics);
107 
108 /// \brief Delete the replacement files.
109 ///
110 /// \param[in] Files Replacement files to delete.
111 /// \param[in] Diagnostics DiagnosticsEngine used for error output.
112 ///
113 /// \returns \parblock
114 /// \li true If all files have been deleted successfully.
115 /// \li false If at least one or more failures occur when deleting
116 /// files.
117 bool deleteReplacementFiles(const TUReplacementFiles &Files,
118  clang::DiagnosticsEngine &Diagnostics);
119 
120 } // end namespace replace
121 } // end namespace clang
122 
123 #endif // LLVM_CLANG_APPLYREPLACEMENTS_H
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
static cl::opt< std::string > Directory(cl::Positional, cl::Required, cl::desc("<Search Root Directory>"))
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.
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.