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