PopulateSwitch.cpp
5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===--- PopulateSwitch.cpp --------------------------------------*- C++-*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Tweak that populates an empty switch statement of an enumeration type with
// all of the enumerators of that type.
//
// Before:
// enum Color { RED, GREEN, BLUE };
//
// void f(Color color) {
// switch (color) {}
// }
//
// After:
// enum Color { RED, GREEN, BLUE };
//
// void f(Color color) {
// switch (color) {
// case RED:
// case GREEN:
// case BLUE:
// break;
// }
// }
//
//===----------------------------------------------------------------------===//
#include "AST.h"
#include "Selection.h"
#include "refactor/Tweak.h"
#include "support/Logger.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/Type.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/SmallSet.h"
#include <cassert>
#include <string>
namespace clang {
namespace clangd {
namespace {
class PopulateSwitch : public Tweak {
const char *id() const override;
bool prepare(const Selection &Sel) override;
Expected<Effect> apply(const Selection &Sel) override;
std::string title() const override { return "Populate switch"; }
llvm::StringLiteral kind() const override {
return CodeAction::QUICKFIX_KIND;
}
private:
const DeclContext *DeclCtx = nullptr;
const SwitchStmt *Switch = nullptr;
const CompoundStmt *Body = nullptr;
const EnumType *EnumT = nullptr;
const EnumDecl *EnumD = nullptr;
};
REGISTER_TWEAK(PopulateSwitch)
bool PopulateSwitch::prepare(const Selection &Sel) {
const SelectionTree::Node *CA = Sel.ASTSelection.commonAncestor();
if (!CA)
return false;
const Stmt *CAStmt = CA->ASTNode.get<Stmt>();
if (!CAStmt)
return false;
// Go up a level if we see a compound statement.
// switch (value) {}
// ^^
if (isa<CompoundStmt>(CAStmt)) {
CA = CA->Parent;
if (!CA)
return false;
CAStmt = CA->ASTNode.get<Stmt>();
if (!CAStmt)
return false;
}
DeclCtx = &CA->getDeclContext();
Switch = dyn_cast<SwitchStmt>(CAStmt);
if (!Switch)
return false;
Body = dyn_cast<CompoundStmt>(Switch->getBody());
if (!Body)
return false;
const Expr *Cond = Switch->getCond();
if (!Cond)
return false;
// Ignore implicit casts, since enums implicitly cast to integer types.
Cond = Cond->IgnoreParenImpCasts();
EnumT = Cond->getType()->getAsAdjusted<EnumType>();
if (!EnumT)
return false;
EnumD = EnumT->getDecl();
if (!EnumD)
return false;
// We trigger if there are fewer cases than enum values (and no case covers
// multiple values). This guarantees we'll have at least one case to insert.
// We don't yet determine what the cases are, as that means evaluating
// expressions.
auto I = EnumD->enumerator_begin();
auto E = EnumD->enumerator_end();
for (const SwitchCase *CaseList = Switch->getSwitchCaseList();
CaseList && I != E; CaseList = CaseList->getNextSwitchCase(), I++) {
// Default likely intends to cover cases we'd insert.
if (isa<DefaultStmt>(CaseList))
return false;
const CaseStmt *CS = cast<CaseStmt>(CaseList);
// Case statement covers multiple values, so just counting doesn't work.
if (CS->caseStmtIsGNURange())
return false;
// Case expression is not a constant expression or is value-dependent,
// so we may not be able to work out which cases are covered.
const ConstantExpr *CE = dyn_cast<ConstantExpr>(CS->getLHS());
if (!CE || CE->isValueDependent())
return false;
}
// Only suggest tweak if we have more enumerators than cases.
return I != E;
}
Expected<Tweak::Effect> PopulateSwitch::apply(const Selection &Sel) {
ASTContext &Ctx = Sel.AST->getASTContext();
// Get the enum's integer width and signedness, for adjusting case literals.
unsigned EnumIntWidth = Ctx.getIntWidth(QualType(EnumT, 0));
bool EnumIsSigned = EnumT->isSignedIntegerOrEnumerationType();
llvm::SmallSet<llvm::APSInt, 32> ExistingEnumerators;
for (const SwitchCase *CaseList = Switch->getSwitchCaseList(); CaseList;
CaseList = CaseList->getNextSwitchCase()) {
const CaseStmt *CS = cast<CaseStmt>(CaseList);
assert(!CS->caseStmtIsGNURange());
const ConstantExpr *CE = cast<ConstantExpr>(CS->getLHS());
assert(!CE->isValueDependent());
llvm::APSInt Val = CE->getResultAsAPSInt();
Val = Val.extOrTrunc(EnumIntWidth);
Val.setIsSigned(EnumIsSigned);
ExistingEnumerators.insert(Val);
}
SourceLocation Loc = Body->getRBracLoc();
ASTContext &DeclASTCtx = DeclCtx->getParentASTContext();
std::string Text;
for (EnumConstantDecl *Enumerator : EnumD->enumerators()) {
if (ExistingEnumerators.contains(Enumerator->getInitVal()))
continue;
Text += "case ";
Text += getQualification(DeclASTCtx, DeclCtx, Loc, EnumD);
if (EnumD->isScoped()) {
Text += EnumD->getName();
Text += "::";
}
Text += Enumerator->getName();
Text += ":";
}
assert(!Text.empty() && "No enumerators to insert!");
Text += "break;";
const SourceManager &SM = Ctx.getSourceManager();
return Effect::mainFileEdit(
SM, tooling::Replacements(tooling::Replacement(SM, Loc, 0, Text)));
}
} // namespace
} // namespace clangd
} // namespace clang