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