forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSource.h
391 lines (335 loc) · 10.3 KB
/
Source.h
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* This file is part of RTags (http://rtags.net).
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#ifndef Source_h
#define Source_h
#include <cstdint>
#include "Location.h"
#include "rct/Flags.h"
#include "rct/List.h"
#include "rct/Path.h"
#include "rct/Serializer.h"
struct SourceCache;
class SourceList;
struct Source
{
inline Source();
uint32_t fileId, compilerId, buildRootId, compileCommandsFileId;
Path extraCompiler;
uint64_t includePathHash;
enum Language {
NoLanguage,
C,
CPlusPlus,
CPlusPlus11,
CHeader,
CPlusPlusHeader,
CPlusPlus11Header,
ObjectiveC,
ObjectiveCPlusPlus
} language;
static const char *languageName(Language language);
enum Flag {
NoFlag = 0x0,
NoRtti = 0x1,
M32 = 0x2,
M64 = 0x4
};
Flags<Flag> flags;
enum CommandLineFlag {
IncludeExtraCompiler = 0x0001,
IncludeCompiler = 0x0002|IncludeExtraCompiler,
IncludeSourceFile = 0x0004,
IncludeDefines = 0x0008,
IncludeIncludePaths = 0x0010,
QuoteDefines = 0x0020,
FilterBlacklist = 0x0040,
ExcludeDefaultArguments = 0x0080,
ExcludeDefaultIncludePaths = 0x0100,
ExcludeDefaultDefines = 0x0200,
IncludeRTagsConfig = 0x0400,
PCHEnabled = 0x0800,
IncludeOutputFilename = 0x1000,
Default = IncludeDefines|IncludeIncludePaths|FilterBlacklist|IncludeRTagsConfig|IncludeOutputFilename,
};
struct Define {
enum Flag {
None = 0x0,
NoValue = 0x1
};
inline Define(const String &def = String(), const String &val = String(), Flags<Flag> f = NullFlags);
String define;
String value;
Flags<Flag> flags;
inline String toString(Flags<CommandLineFlag> flags = Flags<CommandLineFlag>()) const;
inline bool operator==(const Define &other) const { return !compare(other); }
inline bool operator!=(const Define &other) const { return compare(other) != 0; }
inline bool operator<(const Define &other) const { return compare(other) < 0; }
inline bool operator>(const Define &other) const { return compare(other) > 0; }
inline int compare(const Source::Define &other) const
{
if (flags != other.flags)
return flags.cast<int>() - other.flags.cast<int>();
int cmp = define.compare(other.define);
if (!cmp)
cmp = value.compare(other.value);
return cmp;
}
};
Set<Define> defines;
struct Include {
enum Type {
Type_None
#define DECLARE_INCLUDE_TYPE(type, arg, space) , type
#include "IncludeTypesInternal.h"
#undef DECLARE_INCLUDE_TYPE
};
Include(Type t = Type_None, const Path &p = Path())
: type(t), path(p)
{}
bool isPch() const;
Type type;
Path path;
inline String toString() const
{
switch (type) {
#define DECLARE_INCLUDE_TYPE(type, arg, space) case type: return String::format<128>("%s%s%s", #arg, space, path.constData());
#include "IncludeTypesInternal.h"
#undef DECLARE_INCLUDE_TYPE
case Type_None: break;
}
return String();
}
inline int compare(const Source::Include &other) const
{
if (type != other.type) {
return type < other.type ? -1 : 1;
}
return path.compare(other.path);
}
inline bool operator==(const Include &other) const { return !compare(other); }
inline bool operator!=(const Include &other) const { return compare(other) != 0; }
inline bool operator<(const Include &other) const { return compare(other) < 0; }
inline bool operator>(const Include &other) const { return compare(other) > 0; }
};
List<Include> includePaths;
List<String> arguments;
// int32_t sysRootIndex;
Path directory;
Path outputFilename;
bool isValid() const { return fileId; }
bool isNull() const { return !fileId; }
int compare(const Source &other) const;
bool compareArguments(const Source &other) const;
bool operator==(const Source &other) const;
bool operator!=(const Source &other) const;
bool operator<(const Source &other) const;
bool operator>(const Source &other) const;
List<String> toCommandLine(Flags<CommandLineFlag> flags = Flags<CommandLineFlag>(), bool *usedPch = 0) const;
inline bool isIndexable() const;
static inline bool isIndexable(Language lang);
Path sourceFile() const;
Path buildRoot() const;
Path compileCommands() const;
Path compiler() const;
void clear();
String toString() const;
static SourceList parse(const String &cmdLine,
const Path &pwd,
const List<String> &environment,
List<Path> *unresolvedInputLocation = 0,
SourceCache *cache = 0);
enum EncodeMode {
IgnoreSandbox,
EncodeSandbox
};
void encode(Serializer &serializer, EncodeMode mode) const;
void decode(Deserializer &deserializer, EncodeMode mode);
};
RCT_FLAGS(Source::Flag);
RCT_FLAGS(Source::CommandLineFlag);
RCT_FLAGS(Source::Define::Flag);
inline Source::Source()
: fileId(0), compilerId(0), buildRootId(0), includePathHash(0),
language(NoLanguage)
{
}
inline const char *Source::languageName(Language language)
{
switch (language) {
case NoLanguage: return "NoLanguage";
case C: return "C";
case CPlusPlus: return "CPlusPlus";
case CPlusPlus11: return "CPlusPlus11";
case CHeader: return "CHeader";
case CPlusPlusHeader: return "CPlusPlusHeader";
case CPlusPlus11Header: return "CPlusPlus11Header";
case ObjectiveC: return "ObjectiveC";
case ObjectiveCPlusPlus: return "ObjectiveCPlusPlus";
}
return "";
}
inline bool Source::isIndexable() const
{
return Source::isIndexable(language);
}
inline bool Source::isIndexable(Language lang)
{
switch (lang) {
case C:
case CPlusPlus:
case CPlusPlus11:
case ObjectiveC:
case ObjectiveCPlusPlus:
case CPlusPlus11Header:
case CPlusPlusHeader:
case CHeader:
return true;
default:
break;
}
return false;
}
inline int Source::compare(const Source &other) const
{
if (fileId < other.fileId) {
return -1;
} else if (fileId > other.fileId) {
return 1;
}
if (compilerId < other.compilerId) {
return -1;
} else if (compilerId > other.compilerId) {
return 1;
}
if (int cmp = arguments.compare(other.arguments)) {
return cmp;
}
if (int cmp = defines.compare(other.defines)) {
return cmp;
}
if (int cmp = includePaths.compare(other.includePaths)) {
return cmp;
}
if (language < other.language) {
return -1;
} else if (language > other.language) {
return 1;
}
return 0;
}
inline bool Source::operator==(const Source &other) const
{
return !compare(other);
}
inline bool Source::operator!=(const Source &other) const
{
return compare(other);
}
inline bool Source::operator<(const Source &other) const
{
return compare(other) < 0;
}
inline bool Source::operator>(const Source &other) const
{
return compare(other) > 0;
}
inline Source::Define::Define(const String &def, const String &val, Flags<Flag> f)
: define(def), value(val), flags(f)
{
}
template <> inline Serializer &operator<<(Serializer &s, const Source::Define &d)
{
s << d.define << d.value << d.flags;
return s;
}
template <> inline Deserializer &operator>>(Deserializer &s, Source::Define &d)
{
s >> d.define >> d.value >> d.flags;
return s;
}
template <> inline Serializer &operator<<(Serializer &s, const Source::Include &d)
{
s << static_cast<uint8_t>(d.type) << d.path;
return s;
}
template <> inline Deserializer &operator>>(Deserializer &s, Source::Include &d)
{
uint8_t type;
s >> type >> d.path;
d.type = static_cast<Source::Include::Type>(type);
return s;
}
template <> inline Serializer &operator<<(Serializer &s, const Source &b)
{
b.encode(s, Source::EncodeSandbox);
return s;
}
template <> inline Deserializer &operator>>(Deserializer &s, Source &b)
{
b.decode(s, Source::EncodeSandbox);
return s;
}
static inline Log operator<<(Log dbg, const Source &s)
{
dbg << String::format<256>("Source(%s)", s.toString().constData());
return dbg;
}
static inline Log operator<<(Log dbg, const Source::Define &def)
{
dbg << def.toString();
return dbg;
}
static inline Log operator<<(Log dbg, const Source::Include &inc)
{
dbg << inc.toString();
return dbg;
}
inline String Source::Define::toString(Flags<CommandLineFlag> f) const
{
String ret;
ret.reserve(2 + define.size() + value.size() + 5);
ret += "-D";
ret += define;
if (!(flags & NoValue)) {
ret += '=';
if (!value.isEmpty()) {
if (f & Source::QuoteDefines) {
String out = value;
out.replace("\\", "\\\\");
out.replace("\"", "\\\"");
ret += out;
} else {
ret += value;
}
}
}
return ret;
}
class SourceList : public List<Source>
{
public:
uint64_t parsed = 0;
uint32_t fileId() const { return isEmpty() ? 0 : front().fileId; }
};
template <>
inline Serializer &operator<<(Serializer &s, const SourceList &sources)
{
s << static_cast<const List<Source> &>(sources) << sources.parsed;
return s;
}
template <>
inline Deserializer &operator>>(Deserializer &d, SourceList &sources)
{
d >> static_cast<List<Source> &>(sources) >> sources.parsed;
return d;
}
#endif