ASTSelectionRequirements.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include "clang/AST/Attr.h"
  10. using namespace clang;
  11. using namespace tooling;
  12. Expected<SelectedASTNode>
  13. ASTSelectionRequirement::evaluate(RefactoringRuleContext &Context) const {
  14. // FIXME: Memoize so that selection is evaluated only once.
  15. Expected<SourceRange> Range =
  16. SourceRangeSelectionRequirement::evaluate(Context);
  17. if (!Range)
  18. return Range.takeError();
  19. Optional<SelectedASTNode> Selection =
  20. findSelectedASTNodes(Context.getASTContext(), *Range);
  21. if (!Selection)
  22. return Context.createDiagnosticError(
  23. Range->getBegin(), diag::err_refactor_selection_invalid_ast);
  24. return std::move(*Selection);
  25. }
  26. Expected<CodeRangeASTSelection> CodeRangeASTSelectionRequirement::evaluate(
  27. RefactoringRuleContext &Context) const {
  28. // FIXME: Memoize so that selection is evaluated only once.
  29. Expected<SelectedASTNode> ASTSelection =
  30. ASTSelectionRequirement::evaluate(Context);
  31. if (!ASTSelection)
  32. return ASTSelection.takeError();
  33. std::unique_ptr<SelectedASTNode> StoredSelection =
  34. std::make_unique<SelectedASTNode>(std::move(*ASTSelection));
  35. Optional<CodeRangeASTSelection> CodeRange = CodeRangeASTSelection::create(
  36. Context.getSelectionRange(), *StoredSelection);
  37. if (!CodeRange)
  38. return Context.createDiagnosticError(
  39. Context.getSelectionRange().getBegin(),
  40. diag::err_refactor_selection_invalid_ast);
  41. Context.setASTSelection(std::move(StoredSelection));
  42. return std::move(*CodeRange);
  43. }