matcher.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "migrate_cpp/cpp_refactoring/matcher.h"
  5. namespace ct = ::clang::tooling;
  6. namespace Carbon {
  7. void Matcher::AddReplacement(const clang::SourceManager& sm,
  8. clang::CharSourceRange range,
  9. llvm::StringRef replacement_text) {
  10. if (!range.isValid()) {
  11. llvm::errs() << "Invalid range: " << range.getAsRange().printToString(sm)
  12. << "\n";
  13. return;
  14. }
  15. if (sm.getDecomposedLoc(range.getBegin()).first !=
  16. sm.getDecomposedLoc(range.getEnd()).first) {
  17. llvm::errs() << "Range spans macro expansions: "
  18. << range.getAsRange().printToString(sm) << "\n";
  19. return;
  20. }
  21. if (sm.getFileID(range.getBegin()) != sm.getFileID(range.getEnd())) {
  22. llvm::errs() << "Range spans files: "
  23. << range.getAsRange().printToString(sm) << "\n";
  24. return;
  25. }
  26. auto rep = ct::Replacement(sm, sm.getExpansionRange(range), replacement_text);
  27. auto entry = replacements->find(std::string(rep.getFilePath()));
  28. if (entry == replacements->end()) {
  29. // The replacement was in a file which isn't being updated, such as a system
  30. // header.
  31. return;
  32. }
  33. auto err = entry->second.add(rep);
  34. if (err) {
  35. llvm::report_fatal_error("Error with replacement `" + rep.toString() +
  36. "`: " + llvm::toString(std::move(err)) + "\n");
  37. }
  38. }
  39. } // namespace Carbon