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