Skip to content

Commit 8d946c7

Browse files
authored
[clangd] use existing functions for code locations in the scopify enum tweak (#88737)
Clangd already implements some utility functions for converting between `SourceLocation`s, `Position`s and `Offset`s into a buffer.
1 parent 2755c69 commit 8d946c7

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,10 @@ class ScopifyEnum : public Tweak {
6363
llvm::Error scopifyEnumValue(const EnumConstantDecl &CD, StringRef EnumName,
6464
bool StripPrefix);
6565
llvm::Expected<StringRef> getContentForFile(StringRef FilePath);
66-
unsigned getOffsetFromPosition(const Position &Pos, StringRef Content) const;
6766
llvm::Error addReplacementForReference(const ReferencesResult::Reference &Ref,
6867
const MakeReplacement &GetReplacement);
6968
llvm::Error addReplacement(StringRef FilePath, StringRef Content,
7069
const tooling::Replacement &Replacement);
71-
Position getPosition(const Decl &D) const;
7270

7371
const EnumDecl *D = nullptr;
7472
const Selection *S = nullptr;
@@ -107,7 +105,8 @@ Expected<Tweak::Effect> ScopifyEnum::apply(const Selection &Inputs) {
107105

108106
llvm::Error ScopifyEnum::addClassKeywordToDeclarations() {
109107
for (const auto &Ref :
110-
findReferences(*S->AST, getPosition(*D), 0, S->Index, false)
108+
findReferences(*S->AST, sourceLocToPosition(*SM, D->getBeginLoc()), 0,
109+
S->Index, false)
111110
.References) {
112111
if (!(Ref.Attributes & ReferencesResult::Declaration))
113112
continue;
@@ -142,7 +141,8 @@ llvm::Error ScopifyEnum::scopifyEnumValue(const EnumConstantDecl &CD,
142141
StringRef EnumName,
143142
bool StripPrefix) {
144143
for (const auto &Ref :
145-
findReferences(*S->AST, getPosition(CD), 0, S->Index, false)
144+
findReferences(*S->AST, sourceLocToPosition(*SM, CD.getBeginLoc()), 0,
145+
S->Index, false)
146146
.References) {
147147
if (Ref.Attributes & ReferencesResult::Declaration) {
148148
if (StripPrefix) {
@@ -214,27 +214,19 @@ llvm::Expected<StringRef> ScopifyEnum::getContentForFile(StringRef FilePath) {
214214
return Content;
215215
}
216216

217-
unsigned int ScopifyEnum::getOffsetFromPosition(const Position &Pos,
218-
StringRef Content) const {
219-
unsigned int Offset = 0;
220-
221-
for (std::size_t LinesRemaining = Pos.line;
222-
Offset < Content.size() && LinesRemaining;) {
223-
if (Content[Offset++] == '\n')
224-
--LinesRemaining;
225-
}
226-
return Offset + Pos.character;
227-
}
228-
229217
llvm::Error
230218
ScopifyEnum::addReplacementForReference(const ReferencesResult::Reference &Ref,
231219
const MakeReplacement &GetReplacement) {
232220
StringRef FilePath = Ref.Loc.uri.file();
233-
auto Content = getContentForFile(FilePath);
221+
llvm::Expected<StringRef> Content = getContentForFile(FilePath);
234222
if (!Content)
235223
return Content.takeError();
236-
unsigned Offset = getOffsetFromPosition(Ref.Loc.range.start, *Content);
237-
tooling::Replacement Replacement = GetReplacement(FilePath, *Content, Offset);
224+
llvm::Expected<size_t> Offset =
225+
positionToOffset(*Content, Ref.Loc.range.start);
226+
if (!Offset)
227+
return Offset.takeError();
228+
tooling::Replacement Replacement =
229+
GetReplacement(FilePath, *Content, *Offset);
238230
if (Replacement.isApplicable())
239231
return addReplacement(FilePath, *Content, Replacement);
240232
return llvm::Error::success();
@@ -250,13 +242,5 @@ ScopifyEnum::addReplacement(StringRef FilePath, StringRef Content,
250242
return llvm::Error::success();
251243
}
252244

253-
Position ScopifyEnum::getPosition(const Decl &D) const {
254-
const SourceLocation Loc = D.getLocation();
255-
Position Pos;
256-
Pos.line = SM->getSpellingLineNumber(Loc) - 1;
257-
Pos.character = SM->getSpellingColumnNumber(Loc) - 1;
258-
return Pos;
259-
}
260-
261245
} // namespace
262246
} // namespace clang::clangd

0 commit comments

Comments
 (0)