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