diff options
Diffstat (limited to 'tools/libclang/CXString.h')
-rw-r--r-- | tools/libclang/CXString.h | 82 |
1 files changed, 64 insertions, 18 deletions
diff --git a/tools/libclang/CXString.h b/tools/libclang/CXString.h index c354bd2..7032033 100644 --- a/tools/libclang/CXString.h +++ b/tools/libclang/CXString.h @@ -16,36 +16,82 @@ #include "clang-c/Index.h" #include "clang/Basic/LLVM.h" -#include "llvm/ADT/StringRef.h" #include "llvm/ADT/SmallString.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/Compiler.h" +#include <vector> +#include <string> namespace clang { namespace cxstring { - -struct CXStringBuf { - SmallString<128> Data; - CXTranslationUnit TU; - CXStringBuf(CXTranslationUnit tu) : TU(tu) {} -}; -/// \brief Create a CXString object from a C string. -CXString createCXString(const char *String, bool DupString = false); +struct CXStringBuf; + +/// \brief Create a CXString object for an empty "" string. +CXString createEmpty(); + +/// \brief Create a CXString object for an NULL string. +/// +/// A NULL string should be used as an "invalid" value in case of errors. +CXString createNull(); + +/// \brief Create a CXString object from a nul-terminated C string. New +/// CXString may contain a pointer to \p String. +/// +/// \p String should not be changed by the caller afterwards. +CXString createRef(const char *String); + +/// \brief Create a CXString object from a nul-terminated C string. New +/// CXString will contain a copy of \p String. +/// +/// \p String can be changed or freed by the caller. +CXString createDup(const char *String); + +/// \brief Create a CXString object from a StringRef. New CXString may +/// contain a pointer to the undrelying data of \p String. +/// +/// \p String should not be changed by the caller afterwards. +CXString createRef(StringRef String); + +/// \brief Create a CXString object from a StringRef. New CXString will +/// contain a copy of \p String. +/// +/// \p String can be changed or freed by the caller. +CXString createDup(StringRef String); -/// \brief Create a CXString object from a StringRef. -CXString createCXString(StringRef String, bool DupString = true); +// Usually std::string is intended to be used as backing storage for CXString. +// In this case, call \c createRef(String.c_str()). +// +// If you need to make a copy, call \c createDup(StringRef(String)). +CXString createRef(std::string String) LLVM_DELETED_FUNCTION; /// \brief Create a CXString object that is backed by a string buffer. CXString createCXString(CXStringBuf *buf); -/// \brief Create an opaque string pool used for fast geneneration of strings. -void *createCXStringPool(); +/// \brief A string pool used for fast allocation/deallocation of strings. +class CXStringPool { +public: + ~CXStringPool(); + + CXStringBuf *getCXStringBuf(CXTranslationUnit TU); + +private: + std::vector<CXStringBuf *> Pool; + + friend struct CXStringBuf; +}; + +struct CXStringBuf { + SmallString<128> Data; + CXTranslationUnit TU; + + CXStringBuf(CXTranslationUnit TU) : TU(TU) {} + + /// \brief Return this buffer to the pool. + void dispose(); +}; -/// \brief Dispose of a string pool. -void disposeCXStringPool(void *pool); - CXStringBuf *getCXStringBuf(CXTranslationUnit TU); - -void disposeCXStringBuf(CXStringBuf *buf); /// \brief Returns true if the CXString data is managed by a pool. bool isManagedByPool(CXString str); |