clang-tools 10.0.0

SwapIfBranches.cpp
Go to the documentation of this file.
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 "Logger.h"
9 #include "ParsedAST.h"
10 #include "SourceCode.h"
11 #include "refactor/Tweak.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;
41 std::string title() const override { return "Swap if branches"; }
42 Intent intent() const override { return Refactor; }
43
44 private:
45 const IfStmt *If = nullptr;
46 };
47
48 REGISTER_TWEAK(SwapIfBranches)
49
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 (dyn_cast_or_null<CompoundStmt>(N->ASTNode.get<Stmt>()))
55 return false;
56 If = dyn_cast_or_null<IfStmt>(N->ASTNode.get<Stmt>());
57 }
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 && dyn_cast_or_null<CompoundStmt>(If->getThen()) &&
61 dyn_cast_or_null<CompoundStmt>(If->getElse());
62 }
63
64 Expected<Tweak::Effect> SwapIfBranches::apply(const Selection &Inputs) {
65 auto &Ctx = Inputs.AST->getASTContext();
66 auto &SrcMgr = Inputs.AST->getSourceManager();
67
68 auto ThenRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
69 If->getThen()->getSourceRange());
70 if (!ThenRng)
71 return llvm::createStringError(
72 llvm::inconvertibleErrorCode(),
73 "Could not obtain range of the 'then' branch. Macros?");
74 auto ElseRng = toHalfOpenFileRange(SrcMgr, Ctx.getLangOpts(),
75 If->getElse()->getSourceRange());
76 if (!ElseRng)
77 return llvm::createStringError(
78 llvm::inconvertibleErrorCode(),
79 "Could not obtain range of the 'else' branch. Macros?");
80
81 auto ThenCode = toSourceCode(SrcMgr, *ThenRng);
82 auto ElseCode = toSourceCode(SrcMgr, *ElseRng);
83
84 tooling::Replacements Result;
85 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
86 ThenRng->getBegin(),
87 ThenCode.size(), ElseCode)))
88 return std::move(Err);
89 if (auto Err = Result.add(tooling::Replacement(Ctx.getSourceManager(),
90 ElseRng->getBegin(),
91 ElseCode.size(), ThenCode)))
92 return std::move(Err);
93 return Effect::mainFileEdit(SrcMgr, std::move(Result));
94 }
95
96 } // namespace
97 } // namespace clangd
98 } // namespace clang
SourceCode.h
REGISTER_TWEAK
#define REGISTER_TWEAK(Subclass)
Definition: Tweak.h:129
Tweak.h
Ctx
Context Ctx
Definition: TUScheduler.cpp:237
clang::clangd::toHalfOpenFileRange
llvm::Optional< SourceRange > toHalfOpenFileRange(const SourceManager &SM, const LangOptions &LangOpts, SourceRange R)
Turns a token range into a half-open range and checks its correctness.
Definition: SourceCode.cpp:538
Logger.h
clang::clangd::toSourceCode
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
Definition: SourceCode.cpp:560
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ParsedAST.h
Expected
std::vector< const char * > Expected
Definition: PrintASTTests.cpp:27