ASTSelectionRequirements.cpp 1.9 KB

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