ASTSelectionRequirements.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //===--- ASTSelectionRequirements.cpp - Clang refactoring library ---------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h"
  9. using namespace clang;
  10. using namespace tooling;
  11. Expected<SelectedASTNode>
  12. ASTSelectionRequirement::evaluate(RefactoringRuleContext &Context) const {
  13. // FIXME: Memoize so that selection is evaluated only once.
  14. Expected<SourceRange> Range =
  15. SourceRangeSelectionRequirement::evaluate(Context);
  16. if (!Range)
  17. return Range.takeError();
  18. Optional<SelectedASTNode> Selection =
  19. findSelectedASTNodes(Context.getASTContext(), *Range);
  20. if (!Selection)
  21. return Context.createDiagnosticError(
  22. Range->getBegin(), diag::err_refactor_selection_invalid_ast);
  23. return std::move(*Selection);
  24. }
  25. Expected<CodeRangeASTSelection> CodeRangeASTSelectionRequirement::evaluate(
  26. RefactoringRuleContext &Context) const {
  27. // FIXME: Memoize so that selection is evaluated only once.
  28. Expected<SelectedASTNode> ASTSelection =
  29. ASTSelectionRequirement::evaluate(Context);
  30. if (!ASTSelection)
  31. return ASTSelection.takeError();
  32. std::unique_ptr<SelectedASTNode> StoredSelection =
  33. std::make_unique<SelectedASTNode>(std::move(*ASTSelection));
  34. Optional<CodeRangeASTSelection> CodeRange = CodeRangeASTSelection::create(
  35. Context.getSelectionRange(), *StoredSelection);
  36. if (!CodeRange)
  37. return Context.createDiagnosticError(
  38. Context.getSelectionRange().getBegin(),
  39. diag::err_refactor_selection_invalid_ast);
  40. Context.setASTSelection(std::move(StoredSelection));
  41. return std::move(*CodeRange);
  42. }