FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
simplecpp/simplecpp.h at master · chmodawk/simplecpp · GitHub
chmodawk
/
simplecpp
Public
forked from
cppcheck-opensource/simplecpp
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
simplecpp
/
simplecpp.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
341 lines (287 loc) · 10.7 KB
Breadcrumbs
simplecpp
/
simplecpp.h
Copy path
File metadata and controls
executable file
·
341 lines (287 loc) · 10.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
* simplecpp - A simple and high-fidelity C/C++ preprocessor library
* Copyright (C) 2016 Daniel Marjamäki.
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#
ifndef
simplecppH
#
define
simplecppH
#
include
<
cctype
>
#
include
<
cstddef
>
#
include
<
istream
>
#
include
<
list
>
#
include
<
map
>
#
include
<
set
>
#
include
<
string
>
#
include
<
vector
>
#
ifdef
_WIN32
#
ifdef
SIMPLECPP_EXPORT
#
define
SIMPLECPP_LIB
__declspec
(dllexport)
#
elif
defined(SIMPLECPP_IMPORT)
#
define
SIMPLECPP_LIB
__declspec
(dllimport)
#
else
#
define
SIMPLECPP_LIB
#
endif
#
else
#
define
SIMPLECPP_LIB
#
endif
namespace
simplecpp
{
typedef
std::string TokenString;
/*
*
* Location in source code
*/
class
SIMPLECPP_LIB
Location {
public:
explicit
Location
(
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
*/
void
adjust
(
const
std::string &str);
bool
operator
<(
const
Location &rhs)
const
{
if
(fileIndex != rhs.
fileIndex
)
return
fileIndex < rhs.
fileIndex
;
if
(line != rhs.
line
)
return
line < rhs.
line
;
return
col < rhs.
col
;
}
bool
sameline
(
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;
unsigned
int
fileIndex;
unsigned
int
line;
unsigned
int
col;
private:
static
const
std::string emptyFileName;
};
/*
*
* token class.
* @todo don't use std::string representation - for both memory and performance reasons
*/
class
SIMPLECPP_LIB
Token {
public:
Token
(
const
TokenString &s,
const
Location &loc) :
location
(loc), previous(
NULL
), next(
NULL
), string(s) {
flags
();
}
Token
(
const
Token &tok) :
macro
(tok.macro), location(tok.location), previous(
NULL
), next(
NULL
), string(tok.string) {
flags
();
}
void
flags
() {
name = (
std::isalpha
((
unsigned
char
)string[
0
]) || string[
0
] ==
'
_
'
|| string[
0
] ==
'
$
'
)
&& (string.
find
(
'
\'
'
) == string.
npos
);
comment = string.
size
() >
1U
&& string[
0
] ==
'
/
'
&& (string[
1
] ==
'
/
'
|| string[
1
] ==
'
*
'
);
number =
std::isdigit
((
unsigned
char
)string[
0
]) || (string.
size
() >
1U
&& string[
0
] ==
'
-
'
&&
std::isdigit
((
unsigned
char
)string[
1
]));
op = (string.
size
() ==
1U
) ? string[
0
] :
'
\0
'
;
}
const
TokenString&
str
()
const
{
return
string;
}
void
setstr
(
const
std::string &s) {
string = s;
flags
();
}
bool
isOneOf
(
const
char
ops[])
const
;
bool
startsWithOneOf
(
const
char
c[])
const
;
bool
endsWithOneOf
(
const
char
c[])
const
;
TokenString macro;
char
op;
bool
comment;
bool
name;
bool
number;
Location location;
Token *previous;
Token *next;
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;
}
void
printAll
()
const
;
void
printOut
()
const
;
private:
TokenString string;
//
Not implemented - prevent assignment
Token &
operator
=(
const
Token &tok);
};
/*
* Output from preprocessor
*/
struct
SIMPLECPP_LIB
Output {
explicit
Output
(
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
} type;
Location location;
std::string msg;
};
typedef
std::list<Output> OutputList;
/*
* List of tokens.
*/
class
SIMPLECPP_LIB
TokenList {
public:
explicit
TokenList
(std::vector<std::string> &filenames);
TokenList
(std::istream &istr, std::vector<std::string> &filenames,
const
std::string &filename=std::string(), OutputList *outputList =
NULL
);
TokenList
(
const
TokenList &other);
#
if
__cplusplus >= 201103L
TokenList
(TokenList &&other);
#
endif
~TokenList
();
TokenList &
operator
=(
const
TokenList &other);
#
if
__cplusplus >= 201103L
TokenList &
operator
=(TokenList &&other);
#
endif
void
clear
();
bool
empty
()
const
{
return
!frontToken;
}
void
push_back
(Token *tok);
void
dump
()
const
;
std::string
stringify
()
const
;
void
readfile
(std::istream &istr,
const
std::string &filename=std::string(), OutputList *outputList = NULL);
void
constFold
();
void
removeComments
();
Token *
front
() {
return
frontToken;
}
const
Token *
cfront
()
const
{
return
frontToken;
}
Token *
back
() {
return
backToken;
}
const
Token *
cback
()
const
{
return
backToken;
}
void
deleteToken
(Token *tok) {
if
(!tok)
return
;
Token *prev = tok->
previous
;
Token *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;
}
void
takeTokens
(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
=
NULL
;
}
/*
* sizeof(T)
*/
std::map<std::string, std::
size_t
> sizeOfType;
private:
void
combineOperators
();
void
constFoldUnaryNotPosNeg
(Token *tok);
void
constFoldMulDivRem
(Token *tok);
void
constFoldAddSub
(Token *tok);
void
constFoldShift
(Token *tok);
void
constFoldComparison
(Token *tok);
void
constFoldBitwise
(Token *tok);
void
constFoldLogicalOp
(Token *tok);
void
constFoldQuestionOp
(Token **tok1);
std::string
readUntil
(std::istream &istr,
const
Location &location,
char
start,
char
end, OutputList *outputList,
unsigned
int
bom);
void
lineDirective
(
unsigned
int
fileIndex,
unsigned
int
line, Location *location);
std::string
lastLine
(
int
maxsize=
100000
)
const
;
unsigned
int
fileIndex
(
const
std::string &filename);
Token *frontToken;
Token *backToken;
std::vector<std::string> &files;
};
/*
* Tracking how macros are used
*/
struct
SIMPLECPP_LIB
MacroUsage {
explicit
MacroUsage
(
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
*/
struct
SIMPLECPP_LIB
IfCond {
explicit
IfCond
(
const
Location& location,
const
std::string &E,
long
long
result) : location(location), E(E), result(result) {}
Location location;
//
location of #if/#elif
std::string E;
//
preprocessed condition
long
long
result;
//
condition result
};
/*
*
* Command line preprocessor settings.
* On the command line these are configured by -D, -U, -I, --include, -std
*/
struct
SIMPLECPP_LIB
DUI
{
DUI
() {}
std::list<std::string> defines;
std::set<std::string> undefined;
std::list<std::string> includePaths;
std::list<std::string> includes;
std::string std;
};
SIMPLECPP_LIB
long
long
characterLiteralToLL
(
const
std::string& str);
SIMPLECPP_LIB
std::map<std::string, TokenList*>
load
(
const
TokenList &rawtokens, std::vector<std::string> &filenames,
const
DUI
&dui, OutputList *outputList =
NULL
);
/*
*
* 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 filedata 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_LIB
void
preprocess
(TokenList &output,
const
TokenList &rawtokens, std::vector<std::string> &files, std::map<std::string, TokenList*> &filedata,
const
DUI
&dui, OutputList *outputList =
NULL
, std::list<MacroUsage> *macroUsage =
NULL
, std::list<IfCond> *ifCond =
NULL
);
/*
*
* Deallocate data
*/
SIMPLECPP_LIB
void
cleanup
(std::map<std::string, TokenList*> &filedata);
/*
* 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);
}
#
endif
Back
|
FazBrowse Home
|
New Git URL