1219820Sjeff//===- CXString.h - Routines for manipulating CXStrings -------------------===// 2219820Sjeff// 3219820Sjeff// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4219820Sjeff// See https://llvm.org/LICENSE.txt for license information. 5219820Sjeff// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6219820Sjeff// 7219820Sjeff//===----------------------------------------------------------------------===// 8219820Sjeff// 9219820Sjeff// This file defines routines for manipulating CXStrings. 10219820Sjeff// 11219820Sjeff//===----------------------------------------------------------------------===// 12219820Sjeff 13219820Sjeff#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H 14219820Sjeff#define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H 15219820Sjeff 16219820Sjeff#include "clang-c/Index.h" 17219820Sjeff#include "clang/Basic/LLVM.h" 18219820Sjeff#include "llvm/ADT/SmallString.h" 19219820Sjeff#include "llvm/ADT/StringRef.h" 20219820Sjeff#include "llvm/Support/Compiler.h" 21219820Sjeff#include <string> 22219820Sjeff#include <vector> 23219820Sjeff 24219820Sjeffnamespace clang { 25219820Sjeffnamespace cxstring { 26219820Sjeff 27219820Sjeffstruct CXStringBuf; 28219820Sjeff 29219820Sjeff/// Create a CXString object for an empty "" string. 30219820SjeffCXString createEmpty(); 31219820Sjeff 32219820Sjeff/// Create a CXString object for an NULL string. 33219820Sjeff/// 34219820Sjeff/// A NULL string should be used as an "invalid" value in case of errors. 35219820SjeffCXString createNull(); 36219820Sjeff 37219820Sjeff/// Create a CXString object from a nul-terminated C string. New 38219820Sjeff/// CXString may contain a pointer to \p String. 39219820Sjeff/// 40219820Sjeff/// \p String should not be changed by the caller afterwards. 41219820SjeffCXString createRef(const char *String); 42219820Sjeff 43219820Sjeff/// Create a CXString object from a nul-terminated C string. New 44219820Sjeff/// CXString will contain a copy of \p String. 45219820Sjeff/// 46219820Sjeff/// \p String can be changed or freed by the caller. 47219820SjeffCXString createDup(const char *String); 48219820Sjeff 49219820Sjeff/// Create a CXString object from a StringRef. New CXString may 50219820Sjeff/// contain a pointer to the undrelying data of \p String. 51219820Sjeff/// 52219820Sjeff/// \p String should not be changed by the caller afterwards. 53219820SjeffCXString createRef(StringRef String); 54219820Sjeff 55219820Sjeff/// Create a CXString object from a StringRef. New CXString will 56219820Sjeff/// contain a copy of \p String. 57219820Sjeff/// 58219820Sjeff/// \p String can be changed or freed by the caller. 59219820SjeffCXString createDup(StringRef String); 60219820Sjeff 61219820Sjeff// Usually std::string is intended to be used as backing storage for CXString. 62219820Sjeff// In this case, call \c createRef(String.c_str()). 63219820Sjeff// 64219820Sjeff// If you need to make a copy, call \c createDup(StringRef(String)). 65219820SjeffCXString createRef(std::string String) = delete; 66219820Sjeff 67219820Sjeff/// Create a CXString object that is backed by a string buffer. 68219820SjeffCXString createCXString(CXStringBuf *buf); 69219820Sjeff 70219820SjeffCXStringSet *createSet(const std::vector<std::string> &Strings); 71219820Sjeff 72219820Sjeff/// A string pool used for fast allocation/deallocation of strings. 73219820Sjeffclass CXStringPool { 74219820Sjeffpublic: 75219820Sjeff ~CXStringPool(); 76219820Sjeff 77219820Sjeff CXStringBuf *getCXStringBuf(CXTranslationUnit TU); 78219820Sjeff 79219820Sjeffprivate: 80278886Shselasky std::vector<CXStringBuf *> Pool; 81278886Shselasky 82278886Shselasky friend struct CXStringBuf; 83278886Shselasky}; 84278886Shselasky 85278886Shselaskystruct CXStringBuf { 86278886Shselasky SmallString<128> Data; 87278886Shselasky CXTranslationUnit TU; 88278886Shselasky 89219820Sjeff CXStringBuf(CXTranslationUnit TU) : TU(TU) {} 90219820Sjeff 91219820Sjeff /// Return this buffer to the pool. 92219820Sjeff void dispose(); 93219820Sjeff}; 94219820Sjeff 95219820SjeffCXStringBuf *getCXStringBuf(CXTranslationUnit TU); 96219820Sjeff 97219820Sjeff/// Returns true if the CXString data is managed by a pool. 98219820Sjeffbool isManagedByPool(CXString str); 99219820Sjeff 100219820Sjeff} 101219820Sjeff 102219820Sjeffstatic inline StringRef getContents(const CXUnsavedFile &UF) { 103219820Sjeff return StringRef(UF.Contents, UF.Length); 104219820Sjeff} 105219820Sjeff} 106219820Sjeff 107219820Sjeff#endif 108219820Sjeff 109219820Sjeff