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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
// Copyright (c) 2008 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "TranslationUnit.h"
#include "Control.h"
#include "Parser.h"
#include "Lexer.h"
#include "MemoryPool.h"
#include "AST.h"
#include "Literals.h"
#include "DiagnosticClient.h"
#include <stack>
#include <vector>
#include <cstdarg>
#include <algorithm>
#include <utility>
#ifdef _MSC_VER
# define va_copy(dst, src) ((dst) = (src))
#elif defined(__INTEL_COMPILER) && !defined(va_copy)
# define va_copy __va_copy
#endif
using namespace CPlusPlus;
TranslationUnit::TranslationUnit(Control *control, const StringLiteral *fileId)
: _control(control),
_fileId(fileId),
_firstSourceChar(0),
_lastSourceChar(0),
_pool(0),
_ast(0),
_flags(0)
{
_tokens = new std::vector<Token>();
_comments = new std::vector<Token>();
_previousTranslationUnit = control->switchTranslationUnit(this);
_pool = new MemoryPool();
}
TranslationUnit::~TranslationUnit()
{
(void) _control->switchTranslationUnit(_previousTranslationUnit);
delete _tokens;
delete _comments;
delete _pool;
}
bool TranslationUnit::qtMocRunEnabled() const
{ return f._qtMocRunEnabled; }
void TranslationUnit::setQtMocRunEnabled(bool onoff)
{ f._qtMocRunEnabled = onoff; }
bool TranslationUnit::cxx0xEnabled() const
{ return f._cxx0xEnabled; }
void TranslationUnit::setCxxOxEnabled(bool onoff)
{ f._cxx0xEnabled = onoff; }
bool TranslationUnit::objCEnabled() const
{ return f._objCEnabled; }
void TranslationUnit::setObjCEnabled(bool onoff)
{ f._objCEnabled = onoff; }
Control *TranslationUnit::control() const
{ return _control; }
const StringLiteral *TranslationUnit::fileId() const
{ return _fileId; }
const char *TranslationUnit::fileName() const
{ return _fileId->chars(); }
unsigned TranslationUnit::fileNameLength() const
{ return _fileId->size(); }
const char *TranslationUnit::firstSourceChar() const
{ return _firstSourceChar; }
const char *TranslationUnit::lastSourceChar() const
{ return _lastSourceChar; }
unsigned TranslationUnit::sourceLength() const
{ return _lastSourceChar - _firstSourceChar; }
void TranslationUnit::setSource(const char *source, unsigned size)
{
_firstSourceChar = source;
_lastSourceChar = source + size;
}
unsigned TranslationUnit::tokenCount() const
{ return _tokens->size(); }
const Token &TranslationUnit::tokenAt(unsigned index) const
{ return _tokens->at(index); }
int TranslationUnit::tokenKind(unsigned index) const
{ return _tokens->at(index).f.kind; }
const char *TranslationUnit::spell(unsigned index) const
{
if (! index)
return 0;
return _tokens->at(index).spell();
}
unsigned TranslationUnit::commentCount() const
{ return _comments->size(); }
const Token &TranslationUnit::commentAt(unsigned index) const
{ return _comments->at(index); }
const Identifier *TranslationUnit::identifier(unsigned index) const
{ return _tokens->at(index).identifier; }
const Literal *TranslationUnit::literal(unsigned index) const
{ return _tokens->at(index).literal; }
const StringLiteral *TranslationUnit::stringLiteral(unsigned index) const
{ return _tokens->at(index).string; }
const NumericLiteral *TranslationUnit::numericLiteral(unsigned index) const
{ return _tokens->at(index).number; }
unsigned TranslationUnit::matchingBrace(unsigned index) const
{ return _tokens->at(index).close_brace; }
MemoryPool *TranslationUnit::memoryPool() const
{ return _pool; }
AST *TranslationUnit::ast() const
{ return _ast; }
bool TranslationUnit::isTokenized() const
{ return f._tokenized; }
bool TranslationUnit::isParsed() const
{ return f._parsed; }
void TranslationUnit::tokenize()
{
if (isTokenized())
return;
f._tokenized = true;
Lexer lex(this);
lex.setQtMocRunEnabled(f._qtMocRunEnabled);
lex.setCxxOxEnabled(f._cxx0xEnabled);
lex.setObjCEnabled(f._objCEnabled);
lex.setScanCommentTokens(true);
std::stack<unsigned> braces;
_tokens->push_back(Token()); // the first token needs to be invalid!
pushLineOffset(0);
pushPreprocessorLine(0, 1, fileId());
const Identifier *lineId = control()->identifier("line");
const Identifier *expansionId = control()->identifier("expansion");
const Identifier *beginId = control()->identifier("begin");
const Identifier *endId = control()->identifier("end");
// We need to track information about the expanded tokens. A vector with an addition
// explicit index control is used instead of queue mainly for performance reasons.
std::vector<std::pair<unsigned, unsigned> > lineColumn;
unsigned lineColumnIdx = 0;
Token tk;
do {
lex(&tk);
_Lrecognize:
if (tk.is(T_POUND) && tk.newline()) {
unsigned offset = tk.offset;
lex(&tk);
if (! tk.f.newline && tk.is(T_IDENTIFIER) && tk.identifier == expansionId) {
// It's an expansion mark.
lex(&tk);
if (!tk.f.newline && tk.is(T_IDENTIFIER)) {
if (tk.identifier == beginId) {
// Start of a macro expansion section.
lex(&tk);
// Gather where the expansion happens and its length.
unsigned macroOffset = static_cast<unsigned>(strtoul(tk.spell(), 0, 0));
lex(&tk);
lex(&tk); // Skip the separating comma
unsigned macroLength = static_cast<unsigned>(strtoul(tk.spell(), 0, 0));
lex(&tk);
// NOTE: We are currently not using the macro offset and length. They
// are kept here for now because of future use.
Q_UNUSED(macroOffset)
Q_UNUSED(macroLength)
// Now we need to gather the real line and columns from the upcoming
// tokens. But notice this is only relevant for tokens which are expanded
// but not generated.
while (tk.isNot(T_EOF_SYMBOL) && !tk.f.newline) {
// When we get a ~ it means there's a number of generated tokens
// following. Otherwise, we have actual data.
if (tk.is(T_TILDE)) {
lex(&tk);
// Get the total number of generated tokens and specifiy "null"
// information for them.
unsigned totalGenerated =
static_cast<unsigned>(strtoul(tk.spell(), 0, 0));
const std::size_t previousSize = lineColumn.size();
lineColumn.resize(previousSize + totalGenerated);
std::fill(lineColumn.begin() + previousSize,
lineColumn.end(),
std::make_pair(0, 0));
lex(&tk);
} else if (tk.is(T_NUMERIC_LITERAL)) {
unsigned line = static_cast<unsigned>(strtoul(tk.spell(), 0, 0));
lex(&tk);
lex(&tk); // Skip the separating colon
unsigned column = static_cast<unsigned>(strtoul(tk.spell(), 0, 0));
// Store line and column for this non-generated token.
lineColumn.push_back(std::make_pair(line, column));
lex(&tk);
}
}
} else if (tk.identifier == endId) {
// End of a macro expansion.
lineColumn.clear();
lineColumnIdx = 0;
lex(&tk);
}
}
} else {
if (! tk.f.newline && tk.is(T_IDENTIFIER) && tk.identifier == lineId)
lex(&tk);
if (! tk.f.newline && tk.is(T_NUMERIC_LITERAL)) {
unsigned line = (unsigned) strtoul(tk.spell(), 0, 0);
lex(&tk);
if (! tk.f.newline && tk.is(T_STRING_LITERAL)) {
const StringLiteral *fileName =
control()->stringLiteral(tk.string->chars(), tk.string->size());
pushPreprocessorLine(offset, line, fileName);
lex(&tk);
}
}
while (tk.isNot(T_EOF_SYMBOL) && ! tk.f.newline)
lex(&tk);
}
goto _Lrecognize;
} else if (tk.f.kind == T_LBRACE) {
braces.push(_tokens->size());
} else if (tk.f.kind == T_RBRACE && ! braces.empty()) {
const unsigned open_brace_index = braces.top();
braces.pop();
(*_tokens)[open_brace_index].close_brace = _tokens->size();
} else if (tk.isComment()) {
_comments->push_back(tk);
continue; // comments are not in the regular token stream
}
bool currentExpanded = false;
bool currentGenerated = false;
if (!lineColumn.empty() && lineColumnIdx < lineColumn.size()) {
currentExpanded = true;
const std::pair<unsigned, unsigned> &p = lineColumn[lineColumnIdx];
if (p.first)
_expandedLineColumn.insert(std::make_pair(tk.offset, p));
else
currentGenerated = true;
++lineColumnIdx;
}
tk.f.expanded = currentExpanded;
tk.f.generated = currentGenerated;
_tokens->push_back(tk);
} while (tk.f.kind);
for (; ! braces.empty(); braces.pop()) {
unsigned open_brace_index = braces.top();
(*_tokens)[open_brace_index].close_brace = _tokens->size();
}
}
bool TranslationUnit::skipFunctionBody() const
{ return f._skipFunctionBody; }
void TranslationUnit::setSkipFunctionBody(bool skipFunctionBody)
{ f._skipFunctionBody = skipFunctionBody; }
bool TranslationUnit::parse(ParseMode mode)
{
if (isParsed())
return false;
if (! isTokenized())
tokenize();
f._parsed = true;
Parser parser(this);
parser.setQtMocRunEnabled(f._qtMocRunEnabled);
parser.setCxxOxEnabled(f._cxx0xEnabled);
parser.setObjCEnabled(f._objCEnabled);
bool parsed = false;
switch (mode) {
case ParseTranlationUnit: {
TranslationUnitAST *node = 0;
parsed = parser.parseTranslationUnit(node);
_ast = node;
} break;
case ParseDeclaration: {
DeclarationAST *node = 0;
parsed = parser.parseDeclaration(node);
_ast = node;
} break;
case ParseExpression: {
ExpressionAST *node = 0;
parsed = parser.parseExpression(node);
_ast = node;
} break;
case ParseDeclarator: {
DeclaratorAST *node = 0;
parsed = parser.parseDeclarator(node, /*decl_specifier_list =*/ 0);
_ast = node;
} break;
case ParseStatement: {
StatementAST *node = 0;
parsed = parser.parseStatement(node);
_ast = node;
} break;
default:
break;
} // switch
return parsed;
}
void TranslationUnit::pushLineOffset(unsigned offset)
{ _lineOffsets.push_back(offset); }
void TranslationUnit::pushPreprocessorLine(unsigned offset,
unsigned line,
const StringLiteral *fileName)
{ _ppLines.push_back(PPLine(offset, line, fileName)); }
unsigned TranslationUnit::findLineNumber(unsigned offset) const
{
std::vector<unsigned>::const_iterator it =
std::lower_bound(_lineOffsets.begin(), _lineOffsets.end(), offset);
if (it != _lineOffsets.begin())
--it;
return it - _lineOffsets.begin();
}
TranslationUnit::PPLine TranslationUnit::findPreprocessorLine(unsigned offset) const
{
std::vector<PPLine>::const_iterator it =
std::lower_bound(_ppLines.begin(), _ppLines.end(), PPLine(offset));
if (it != _ppLines.begin())
--it;
return *it;
}
unsigned TranslationUnit::findColumnNumber(unsigned offset, unsigned lineNumber) const
{
if (! offset)
return 0;
return offset - _lineOffsets[lineNumber];
}
void TranslationUnit::getTokenPosition(unsigned index,
unsigned *line,
unsigned *column,
const StringLiteral **fileName) const
{ return getPosition(tokenAt(index).offset, line, column, fileName); }
void TranslationUnit::getTokenStartPosition(unsigned index, unsigned *line,
unsigned *column,
const StringLiteral **fileName) const
{ return getPosition(tokenAt(index).begin(), line, column, fileName); }
void TranslationUnit::getTokenEndPosition(unsigned index, unsigned *line,
unsigned *column,
const StringLiteral **fileName) const
{ return getPosition(tokenAt(index).end(), line, column, fileName); }
void TranslationUnit::getPosition(unsigned tokenOffset,
unsigned *line,
unsigned *column,
const StringLiteral **fileName) const
{
unsigned lineNumber = 0;
unsigned columnNumber = 0;
const StringLiteral *file = 0;
// If this token is expanded we already have the information directly from the expansion
// section header. Otherwise, we need to calculate it.
std::map<unsigned, std::pair<unsigned, unsigned> >::const_iterator it =
_expandedLineColumn.find(tokenOffset);
if (it != _expandedLineColumn.end()) {
lineNumber = it->second.first;
columnNumber = it->second.second + 1;
file = _fileId;
} else {
// Identify line within the entire translation unit.
lineNumber = findLineNumber(tokenOffset);
// Identify column.
columnNumber = findColumnNumber(tokenOffset, lineNumber);
// Adjust the line in regards to the preprocessing markers.
const PPLine ppLine = findPreprocessorLine(tokenOffset);
lineNumber -= findLineNumber(ppLine.offset) + 1;
lineNumber += ppLine.line;
file = ppLine.fileName;
}
if (line)
*line = lineNumber;
if (column)
*column = columnNumber;
if (fileName)
*fileName = file;
}
bool TranslationUnit::blockErrors(bool block)
{
bool previous = f._blockErrors;
f._blockErrors = block;
return previous;
}
void TranslationUnit::message(DiagnosticClient::Level level, unsigned index, const char *format, va_list args)
{
if (f._blockErrors)
return;
index = std::min(index, tokenCount() - 1);
unsigned line = 0, column = 0;
const StringLiteral *fileName = 0;
getTokenPosition(index, &line, &column, &fileName);
if (DiagnosticClient *client = control()->diagnosticClient()) {
client->report(level, fileName, line, column, format, args);
} else {
fprintf(stderr, "%s:%d: ", fileName->chars(), line);
const char *l = "error";
if (level == DiagnosticClient::Warning)
l = "warning";
else if (level == DiagnosticClient::Fatal)
l = "fatal";
fprintf(stderr, "%s: ", l);
vfprintf(stderr, format, args);
fputc('\n', stderr);
showErrorLine(index, column, stderr);
}
if (level == DiagnosticClient::Fatal)
exit(EXIT_FAILURE);
}
void TranslationUnit::warning(unsigned index, const char *format, ...)
{
if (f._blockErrors)
return;
va_list args, ap;
va_start(args, format);
va_copy(ap, args);
message(DiagnosticClient::Warning, index, format, args);
va_end(ap);
va_end(args);
}
void TranslationUnit::error(unsigned index, const char *format, ...)
{
if (f._blockErrors)
return;
va_list args, ap;
va_start(args, format);
va_copy(ap, args);
message(DiagnosticClient::Error, index, format, args);
va_end(ap);
va_end(args);
}
void TranslationUnit::fatal(unsigned index, const char *format, ...)
{
if (f._blockErrors)
return;
va_list args, ap;
va_start(args, format);
va_copy(ap, args);
message(DiagnosticClient::Fatal, index, format, args);
va_end(ap);
va_end(args);
}
unsigned TranslationUnit::findPreviousLineOffset(unsigned tokenIndex) const
{
unsigned lineOffset = _lineOffsets[findLineNumber(_tokens->at(tokenIndex).offset)];
return lineOffset;
}
void TranslationUnit::showErrorLine(unsigned index, unsigned column, FILE *out)
{
unsigned lineOffset = _lineOffsets[findLineNumber(_tokens->at(index).offset)];
for (const char *cp = _firstSourceChar + lineOffset + 1; *cp && *cp != '\n'; ++cp) {
fputc(*cp, out);
}
fputc('\n', out);
const char *end = _firstSourceChar + lineOffset + 1 + column - 1;
for (const char *cp = _firstSourceChar + lineOffset + 1; cp != end; ++cp) {
if (*cp != '\t')
fputc(' ', out);
else
fputc('\t', out);
}
fputc('^', out);
fputc('\n', out);
}
void TranslationUnit::resetAST()
{
delete _pool;
_pool = 0;
_ast = 0;
}
void TranslationUnit::release()
{
resetAST();
delete _tokens;
_tokens = 0;
delete _comments;
_comments = 0;
}
|