1 //===- CXString.h - Routines for manipulating CXStrings -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines routines for manipulating CXStrings.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_CXSTRING_H
15 #define LLVM_CLANG_CXSTRING_H
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"
30 /// \brief Create a CXString object for an empty "" string.
31 CXString createEmpty();
33 /// \brief Create a CXString object for an NULL string.
35 /// A NULL string should be used as an "invalid" value in case of errors.
36 CXString createNull();
38 /// \brief Create a CXString object from a nul-terminated C string. New
39 /// CXString may contain a pointer to \p String.
41 /// \p String should not be changed by the caller afterwards.
42 CXString createRef(const char *String);
44 /// \brief Create a CXString object from a nul-terminated C string. New
45 /// CXString will contain a copy of \p String.
47 /// \p String can be changed or freed by the caller.
48 CXString createDup(const char *String);
50 /// \brief Create a CXString object from a StringRef. New CXString may
51 /// contain a pointer to the undrelying data of \p String.
53 /// \p String should not be changed by the caller afterwards.
54 CXString createRef(StringRef String);
56 /// \brief Create a CXString object from a StringRef. New CXString will
57 /// contain a copy of \p String.
59 /// \p String can be changed or freed by the caller.
60 CXString createDup(StringRef String);
62 // Usually std::string is intended to be used as backing storage for CXString.
63 // In this case, call \c createRef(String.c_str()).
65 // If you need to make a copy, call \c createDup(StringRef(String)).
66 CXString createRef(std::string String) LLVM_DELETED_FUNCTION;
68 /// \brief Create a CXString object that is backed by a string buffer.
69 CXString createCXString(CXStringBuf *buf);
71 /// \brief A string pool used for fast allocation/deallocation of strings.
76 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
79 std::vector<CXStringBuf *> Pool;
81 friend struct CXStringBuf;
85 SmallString<128> Data;
88 CXStringBuf(CXTranslationUnit TU) : TU(TU) {}
90 /// \brief Return this buffer to the pool.
94 CXStringBuf *getCXStringBuf(CXTranslationUnit TU);
96 /// \brief Returns true if the CXString data is managed by a pool.
97 bool isManagedByPool(CXString str);
101 static inline StringRef getContents(const CXUnsavedFile &UF) {
102 return StringRef(UF.Contents, UF.Length);