for_range.cpp 972 B

1234567891011121314151617181920212223242526272829
  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/for_range.h"
  5. #include "clang/ASTMatchers/ASTMatchers.h"
  6. namespace cam = ::clang::ast_matchers;
  7. namespace Carbon {
  8. static constexpr char Label[] = "ForRange";
  9. void ForRange::Run() {
  10. const auto& stmt = GetNodeAsOrDie<clang::CXXForRangeStmt>(Label);
  11. // Wrap `in` with spaces so that `for (auto i:items)` has valid results.
  12. AddReplacement(clang::CharSourceRange::getTokenRange(stmt.getColonLoc(),
  13. stmt.getColonLoc()),
  14. " in ");
  15. }
  16. void ForRangeFactory::AddMatcher(cam::MatchFinder* finder,
  17. cam::MatchFinder::MatchCallback* callback) {
  18. finder->addMatcher(cam::cxxForRangeStmt().bind(Label), callback);
  19. }
  20. } // namespace Carbon