17fe441b2SAlex Lorenz //===--- ASTSelectionRequirements.cpp - Clang refactoring library ---------===// 27fe441b2SAlex Lorenz // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 67fe441b2SAlex Lorenz // 77fe441b2SAlex Lorenz //===----------------------------------------------------------------------===// 87fe441b2SAlex Lorenz 97fe441b2SAlex Lorenz #include "clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h" 1060573ae6SReid Kleckner #include "clang/AST/Attr.h" 11a1580d7bSKazu Hirata #include <optional> 127fe441b2SAlex Lorenz 137fe441b2SAlex Lorenz using namespace clang; 147fe441b2SAlex Lorenz using namespace tooling; 157fe441b2SAlex Lorenz 167fe441b2SAlex Lorenz Expected<SelectedASTNode> evaluate(RefactoringRuleContext & Context) const177fe441b2SAlex LorenzASTSelectionRequirement::evaluate(RefactoringRuleContext &Context) const { 187fe441b2SAlex Lorenz // FIXME: Memoize so that selection is evaluated only once. 197fe441b2SAlex Lorenz Expected<SourceRange> Range = 207fe441b2SAlex Lorenz SourceRangeSelectionRequirement::evaluate(Context); 217fe441b2SAlex Lorenz if (!Range) 227fe441b2SAlex Lorenz return Range.takeError(); 237fe441b2SAlex Lorenz 24*6ad0788cSKazu Hirata std::optional<SelectedASTNode> Selection = 257fe441b2SAlex Lorenz findSelectedASTNodes(Context.getASTContext(), *Range); 267fe441b2SAlex Lorenz if (!Selection) 277fe441b2SAlex Lorenz return Context.createDiagnosticError( 287fe441b2SAlex Lorenz Range->getBegin(), diag::err_refactor_selection_invalid_ast); 297fe441b2SAlex Lorenz return std::move(*Selection); 307fe441b2SAlex Lorenz } 317fe441b2SAlex Lorenz evaluate(RefactoringRuleContext & Context) const327fe441b2SAlex LorenzExpected<CodeRangeASTSelection> CodeRangeASTSelectionRequirement::evaluate( 337fe441b2SAlex Lorenz RefactoringRuleContext &Context) const { 347fe441b2SAlex Lorenz // FIXME: Memoize so that selection is evaluated only once. 357fe441b2SAlex Lorenz Expected<SelectedASTNode> ASTSelection = 367fe441b2SAlex Lorenz ASTSelectionRequirement::evaluate(Context); 377fe441b2SAlex Lorenz if (!ASTSelection) 387fe441b2SAlex Lorenz return ASTSelection.takeError(); 397fe441b2SAlex Lorenz std::unique_ptr<SelectedASTNode> StoredSelection = 402b3d49b6SJonas Devlieghere std::make_unique<SelectedASTNode>(std::move(*ASTSelection)); 41*6ad0788cSKazu Hirata std::optional<CodeRangeASTSelection> CodeRange = 42*6ad0788cSKazu Hirata CodeRangeASTSelection::create(Context.getSelectionRange(), 43*6ad0788cSKazu Hirata *StoredSelection); 447fe441b2SAlex Lorenz if (!CodeRange) 457fe441b2SAlex Lorenz return Context.createDiagnosticError( 467fe441b2SAlex Lorenz Context.getSelectionRange().getBegin(), 477fe441b2SAlex Lorenz diag::err_refactor_selection_invalid_ast); 487fe441b2SAlex Lorenz Context.setASTSelection(std::move(StoredSelection)); 497fe441b2SAlex Lorenz return std::move(*CodeRange); 507fe441b2SAlex Lorenz } 51