var_decl.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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/var_decl.h"
  5. #include "clang/ASTMatchers/ASTMatchers.h"
  6. #include "clang/Lex/Lexer.h"
  7. namespace cam = ::clang::ast_matchers;
  8. namespace Carbon {
  9. VarDecl::VarDecl(std::map<std::string, Replacements>& in_replacements,
  10. cam::MatchFinder* finder)
  11. : Matcher(in_replacements) {
  12. finder->addMatcher(cam::varDecl().bind(Label), this);
  13. }
  14. void VarDecl::run(const cam::MatchFinder::MatchResult& result) {
  15. const auto* decl = result.Nodes.getNodeAs<clang::VarDecl>(Label);
  16. if (!decl) {
  17. llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
  18. }
  19. auto& sm = *(result.SourceManager);
  20. auto lang_opts = result.Context->getLangOpts();
  21. std::string after;
  22. // Start the replacement with "var" unless it's a parameter.
  23. if (result.Nodes.getNodeAs<clang::ParmVarDecl>(Label) == nullptr) {
  24. after = "var ";
  25. }
  26. // Finish the "type: name" replacement.
  27. after += decl->getNameAsString() + ": " +
  28. clang::QualType::getAsString(decl->getType().split(), lang_opts);
  29. if (decl->getTypeSourceInfo() == nullptr) {
  30. // TODO: Need to understand what's happening in this case. Not sure if we
  31. // need to address it.
  32. return;
  33. }
  34. // This decides the range to replace. Normally the entire decl is replaced,
  35. // but for code like `int i, j` we need to detect the comma between the
  36. // declared names. That case currently results in `var i: int, var j: int`.
  37. auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
  38. auto after_type_loc =
  39. clang::Lexer::getLocForEndOfToken(type_loc.getEndLoc(), 0, sm, lang_opts);
  40. // If there's a comma, this range will be non-empty.
  41. auto comma_source_text = clang::Lexer::getSourceText(
  42. clang::CharSourceRange::getCharRange(after_type_loc, decl->getLocation()),
  43. sm, lang_opts);
  44. bool has_comma = !comma_source_text.trim().empty();
  45. clang::CharSourceRange replace_range = clang::CharSourceRange::getTokenRange(
  46. has_comma ? decl->getLocation() : decl->getBeginLoc(), decl->getEndLoc());
  47. AddReplacement(sm, replace_range, after);
  48. }
  49. } // namespace Carbon