var_decl.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. // Returns a string for the type.
  15. static auto GetTypeStr(const clang::VarDecl* decl,
  16. const clang::SourceManager& sm,
  17. const clang::LangOptions& lang_opts) -> std::string {
  18. auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
  19. std::vector<clang::SourceRange> segments;
  20. while (!type_loc.isNull()) {
  21. switch (type_loc.getTypeLocClass()) {
  22. case clang::TypeLoc::LValueReference:
  23. case clang::TypeLoc::RValueReference:
  24. case clang::TypeLoc::Pointer:
  25. case clang::TypeLoc::Auto:
  26. case clang::TypeLoc::Qualified:
  27. segments.push_back(type_loc.getLocalSourceRange());
  28. type_loc = type_loc.getNextTypeLoc();
  29. break;
  30. default:
  31. // For non-auto types, use the canonical type, which adds things like
  32. // namespace qualifiers.
  33. return clang::QualType::getAsString(decl->getType().split(), lang_opts);
  34. }
  35. }
  36. // Sort type segments as they're written in the file. This avoids needing to
  37. // understand TypeLoc traversal ordering.
  38. std::sort(segments.begin(), segments.end(),
  39. [](clang::SourceRange a, clang::SourceRange b) {
  40. return a.getBegin() < b.getBegin();
  41. });
  42. std::string type_str;
  43. for (const auto& segment : segments) {
  44. type_str += clang::Lexer::getSourceText(
  45. clang::CharSourceRange::getTokenRange(segment), sm, lang_opts);
  46. }
  47. return type_str;
  48. }
  49. void VarDecl::run(const cam::MatchFinder::MatchResult& result) {
  50. const auto* decl = result.Nodes.getNodeAs<clang::VarDecl>(Label);
  51. if (!decl) {
  52. llvm::report_fatal_error(std::string("getNodeAs failed for ") + Label);
  53. }
  54. if (decl->getTypeSourceInfo() == nullptr) {
  55. // TODO: Need to understand what's happening in this case. Not sure if we
  56. // need to address it.
  57. return;
  58. }
  59. auto& sm = *(result.SourceManager);
  60. auto lang_opts = result.Context->getLangOpts();
  61. std::string after;
  62. if (decl->getType().isConstQualified()) {
  63. after = "let ";
  64. } else if (result.Nodes.getNodeAs<clang::ParmVarDecl>(Label) == nullptr) {
  65. // Start the replacement with "var" unless it's a parameter.
  66. after = "var ";
  67. }
  68. // Add "identifier: type" to the replacement.
  69. after += decl->getNameAsString() + ": " + GetTypeStr(decl, sm, lang_opts);
  70. // This decides the range to replace. Normally the entire decl is replaced,
  71. // but for code like `int i, j` we need to detect the comma between the
  72. // declared names. That case currently results in `var i: int, var j: int`.
  73. // If there's a comma, this range will be non-empty.
  74. auto type_loc = decl->getTypeSourceInfo()->getTypeLoc();
  75. auto after_type_loc =
  76. clang::Lexer::getLocForEndOfToken(type_loc.getEndLoc(), 0, sm, lang_opts);
  77. auto comma_source_text = clang::Lexer::getSourceText(
  78. clang::CharSourceRange::getCharRange(after_type_loc, decl->getLocation()),
  79. sm, lang_opts);
  80. bool has_comma = !comma_source_text.trim().empty();
  81. clang::CharSourceRange replace_range = clang::CharSourceRange::getTokenRange(
  82. has_comma ? decl->getLocation() : decl->getBeginLoc(), decl->getEndLoc());
  83. AddReplacement(sm, replace_range, after);
  84. }
  85. } // namespace Carbon