Skip to content

[clang] Adding an API to create a CXStringSet from a Vector of StringRefs #136773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions clang/tools/libclang/CXString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,28 @@ CXString createCXString(CXStringBuf *buf) {
return Str;
}

CXStringSet *createSet(const std::vector<std::string> &Strings) {
template <typename StringTy, bool Copy>
static CXStringSet *createSetImpl(ArrayRef<StringTy> Strings) {
CXStringSet *Set = new CXStringSet;
Set->Count = Strings.size();
Set->Strings = new CXString[Set->Count];
for (unsigned SI = 0, SE = Set->Count; SI < SE; ++SI)
Set->Strings[SI] = createDup(Strings[SI]);
for (unsigned SI = 0, SE = Set->Count; SI < SE; ++SI) {
if constexpr (Copy) {
Set->Strings[SI] = createDup(Strings[SI]);
} else {
Set->Strings[SI] = createRef(Strings[SI]);
}
}
return Set;
}

CXStringSet *createSet(const std::vector<std::string> &Strings) {
return createSetImpl<std::string, true>(Strings);
}

CXStringSet *createRefSet(ArrayRef<StringRef> Strings) {
return createSetImpl<StringRef, false>(Strings);
}

//===----------------------------------------------------------------------===//
// String pools.
Expand Down
7 changes: 6 additions & 1 deletion clang/tools/libclang/CXString.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ CXString createRef(std::string String) = delete;
/// Create a CXString object that is backed by a string buffer.
CXString createCXString(CXStringBuf *buf);

/// Create a CXStringSet object that owns the strings. Such an object should be
/// disposed with clang_disposeStringSet.
CXStringSet *createSet(const std::vector<std::string> &Strings);

/// Create a CXStringSet object that does not own the strings. Such an object
/// should still be disposed with clang_disposeStringSet.
CXStringSet *createRefSet(ArrayRef<StringRef> Strings);

/// A string pool used for fast allocation/deallocation of strings.
class CXStringPool {
public:
Expand Down Expand Up @@ -105,4 +111,3 @@ static inline StringRef getContents(const CXUnsavedFile &UF) {
}

#endif

Loading