CXString.cpp
5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//===- CXString.cpp - Routines for manipulating CXStrings -----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines routines for manipulating CXStrings. It should be the
// only file that has internal knowledge of the encoding of the data in
// CXStrings.
//
//===----------------------------------------------------------------------===//
#include "CXString.h"
#include "CXTranslationUnit.h"
#include "clang-c/Index.h"
#include "clang/Frontend/ASTUnit.h"
#include "llvm/Support/ErrorHandling.h"
using namespace clang;
/// Describes the kind of underlying data in CXString.
enum CXStringFlag {
/// CXString contains a 'const char *' that it doesn't own.
CXS_Unmanaged,
/// CXString contains a 'const char *' that it allocated with malloc().
CXS_Malloc,
/// CXString contains a CXStringBuf that needs to be returned to the
/// CXStringPool.
CXS_StringBuf
};
namespace clang {
namespace cxstring {
//===----------------------------------------------------------------------===//
// Basic generation of CXStrings.
//===----------------------------------------------------------------------===//
CXString createEmpty() {
CXString Str;
Str.data = "";
Str.private_flags = CXS_Unmanaged;
return Str;
}
CXString createNull() {
CXString Str;
Str.data = nullptr;
Str.private_flags = CXS_Unmanaged;
return Str;
}
CXString createRef(const char *String) {
if (String && String[0] == '\0')
return createEmpty();
CXString Str;
Str.data = String;
Str.private_flags = CXS_Unmanaged;
return Str;
}
CXString createDup(const char *String) {
if (!String)
return createNull();
if (String[0] == '\0')
return createEmpty();
CXString Str;
Str.data = strdup(String);
Str.private_flags = CXS_Malloc;
return Str;
}
CXString createRef(StringRef String) {
// If the string is not nul-terminated, we have to make a copy.
// FIXME: This is doing a one past end read, and should be removed! For memory
// we don't manage, the API string can become unterminated at any time outside
// our control.
if (!String.empty() && String.data()[String.size()] != 0)
return createDup(String);
CXString Result;
Result.data = String.data();
Result.private_flags = (unsigned) CXS_Unmanaged;
return Result;
}
CXString createDup(StringRef String) {
CXString Result;
char *Spelling = static_cast<char *>(llvm::safe_malloc(String.size() + 1));
memmove(Spelling, String.data(), String.size());
Spelling[String.size()] = 0;
Result.data = Spelling;
Result.private_flags = (unsigned) CXS_Malloc;
return Result;
}
CXString createCXString(CXStringBuf *buf) {
CXString Str;
Str.data = buf;
Str.private_flags = (unsigned) CXS_StringBuf;
return Str;
}
CXStringSet *createSet(const std::vector<std::string> &Strings) {
CXStringSet *Set = new CXStringSet;
Set->Count = Strings.size();
Set->Strings = new CXString[Set->Count];
for (unsigned SI = 0, SE = Set->Count; SI < SE; ++SI)
Set->Strings[SI] = createDup(Strings[SI]);
return Set;
}
//===----------------------------------------------------------------------===//
// String pools.
//===----------------------------------------------------------------------===//
CXStringPool::~CXStringPool() {
for (std::vector<CXStringBuf *>::iterator I = Pool.begin(), E = Pool.end();
I != E; ++I) {
delete *I;
}
}
CXStringBuf *CXStringPool::getCXStringBuf(CXTranslationUnit TU) {
if (Pool.empty())
return new CXStringBuf(TU);
CXStringBuf *Buf = Pool.back();
Buf->Data.clear();
Pool.pop_back();
return Buf;
}
CXStringBuf *getCXStringBuf(CXTranslationUnit TU) {
return TU->StringPool->getCXStringBuf(TU);
}
void CXStringBuf::dispose() {
TU->StringPool->Pool.push_back(this);
}
bool isManagedByPool(CXString str) {
return ((CXStringFlag) str.private_flags) == CXS_StringBuf;
}
} // end namespace cxstring
} // end namespace clang
//===----------------------------------------------------------------------===//
// libClang public APIs.
//===----------------------------------------------------------------------===//
const char *clang_getCString(CXString string) {
if (string.private_flags == (unsigned) CXS_StringBuf) {
return static_cast<const cxstring::CXStringBuf *>(string.data)->Data.data();
}
return static_cast<const char *>(string.data);
}
void clang_disposeString(CXString string) {
switch ((CXStringFlag) string.private_flags) {
case CXS_Unmanaged:
break;
case CXS_Malloc:
if (string.data)
free(const_cast<void *>(string.data));
break;
case CXS_StringBuf:
static_cast<cxstring::CXStringBuf *>(
const_cast<void *>(string.data))->dispose();
break;
}
}
void clang_disposeStringSet(CXStringSet *set) {
for (unsigned SI = 0, SE = set->Count; SI < SE; ++SI)
clang_disposeString(set->Strings[SI]);
delete[] set->Strings;
delete set;
}