From: Argyrios Kyrtzidis Date: Mon, 3 Mar 2014 06:38:52 +0000 (+0000) Subject: [libclang] Change clang_VirtualFileOverlay_writeToBuffer to return a malloc'ed buffer. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=28b7261a1a98f2f5365f3891679c4a49c0109261;p=clang [libclang] Change clang_VirtualFileOverlay_writeToBuffer to return a malloc'ed buffer. Returning CXString is not appropriate if we want to switch to a non-string format buffer. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202675 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/BuildSystem.h b/include/clang-c/BuildSystem.h index ab581fdcce..a1f243fd48 100644 --- a/include/clang-c/BuildSystem.h +++ b/include/clang-c/BuildSystem.h @@ -62,13 +62,15 @@ clang_VirtualFileOverlay_addFileMapping(CXVirtualFileOverlay, * \brief Write out the \c CXVirtualFileOverlay object to a char buffer. * * \param options is reserved, always pass 0. - * \param out_buffer pointer to receive the CXString object, which should be - * disposed using \c clang_disposeString(). + * \param out_buffer_ptr pointer to receive the buffer pointer, which should be + * disposed using \c free(). + * \param out_buffer_size pointer to receive the buffer size. * \returns 0 for success, non-zero to indicate an error. */ CINDEX_LINKAGE enum CXErrorCode clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay, unsigned options, - CXString *out_buffer); + char **out_buffer_ptr, + unsigned *out_buffer_size); /** * \brief Dispose a \c CXVirtualFileOverlay object. diff --git a/tools/libclang/BuildSystem.cpp b/tools/libclang/BuildSystem.cpp index 311319ab96..5fd95b0a85 100644 --- a/tools/libclang/BuildSystem.cpp +++ b/tools/libclang/BuildSystem.cpp @@ -162,9 +162,10 @@ private: } enum CXErrorCode -clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, - unsigned, CXString *out_buffer) { - if (!VFO || !out_buffer) +clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, unsigned, + char **out_buffer_ptr, + unsigned *out_buffer_size) { + if (!VFO || !out_buffer_ptr || !out_buffer_size) return CXError_InvalidArguments; llvm::SmallVector Entries; @@ -186,7 +187,10 @@ clang_VirtualFileOverlay_writeToBuffer(CXVirtualFileOverlay VFO, JSONVFSPrinter Printer(OS); Printer.print(Entries); - *out_buffer = cxstring::createDup(OS.str()); + StringRef Data = OS.str(); + *out_buffer_ptr = (char*)malloc(Data.size()); + *out_buffer_size = Data.size(); + memcpy(*out_buffer_ptr, Data.data(), Data.size()); return CXError_Success; } diff --git a/unittests/libclang/LibclangTest.cpp b/unittests/libclang/LibclangTest.cpp index f855ecdd3c..3809c1ce88 100644 --- a/unittests/libclang/LibclangTest.cpp +++ b/unittests/libclang/LibclangTest.cpp @@ -51,10 +51,12 @@ struct TestVFO { ~TestVFO() { if (!Contents) return; - CXString Buf; - clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &Buf); - EXPECT_STREQ(Contents, clang_getCString(Buf)); - clang_disposeString(Buf); + char *BufPtr; + unsigned BufSize; + clang_VirtualFileOverlay_writeToBuffer(VFO, 0, &BufPtr, &BufSize); + std::string BufStr(BufPtr, BufSize); + EXPECT_STREQ(Contents, BufStr.c_str()); + free(BufPtr); clang_VirtualFileOverlay_dispose(VFO); } };