1 //===- CXString.h - Routines for manipulating CXStrings -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines routines for manipulating CXStrings.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
14 #define LLVM_CLANG_TOOLS_LIBCLANG_CXSTRING_H
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"
29 /// Create a CXString object for an empty "" string.
30 CXString
createEmpty();
32 /// Create a CXString object for an NULL string.
34 /// A NULL string should be used as an "invalid" value in case of errors.
35 CXString
createNull();
37 /// Create a CXString object from a nul-terminated C string. New
38 /// CXString may contain a pointer to \p String.
40 /// \p String should not be changed by the caller afterwards.
41 CXString
createRef(const char *String
);
43 /// Create a CXString object from a nul-terminated C string. New
44 /// CXString will contain a copy of \p String.
46 /// \p String can be changed or freed by the caller.
47 CXString
createDup(const char *String
);
49 /// Create a CXString object from a StringRef. New CXString may
50 /// contain a pointer to the undrelying data of \p String.
52 /// \p String should not be changed by the caller afterwards.
53 CXString
createRef(StringRef String
);
55 /// Create a CXString object from a StringRef. New CXString will
56 /// contain a copy of \p String.
58 /// \p String can be changed or freed by the caller.
59 CXString
createDup(StringRef String
);
61 // Usually std::string is intended to be used as backing storage for CXString.
62 // In this case, call \c createRef(String.c_str()).
64 // If you need to make a copy, call \c createDup(StringRef(String)).
65 CXString
createRef(std::string String
) = delete;
67 /// Create a CXString object that is backed by a string buffer.
68 CXString
createCXString(CXStringBuf
*buf
);
70 CXStringSet
*createSet(const std::vector
<std::string
> &Strings
);
72 /// A string pool used for fast allocation/deallocation of strings.
77 CXStringBuf
*getCXStringBuf(CXTranslationUnit TU
);
80 std::vector
<CXStringBuf
*> Pool
;
82 friend struct CXStringBuf
;
86 SmallString
<128> Data
;
89 CXStringBuf(CXTranslationUnit TU
) : TU(TU
) {}
91 /// Return this buffer to the pool.
95 CXStringBuf
*getCXStringBuf(CXTranslationUnit TU
);
97 /// Returns true if the CXString data is managed by a pool.
98 bool isManagedByPool(CXString str
);
102 static inline StringRef
getContents(const CXUnsavedFile
&UF
) {
103 return StringRef(UF
.Contents
, UF
.Length
);