var_decl.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. namespace cam = ::clang::ast_matchers;
  7. namespace Carbon {
  8. static constexpr char Label[] = "VarDecl";
  9. // Helper function for printing TypeLocClass. Useful for debugging.
  10. LLVM_ATTRIBUTE_UNUSED
  11. static auto TypeLocClassToString(clang::TypeLoc::TypeLocClass c)
  12. -> std::string {
  13. switch (c) {
  14. // Mirrors the definition in clang/AST/TypeLoc.h in order to print names.
  15. #define ABSTRACT_TYPE(Class, Base)
  16. #define TYPE(Class, Base) \
  17. case clang::TypeLoc::Class: \
  18. return #Class;
  19. #include "clang/AST/TypeNodes.inc"
  20. case clang::TypeLoc::Qualified:
  21. return "Qualified";
  22. }
  23. }
  24. // Returns a string for the type.
  25. auto VarDecl::GetTypeStr(const clang::VarDecl& decl) -> std::string {
  26. // Built a vector of class information, because we'll be traversing reverse
  27. // order to construct the final type.
  28. auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
  29. std::vector<std::pair<clang::TypeLoc::TypeLocClass, std::string>> segments;
  30. while (!type_loc.isNull()) {
  31. std::string text;
  32. auto qualifiers = type_loc.getType().getLocalQualifiers();
  33. std::string qual_str;
  34. if (!qualifiers.empty()) {
  35. qual_str = qualifiers.getAsString();
  36. }
  37. auto range =
  38. clang::CharSourceRange::getTokenRange(type_loc.getLocalSourceRange());
  39. std::string range_str = GetSourceText(range).str();
  40. // Make a list of segments with their TypeLocClass for reconstruction of the
  41. // string. Locally, we will have a qualifier (such as `const`) and a type
  42. // string (such as `int`) which is also used.
  43. auto type_loc_class = type_loc.getTypeLocClass();
  44. if (qual_str.empty()) {
  45. segments.push_back({type_loc_class, range_str});
  46. } else if (range_str.empty()) {
  47. segments.push_back({type_loc_class, qual_str});
  48. } else {
  49. segments.push_back({type_loc_class, qual_str + " " + range_str});
  50. }
  51. type_loc = type_loc.getNextTypeLoc();
  52. }
  53. // Construct the final type based on the class of each step. This reverses to
  54. // start from the "inside" of the type and go "out" when constructing
  55. // type_str.
  56. std::string type_str;
  57. auto prev_class = clang::TypeLoc::Auto; // Placeholder class, used in loop.
  58. for (const auto& [type_loc_class, text] : llvm::reverse(segments)) {
  59. switch (type_loc_class) {
  60. case clang::TypeLoc::Elaborated:
  61. type_str.insert(0, text);
  62. break;
  63. case clang::TypeLoc::Qualified:
  64. if (prev_class == clang::TypeLoc::Pointer) {
  65. type_str += " " + text;
  66. } else {
  67. if (!type_str.empty()) {
  68. type_str.insert(0, " ");
  69. }
  70. type_str.insert(0, text);
  71. }
  72. break;
  73. default:
  74. type_str += text;
  75. break;
  76. }
  77. prev_class = type_loc_class;
  78. }
  79. return type_str;
  80. }
  81. void VarDecl::Run() {
  82. const auto& decl = GetNodeAsOrDie<clang::VarDecl>(Label);
  83. if (decl.getTypeSourceInfo() == nullptr) {
  84. // TODO: Need to understand what's happening in this case. Not sure if we
  85. // need to address it.
  86. return;
  87. }
  88. std::string after;
  89. if (decl.getType().isConstQualified()) {
  90. after = "let ";
  91. } else if (!llvm::isa<clang::ParmVarDecl>(&decl)) {
  92. // Start the replacement with "var" unless it's a parameter.
  93. after = "var ";
  94. }
  95. // Add "identifier: type" to the replacement.
  96. after += decl.getNameAsString() + ": " + GetTypeStr(decl);
  97. // This decides the range to replace. Normally the entire decl is replaced,
  98. // but for code like `int i, j` we need to detect the comma between the
  99. // declared names. That case currently results in `var i: int, var j: int`.
  100. // If there's a comma, this range will be non-empty.
  101. auto type_loc = decl.getTypeSourceInfo()->getTypeLoc();
  102. clang::SourceLocation after_type_loc = clang::Lexer::getLocForEndOfToken(
  103. type_loc.getEndLoc(), 0, GetSourceManager(), GetLangOpts());
  104. llvm::StringRef comma_source_text = GetSourceText(
  105. clang::CharSourceRange::getCharRange(after_type_loc, decl.getLocation()));
  106. bool has_comma = !comma_source_text.trim().empty();
  107. clang::CharSourceRange replace_range = clang::CharSourceRange::getTokenRange(
  108. has_comma ? decl.getLocation() : decl.getBeginLoc(), decl.getEndLoc());
  109. AddReplacement(replace_range, after);
  110. }
  111. auto VarDeclFactory::GetAstMatcher() -> cam::DeclarationMatcher {
  112. return cam::varDecl(cam::unless(cam::hasParent(cam::declStmt(
  113. cam::hasParent(cam::cxxForRangeStmt())))))
  114. .bind(Label);
  115. }
  116. } // namespace Carbon