matcher.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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 err = (*replacements)[std::string(rep.getFilePath())].add(rep);
  28. if (err) {
  29. llvm::report_fatal_error("Error with replacement `" + rep.toString() +
  30. "`: " + llvm::toString(std::move(err)) + "\n");
  31. }
  32. }
  33. } // namespace Carbon