summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulian Schmidt <git.julian.schmidt@gmail.com>2024-05-03 18:23:13 +0200
committerGitHub <noreply@github.com>2024-05-03 18:23:13 +0200
commit8d946c71712daeabf6636ff3844fa49e4638324b (patch)
tree8f576b8aa3bb946d670e27a6757a42f7beab6307
parent2755c69098c9d0cf33bbbd3ff90f63ab819acfe1 (diff)
[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.
-rw-r--r--clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp38
1 files changed, 11 insertions, 27 deletions
diff --git a/clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp b/clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp
index 2be2bbc422af..44080802a289 100644
--- a/clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp
+++ b/clang-tools-extra/clangd/refactor/tweaks/ScopifyEnum.cpp
@@ -63,12 +63,10 @@ class ScopifyEnum : public Tweak {
llvm::Error scopifyEnumValue(const EnumConstantDecl &CD, StringRef EnumName,
bool StripPrefix);
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;
@@ -107,7 +105,8 @@ Expected<Tweak::Effect> ScopifyEnum::apply(const Selection &Inputs) {
llvm::Error ScopifyEnum::addClassKeywordToDeclarations() {
for (const auto &Ref :
- findReferences(*S->AST, getPosition(*D), 0, S->Index, false)
+ findReferences(*S->AST, sourceLocToPosition(*SM, D->getBeginLoc()), 0,
+ S->Index, false)
.References) {
if (!(Ref.Attributes & ReferencesResult::Declaration))
continue;
@@ -142,7 +141,8 @@ llvm::Error ScopifyEnum::scopifyEnumValue(const EnumConstantDecl &CD,
StringRef EnumName,
bool StripPrefix) {
for (const auto &Ref :
- findReferences(*S->AST, getPosition(CD), 0, S->Index, false)
+ findReferences(*S->AST, sourceLocToPosition(*SM, CD.getBeginLoc()), 0,
+ S->Index, false)
.References) {
if (Ref.Attributes & ReferencesResult::Declaration) {
if (StripPrefix) {
@@ -214,27 +214,19 @@ llvm::Expected<StringRef> ScopifyEnum::getContentForFile(StringRef FilePath) {
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);
+ llvm::Expected<StringRef> Content = getContentForFile(FilePath);
if (!Content)
return Content.takeError();
- unsigned Offset = getOffsetFromPosition(Ref.Loc.range.start, *Content);
- tooling::Replacement Replacement = GetReplacement(FilePath, *Content, Offset);
+ llvm::Expected<size_t> Offset =
+ positionToOffset(*Content, Ref.Loc.range.start);
+ if (!Offset)
+ return Offset.takeError();
+ tooling::Replacement Replacement =
+ GetReplacement(FilePath, *Content, *Offset);
if (Replacement.isApplicable())
return addReplacement(FilePath, *Content, Replacement);
return llvm::Error::success();
@@ -250,13 +242,5 @@ ScopifyEnum::addReplacement(StringRef FilePath, StringRef Content,
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