clang 22.0.0git
ASTSelectionRequirements.cpp
Go to the documentation of this file.
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
10#include <optional>
11
12using namespace clang;
13using namespace tooling;
14
17 // FIXME: Memoize so that selection is evaluated only once.
20 if (!Range)
21 return Range.takeError();
22
23 std::optional<SelectedASTNode> Selection =
24 findSelectedASTNodes(Context.getASTContext(), *Range);
25 if (!Selection)
26 return Context.createDiagnosticError(
27 Range->getBegin(), diag::err_refactor_selection_invalid_ast);
28 return std::move(*Selection);
29}
30
32 RefactoringRuleContext &Context) const {
33 // FIXME: Memoize so that selection is evaluated only once.
34 Expected<SelectedASTNode> ASTSelection =
36 if (!ASTSelection)
37 return ASTSelection.takeError();
38 std::unique_ptr<SelectedASTNode> StoredSelection =
39 std::make_unique<SelectedASTNode>(std::move(*ASTSelection));
40 std::optional<CodeRangeASTSelection> CodeRange =
41 CodeRangeASTSelection::create(Context.getSelectionRange(),
42 *StoredSelection);
43 if (!CodeRange)
44 return Context.createDiagnosticError(
45 Context.getSelectionRange().getBegin(),
46 diag::err_refactor_selection_invalid_ast);
47 Context.setASTSelection(std::move(StoredSelection));
48 return std::move(*CodeRange);
49}
Expected< SelectedASTNode > evaluate(RefactoringRuleContext &Context) const
Expected< CodeRangeASTSelection > evaluate(RefactoringRuleContext &Context) const
static std::optional< CodeRangeASTSelection > create(SourceRange SelectionRange, const SelectedASTNode &ASTSelection)
A source range independent of the SourceManager.
Definition Replacement.h:44
The refactoring rule context stores all of the inputs that might be needed by a refactoring action ru...
Expected< SourceRange > evaluate(RefactoringRuleContext &Context) const
std::optional< SelectedASTNode > findSelectedASTNodes(const ASTContext &Context, SourceRange SelectionRange)
Traverses the given ASTContext and creates a tree of selected AST nodes.
The JSON file list parser is used to communicate input to InstallAPI.