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