clang  19.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 "clang/AST/Attr.h"
11 #include <optional>
12 
13 using namespace clang;
14 using namespace tooling;
15 
18  // FIXME: Memoize so that selection is evaluated only once.
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 
33  RefactoringRuleContext &Context) const {
34  // FIXME: Memoize so that selection is evaluated only once.
35  Expected<SelectedASTNode> ASTSelection =
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 }
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.