File: ScopifyEnum.cpp

package info (click to toggle)
llvm-toolchain-18 1%3A18.1.8-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,908,340 kB
  • sloc: cpp: 6,667,937; ansic: 1,440,452; asm: 883,619; python: 230,549; objc: 76,880; f90: 74,238; lisp: 35,989; pascal: 16,571; sh: 10,229; perl: 7,459; ml: 5,047; awk: 3,523; makefile: 2,987; javascript: 2,149; xml: 892; fortran: 649; cs: 573
file content (235 lines) | stat: -rw-r--r-- 7,838 bytes parent folder | download
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
//===--- ScopifyEnum.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 "Protocol.h"
#include "Selection.h"
#include "SourceCode.h"
#include "XRefs.h"
#include "refactor/Tweak.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/Basic/LLVM.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"

#include <cstddef>
#include <functional>
#include <memory>
#include <string>
#include <utility>

namespace clang::clangd {
namespace {

/// Turns an unscoped into a scoped enum type.
/// Before:
///   enum E { EV1, EV2 };
///        ^
///   void f() { E e1 = EV1; }
///
/// After:
///   enum class E { EV1, EV2 };
///   void f() { E e1 = E::EV1; }
///
/// Note that the respective project code might not compile anymore
/// if it made use of the now-gone implicit conversion to int.
/// This is out of scope for this tweak.
///
/// TODO: In the above example, we could detect that the values
///       start with the enum name, and remove that prefix.

class ScopifyEnum : public Tweak {
  const char *id() const final;
  std::string title() const override { return "Convert to scoped enum"; }
  llvm::StringLiteral kind() const override {
    return CodeAction::REFACTOR_KIND;
  }
  bool prepare(const Selection &Inputs) override;
  Expected<Tweak::Effect> apply(const Selection &Inputs) override;

  using MakeReplacement =
      std::function<tooling::Replacement(StringRef, StringRef, unsigned)>;
  llvm::Error addClassKeywordToDeclarations();
  llvm::Error scopifyEnumValues();
  llvm::Error scopifyEnumValue(const EnumConstantDecl &CD, StringRef Prefix);
  llvm::Expected<StringRef> getContentForFile(StringRef FilePath);
  unsigned getOffsetFromPosition(const Position &Pos, StringRef Content) const;
  llvm::Error addReplacementForReference(const ReferencesResult::Reference &Ref,
                                         const MakeReplacement &GetReplacement);
  llvm::Error addReplacement(StringRef FilePath, StringRef Content,
                             const tooling::Replacement &Replacement);
  Position getPosition(const Decl &D) const;

  const EnumDecl *D = nullptr;
  const Selection *S = nullptr;
  SourceManager *SM = nullptr;
  llvm::SmallVector<std::unique_ptr<llvm::MemoryBuffer>> ExtraBuffers;
  llvm::StringMap<StringRef> ContentPerFile;
  Effect E;
};

REGISTER_TWEAK(ScopifyEnum)

bool ScopifyEnum::prepare(const Selection &Inputs) {
  if (!Inputs.AST->getLangOpts().CPlusPlus11)
    return false;
  const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
  if (!N)
    return false;
  D = N->ASTNode.get<EnumDecl>();
  return D && !D->isScoped() && D->isThisDeclarationADefinition();
}

Expected<Tweak::Effect> ScopifyEnum::apply(const Selection &Inputs) {
  S = &Inputs;
  SM = &S->AST->getSourceManager();
  E.FormatEdits = false;
  ContentPerFile.insert(std::make_pair(SM->getFilename(D->getLocation()),
                                       SM->getBufferData(SM->getMainFileID())));

  if (auto Err = addClassKeywordToDeclarations())
    return std::move(Err);
  if (auto Err = scopifyEnumValues())
    return std::move(Err);

  return E;
}

llvm::Error ScopifyEnum::addClassKeywordToDeclarations() {
  for (const auto &Ref :
       findReferences(*S->AST, getPosition(*D), 0, S->Index, false)
           .References) {
    if (!(Ref.Attributes & ReferencesResult::Declaration))
      continue;

    static const auto MakeReplacement = [](StringRef FilePath,
                                           StringRef Content, unsigned Offset) {
      return tooling::Replacement(FilePath, Offset, 0, "class ");
    };
    if (auto Err = addReplacementForReference(Ref, MakeReplacement))
      return Err;
  }
  return llvm::Error::success();
}

llvm::Error ScopifyEnum::scopifyEnumValues() {
  std::string PrefixToInsert(D->getName());
  PrefixToInsert += "::";
  for (auto E : D->enumerators()) {
    if (auto Err = scopifyEnumValue(*E, PrefixToInsert))
      return Err;
  }
  return llvm::Error::success();
}

llvm::Error ScopifyEnum::scopifyEnumValue(const EnumConstantDecl &CD,
                                          StringRef Prefix) {
  for (const auto &Ref :
       findReferences(*S->AST, getPosition(CD), 0, S->Index, false)
           .References) {
    if (Ref.Attributes & ReferencesResult::Declaration)
      continue;

    const auto MakeReplacement = [&Prefix](StringRef FilePath,
                                           StringRef Content, unsigned Offset) {
      const auto IsAlreadyScoped = [Content, Offset] {
        if (Offset < 2)
          return false;
        unsigned I = Offset;
        while (--I > 0) {
          switch (Content[I]) {
          case ' ':
          case '\t':
          case '\n':
            continue;
          case ':':
            if (Content[I - 1] == ':')
              return true;
            [[fallthrough]];
          default:
            return false;
          }
        }
        return false;
      };
      return IsAlreadyScoped()
                 ? tooling::Replacement()
                 : tooling::Replacement(FilePath, Offset, 0, Prefix);
    };
    if (auto Err = addReplacementForReference(Ref, MakeReplacement))
      return Err;
  }

  return llvm::Error::success();
}

llvm::Expected<StringRef> ScopifyEnum::getContentForFile(StringRef FilePath) {
  if (auto It = ContentPerFile.find(FilePath); It != ContentPerFile.end())
    return It->second;
  auto Buffer = S->FS->getBufferForFile(FilePath);
  if (!Buffer)
    return llvm::errorCodeToError(Buffer.getError());
  StringRef Content = Buffer->get()->getBuffer();
  ExtraBuffers.push_back(std::move(*Buffer));
  ContentPerFile.insert(std::make_pair(FilePath, Content));
  return Content;
}

unsigned int ScopifyEnum::getOffsetFromPosition(const Position &Pos,
                                                StringRef Content) const {
  unsigned int Offset = 0;

  for (std::size_t LinesRemaining = Pos.line;
       Offset < Content.size() && LinesRemaining;) {
    if (Content[Offset++] == '\n')
      --LinesRemaining;
  }
  return Offset + Pos.character;
}

llvm::Error
ScopifyEnum::addReplacementForReference(const ReferencesResult::Reference &Ref,
                                        const MakeReplacement &GetReplacement) {
  StringRef FilePath = Ref.Loc.uri.file();
  auto Content = getContentForFile(FilePath);
  if (!Content)
    return Content.takeError();
  unsigned Offset = getOffsetFromPosition(Ref.Loc.range.start, *Content);
  tooling::Replacement Replacement = GetReplacement(FilePath, *Content, Offset);
  if (Replacement.isApplicable())
    return addReplacement(FilePath, *Content, Replacement);
  return llvm::Error::success();
}

llvm::Error
ScopifyEnum::addReplacement(StringRef FilePath, StringRef Content,
                            const tooling::Replacement &Replacement) {
  Edit &TheEdit = E.ApplyEdits[FilePath];
  TheEdit.InitialCode = Content;
  if (auto Err = TheEdit.Replacements.add(Replacement))
    return Err;
  return llvm::Error::success();
}

Position ScopifyEnum::getPosition(const Decl &D) const {
  const SourceLocation Loc = D.getLocation();
  Position Pos;
  Pos.line = SM->getSpellingLineNumber(Loc) - 1;
  Pos.character = SM->getSpellingColumnNumber(Loc) - 1;
  return Pos;
}

} // namespace
} // namespace clang::clangd