Clang Project

clang_source_code/lib/Tooling/Refactoring/ASTSelectionRequirements.cpp
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
11using namespace clang;
12using namespace tooling;
13
14Expected<SelectedASTNode>
15ASTSelectionRequirement::evaluate(RefactoringRuleContext &Contextconst {
16  // FIXME: Memoize so that selection is evaluated only once.
17  Expected<SourceRangeRange =
18      SourceRangeSelectionRequirement::evaluate(Context);
19  if (!Range)
20    return Range.takeError();
21
22  Optional<SelectedASTNodeSelection =
23      findSelectedASTNodes(Context.getASTContext(), *Range);
24  if (!Selection)
25    return Context.createDiagnosticError(
26        Range->getBegin(), diag::err_refactor_selection_invalid_ast);
27  return std::move(*Selection);
28}
29
30Expected<CodeRangeASTSelectionCodeRangeASTSelectionRequirement::evaluate(
31    RefactoringRuleContext &Contextconst {
32  // FIXME: Memoize so that selection is evaluated only once.
33  Expected<SelectedASTNodeASTSelection =
34      ASTSelectionRequirement::evaluate(Context);
35  if (!ASTSelection)
36    return ASTSelection.takeError();
37  std::unique_ptr<SelectedASTNodeStoredSelection =
38      llvm::make_unique<SelectedASTNode>(std::move(*ASTSelection));
39  Optional<CodeRangeASTSelectionCodeRange = CodeRangeASTSelection::create(
40      Context.getSelectionRange(), *StoredSelection);
41  if (!CodeRange)
42    return Context.createDiagnosticError(
43        Context.getSelectionRange().getBegin(),
44        diag::err_refactor_selection_invalid_ast);
45  Context.setASTSelection(std::move(StoredSelection));
46  return std::move(*CodeRange);
47}
48
clang::tooling::ASTSelectionRequirement::evaluate
clang::tooling::CodeRangeASTSelectionRequirement::evaluate