forked from cppcheck-opensource/simplecpp
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplecpp.h
More file actions
Latest commit
executable file
·514 lines (430 loc) · 17.2 KB
/
Copy pathsimplecpp.h
File metadata and controls
executable file
·514 lines (430 loc) · 17.2 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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* -*- C++ -*-
* simplecpp - A simple and high-fidelity C/C++ preprocessor library
* Copyright (C) 2016-2023 simplecpp team
*/
#ifndef simplecppH
#definesimplecppH
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
# defineSIMPLECPP_WINDOWS
#endif
#include<cctype>
#include<cstring>
#include<iosfwd>
#include<list>
#include<map>
#include<memory>
#include<set>
#include<string>
#include<unordered_map>
#include<vector>
#ifdef _WIN32
# ifdef SIMPLECPP_EXPORT
# defineSIMPLECPP_LIB__declspec(dllexport)
# elif defined(SIMPLECPP_IMPORT)
# defineSIMPLECPP_LIB__declspec(dllimport)
# else
# defineSIMPLECPP_LIB
# endif
#else
# defineSIMPLECPP_LIB
#endif
#ifdef SIMPLECPP_WINDOWS
# include<cstdint>
#else
# include<sys/stat.h>
#endif
#if defined(_MSC_VER)
# pragma warning(push)
// suppress warnings about "conversion from 'type1' to 'type2', possible loss of data"
# pragma warning(disable : 4267)
# pragma warning(disable : 4244)
#endif
namespacesimplecpp {
/** C code standard */
enumcstd_t { CUnknown=-1, C89, C99, C11, C17, C23 };
/** C++ code standard */
enumcppstd_t { CPPUnknown=-1, CPP03, CPP11, CPP14, CPP17, CPP20, CPP23, CPP26 };
typedef std::string TokenString;
classMacro;
classFileDataCache;
/**
* Location in source code
*/
classSIMPLECPP_LIB Location {
public:
explicitLocation(const std::vector<std::string> &f) : files(f), fileIndex(0), line(1U), col(0U) {}
Location(const Location &loc) : files(loc.files), fileIndex(loc.fileIndex), line(loc.line), col(loc.col) {}
Location &operator=(const Location &other) {
if (this != &other) {
fileIndex = other.fileIndex;
line = other.line;
col = other.col;
}
return *this;
}
/** increment this location by string */
voidadjust(const std::string &str);
booloperator<(const Location &rhs) const {
if (fileIndex != rhs.fileIndex)
return fileIndex < rhs.fileIndex;
if (line != rhs.line)
return line < rhs.line;
return col < rhs.col;
}
boolsameline(const Location &other) const {
return fileIndex == other.fileIndex && line == other.line;
}
const std::string& file() const {
return fileIndex < files.size() ? files[fileIndex] : emptyFileName;
}
const std::vector<std::string> &files;
unsignedint fileIndex;
unsignedint line;
unsignedint col;
private:
staticconst std::string emptyFileName;
};
/**
* token class.
* @todo don't use std::string representation - for both memory and performance reasons
*/
classSIMPLECPP_LIB Token {
public:
Token(const TokenString &s, const Location &loc, bool wsahead = false) :
whitespaceahead(wsahead), location(loc), previous(nullptr), next(nullptr), nextcond(nullptr), string(s) {
flags();
}
Token(const Token &tok) :
macro(tok.macro), op(tok.op), comment(tok.comment), name(tok.name), number(tok.number), whitespaceahead(tok.whitespaceahead), location(tok.location), previous(nullptr), next(nullptr), nextcond(nullptr), string(tok.string), mExpandedFrom(tok.mExpandedFrom) {
}
voidflags() {
name = (std::isalpha(static_cast<unsignedchar>(string[0])) || string[0] == '_' || string[0] == '$')
&& (std::memchr(string.c_str(), '\'', string.size()) == nullptr);
comment = string.size() > 1U && string[0] == '/' && (string[1] == '/' || string[1] == '*');
number = isNumberLike(string);
op = (string.size() == 1U && !name && !comment && !number) ? string[0] : '\0';
}
const TokenString& str() const {
return string;
}
voidsetstr(const std::string &s) {
string = s;
flags();
}
boolisOneOf(constchar ops[]) const;
boolstartsWithOneOf(constchar c[]) const;
boolendsWithOneOf(constchar c[]) const;
staticboolisNumberLike(const std::string& str) {
returnstd::isdigit(static_cast<unsignedchar>(str[0])) ||
(str.size() > 1U && (str[0] == '-' || str[0] == '+') && std::isdigit(static_cast<unsignedchar>(str[1])));
}
TokenString macro;
char op;
bool comment;
bool name;
bool number;
bool whitespaceahead;
Location location;
Token *previous;
Token *next;
mutableconst Token *nextcond;
const Token *previousSkipComments() const {
const Token *tok = this->previous;
while (tok && tok->comment)
tok = tok->previous;
return tok;
}
const Token *nextSkipComments() const {
const Token *tok = this->next;
while (tok && tok->comment)
tok = tok->next;
return tok;
}
voidsetExpandedFrom(const Token *tok, const Macro* m) {
mExpandedFrom = tok->mExpandedFrom;
mExpandedFrom.insert(m);
if (tok->whitespaceahead)
whitespaceahead = true;
}
boolisExpandedFrom(const Macro* m) const {
returnmExpandedFrom.find(m) != mExpandedFrom.end();
}
voidprintAll() const;
voidprintOut() const;
private:
TokenString string;
std::set<const Macro*> mExpandedFrom;
// Not implemented - prevent assignment
Token &operator=(const Token &tok);
};
/** Output from preprocessor */
structSIMPLECPP_LIB Output {
explicitOutput(const std::vector<std::string> &files) : type(ERROR), location(files) {}
enum Type {
ERROR, /* #error */
WARNING, /* #warning */
MISSING_HEADER,
INCLUDE_NESTED_TOO_DEEPLY,
SYNTAX_ERROR,
PORTABILITY_BACKSLASH,
UNHANDLED_CHAR_ERROR,
EXPLICIT_INCLUDE_NOT_FOUND,
FILE_NOT_FOUND,
DUI_ERROR
} type;
explicitOutput(const std::vector<std::string>& files, Type type, const std::string& msg) : type(type), location(files), msg(msg) {}
Location location;
std::string msg;
};
typedef std::list<Output> OutputList;
/** List of tokens. */
classSIMPLECPP_LIB TokenList {
public:
classStream;
explicitTokenList(std::vector<std::string> &filenames);
/** generates a token list from the given std::istream parameter */
TokenList(std::istream &istr, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
/** generates a token list from the given buffer */
TokenList(constunsignedchar* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
/** generates a token list from the given buffer */
TokenList(constchar* data, std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), OutputList *outputList = nullptr);
/** generates a token list from the given filename parameter */
TokenList(const std::string &filename, std::vector<std::string> &filenames, OutputList *outputList = nullptr);
TokenList(const TokenList &other);
TokenList(TokenList &&other);
~TokenList();
TokenList &operator=(const TokenList &other);
TokenList &operator=(TokenList &&other);
voidclear();
boolempty() const {
return !frontToken;
}
voidpush_back(Token *tok);
voiddump() const;
std::string stringify() const;
voidreadfile(Stream &stream, const std::string &filename=std::string(), OutputList *outputList = nullptr);
voidconstFold();
voidremoveComments();
Token *front() {
return frontToken;
}
const Token *cfront() const {
return frontToken;
}
Token *back() {
return backToken;
}
const Token *cback() const {
return backToken;
}
voiddeleteToken(Token *tok) {
if (!tok)
return;
Token * const prev = tok->previous;
Token * const next = tok->next;
if (prev)
prev->next = next;
if (next)
next->previous = prev;
if (frontToken == tok)
frontToken = next;
if (backToken == tok)
backToken = prev;
delete tok;
}
voidtakeTokens(TokenList &other) {
if (!other.frontToken)
return;
if (!frontToken) {
frontToken = other.frontToken;
} else {
backToken->next = other.frontToken;
other.frontToken->previous = backToken;
}
backToken = other.backToken;
other.frontToken = other.backToken = nullptr;
}
/** sizeof(T) */
std::map<std::string, std::size_t> sizeOfType;
const std::vector<std::string>& getFiles() const {
return files;
}
private:
voidcombineOperators();
voidconstFoldUnaryNotPosNeg(Token *tok);
voidconstFoldMulDivRem(Token *tok);
voidconstFoldAddSub(Token *tok);
voidconstFoldShift(Token *tok);
voidconstFoldComparison(Token *tok);
voidconstFoldBitwise(Token *tok);
voidconstFoldLogicalOp(Token *tok);
voidconstFoldQuestionOp(Token **tok1);
std::string readUntil(Stream &stream, const Location &location, char start, char end, OutputList *outputList);
voidlineDirective(unsignedint fileIndex, unsignedint line, Location *location);
std::string lastLine(int maxsize=1000) const;
const Token* lastLineTok(int maxsize=1000) const;
boolisLastLinePreprocessor(int maxsize=1000) const;
unsignedintfileIndex(const std::string &filename);
Token *frontToken;
Token *backToken;
std::vector<std::string> &files;
};
/** Tracking how macros are used */
structSIMPLECPP_LIB MacroUsage {
explicitMacroUsage(const std::vector<std::string> &f, bool macroValueKnown_) : macroLocation(f), useLocation(f), macroValueKnown(macroValueKnown_) {}
std::string macroName;
Location macroLocation;
Location useLocation;
bool macroValueKnown;
};
/** Tracking #if/#elif expressions */
structSIMPLECPP_LIB IfCond {
explicitIfCond(const Location& location, const std::string &E, longlong result) : location(location), E(E), result(result) {}
Location location; // location of #if/#elif
std::string E; // preprocessed condition
longlong result; // condition result
};
/**
* Command line preprocessor settings.
* On the command line these are configured by -D, -U, -I, --include, -std
*/
structSIMPLECPP_LIBDUI {
DUI() : clearIncludeCache(false), removeComments(false) {}
std::list<std::string> defines;
std::set<std::string> undefined;
std::list<std::string> includePaths;
std::list<std::string> includes;
std::string std;
bool clearIncludeCache;
bool removeComments; /** remove comment tokens from included files */
};
SIMPLECPP_LIBlonglongcharacterLiteralToLL(const std::string& str);
SIMPLECPP_LIB FileDataCache load(const TokenList &rawtokens, std::vector<std::string> &filenames, constDUI &dui, OutputList *outputList = nullptr);
/**
* Preprocess
* @todo simplify interface
* @param output TokenList that receives the preprocessing output
* @param rawtokens Raw tokenlist for top sourcefile
* @param files internal data of simplecpp
* @param cache output from simplecpp::load()
* @param dui defines, undefs, and include paths
* @param outputList output: list that will receive output messages
* @param macroUsage output: macro usage
* @param ifCond output: #if/#elif expressions
*/
SIMPLECPP_LIBvoidpreprocess(TokenList &output, const TokenList &rawtokens, std::vector<std::string> &files, FileDataCache &cache, constDUI &dui, OutputList *outputList = nullptr, std::list<MacroUsage> *macroUsage = nullptr, std::list<IfCond> *ifCond = nullptr);
/**
* Deallocate data
*/
SIMPLECPP_LIBvoidcleanup(FileDataCache &cache);
/** Simplify path */
SIMPLECPP_LIB std::string simplifyPath(std::string path);
/** Convert Cygwin path to Windows path */
SIMPLECPP_LIB std::string convertCygwinToWindowsPath(const std::string &cygwinPath);
/** Returns the C version a given standard */
SIMPLECPP_LIBcstd_tgetCStd(const std::string &std);
/** Returns the C++ version a given standard */
SIMPLECPP_LIBcppstd_tgetCppStd(const std::string &std);
/** Returns the __STDC_VERSION__ value for a given standard */
SIMPLECPP_LIB std::string getCStdString(const std::string &std);
SIMPLECPP_LIB std::string getCStdString(cstd_t std);
/** Returns the __cplusplus value for a given standard */
SIMPLECPP_LIB std::string getCppStdString(const std::string &std);
SIMPLECPP_LIB std::string getCppStdString(cppstd_t std);
structSIMPLECPP_LIB FileData {
/** The canonical filename associated with this data */
std::string filename;
/** The tokens associated with this file */
TokenList tokens;
};
classSIMPLECPP_LIB FileDataCache {
public:
FileDataCache() = default;
FileDataCache(const FileDataCache &) = delete;
FileDataCache(FileDataCache &&) = default;
FileDataCache &operator=(const FileDataCache &) = delete;
FileDataCache &operator=(FileDataCache &&) = default;
/** Get the cached data for a file, or load and then return it if it isn't cached.
* returns the file data and true if the file was loaded, false if it was cached. */
std::pair<FileData *, bool> get(const std::string &sourcefile, const std::string &header, constDUI &dui, bool systemheader, std::vector<std::string> &filenames, OutputList *outputList);
voidinsert(FileData data) {
FileData *const newdata = newFileData(std::move(data));
mData.emplace_back(newdata);
mNameMap.emplace(newdata->filename, newdata);
}
voidclear() {
mNameMap.clear();
mIdMap.clear();
mData.clear();
}
typedef std::vector<std::unique_ptr<FileData>> container_type;
typedef container_type::iterator iterator;
typedef container_type::const_iterator const_iterator;
typedef container_type::size_type size_type;
size_type size() const {
returnmData.size();
}
iterator begin() {
returnmData.begin();
}
iterator end() {
returnmData.end();
}
const_iterator begin() const {
returnmData.begin();
}
const_iterator end() const {
returnmData.end();
}
const_iterator cbegin() const {
returnmData.cbegin();
}
const_iterator cend() const {
returnmData.cend();
}
private:
structFileID {
#ifdef SIMPLECPP_WINDOWS
struct {
std::uint64_t VolumeSerialNumber;
struct {
std::uint64_t IdentifierHi;
std::uint64_t IdentifierLo;
} FileId;
} fileIdInfo;
booloperator==(const FileID &that) constnoexcept {
return fileIdInfo.VolumeSerialNumber == that.fileIdInfo.VolumeSerialNumber &&
fileIdInfo.FileId.IdentifierHi == that.fileIdInfo.FileId.IdentifierHi &&
fileIdInfo.FileId.IdentifierLo == that.fileIdInfo.FileId.IdentifierLo;
}
#else
dev_t dev;
ino_t ino;
booloperator==(const FileID& that) constnoexcept {
return dev == that.dev && ino == that.ino;
}
#endif
structHasher {
std::size_toperator()(const FileID &id) const {
#ifdef SIMPLECPP_WINDOWS
returnstatic_cast<std::size_t>(id.fileIdInfo.FileId.IdentifierHi ^ id.fileIdInfo.FileId.IdentifierLo ^
id.fileIdInfo.VolumeSerialNumber);
#else
returnstatic_cast<std::size_t>(id.dev) ^ static_cast<std::size_t>(id.ino);
#endif
}
};
};
using name_map_type = std::unordered_map<std::string, FileData *>;
using id_map_type = std::unordered_map<FileID, FileData *, FileID::Hasher>;
staticboolgetFileId(const std::string &path, FileID &id);
std::pair<FileData *, bool> tryload(name_map_type::iterator &name_it, constDUI &dui, std::vector<std::string> &filenames, OutputList *outputList);
container_type mData;
name_map_type mNameMap;
id_map_type mIdMap;
};
}
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
#endif