Skip to content

Commit

Permalink
[SourceKit/CursorInfo] Add constructor to call results
Browse files Browse the repository at this point in the history
Cursor info for a constructor would previously give the cursor info for
the containing type only. It now also adds cursor info for the
constructor itself in a "secondary_symbols" field.

Refactor `passCursorInfoForDecl` to use a single allocator rather than
keeping track of positions in a buffer and assigning everything at the
end of the function.

Refactor the various available refactoring gathering functions to take a
SmallVectorImpl and to not copy strings where they don't need to.

Resolves rdar://75385556
  • Loading branch information
bnbarham committed Mar 30, 2021
1 parent 8f52c26 commit 3ea9bed
Show file tree
Hide file tree
Showing 21 changed files with 836 additions and 858 deletions.
2 changes: 1 addition & 1 deletion include/swift/AST/FileUnit.h
Expand Up @@ -166,7 +166,7 @@ class FileUnit : public DeclContext {
return None;
}

virtual void collectAllGroups(std::vector<StringRef> &Names) const {}
virtual void collectAllGroups(SmallVectorImpl<StringRef> &Names) const {}

/// Returns an implementation-defined "discriminator" for \p D, which
/// distinguishes \p D from other declarations in the same module with the
Expand Down
3 changes: 1 addition & 2 deletions include/swift/IDE/ModuleInterfacePrinting.h
Expand Up @@ -42,8 +42,7 @@ enum class ModuleTraversal : unsigned {
/// Options used to describe the traversal of a module for printing.
using ModuleTraversalOptions = OptionSet<ModuleTraversal>;

ArrayRef<StringRef> collectModuleGroups(ModuleDecl *M,
std::vector<StringRef> &Scratch);
void collectModuleGroups(ModuleDecl *M, SmallVectorImpl<StringRef> &Into);

Optional<StringRef>
findGroupNameForUSR(ModuleDecl *M, StringRef USR);
Expand Down
35 changes: 16 additions & 19 deletions include/swift/IDE/Refactoring.h
Expand Up @@ -81,13 +81,14 @@ enum class RenameAvailableKind {
Unavailable_decl_from_clang,
};

struct RenameAvailabiliyInfo {
struct RenameAvailabilityInfo {
RefactoringKind Kind;
RenameAvailableKind AvailableKind;
RenameAvailabiliyInfo(RefactoringKind Kind, RenameAvailableKind AvailableKind) :
Kind(Kind), AvailableKind(AvailableKind) {}
RenameAvailabiliyInfo(RefactoringKind Kind) :
RenameAvailabiliyInfo(Kind, RenameAvailableKind::Available) {}
RenameAvailabilityInfo(RefactoringKind Kind,
RenameAvailableKind AvailableKind)
: Kind(Kind), AvailableKind(AvailableKind) {}
RenameAvailabilityInfo(RefactoringKind Kind)
: RenameAvailabilityInfo(Kind, RenameAvailableKind::Available) {}
};

class FindRenameRangesConsumer {
Expand Down Expand Up @@ -130,17 +131,14 @@ int findLocalRenameRanges(SourceFile *SF, RangeConfig Range,
FindRenameRangesConsumer &RenameConsumer,
DiagnosticConsumer &DiagConsumer);

ArrayRef<RefactoringKind>
collectAvailableRefactorings(SourceFile *SF, RangeConfig Range,
bool &RangeStartMayNeedRename,
std::vector<RefactoringKind> &Scratch,
llvm::ArrayRef<DiagnosticConsumer*> DiagConsumers);
void collectAvailableRefactorings(
SourceFile *SF, RangeConfig Range, bool &RangeStartMayNeedRename,
llvm::SmallVectorImpl<RefactoringKind> &Kinds,
llvm::ArrayRef<DiagnosticConsumer *> DiagConsumers);

ArrayRef<RefactoringKind>
collectAvailableRefactorings(SourceFile *SF,
const ResolvedCursorInfo &CursorInfo,
std::vector<RefactoringKind> &Scratch,
bool ExcludeRename);
void collectAvailableRefactorings(const ResolvedCursorInfo &CursorInfo,
llvm::SmallVectorImpl<RefactoringKind> &Kinds,
bool ExcludeRename);

/// Stores information about the reference that rename availability is being
/// queried on.
Expand All @@ -150,10 +148,9 @@ struct RenameRefInfo {
bool IsArgLabel; ///< Whether Loc is on an arg label, rather than base name.
};

ArrayRef<RenameAvailabiliyInfo>
collectRenameAvailabilityInfo(const ValueDecl *VD,
Optional<RenameRefInfo> RefInfo,
std::vector<RenameAvailabiliyInfo> &Scratch);
void collectRenameAvailabilityInfo(
const ValueDecl *VD, Optional<RenameRefInfo> RefInfo,
llvm::SmallVectorImpl<RenameAvailabilityInfo> &Infos);

} // namespace ide
} // namespace swift
Expand Down
41 changes: 3 additions & 38 deletions include/swift/IDE/Utils.h
Expand Up @@ -152,7 +152,7 @@ enum class CursorInfoKind {

struct ResolvedCursorInfo {
CursorInfoKind Kind = CursorInfoKind::Invalid;
SourceFile *SF;
SourceFile *SF = nullptr;
SourceLoc Loc;
ValueDecl *ValueD = nullptr;
TypeDecl *CtorTyRef = nullptr;
Expand All @@ -173,6 +173,8 @@ struct ResolvedCursorInfo {
ResolvedCursorInfo() = default;
ResolvedCursorInfo(SourceFile *SF) : SF(SF) {}

ValueDecl *typeOrValue() { return CtorTyRef ? CtorTyRef : ValueD; }

friend bool operator==(const ResolvedCursorInfo &lhs,
const ResolvedCursorInfo &rhs) {
return lhs.SF == rhs.SF &&
Expand Down Expand Up @@ -427,43 +429,6 @@ class DeclNameViewer {
bool isFunction() const { return HasParen; }
};

/// This provide a utility for writing to an underlying string buffer multiple
/// string pieces and retrieve them later when the underlying buffer is stable.
class DelayedStringRetriever : public raw_ostream {
SmallVectorImpl<char> &OS;
llvm::raw_svector_ostream Underlying;
SmallVector<std::pair<unsigned, unsigned>, 4> StartEnds;
unsigned CurrentStart;

public:
explicit DelayedStringRetriever(SmallVectorImpl<char> &OS) : OS(OS),
Underlying(OS) {}
void startPiece() {
CurrentStart = OS.size();
}
void endPiece() {
StartEnds.emplace_back(CurrentStart, OS.size());
}
void write_impl(const char *ptr, size_t size) override {
Underlying.write(ptr, size);
}
uint64_t current_pos() const override {
return Underlying.tell();
}
size_t preferred_buffer_size() const override {
return 0;
}
void retrieve(llvm::function_ref<void(StringRef)> F) const {
for (auto P : StartEnds) {
F(StringRef(OS.begin() + P.first, P.second - P.first));
}
}
StringRef operator[](unsigned I) const {
auto P = StartEnds[I];
return StringRef(OS.begin() + P.first, P.second - P.first);
}
};

enum class RegionType {
Unmatched,
Mismatch,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Serialization/SerializedModuleLoader.h
Expand Up @@ -394,7 +394,7 @@ class SerializedASTFile final : public LoadedFile {

Optional<BasicDeclLocs> getBasicLocsForDecl(const Decl *D) const override;

void collectAllGroups(std::vector<StringRef> &Names) const override;
void collectAllGroups(SmallVectorImpl<StringRef> &Names) const override;

virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;

Expand Down
12 changes: 5 additions & 7 deletions lib/IDE/ModuleInterfacePrinting.cpp
Expand Up @@ -202,15 +202,13 @@ static void adjustPrintOptions(PrintOptions &AdjustedOptions) {
AdjustedOptions.VarInitializers = false;
}

ArrayRef<StringRef>
swift::ide::collectModuleGroups(ModuleDecl *M, std::vector<StringRef> &Scratch) {
void swift::ide::collectModuleGroups(ModuleDecl *M,
SmallVectorImpl<StringRef> &Into) {
for (auto File : M->getFiles()) {
File->collectAllGroups(Scratch);
File->collectAllGroups(Into);
}
std::sort(Scratch.begin(), Scratch.end(), [](StringRef L, StringRef R) {
return L.compare_lower(R) < 0;
});
return llvm::makeArrayRef(Scratch);
std::sort(Into.begin(), Into.end(),
[](StringRef L, StringRef R) { return L.compare_lower(R) < 0; });
}

/// Determine whether the given extension has a Clang node that
Expand Down

0 comments on commit 3ea9bed

Please sign in to comment.