@@ -63,12 +63,10 @@ class ScopifyEnum : public Tweak {
63
63
llvm::Error scopifyEnumValue (const EnumConstantDecl &CD, StringRef EnumName,
64
64
bool StripPrefix);
65
65
llvm::Expected<StringRef> getContentForFile (StringRef FilePath);
66
- unsigned getOffsetFromPosition (const Position &Pos, StringRef Content) const ;
67
66
llvm::Error addReplacementForReference (const ReferencesResult::Reference &Ref,
68
67
const MakeReplacement &GetReplacement);
69
68
llvm::Error addReplacement (StringRef FilePath, StringRef Content,
70
69
const tooling::Replacement &Replacement);
71
- Position getPosition (const Decl &D) const ;
72
70
73
71
const EnumDecl *D = nullptr ;
74
72
const Selection *S = nullptr ;
@@ -107,7 +105,8 @@ Expected<Tweak::Effect> ScopifyEnum::apply(const Selection &Inputs) {
107
105
108
106
llvm::Error ScopifyEnum::addClassKeywordToDeclarations () {
109
107
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 )
111
110
.References ) {
112
111
if (!(Ref.Attributes & ReferencesResult::Declaration))
113
112
continue ;
@@ -142,7 +141,8 @@ llvm::Error ScopifyEnum::scopifyEnumValue(const EnumConstantDecl &CD,
142
141
StringRef EnumName,
143
142
bool StripPrefix) {
144
143
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 )
146
146
.References ) {
147
147
if (Ref.Attributes & ReferencesResult::Declaration) {
148
148
if (StripPrefix) {
@@ -214,27 +214,19 @@ llvm::Expected<StringRef> ScopifyEnum::getContentForFile(StringRef FilePath) {
214
214
return Content;
215
215
}
216
216
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
-
229
217
llvm::Error
230
218
ScopifyEnum::addReplacementForReference (const ReferencesResult::Reference &Ref,
231
219
const MakeReplacement &GetReplacement) {
232
220
StringRef FilePath = Ref.Loc .uri .file ();
233
- auto Content = getContentForFile (FilePath);
221
+ llvm::Expected<StringRef> Content = getContentForFile (FilePath);
234
222
if (!Content)
235
223
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);
238
230
if (Replacement.isApplicable ())
239
231
return addReplacement (FilePath, *Content, Replacement);
240
232
return llvm::Error::success ();
@@ -250,13 +242,5 @@ ScopifyEnum::addReplacement(StringRef FilePath, StringRef Content,
250
242
return llvm::Error::success ();
251
243
}
252
244
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
-
261
245
} // namespace
262
246
} // namespace clang::clangd
0 commit comments