Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions clang/include/clang/Tooling/Refactoring/SwitchToIf/SwitchToIf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===--- SwitchToIf.h - Switch to if refactoring -------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H
#define LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H

#include "clang/Tooling/Refactoring/ASTSelection.h"
#include "clang/Tooling/Refactoring/RefactoringActionRules.h"

namespace clang {
class SwitchStmt;

namespace tooling {

/// A "Switch to If" refactoring converts a switch statement into an if-else
/// chain.
class SwitchToIf final : public SourceChangeRefactoringRule {
public:
/// Initiates the switch-to-if refactoring operation.
///
/// \param Selection The selected AST node, which should be a switch statement.
static Expected<SwitchToIf>
initiate(RefactoringRuleContext &Context,
SelectedASTNode Selection);

static const RefactoringDescriptor &describe();

private:
SwitchToIf(const SwitchStmt *Switch) : TheSwitch(Switch) {}

Expected<AtomicChanges>
createSourceReplacements(RefactoringRuleContext &Context) override;

const SwitchStmt *TheSwitch;
};

} // end namespace tooling
} // end namespace clang

#endif // LLVM_CLANG_TOOLING_REFACTORING_SWITCHTOIF_SWITCHTOIF_H

1 change: 1 addition & 0 deletions clang/lib/Tooling/Refactoring/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_clang_library(clangToolingRefactoring
Rename/USRFinder.cpp
Rename/USRFindingAction.cpp
Rename/USRLocFinder.cpp
SwitchToIf/SwitchToIf.cpp

LINK_LIBS
clangAST
Expand Down
20 changes: 20 additions & 0 deletions clang/lib/Tooling/Refactoring/RefactoringActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "clang/Tooling/Refactoring/RefactoringAction.h"
#include "clang/Tooling/Refactoring/RefactoringOptions.h"
#include "clang/Tooling/Refactoring/Rename/RenamingAction.h"
#include "clang/Tooling/Refactoring/SwitchToIf/SwitchToIf.h"

namespace clang {
namespace tooling {
Expand Down Expand Up @@ -93,13 +94,32 @@ class LocalRename final : public RefactoringAction {
}
};

class SwitchToIfRefactoring final : public RefactoringAction {
public:
StringRef getCommand() const override { return "switch-to-if"; }

StringRef getDescription() const override {
return "Converts a switch statement into an if-else chain";
}

/// Returns a set of refactoring actions rules that are defined by this
/// action.
RefactoringActionRules createActionRules() const override {
RefactoringActionRules Rules;
Rules.push_back(createRefactoringActionRule<SwitchToIf>(
ASTSelectionRequirement()));
return Rules;
}
};

} // end anonymous namespace

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

Actions.push_back(std::make_unique<LocalRename>());
Actions.push_back(std::make_unique<ExtractRefactoring>());
Actions.push_back(std::make_unique<SwitchToIfRefactoring>());

return Actions;
}
Expand Down
Loading