| 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 |
|
| 13 |
using namespace clang; |
| 14 |
using namespace tooling; |
| 15 |
|
| 16 |
Expected<SelectedASTNode> |
| 17 |
ASTSelectionRequirement::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.getASTContext(), *Range); |
| 26 |
if (!Selection) |
| 27 |
return Context.createDiagnosticError( |
| 28 |
Range->getBegin(), diag::err_refactor_selection_invalid_ast); |
| 29 |
return std::move(*Selection); |
| 30 |
} |
| 31 |
|
| 32 |
Expected<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>(std::move(*ASTSelection)); |
| 41 |
std::optional<CodeRangeASTSelection> CodeRange = |
| 42 |
CodeRangeASTSelection::create(Context.getSelectionRange(), |
| 43 |
*StoredSelection); |
| 44 |
if (!CodeRange) |
| 45 |
return Context.createDiagnosticError( |
| 46 |
Context.getSelectionRange().getBegin(), |
| 47 |
diag::err_refactor_selection_invalid_ast); |
| 48 |
Context.setASTSelection(std::move(StoredSelection)); |
| 49 |
return std::move(*CodeRange); |
| 50 |
} |