Files
carbon-lang/migrate_cpp/cpp_refactoring/matcher.cpp
T
Jon Meow f180c64454 Change how replacements are filtered (#541)
This replaces the use of `isExpansionInMainFile` with instead taking the list of input files, and only touching them. This doesn't keep `isExpansionInMainFile` because it should be redundant, and as such it'd be easy to forget. Unless it starts being a performance issue, it's probably better to omit.
2021-06-03 16:35:55 -07:00

47 lines
1.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "migrate_cpp/cpp_refactoring/matcher.h"
namespace ct = ::clang::tooling;
namespace Carbon {
void Matcher::AddReplacement(const clang::SourceManager& sm,
clang::CharSourceRange range,
llvm::StringRef replacement_text) {
if (!range.isValid()) {
llvm::errs() << "Invalid range: " << range.getAsRange().printToString(sm)
<< "\n";
return;
}
if (sm.getDecomposedLoc(range.getBegin()).first !=
sm.getDecomposedLoc(range.getEnd()).first) {
llvm::errs() << "Range spans macro expansions: "
<< range.getAsRange().printToString(sm) << "\n";
return;
}
if (sm.getFileID(range.getBegin()) != sm.getFileID(range.getEnd())) {
llvm::errs() << "Range spans files: "
<< range.getAsRange().printToString(sm) << "\n";
return;
}
auto rep = ct::Replacement(sm, sm.getExpansionRange(range), replacement_text);
auto entry = replacements->find(std::string(rep.getFilePath()));
if (entry == replacements->end()) {
// The replacement was in a file which isn't being updated, such as a system
// header.
return;
}
auto err = entry->second.add(rep);
if (err) {
llvm::report_fatal_error("Error with replacement `" + rep.toString() +
"`: " + llvm::toString(std::move(err)) + "\n");
}
}
} // namespace Carbon