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/RecursiveASTVisitor.h"
14 #include "clang/AST/Stmt.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Tooling/Core/Replacement.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Error.h"
25
26 namespace clang {
27 namespace clangd {
28 namespace {
29 /// Swaps the 'then' and 'else' branch of the if statement.
30 /// Before:
31 /// if (foo) { return 10; } else { continue; }
32 /// ^^^^^^^ ^^^^
33 /// After:
34 /// if (foo) { continue; } else { return 10; }
35 class SwapIfBranches : public Tweak {
36 public:
37 const char *id() const override final;
38
39 bool prepare(const Selection &Inputs) override;
40 Expected<Effect> apply(const Selection &Inputs) override;
title() const41 std::string title() const override { return "Swap if branches"; }
kind() const42 llvm::StringLiteral kind() const override {
43 return CodeAction::REFACTOR_KIND;
44 }
hidden() const45 bool hidden() const override { return true; }
46
47 private:
48 const IfStmt *If = nullptr;
49 };
50
REGISTER_TWEAK(SwapIfBranches)51 REGISTER_TWEAK(SwapIfBranches)
52
53 bool SwapIfBranches::prepare(const Selection &Inputs) {
54 for (const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
55 N && !If; N = N->Parent) {
56 // Stop once we hit a block, e.g. a lambda in the if condition.
57 if (dyn_cast_or_null<CompoundStmt>(N->ASTNode.get<Stmt>()))
58 return false;
59 If = dyn_cast_or_null<IfStmt>(N->ASTNode.get<Stmt>());
60 }
61 // avoid dealing with single-statement brances, they require careful handling
62 // to avoid changing semantics of the code (i.e. dangling else).
63 return If && dyn_cast_or_null<CompoundStmt>(If->getThen()) &&
64 dyn_cast_or_null<CompoundStmt>(If->getElse());
65 }
66
apply(const Selection & Inputs)67 Expected<Tweak::Effect> SwapIfBranches::apply(const Selection &Inputs) {
68 auto &Ctx = Inputs.AST->getASTContext();
69 auto &SrcMgr = Inputs.AST->getSourceManager();
70
71 auto ThenRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
72 If->getThen()->getSourceRange());
73 if (!ThenRng)
74 return error("Could not obtain range of the 'then' branch. Macros?");
75 auto ElseRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
76 If->getElse()->getSourceRange());
77 if (!ElseRng)
78 return error("Could not obtain range of the 'else' branch. Macros?");
79
80 auto ThenCode = toSourceCode(SrcMgr, *ThenRng);
81 auto ElseCode = toSourceCode(SrcMgr, *ElseRng);
82
83 tooling::Replacements Result;
84 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
85 ThenRng->getBegin(),
86 ThenCode.size(), ElseCode)))
87 return std::move(Err);
88 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
89 ElseRng->getBegin(),
90 ElseCode.size(), ThenCode)))
91 return std::move(Err);
92 return Effect::mainFileEdit(SrcMgr, std::move(Result));
93 }
94
95 } // namespace
96 } // namespace clangd
97 } // namespace clang
98