clang-tools 10.0.0

ObjCLocalizeStringLiteral.cpp
Go to the documentation of this file.
1 //===--- ObjcLocalizeStringLiteral.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
9 #include "Logger.h"
10 #include "ParsedAST.h"
11 #include "SourceCode.h"
12 #include "refactor/Tweak.h"
13 #include "clang/AST/ExprObjC.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/None.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Error.h"
24
25 namespace clang {
26 namespace clangd {
27 namespace {
28
29 /// Wraps an Objective-C string literal with the NSLocalizedString macro.
30 /// Before:
31 /// @"description"
32 /// ^^^
33 /// After:
34 /// NSLocalizedString(@"description", @"")
35 class ObjCLocalizeStringLiteral : public Tweak {
36 public:
37 const char *id() const override final;
38 Intent intent() const override { return Intent::Refactor; }
39
40 bool prepare(const Selection &Inputs) override;
41 Expected<Tweak::Effect> apply(const Selection &Inputs) override;
42 std::string title() const override;
43
44 private:
45 const clang::ObjCStringLiteral *Str = nullptr;
46 };
47
48 REGISTER_TWEAK(ObjCLocalizeStringLiteral)
49
50 bool ObjCLocalizeStringLiteral::prepare(const Selection &Inputs) {
51 const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
52 if (!N)
53 return false;
54 // Allow the refactoring even if the user selected only the C string part
55 // of the expression.
56 if (N->ASTNode.get<StringLiteral>()) {
57 if (N->Parent)
58 N = N->Parent;
59 }
60 Str = dyn_cast_or_null<ObjCStringLiteral>(N->ASTNode.get<Stmt>());
61 return Str;
62 }
63
64 Expected<Tweak::Effect>
65 ObjCLocalizeStringLiteral::apply(const Selection &Inputs) {
66 auto &SM = Inputs.AST->getSourceManager();
67 auto &LangOpts = Inputs.AST->getASTContext().getLangOpts();
68 auto Reps = tooling::Replacements(tooling::Replacement(
69 SM, CharSourceRange::getCharRange(Str->getBeginLoc()),
70 "NSLocalizedString(", LangOpts));
71 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
72 Str->getEndLoc(), 0, Inputs.AST->getSourceManager(), LangOpts);
73 if (auto Err = Reps.add(tooling::Replacement(
74 SM, CharSourceRange::getCharRange(EndLoc), ", @\"\")", LangOpts)))
75 return std::move(Err);
76 return Effect::mainFileEdit(SM, std::move(Reps));
77 }
78
79 std::string ObjCLocalizeStringLiteral::title() const {
80 return "Wrap in NSLocalizedString";
81 }
82
83 } // namespace
84 } // namespace clangd
85 } // namespace clang
SourceCode.h
REGISTER_TWEAK
#define REGISTER_TWEAK(Subclass)
Definition: Tweak.h:129
Tweak.h
Logger.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ParsedAST.h