teak-llvm/clang/lib/Tooling/Refactoring/ASTSelectionRequirements.cpp
Alex Lorenz 7fe441b20d [refactor] Initial outline of implementation of "extract function" refactoring
This commit adds an initial, skeleton outline of the "extract function"
refactoring. The extracted function doesn't capture variables / rewrite code
yet, it just basically does a simple copy-paste.
The following initiation rules are specified:

- extraction can only be done for executable code in a function/method/block.
  This means that you can't extract a global variable initialize into a function
  right now.
- simple literals and references are not extractable.

This commit also adds support for full source ranges to clang-refactor's test
mode.

Differential Revision: https://reviews.llvm.org/D38982

llvm-svn: 316465
2017-10-24 17:18:45 +00:00

49 lines
1.8 KiB
C++

//===--- ASTSelectionRequirements.cpp - Clang refactoring library ---------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h"
using namespace clang;
using namespace tooling;
Expected<SelectedASTNode>
ASTSelectionRequirement::evaluate(RefactoringRuleContext &Context) const {
// FIXME: Memoize so that selection is evaluated only once.
Expected<SourceRange> Range =
SourceRangeSelectionRequirement::evaluate(Context);
if (!Range)
return Range.takeError();
Optional<SelectedASTNode> Selection =
findSelectedASTNodes(Context.getASTContext(), *Range);
if (!Selection)
return Context.createDiagnosticError(
Range->getBegin(), diag::err_refactor_selection_invalid_ast);
return std::move(*Selection);
}
Expected<CodeRangeASTSelection> CodeRangeASTSelectionRequirement::evaluate(
RefactoringRuleContext &Context) const {
// FIXME: Memoize so that selection is evaluated only once.
Expected<SelectedASTNode> ASTSelection =
ASTSelectionRequirement::evaluate(Context);
if (!ASTSelection)
return ASTSelection.takeError();
std::unique_ptr<SelectedASTNode> StoredSelection =
llvm::make_unique<SelectedASTNode>(std::move(*ASTSelection));
Optional<CodeRangeASTSelection> CodeRange = CodeRangeASTSelection::create(
Context.getSelectionRange(), *StoredSelection);
if (!CodeRange)
return Context.createDiagnosticError(
Context.getSelectionRange().getBegin(),
diag::err_refactor_selection_invalid_ast);
Context.setASTSelection(std::move(StoredSelection));
return std::move(*CodeRange);
}