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