[clang] [clang] Adding an API to create a `CXStringSet` from a Vector of `StringRef`s (PR #136773)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 23 09:10:16 PDT 2025


================
@@ -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>(ArrayRef<std::string>(Strings));
----------------
jansvoboda11 wrote:

Nit: With the `StringTy` template parameter specified explicitly, I don't think you need to explicitly call the `ArrayRef` constructor here. The one from `const std::vector &` is implicit, so it will get called automatically.

https://github.com/llvm/llvm-project/pull/136773


More information about the cfe-commits mailing list