17#include "clang/Basic/LangOptions.h"
18#include "clang/Basic/SourceManager.h"
19#include "clang/Format/Format.h"
20#include "clang/Lex/Lexer.h"
21#include "clang/Rewrite/Core/Rewriter.h"
22#include "clang/Tooling/Core/Diagnostic.h"
23#include "clang/Tooling/DiagnosticsYaml.h"
24#include "clang/Tooling/ReplacementsYaml.h"
25#include "llvm/ADT/ArrayRef.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/StringSet.h"
29#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
31#include "llvm/Support/Path.h"
32#include "llvm/Support/raw_ostream.h"
48template <
typename TranslationUnits>
50 const llvm::StringRef
Directory, TranslationUnits &TUs,
52 using namespace llvm::sys::fs;
53 using namespace llvm::sys::path;
55 std::error_code ErrorCode;
57 for (recursive_directory_iterator I(
Directory, ErrorCode),
E;
58 I !=
E && !ErrorCode; I.increment(ErrorCode)) {
59 if (filename(I->path())[0] ==
'.') {
68 TUFiles.push_back(I->path());
70 ErrorOr<std::unique_ptr<MemoryBuffer>>
Out =
71 MemoryBuffer::getFile(I->path());
72 if (std::error_code BufferError =
Out.getError()) {
73 errs() <<
"Error reading " << I->path() <<
": " << BufferError.message()
79 typename TranslationUnits::value_type TU;
122static llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
124 const clang::SourceManager &SM) {
125 llvm::StringSet<> Warned;
126 llvm::DenseMap<FileEntryRef, std::vector<tooling::Replacement>>
132 llvm::DenseMap<
const FileEntry *,
133 std::map<tooling::Replacement,
134 const tooling::TranslationUnitDiagnostics *>>
137 auto AddToGroup = [&](
const tooling::Replacement &R,
138 const tooling::TranslationUnitDiagnostics *SourceTU,
139 const std::optional<std::string>
BuildDir) {
143 SmallString<128>
Path = R.getFilePath();
147 SM.getFileManager().makeAbsolutePath(
Path);
149 if (
auto Entry = SM.getFileManager().getOptionalFileRef(
Path)) {
155 else if (It->second != SourceTU)
159 GroupedReplacements[*
Entry].push_back(R);
160 }
else if (Warned.insert(
Path).second) {
161 errs() <<
"Described file '" << R.getFilePath()
162 <<
"' doesn't exist. Ignoring...\n";
166 for (
const auto &TU : TUs)
167 for (
const tooling::Replacement &R : TU.Replacements)
168 AddToGroup(R,
nullptr, {});
170 for (
const auto &TU : TUDs)
171 for (
const auto &D : TU.Diagnostics)
172 if (
const auto *ChoosenFix = tooling::selectFirstFix(D)) {
173 for (
const auto &
Fix : *ChoosenFix)
174 for (
const tooling::Replacement &R :
Fix.second)
175 AddToGroup(R, &TU, D.BuildDirectory);
180 for (
auto &FileAndReplacements : GroupedReplacements) {
181 llvm::sort(FileAndReplacements.second);
184 return GroupedReplacements;
191 bool ConflictDetected =
false;
195 for (
const auto &FileAndReplacements : GroupedReplacements) {
196 FileEntryRef
Entry = FileAndReplacements.first;
197 const SourceLocation BeginLoc =
198 SM.getLocForStartOfFile(SM.getOrCreateFileID(
Entry, SrcMgr::C_User));
199 tooling::AtomicChange FileChange(
Entry.getName(),
Entry.getName());
200 for (
const auto &R : FileAndReplacements.second) {
202 FileChange.replace(SM, BeginLoc.getLocWithOffset(R.getOffset()),
203 R.getLength(), R.getReplacementText());
217 errs() << llvm::toString(std::move(Err)) <<
"\n";
219 tooling::Replacements &Replacements = FileChange.getReplacements();
221 Replacements.getShiftedCodePosition(R.getOffset());
222 unsigned NewLength = Replacements.getShiftedCodePosition(
223 R.getOffset() + R.getLength()) -
225 if (NewLength == R.getLength()) {
226 tooling::Replacement RR = tooling::Replacement(
227 R.getFilePath(), NewOffset, NewLength, R.getReplacementText());
228 Replacements = Replacements.merge(tooling::Replacements(RR));
231 <<
"Can't resolve conflict, skipping the replacement.\n";
232 ConflictDetected =
true;
235 ConflictDetected =
true;
238 FileChanges.try_emplace(
Entry,
239 std::vector<tooling::AtomicChange>{FileChange});
242 return !ConflictDetected;
245llvm::Expected<std::string>
247 const tooling::ApplyChangesSpec &Spec,
249 FileManager Files((FileSystemOptions()));
252 llvm::ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer =
253 SM.getFileManager().getBufferForFile(File);
255 return errorCodeToError(Buffer.getError());
256 return tooling::applyAtomicChanges(File, Buffer.get()->getBuffer(),
Changes,
263 for (
const auto &
Filename : Files) {
268 errs() <<
"Error deleting file: " <<
Filename <<
"\n";
269 errs() <<
Error.message() <<
"\n";
270 errs() <<
"Please delete the file manually\n";
static void eatDiagnostics(const SMDiagnostic &, void *)
This file provides the interface for deduplicating, detecting conflicts in, and applying collections ...
static cl::opt< bool > IgnoreInsertConflict("ignore-insert-conflict", cl::desc("Ignore insert conflict and keep running to fix."), cl::init(false), cl::cat(ReplacementCategory))
static cl::opt< bool > Fix("fix", desc(R"(
Apply suggested fixes. Without -fix-errors
clang-tidy will bail out if any compilation
errors were found.
)"), cl::init(false), cl::cat(ClangTidyCategory))
static constexpr llvm::SourceMgr::DiagKind Error
CompiledFragmentImpl & Out
std::string Filename
Filename as a string.
std::vector< HeaderHandle > Path
llvm::StringRef Directory
WantDiagnostics Diagnostics
static constexpr std::array< StringRef, 2 > AllowedExtensions
static std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TranslationUnits &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &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.
static llvm::DenseMap< FileEntryRef, std::vector< tooling::Replacement > > groupReplacements(const TUReplacements &TUs, const TUDiagnostics &TUDs, const clang::SourceManager &SM)
Extract replacements from collected TranslationUnitReplacements and TranslationUnitDiagnostics and gr...
bool deleteReplacementFiles(const TUReplacementFiles &Files, clang::DiagnosticsEngine &Diagnostics)
Delete the replacement files.
std::error_code collectReplacementsFromDirectory(const llvm::StringRef Directory, TranslationUnits &TUs, TUReplacementFiles &TUFiles, clang::DiagnosticsEngine &Diagnostics)=delete
Recursively descends through a directory structure rooted at Directory and attempts to deserialize *....
std::vector< clang::tooling::TranslationUnitReplacements > TUReplacements
Collection of TranslationUnitReplacements.
std::vector< std::string > TUReplacementFiles
Collection of TranslationUnitReplacement files.
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++ -*-===//
Some operations such as code completion produce a set of candidates.