Skip to content

Commit d631dae

Browse files
author
Игнат Сергеев
committed
Add source location requirement
Added source location requirement for refactoring engine
1 parent b331664 commit d631dae

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

clang/include/clang/Basic/DiagnosticRefactoringKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def err_refactor_extract_simple_expression : Error<"the selected expression "
2828
def err_refactor_extract_prohibited_expression : Error<"the selected "
2929
"expression can't be extracted">;
3030

31+
def err_refactor_no_location : Error<"refactoring action can't be initiated "
32+
"without a location">;
3133
}
3234

3335
} // end of Refactoring diagnostics

clang/include/clang/Tooling/Refactoring/RefactoringActionRule.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class RefactoringActionRule : public RefactoringActionRuleBase {
5656
/// to be fulfilled before refactoring can be performed.
5757
virtual bool hasSelectionRequirement() = 0;
5858

59+
/// Returns true when the rule has a source location requirement that has
60+
/// to be fulfilled before refactoring can be performed.
61+
virtual bool hasLocationRequirement() = 0;
62+
5963
/// Traverses each refactoring option used by the rule and invokes the
6064
/// \c visit callback in the consumer for each option.
6165
///

clang/include/clang/Tooling/Refactoring/RefactoringActionRuleRequirements.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_CLANG_TOOLING_REFACTORING_REFACTORINGACTIONRULEREQUIREMENTS_H
1111

1212
#include "clang/Basic/LLVM.h"
13+
#include "clang/Basic/SourceLocation.h"
1314
#include "clang/Tooling/Refactoring/ASTSelection.h"
1415
#include "clang/Tooling/Refactoring/RefactoringDiagnostic.h"
1516
#include "clang/Tooling/Refactoring/RefactoringOption.h"
@@ -77,6 +78,17 @@ class CodeRangeASTSelectionRequirement : public ASTSelectionRequirement {
7778
evaluate(RefactoringRuleContext &Context) const;
7879
};
7980

81+
/// A base class for any requirement that expects source code position (or the
82+
/// refactoring tool with the -location option).
83+
class SourceLocationRequirement : public RefactoringActionRuleRequirement {
84+
public:
85+
Expected<SourceLocation> evaluate(RefactoringRuleContext &Context) const {
86+
if (Context.getLocation().isValid())
87+
return Context.getLocation();
88+
return Context.createDiagnosticError(diag::err_refactor_no_location);
89+
}
90+
};
91+
8092
/// A base class for any requirement that requires some refactoring options.
8193
class RefactoringOptionsRequirement : public RefactoringActionRuleRequirement {
8294
public:

clang/include/clang/Tooling/Refactoring/RefactoringActionRulesInternal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ createRefactoringActionRule(const RequirementTypes &... Requirements) {
139139
RequirementTypes...>::value;
140140
}
141141

142+
bool hasLocationRequirement() override {
143+
return internal::HasBaseOf<SourceLocationRequirement,
144+
RequirementTypes...>::value;
145+
}
146+
142147
void visitRefactoringOptions(RefactoringOptionVisitor &Visitor) override {
143148
internal::visitRefactoringOptions(
144149
Visitor, Requirements,

clang/include/clang/Tooling/Refactoring/RefactoringRuleContext.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ namespace tooling {
3030
///
3131
/// - SelectionRange: an optional source selection ranges that can be used
3232
/// to represent a selection in an editor.
33+
///
34+
/// - Location: an optional source location that can be used
35+
/// to represent a cursor in an editor.
3336
class RefactoringRuleContext {
3437
public:
3538
RefactoringRuleContext(const SourceManager &SM) : SM(SM) {}
@@ -40,8 +43,14 @@ class RefactoringRuleContext {
4043
/// refactoring engine. Can be invalid.
4144
SourceRange getSelectionRange() const { return SelectionRange; }
4245

46+
/// Returns the current source location as set by the
47+
/// refactoring engine. Can be invalid.
48+
SourceLocation getLocation() const { return Location; }
49+
4350
void setSelectionRange(SourceRange R) { SelectionRange = R; }
4451

52+
void setLocation(SourceLocation L) { Location = L; }
53+
4554
bool hasASTContext() const { return AST; }
4655

4756
ASTContext &getASTContext() const {
@@ -73,6 +82,9 @@ class RefactoringRuleContext {
7382
/// An optional source selection range that's commonly used to represent
7483
/// a selection in an editor.
7584
SourceRange SelectionRange;
85+
/// An optional source location that's commonly used to represent
86+
/// a cursor in an editor.
87+
SourceLocation Location;
7688
/// An optional AST for the translation unit on which a refactoring action
7789
/// might operate on.
7890
ASTContext *AST = nullptr;

0 commit comments

Comments
 (0)