var_decl.cpp 5.3 KB

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