From 90b5ac660ef96b9d59dff837e96fd72d0673d7de Mon Sep 17 00:00:00 2001 From: Dmitri Gribenko Date: Thu, 7 Feb 2013 19:09:22 +0000 Subject: [PATCH] libclang: reduce CXString abuse ContainerUSR is not really a CXString, but it should own the underlying memory buffer. Thus, it is better to change the type to std::string. This will not introduce extra copying overhead, since the workaround that is being removed was already making a copy. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174647 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/libclang/CIndexCodeCompletion.cpp | 45 +++++++++++-------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/tools/libclang/CIndexCodeCompletion.cpp b/tools/libclang/CIndexCodeCompletion.cpp index 0217d2844e..f79de2909e 100644 --- a/tools/libclang/CIndexCodeCompletion.cpp +++ b/tools/libclang/CIndexCodeCompletion.cpp @@ -37,6 +37,7 @@ #include "llvm/Support/raw_ostream.h" #include #include +#include #ifdef UDP_CODE_COMPLETION_LOGGER @@ -240,6 +241,11 @@ namespace { /// \brief The CXCodeCompleteResults structure we allocate internally; /// the client only sees the initial CXCodeCompleteResults structure. +/// +/// Normally, clients of CXString shouldn't care whether or not a CXString is +/// managed by a pool or by explicitly malloc'ed memory. But +/// AllocatedCXCodeCompleteResults outlives the CXTranslationUnit, so we can +/// not rely on the StringPool in the TU. struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts); ~AllocatedCXCodeCompleteResults(); @@ -288,8 +294,10 @@ struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults { /// \brief The kind of the container for the current context for completions. enum CXCursorKind ContainerKind; + /// \brief The USR of the container for the current context for completions. - CXString ContainerUSR; + std::string ContainerUSR; + /// \brief a boolean value indicating whether there is complete information /// about the container unsigned ContainerIsIncomplete; @@ -320,7 +328,6 @@ AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( CodeCompletionAllocator(new clang::GlobalCodeCompletionAllocator), Contexts(CXCompletionContext_Unknown), ContainerKind(CXCursor_InvalidCode), - ContainerUSR(cxstring::createEmpty()), ContainerIsIncomplete(1) { if (getenv("LIBCLANG_OBJTRACKING")) { @@ -331,9 +338,7 @@ AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults( AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() { delete [] Results; - - clang_disposeString(ContainerUSR); - + for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I) TemporaryFiles[I].eraseFromDisk(); for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I) @@ -590,23 +595,13 @@ namespace { if (D != NULL) { CXCursor cursor = cxcursor::MakeCXCursor(D, *TU); - - CXCursorKind cursorKind = clang_getCursorKind(cursor); - CXString cursorUSR = clang_getCursorUSR(cursor); - - // Normally, clients of CXString shouldn't care whether or not - // a CXString is managed by a pool or by explicitly malloc'ed memory. - // However, there are cases when AllocatedResults outlives the - // CXTranslationUnit. This is a workaround that failure mode. - if (cxstring::isManagedByPool(cursorUSR)) { - CXString heapStr = cxstring::createDup(clang_getCString(cursorUSR)); - clang_disposeString(cursorUSR); - cursorUSR = heapStr; - } - - AllocatedResults.ContainerKind = cursorKind; - AllocatedResults.ContainerUSR = cursorUSR; - + + AllocatedResults.ContainerKind = clang_getCursorKind(cursor); + + CXString CursorUSR = clang_getCursorUSR(cursor); + AllocatedResults.ContainerUSR = clang_getCString(CursorUSR); + clang_disposeString(CursorUSR); + const Type *type = baseType.getTypePtrOrNull(); if (type != NULL) { AllocatedResults.ContainerIsIncomplete = type->isIncompleteType(); @@ -617,7 +612,7 @@ namespace { } else { AllocatedResults.ContainerKind = CXCursor_InvalidCode; - AllocatedResults.ContainerUSR = cxstring::createEmpty(); + AllocatedResults.ContainerUSR.clear(); AllocatedResults.ContainerIsIncomplete = 1; } } @@ -911,8 +906,8 @@ CXString clang_codeCompleteGetContainerUSR(CXCodeCompleteResults *ResultsIn) { static_cast(ResultsIn); if (!Results) return cxstring::createEmpty(); - - return cxstring::createRef(clang_getCString(Results->ContainerUSR)); + + return cxstring::createRef(Results->ContainerUSR.c_str()); } -- 2.40.0