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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
//===--- ObjCMemberwiseInitializer.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
//
//===----------------------------------------------------------------------===//
#include "ParsedAST.h"
#include "SourceCode.h"
#include "refactor/InsertionPoint.h"
#include "refactor/Tweak.h"
#include "support/Logger.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/Error.h"
namespace clang {
namespace clangd {
namespace {
static std::string capitalize(std::string Message) {
if (!Message.empty())
Message[0] = llvm::toUpper(Message[0]);
return Message;
}
static std::string getTypeStr(const QualType &OrigT, const Decl &D,
unsigned PropertyAttributes) {
QualType T = OrigT;
PrintingPolicy Policy(D.getASTContext().getLangOpts());
Policy.SuppressStrongLifetime = true;
std::string Prefix;
// If the nullability is specified via a property attribute, use the shorter
// `nullable` form for the method parameter.
if (PropertyAttributes & ObjCPropertyAttribute::kind_nullability) {
if (auto Kind = AttributedType::stripOuterNullability(T)) {
switch (*Kind) {
case NullabilityKind::Nullable:
Prefix = "nullable ";
break;
case NullabilityKind::NonNull:
Prefix = "nonnull ";
break;
case NullabilityKind::Unspecified:
Prefix = "null_unspecified ";
break;
case NullabilityKind::NullableResult:
T = OrigT;
break;
}
}
}
return Prefix + T.getAsString(Policy);
}
struct MethodParameter {
// Parameter name.
llvm::StringRef Name;
// Type of the parameter.
std::string Type;
// Assignment target (LHS).
std::string Assignee;
MethodParameter(const ObjCIvarDecl &ID) {
// Convention maps `@property int foo` to ivar `int _foo`, so drop the
// leading `_` if there is one.
Name = ID.getName();
Name.consume_front("_");
Type = getTypeStr(ID.getType(), ID, ObjCPropertyAttribute::kind_noattr);
Assignee = ID.getName().str();
}
MethodParameter(const ObjCPropertyDecl &PD) {
Name = PD.getName();
Type = getTypeStr(PD.getType(), PD, PD.getPropertyAttributes());
if (const auto *ID = PD.getPropertyIvarDecl())
Assignee = ID->getName().str();
else // Could be a dynamic property or a property in a header.
Assignee = ("self." + Name).str();
}
static llvm::Optional<MethodParameter> parameterFor(const Decl &D) {
if (const auto *ID = dyn_cast<ObjCIvarDecl>(&D))
return MethodParameter(*ID);
if (const auto *PD = dyn_cast<ObjCPropertyDecl>(&D))
if (PD->isInstanceProperty())
return MethodParameter(*PD);
return llvm::None;
}
};
static SmallVector<MethodParameter, 8>
getAllParams(const ObjCInterfaceDecl *ID) {
SmallVector<MethodParameter, 8> Params;
// Currently we only generate based on the ivars and properties declared
// in the interface. We could consider expanding this to include visible
// categories + class extensions in the future (see
// all_declared_ivar_begin).
llvm::DenseSet<llvm::StringRef> Names;
for (const auto *Ivar : ID->ivars()) {
MethodParameter P(*Ivar);
if (Names.insert(P.Name).second)
Params.push_back(P);
}
for (const auto *Prop : ID->properties()) {
MethodParameter P(*Prop);
if (Names.insert(P.Name).second)
Params.push_back(P);
}
return Params;
}
static std::string
initializerForParams(const SmallVector<MethodParameter, 8> &Params,
bool GenerateImpl) {
std::string Code;
llvm::raw_string_ostream Stream(Code);
if (Params.empty()) {
if (GenerateImpl) {
Stream <<
R"cpp(- (instancetype)init {
self = [super init];
if (self) {
}
return self;
})cpp";
} else {
Stream << "- (instancetype)init;";
}
} else {
const auto &First = Params.front();
Stream << llvm::formatv("- (instancetype)initWith{0}:({1}){2}",
capitalize(First.Name.trim().str()), First.Type,
First.Name);
for (auto It = Params.begin() + 1; It != Params.end(); ++It)
Stream << llvm::formatv(" {0}:({1}){0}", It->Name, It->Type);
if (GenerateImpl) {
Stream <<
R"cpp( {
self = [super init];
if (self) {)cpp";
for (const auto &Param : Params)
Stream << llvm::formatv("\n {0} = {1};", Param.Assignee, Param.Name);
Stream <<
R"cpp(
}
return self;
})cpp";
} else {
Stream << ";";
}
}
Stream << "\n\n";
return Code;
}
/// Generate an initializer for an Objective-C class based on selected
/// properties and instance variables.
class ObjCMemberwiseInitializer : public Tweak {
public:
const char *id() const final;
llvm::StringLiteral kind() const override {
return CodeAction::REFACTOR_KIND;
}
bool prepare(const Selection &Inputs) override;
Expected<Tweak::Effect> apply(const Selection &Inputs) override;
std::string title() const override;
private:
SmallVector<MethodParameter, 8>
paramsForSelection(const SelectionTree::Node *N);
const ObjCInterfaceDecl *Interface = nullptr;
// Will be nullptr if running on an interface.
const ObjCImplementationDecl *Impl = nullptr;
};
REGISTER_TWEAK(ObjCMemberwiseInitializer)
bool ObjCMemberwiseInitializer::prepare(const Selection &Inputs) {
const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
if (!N)
return false;
const Decl *D = N->ASTNode.get<Decl>();
if (!D)
return false;
const auto &LangOpts = Inputs.AST->getLangOpts();
// Require ObjC w/ arc enabled since we don't emit retains.
if (!LangOpts.ObjC || !LangOpts.ObjCAutoRefCount)
return false;
// We support the following selected decls:
// - ObjCInterfaceDecl/ObjCImplementationDecl only - generate for all
// properties and ivars
//
// - Specific ObjCPropertyDecl(s)/ObjCIvarDecl(s) - generate only for those
// selected. Note that if only one is selected, the common ancestor will be
// the ObjCPropertyDecl/ObjCIvarDecl itself instead of the container.
if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) {
// Ignore forward declarations (@class Name;).
if (!ID->isThisDeclarationADefinition())
return false;
Interface = ID;
} else if (const auto *ID = dyn_cast<ObjCImplementationDecl>(D)) {
Interface = ID->getClassInterface();
Impl = ID;
} else if (isa<ObjCPropertyDecl, ObjCIvarDecl>(D)) {
const auto *DC = D->getDeclContext();
if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(DC)) {
Interface = ID;
} else if (const auto *ID = dyn_cast<ObjCImplementationDecl>(DC)) {
Interface = ID->getClassInterface();
Impl = ID;
}
}
return Interface != nullptr;
}
SmallVector<MethodParameter, 8>
ObjCMemberwiseInitializer::paramsForSelection(const SelectionTree::Node *N) {
SmallVector<MethodParameter, 8> Params;
// Base case: selected a single ivar or property.
if (const auto *D = N->ASTNode.get<Decl>()) {
if (auto Param = MethodParameter::parameterFor(*D)) {
Params.push_back(*Param);
return Params;
}
}
const ObjCContainerDecl *Container =
Impl ? static_cast<const ObjCContainerDecl *>(Impl)
: static_cast<const ObjCContainerDecl *>(Interface);
if (Container == N->ASTNode.get<ObjCContainerDecl>() && N->Children.empty())
return getAllParams(Interface);
llvm::DenseSet<llvm::StringRef> Names;
// Check for selecting multiple ivars/properties.
for (const auto *CNode : N->Children) {
const Decl *D = CNode->ASTNode.get<Decl>();
if (!D)
continue;
if (auto P = MethodParameter::parameterFor(*D))
if (Names.insert(P->Name).second)
Params.push_back(*P);
}
return Params;
}
Expected<Tweak::Effect>
ObjCMemberwiseInitializer::apply(const Selection &Inputs) {
const auto &SM = Inputs.AST->getASTContext().getSourceManager();
const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
if (!N)
return error("Invalid selection");
SmallVector<MethodParameter, 8> Params = paramsForSelection(N);
// Insert before the first non-init instance method.
std::vector<Anchor> Anchors = {
{[](const Decl *D) {
if (const auto *MD = llvm::dyn_cast<ObjCMethodDecl>(D)) {
return MD->getMethodFamily() != OMF_init && MD->isInstanceMethod();
}
return false;
},
Anchor::Above}};
Effect E;
auto InterfaceReplacement =
insertDecl(initializerForParams(Params, /*GenerateImpl=*/false),
*Interface, Anchors);
if (!InterfaceReplacement)
return InterfaceReplacement.takeError();
auto FE = Effect::fileEdit(SM, SM.getFileID(Interface->getLocation()),
tooling::Replacements(*InterfaceReplacement));
if (!FE)
return FE.takeError();
E.ApplyEdits.insert(std::move(*FE));
if (Impl) {
// If we see the class implementation, add the initializer there too.
// FIXME: merging the edits is awkward, do this elsewhere.
auto ImplReplacement = insertDecl(
initializerForParams(Params, /*GenerateImpl=*/true), *Impl, Anchors);
if (!ImplReplacement)
return ImplReplacement.takeError();
if (SM.isWrittenInSameFile(Interface->getLocation(), Impl->getLocation())) {
// Merge with previous edit if they are in the same file.
if (auto Err =
E.ApplyEdits.begin()->second.Replacements.add(*ImplReplacement))
return std::move(Err);
} else {
// Generate a new edit if the interface and implementation are in
// different files.
auto FE = Effect::fileEdit(SM, SM.getFileID(Impl->getLocation()),
tooling::Replacements(*ImplReplacement));
if (!FE)
return FE.takeError();
E.ApplyEdits.insert(std::move(*FE));
}
}
return E;
}
std::string ObjCMemberwiseInitializer::title() const {
if (Impl)
return "Generate memberwise initializer";
return "Declare memberwise initializer";
}
} // namespace
} // namespace clangd
} // namespace clang
|