[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clangd / refactor / tweaks / SwapIfBranches.cpp
blobb59f1d63f3f0e499c7af938a658f591de8163ef0
1 //===--- SwapIfBranches.cpp --------------------------------------*- C++-*-===//
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 #include "ParsedAST.h"
9 #include "SourceCode.h"
10 #include "refactor/Tweak.h"
11 #include "support/Logger.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Stmt.h"
14 #include "clang/Basic/LangOptions.h"
15 #include "clang/Basic/SourceLocation.h"
16 #include "clang/Basic/SourceManager.h"
17 #include "clang/Tooling/Core/Replacement.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Casting.h"
21 #include "llvm/Support/Error.h"
23 namespace clang {
24 namespace clangd {
25 namespace {
26 /// Swaps the 'then' and 'else' branch of the if statement.
27 /// Before:
28 /// if (foo) { return 10; } else { continue; }
29 /// ^^^^^^^ ^^^^
30 /// After:
31 /// if (foo) { continue; } else { return 10; }
32 class SwapIfBranches : public Tweak {
33 public:
34 const char *id() const final;
36 bool prepare(const Selection &Inputs) override;
37 Expected<Effect> apply(const Selection &Inputs) override;
38 std::string title() const override { return "Swap if branches"; }
39 llvm::StringLiteral kind() const override {
40 return CodeAction::REFACTOR_KIND;
42 bool hidden() const override { return true; }
44 private:
45 const IfStmt *If = nullptr;
48 REGISTER_TWEAK(SwapIfBranches)
50 bool SwapIfBranches::prepare(const Selection &Inputs) {
51 for (const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
52 N && !If; N = N->Parent) {
53 // Stop once we hit a block, e.g. a lambda in the if condition.
54 if (llvm::isa_and_nonnull<CompoundStmt>(N->ASTNode.get<Stmt>()))
55 return false;
56 If = dyn_cast_or_null<IfStmt>(N->ASTNode.get<Stmt>());
58 // avoid dealing with single-statement brances, they require careful handling
59 // to avoid changing semantics of the code (i.e. dangling else).
60 return If && isa_and_nonnull<CompoundStmt>(If->getThen()) &&
61 isa_and_nonnull<CompoundStmt>(If->getElse());
64 Expected<Tweak::Effect> SwapIfBranches::apply(const Selection &Inputs) {
65 auto &Ctx = Inputs.AST->getASTContext();
66 auto &SrcMgr = Inputs.AST->getSourceManager();
68 auto ThenRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
69 If->getThen()->getSourceRange());
70 if (!ThenRng)
71 return error("Could not obtain range of the 'then' branch. Macros?");
72 auto ElseRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
73 If->getElse()->getSourceRange());
74 if (!ElseRng)
75 return error("Could not obtain range of the 'else' branch. Macros?");
77 auto ThenCode = toSourceCode(SrcMgr, *ThenRng);
78 auto ElseCode = toSourceCode(SrcMgr, *ElseRng);
80 tooling::Replacements Result;
81 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
82 ThenRng->getBegin(),
83 ThenCode.size(), ElseCode)))
84 return std::move(Err);
85 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
86 ElseRng->getBegin(),
87 ElseCode.size(), ThenCode)))
88 return std::move(Err);
89 return Effect::mainFileEdit(SrcMgr, std::move(Result));
92 } // namespace
93 } // namespace clangd
94 } // namespace clang