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