Skip to content

Commit 09f494a

Browse files
added code for switch to if
1 parent 4508f44 commit 09f494a

File tree

4 files changed

+383
-0
lines changed

4 files changed

+383
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- SwitchToIf.h - Switch to if refactoring -------------------------===//
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+
#ifndef LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H
10+
#define LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H
11+
12+
#include "clang/Tooling/Refactoring/ASTSelection.h"
13+
#include "clang/Tooling/Refactoring/RefactoringActionRules.h"
14+
15+
namespace clang {
16+
class SwitchStmt;
17+
18+
namespace tooling {
19+
20+
/// A "Switch to If" refactoring converts a switch statement into an if-else
21+
/// chain.
22+
class SwitchToIf final : public SourceChangeRefactoringRule {
23+
public:
24+
/// Initiates the switch-to-if refactoring operation.
25+
///
26+
/// \param Selection The selected AST node, which should be a switch statement.
27+
static Expected<SwitchToIf>
28+
initiate(RefactoringRuleContext &Context,
29+
SelectedASTNode Selection);
30+
31+
static const RefactoringDescriptor &describe();
32+
33+
private:
34+
SwitchToIf(const SwitchStmt *Switch) : TheSwitch(Switch) {}
35+
36+
Expected<AtomicChanges>
37+
createSourceReplacements(RefactoringRuleContext &Context) override;
38+
39+
const SwitchStmt *TheSwitch;
40+
};
41+
42+
} // end namespace tooling
43+
} // end namespace clang
44+
45+
#endif // LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H
46+

clang/lib/Tooling/Refactoring/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_clang_library(clangToolingRefactoring
1313
Rename/USRFinder.cpp
1414
Rename/USRFindingAction.cpp
1515
Rename/USRLocFinder.cpp
16+
SwitchToIf/SwitchToIf.cpp
1617

1718
LINK_LIBS
1819
clangAST

clang/lib/Tooling/Refactoring/RefactoringActions.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "clang/Tooling/Refactoring/RefactoringAction.h"
1111
#include "clang/Tooling/Refactoring/RefactoringOptions.h"
1212
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
13+
#include "clang/Tooling/Refactoring/SwitchToIf/SwitchToIf.h"
1314

1415
namespace clang {
1516
namespace tooling {
@@ -93,13 +94,32 @@ class LocalRename final : public RefactoringAction {
9394
}
9495
};
9596

97+
class SwitchToIfRefactoring final : public RefactoringAction {
98+
public:
99+
StringRef getCommand() const override { return "switch-to-if"; }
100+
101+
StringRef getDescription() const override {
102+
return "Converts a switch statement into an if-else chain";
103+
}
104+
105+
/// Returns a set of refactoring actions rules that are defined by this
106+
/// action.
107+
RefactoringActionRules createActionRules() const override {
108+
RefactoringActionRules Rules;
109+
Rules.push_back(createRefactoringActionRule<SwitchToIf>(
110+
ASTSelectionRequirement()));
111+
return Rules;
112+
}
113+
};
114+
96115
} // end anonymous namespace
97116

98117
std::vector<std::unique_ptr<RefactoringAction>> createRefactoringActions() {
99118
std::vector<std::unique_ptr<RefactoringAction>> Actions;
100119

101120
Actions.push_back(std::make_unique<LocalRename>());
102121
Actions.push_back(std::make_unique<ExtractRefactoring>());
122+
Actions.push_back(std::make_unique<SwitchToIfRefactoring>());
103123

104124
return Actions;
105125
}

0 commit comments

Comments
 (0)