1f4a2713aSLionel Sambuc //===- CXString.h - Routines for manipulating CXStrings -------------------===// 2f4a2713aSLionel Sambuc // 3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure 4f4a2713aSLionel Sambuc // 5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source 6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details. 7f4a2713aSLionel Sambuc // 8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 9f4a2713aSLionel Sambuc // 10f4a2713aSLionel Sambuc // This file defines routines for manipulating CXStrings. 11f4a2713aSLionel Sambuc // 12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===// 13f4a2713aSLionel Sambuc 14*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H 15*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H 16f4a2713aSLionel Sambuc 17f4a2713aSLionel Sambuc #include "clang-c/Index.h" 18f4a2713aSLionel Sambuc #include "clang/Basic/LLVM.h" 19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h" 20f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h" 21f4a2713aSLionel Sambuc #include "llvm/Support/Compiler.h" 22f4a2713aSLionel Sambuc #include <string> 23*0a6a1f1dSLionel Sambuc #include <vector> 24f4a2713aSLionel Sambuc 25f4a2713aSLionel Sambuc namespace clang { 26f4a2713aSLionel Sambuc namespace cxstring { 27f4a2713aSLionel Sambuc 28f4a2713aSLionel Sambuc struct CXStringBuf; 29f4a2713aSLionel Sambuc 30f4a2713aSLionel Sambuc /// \brief Create a CXString object for an empty "" string. 31f4a2713aSLionel Sambuc CXString createEmpty(); 32f4a2713aSLionel Sambuc 33f4a2713aSLionel Sambuc /// \brief Create a CXString object for an NULL string. 34f4a2713aSLionel Sambuc /// 35f4a2713aSLionel Sambuc /// A NULL string should be used as an "invalid" value in case of errors. 36f4a2713aSLionel Sambuc CXString createNull(); 37f4a2713aSLionel Sambuc 38f4a2713aSLionel Sambuc /// \brief Create a CXString object from a nul-terminated C string. New 39f4a2713aSLionel Sambuc /// CXString may contain a pointer to \p String. 40f4a2713aSLionel Sambuc /// 41f4a2713aSLionel Sambuc /// \p String should not be changed by the caller afterwards. 42f4a2713aSLionel Sambuc CXString createRef(const char *String); 43f4a2713aSLionel Sambuc 44f4a2713aSLionel Sambuc /// \brief Create a CXString object from a nul-terminated C string. New 45f4a2713aSLionel Sambuc /// CXString will contain a copy of \p String. 46f4a2713aSLionel Sambuc /// 47f4a2713aSLionel Sambuc /// \p String can be changed or freed by the caller. 48f4a2713aSLionel Sambuc CXString createDup(const char *String); 49f4a2713aSLionel Sambuc 50f4a2713aSLionel Sambuc /// \brief Create a CXString object from a StringRef. New CXString may 51f4a2713aSLionel Sambuc /// contain a pointer to the undrelying data of \p String. 52f4a2713aSLionel Sambuc /// 53f4a2713aSLionel Sambuc /// \p String should not be changed by the caller afterwards. 54f4a2713aSLionel Sambuc CXString createRef(StringRef String); 55f4a2713aSLionel Sambuc 56f4a2713aSLionel Sambuc /// \brief Create a CXString object from a StringRef. New CXString will 57f4a2713aSLionel Sambuc /// contain a copy of \p String. 58f4a2713aSLionel Sambuc /// 59f4a2713aSLionel Sambuc /// \p String can be changed or freed by the caller. 60f4a2713aSLionel Sambuc CXString createDup(StringRef String); 61f4a2713aSLionel Sambuc 62f4a2713aSLionel Sambuc // Usually std::string is intended to be used as backing storage for CXString. 63f4a2713aSLionel Sambuc // In this case, call \c createRef(String.c_str()). 64f4a2713aSLionel Sambuc // 65f4a2713aSLionel Sambuc // If you need to make a copy, call \c createDup(StringRef(String)). 66f4a2713aSLionel Sambuc CXString createRef(std::string String) LLVM_DELETED_FUNCTION; 67f4a2713aSLionel Sambuc 68f4a2713aSLionel Sambuc /// \brief Create a CXString object that is backed by a string buffer. 69f4a2713aSLionel Sambuc CXString createCXString(CXStringBuf *buf); 70f4a2713aSLionel Sambuc 71f4a2713aSLionel Sambuc /// \brief A string pool used for fast allocation/deallocation of strings. 72f4a2713aSLionel Sambuc class CXStringPool { 73f4a2713aSLionel Sambuc public: 74f4a2713aSLionel Sambuc ~CXStringPool(); 75f4a2713aSLionel Sambuc 76f4a2713aSLionel Sambuc CXStringBuf *getCXStringBuf(CXTranslationUnit TU); 77f4a2713aSLionel Sambuc 78f4a2713aSLionel Sambuc private: 79f4a2713aSLionel Sambuc std::vector<CXStringBuf *> Pool; 80f4a2713aSLionel Sambuc 81f4a2713aSLionel Sambuc friend struct CXStringBuf; 82f4a2713aSLionel Sambuc }; 83f4a2713aSLionel Sambuc 84f4a2713aSLionel Sambuc struct CXStringBuf { 85f4a2713aSLionel Sambuc SmallString<128> Data; 86f4a2713aSLionel Sambuc CXTranslationUnit TU; 87f4a2713aSLionel Sambuc CXStringBufCXStringBuf88f4a2713aSLionel Sambuc CXStringBuf(CXTranslationUnit TU) : TU(TU) {} 89f4a2713aSLionel Sambuc 90f4a2713aSLionel Sambuc /// \brief Return this buffer to the pool. 91f4a2713aSLionel Sambuc void dispose(); 92f4a2713aSLionel Sambuc }; 93f4a2713aSLionel Sambuc 94f4a2713aSLionel Sambuc CXStringBuf *getCXStringBuf(CXTranslationUnit TU); 95f4a2713aSLionel Sambuc 96f4a2713aSLionel Sambuc /// \brief Returns true if the CXString data is managed by a pool. 97f4a2713aSLionel Sambuc bool isManagedByPool(CXString str); 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc } 100*0a6a1f1dSLionel Sambuc getContents(const CXUnsavedFile & UF)101*0a6a1f1dSLionel Sambucstatic inline StringRef getContents(const CXUnsavedFile &UF) { 102*0a6a1f1dSLionel Sambuc return StringRef(UF.Contents, UF.Length); 103*0a6a1f1dSLionel Sambuc } 104f4a2713aSLionel Sambuc } 105f4a2713aSLionel Sambuc 106f4a2713aSLionel Sambuc #endif 107f4a2713aSLionel Sambuc 108