/
preprocessor.cpp
1236 lines (1129 loc) · 35.1 KB
/
preprocessor.cpp
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003 by David White <dave@whitevine.net>
Copyright (C) 2005 - 2014 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program 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 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* WML preprocessor.
*/
#include "global.hpp"
#include "buffered_istream.hpp"
#include "config.hpp"
#include "filesystem.hpp"
#include "game_config.hpp"
#include "log.hpp"
#include "wesconfig.h"
#include "serialization/binary_or_text.hpp"
#include "serialization/string_utils.hpp"
#include "serialization/parser.hpp"
#include "filesystem.hpp"
#include "util.hpp"
#include "version.hpp"
#include <boost/foreach.hpp>
#include <stdexcept>
static lg::log_domain log_config("config");
#define ERR_CF LOG_STREAM(err, log_config)
#define WRN_CF LOG_STREAM(warn, log_config)
#define LOG_CF LOG_STREAM(info, log_config)
#define DBG_CF LOG_STREAM(debug, log_config)
static lg::log_domain log_preprocessor("preprocessor");
#define LOG_PREPROC LOG_STREAM(info,log_preprocessor)
using std::streambuf;
// map associating each filename encountered to a number
typedef std::map<std::string, int> t_file_number_map;
static t_file_number_map file_number_map;
static bool encode_filename = true;
static std::string preprocessor_error_detail_prefix = "\n ";
// get filename associated to this code
static std::string get_filename(const std::string& file_code){
if(!encode_filename)
return file_code;
std::stringstream s;
s << file_code;
int n = 0;
s >> std::hex >> n;
BOOST_FOREACH(const t_file_number_map::value_type& p, file_number_map){
if(p.second == n)
return p.first;
}
return "<unknown>";
}
// get code associated to this filename
static std::string get_file_code(const std::string& filename){
if(!encode_filename)
return filename;
// current number of encountered filenames
static int current_file_number = 0;
int& fnum = file_number_map[utils::escape(filename, " \\")];
if(fnum == 0)
fnum = ++current_file_number;
std::ostringstream shex;
shex << std::hex << fnum;
return shex.str();
}
// decode the filenames placed in a location
static std::string get_location(const std::string& loc)
{
std::string res;
std::vector< std::string > pos = utils::quoted_split(loc, ' ');
if(pos.empty())
return res;
std::vector< std::string >::const_iterator i = pos.begin(), end = pos.end();
while (true) {
res += get_filename(*(i++));
if(i == end) break;
res += ' ';
res += *(i++);
if(i == end) break;
res += ' ';
}
return res;
}
bool preproc_define::operator==(preproc_define const &v) const {
return value == v.value && arguments == v.arguments;
}
bool preproc_define::operator<(preproc_define const &v) const {
if (location < v.location)
return true;
if (v.location < location)
return false;
return linenum < v.linenum;
}
void preproc_define::write_argument(config_writer& writer, const std::string& arg) const
{
const std::string key = "argument";
writer.open_child(key);
writer.write_key_val("name", arg);
writer.close_child(key);
}
void preproc_define::write(config_writer& writer, const std::string& name) const
{
const std::string key = "preproc_define";
writer.open_child(key);
writer.write_key_val("name", name);
writer.write_key_val("value", value);
writer.write_key_val("textdomain", textdomain);
writer.write_key_val("linenum", lexical_cast<std::string>(linenum));
writer.write_key_val("location", get_location(location));
BOOST_FOREACH(const std::string &arg, arguments)
write_argument(writer, arg);
writer.close_child(key);
}
void preproc_define::read_argument(const config &cfg)
{
arguments.push_back(cfg["name"]);
}
void preproc_define::read(const config& cfg)
{
value = cfg["value"].str();
textdomain = cfg["textdomain"].str();
linenum = cfg["linenum"];
location = cfg["location"].str();
BOOST_FOREACH(const config &arg, cfg.child_range("argument"))
read_argument(arg);
}
preproc_map::value_type preproc_define::read_pair(const config &cfg)
{
preproc_define second;
second.read(cfg);
return preproc_map::value_type(cfg["name"], second);
}
std::ostream& operator<<(std::ostream& stream, const preproc_define& def)
{
return stream << "value: " << def.value << " arguments: " << def.location;
}
std::ostream& operator<<(std::ostream& stream, const preproc_map::value_type& def)
{
return stream << def.second;
}
class preprocessor;
class preprocessor_file;
class preprocessor_data;
class preprocessor_streambuf;
struct preprocessor_deleter;
/**
* Base class for preprocessing an input.
*/
class preprocessor
{
preprocessor *const old_preprocessor_;
std::string old_textdomain_;
std::string old_location_;
int old_linenum_;
protected:
preprocessor_streambuf &target_;
preprocessor(preprocessor_streambuf &);
public:
/**
* Preprocesses and sends some text to the #target_ buffer.
* @return false when the input has no data left.
*/
virtual bool get_chunk() = 0;
virtual ~preprocessor();
};
/**
* Target for sending preprocessed output.
* Objects of this class can be plugged into an STL stream.
*/
class preprocessor_streambuf: public streambuf
{
std::string out_buffer_; /**< Buffer read by the STL stream. */
virtual int underflow();
std::stringstream buffer_; /**< Buffer filled by the #current_ preprocessor. */
preprocessor *current_; /**< Input preprocessor. */
preproc_map *defines_;
preproc_map default_defines_;
std::string textdomain_;
std::string location_;
int linenum_;
int depth_;
/**
* Set to true if one preprocessor for this target started to read a string.
* Deeper-nested preprocessors are then forbidden to.
*/
bool quoted_;
friend class preprocessor;
friend class preprocessor_file;
friend class preprocessor_data;
friend struct preprocessor_deleter;
preprocessor_streambuf(preprocessor_streambuf const &);
public:
preprocessor_streambuf(preproc_map *);
void error(const std::string &, int);
};
preprocessor_streambuf::preprocessor_streambuf(preproc_map *def) :
streambuf(),
out_buffer_(""),
buffer_(),
current_(NULL),
defines_(def),
default_defines_(),
textdomain_(PACKAGE),
location_(""),
linenum_(0),
depth_(0),
quoted_(false)
{
}
preprocessor_streambuf::preprocessor_streambuf(preprocessor_streambuf const &t) :
streambuf(),
out_buffer_(""),
buffer_(),
current_(NULL),
defines_(t.defines_),
default_defines_(),
textdomain_(PACKAGE),
location_(""),
linenum_(0),
depth_(t.depth_),
quoted_(t.quoted_)
{
}
/**
* Called by an STL stream whenever it has reached the end of #out_buffer_.
* Fills #buffer_ by calling the #current_ preprocessor, then copies its
* content into #out_buffer_.
* @return the first character of #out_buffer_ if any, EOF otherwise.
*/
int preprocessor_streambuf::underflow()
{
unsigned sz = 0;
if (char *gp = gptr()) {
if (gp < egptr()) {
// Sanity check: the internal buffer has not been totally consumed,
// should we force the caller to use what remains first?
return *gp;
}
// The buffer has been completely read; fill it again.
// Keep part of the previous buffer, to ensure putback capabilities.
sz = out_buffer_.size();
buffer_.str(std::string());
if (sz > 3) {
buffer_ << out_buffer_.substr(sz - 3);
sz = 3;
}
else
{
buffer_ << out_buffer_;
}
} else {
// The internal get-data pointer is null
}
const int desired_fill_amount = 2000;
while (current_ && buffer_.rdbuf()->in_avail() < desired_fill_amount)
{
// Process files and data chunks until the desired buffer size is reached
if (!current_->get_chunk()) {
// Delete the current preprocessor item to restore its predecessor
delete current_;
}
}
// Update the internal state and data pointers
out_buffer_ = buffer_.str();
if(out_buffer_.empty()) {
return EOF;
}
char *begin = &*out_buffer_.begin();
unsigned bs = out_buffer_.size();
setg(begin, begin + sz, begin + bs);
if (sz >= bs)
return EOF;
return static_cast<unsigned char>(*(begin + sz));
}
std::string lineno_string(const std::string &lineno)
{
std::vector< std::string > pos = utils::quoted_split(lineno, ' ');
std::vector< std::string >::const_iterator i = pos.begin(), end = pos.end();
std::string included_from =
preprocessor_error_detail_prefix + "included from ";
std::string res;
while (i != end) {
std::string const &line = *(i++);
if (!res.empty())
res += included_from;
if (i != end)
res += get_filename(*(i++));
else
res += "<unknown>";
res += ':' + line;
}
if (res.empty()) res = "???";
return res;
}
void preprocessor_streambuf::error(const std::string& error_type, int l)
{
std::string position, error;
std::ostringstream pos;
pos << l << ' ' << location_;
position = lineno_string(pos.str());
error = error_type + '\n';
error += "at " + position;
ERR_CF << error << '\n';
throw preproc_config::error(error);
}
/**
* Sets up a new preprocessor for stream buffer \a t.
* Saves the current preprocessing context of #target_. It will be
* automatically restored on destruction.
*/
preprocessor::preprocessor(preprocessor_streambuf &t) :
old_preprocessor_(t.current_),
old_textdomain_(t.textdomain_),
old_location_(t.location_),
old_linenum_(t.linenum_),
target_(t)
{
++target_.depth_;
target_.current_ = this;
}
/**
* Restores the old preprocessing context of #target_.
* Appends location and domain directives to the buffer, so that the parser
* notices these changes.
*/
preprocessor::~preprocessor()
{
assert(target_.current_ == this);
if (!old_location_.empty()) {
target_.buffer_ << "\376line " << old_linenum_ << ' ' << old_location_ << '\n';
}
if (!old_textdomain_.empty() && target_.textdomain_ != old_textdomain_) {
target_.buffer_ << "\376textdomain " << old_textdomain_ << '\n';
}
target_.current_ = old_preprocessor_;
target_.location_ = old_location_;
target_.linenum_ = old_linenum_;
target_.textdomain_ = old_textdomain_;
--target_.depth_;
}
/**
* Specialized preprocessor for handling a file or a set of files.
* A preprocessor_file object is created when a preprocessor encounters an
* inclusion directive that resolves to a file or directory, e.g. '{themes/}'.
*/
class preprocessor_file: preprocessor
{
std::vector< std::string > files_;
std::vector< std::string >::const_iterator pos_, end_;
public:
preprocessor_file(preprocessor_streambuf &, std::string const &);
virtual bool get_chunk();
};
/**
* Specialized preprocessor for handling any kind of input stream.
* This is the core of the preprocessor.
*/
class preprocessor_data: preprocessor
{
/** Description of a preprocessing chunk. */
struct token_desc
{
enum TOKEN_TYPE {
START, // Toplevel
PROCESS_IF, // Processing the "if" branch of a ifdef/ifndef (the "else" branch will be skipped)
PROCESS_ELSE, // Processing the "else" branch of a ifdef/ifndef
SKIP_IF, // Skipping the "if" branch of a ifdef/ifndef (the "else" branch, if any, will be processed)
SKIP_ELSE, // Skipping the "else" branch of a ifdef/ifndef
STRING, // Processing a string
VERBATIM, // Processing a verbatim string
MACRO_SPACE, // Processing between chunks of a macro call (skip spaces)
MACRO_CHUNK, // Processing inside a chunk of a macro call (stop on space or '(')
MACRO_PARENS // Processing a parenthesized macro argument
};
token_desc(TOKEN_TYPE type, const int stack_pos, const int linenum)
: type(type)
, stack_pos(stack_pos)
, linenum(linenum)
{
}
TOKEN_TYPE type;
/** Starting position in #strings_ of the delayed text for this chunk. */
int stack_pos;
int linenum;
};
/**
* Manages the lifetime of the @c std::istream pointer we own.
*
* Since @ref in_ uses the stream as well this object must be created
* before @ref in_ and destroyed after @ref in_ is destroyed.
*/
scoped_istream in_scope_;
/** Input stream. */
buffered_istream in_;
std::string directory_;
/** Buffer for delayed input processing. */
std::vector< std::string > strings_;
/** Mapping of macro arguments to their content. */
std::map<std::string, std::string> *local_defines_;
/** Stack of nested preprocessing chunks. */
std::vector< token_desc > tokens_;
/**
* Set to true whenever input tokens cannot be directly sent to the target
* buffer. For instance, this happens with macro arguments. In that case,
* the output is redirected toward #strings_ until it can be processed.
*/
int slowpath_;
/**
* Non-zero when the preprocessor has to skip some input text.
* Increased whenever entering a conditional branch that is not useful,
* e.g. a ifdef that evaluates to false.
*/
int skipping_;
int linenum_;
std::string read_word();
std::string read_line();
std::string read_rest_of_line();
void skip_spaces();
void skip_eol();
void push_token(token_desc::TOKEN_TYPE);
void pop_token();
void put(char);
void put(std::string const & /*, int change_line
= 0 */);
void conditional_skip(bool skip);
public:
preprocessor_data(preprocessor_streambuf &,
std::istream *,
std::string const &history,
std::string const &name, int line,
std::string const &dir, std::string const &domain,
std::map<std::string, std::string> *defines);
~preprocessor_data();
virtual bool get_chunk();
friend bool operator==(preprocessor_data::token_desc::TOKEN_TYPE, char);
friend bool operator==(char, preprocessor_data::token_desc::TOKEN_TYPE);
friend bool operator!=(preprocessor_data::token_desc::TOKEN_TYPE, char);
friend bool operator!=(char, preprocessor_data::token_desc::TOKEN_TYPE);
};
bool operator==(preprocessor_data::token_desc::TOKEN_TYPE, char)
{
throw std::logic_error("don't compare tokens with characters");
}
bool operator==(char lhs, preprocessor_data::token_desc::TOKEN_TYPE rhs){ return rhs == lhs; }
bool operator!=(preprocessor_data::token_desc::TOKEN_TYPE rhs, char lhs){ return !(lhs == rhs); }
bool operator!=(char lhs, preprocessor_data::token_desc::TOKEN_TYPE rhs){ return rhs != lhs; }
preprocessor_file::preprocessor_file(preprocessor_streambuf &t, std::string const &name) :
preprocessor(t),
files_(),
pos_(),
end_()
{
if (is_directory(name))
get_files_in_dir(name, &files_, NULL, ENTIRE_FILE_PATH, SKIP_MEDIA_DIR, DO_REORDER);
else {
std::istream * file_stream = istream_file(name);
if (!file_stream->good()) {
ERR_CF << "Could not open file " << name << "\n";
delete file_stream;
}
else
new preprocessor_data(t, file_stream, "", get_short_wml_path(name),
1, directory_name(name), t.textdomain_, NULL);
}
pos_ = files_.begin();
end_ = files_.end();
}
/**
* preprocessor_file::get_chunk()
*
* Inserts and processes the next file in the list of included files.
* @return false if there is no next file.
*/
bool preprocessor_file::get_chunk()
{
while (pos_ != end_) {
const std::string &name = *(pos_++);
unsigned sz = name.size();
// Use reverse iterator to optimize testing
if (sz < 5 || !std::equal(name.rbegin(), name.rbegin() + 4, "gfc."))
continue;
new preprocessor_file(target_, name);
return true;
}
return false;
}
preprocessor_data::preprocessor_data(preprocessor_streambuf &t,
std::istream *i, std::string const &history, std::string const &name, int linenum,
std::string const &directory, std::string const &domain,
std::map<std::string, std::string> *defines) :
preprocessor(t),
in_scope_(i),
in_(*i),
directory_(directory),
strings_(),
local_defines_(defines),
tokens_(),
slowpath_(0),
skipping_(0),
linenum_(linenum)
{
std::ostringstream s;
s << history;
if (!name.empty()) {
if (!history.empty())
s << ' ';
s << get_file_code(name);
}
if (!t.location_.empty())
s << ' ' << t.linenum_ << ' ' << t.location_;
t.location_ = s.str();
t.linenum_ = linenum;
t.buffer_ << "\376line " << linenum << ' ' << t.location_ << '\n';
if (t.textdomain_ != domain) {
t.buffer_ << "\376textdomain " << domain << '\n';
t.textdomain_ = domain;
}
push_token(token_desc::START);
}
preprocessor_data::~preprocessor_data()
{
delete local_defines_;
}
void preprocessor_data::push_token(token_desc::TOKEN_TYPE t)
{
token_desc token(t, strings_.size(), linenum_);
tokens_.push_back(token);
if (t == token_desc::MACRO_SPACE) {
// Macro expansions do not have any associated storage at start.
return;
} else if (t == token_desc::STRING || t == token_desc::VERBATIM) {
/* Quoted strings are always inlined in the parent token. So
they need neither storage nor metadata, unless the parent
token is a macro expansion. */
token_desc::TOKEN_TYPE &outer_type = tokens_[tokens_.size() - 2].type;
if (outer_type != token_desc::MACRO_SPACE)
return;
outer_type = token_desc::MACRO_CHUNK;
tokens_.back().stack_pos = strings_.size() + 1;
}
std::ostringstream s;
if (!skipping_ && slowpath_) {
s << "\376line " << linenum_ << ' ' << target_.location_
<< "\n\376textdomain " << target_.textdomain_ << '\n';
}
strings_.push_back(s.str());
}
void preprocessor_data::pop_token()
{
token_desc::TOKEN_TYPE inner_type = tokens_.back().type;
unsigned stack_pos = tokens_.back().stack_pos;
tokens_.pop_back();
token_desc::TOKEN_TYPE &outer_type = tokens_.back().type;
if (inner_type == token_desc::MACRO_PARENS) {
// Parenthesized macro arguments are left on the stack.
assert(outer_type == token_desc::MACRO_SPACE);
return;
}
if (inner_type == token_desc::STRING || inner_type == token_desc::VERBATIM) {
// Quoted strings are always inlined.
assert(stack_pos == strings_.size());
return;
}
if (outer_type == token_desc::MACRO_SPACE) {
/* A macro expansion does not have any associated storage.
Instead, storage of the inner token is not discarded
but kept as a new macro argument. But if the inner token
was a macro expansion, it is about to be appended, so
prepare for it. */
if (inner_type == token_desc::MACRO_SPACE || inner_type == token_desc::MACRO_CHUNK) {
strings_.erase(strings_.begin() + stack_pos, strings_.end());
strings_.push_back(std::string());
}
assert(stack_pos + 1 == strings_.size());
outer_type = token_desc::MACRO_CHUNK;
return;
}
strings_.erase(strings_.begin() + stack_pos, strings_.end());
}
void preprocessor_data::skip_spaces()
{
for(;;) {
int c = in_.peek();
if (in_.eof() || (c != ' ' && c != '\t'))
return;
in_.get();
}
}
void preprocessor_data::skip_eol()
{
for(;;) {
int c = in_.get();
if (c == '\n') {
++linenum_;
return;
}
if (in_.eof())
return;
}
}
std::string preprocessor_data::read_word()
{
std::string res;
for(;;) {
int c = in_.peek();
if (c == preprocessor_streambuf::traits_type::eof() || utils::portable_isspace(c)) {
// DBG_CF << "(" << res << ")\n";
return res;
}
in_.get();
res += static_cast<char>(c);
}
}
std::string preprocessor_data::read_line()
{
std::string res;
for(;;) {
int c = in_.get();
if (c == '\n') {
++linenum_;
return res;
}
if (in_.eof())
return res;
if (c != '\r')
res += static_cast<char>(c);
}
}
std::string preprocessor_data::read_rest_of_line()
{
std::string res;
while(!in_.eof() && in_.peek() != '\n') {
int c = in_.get();
if (c != '\r')
res += static_cast<char>(c);
}
return res;
}
void preprocessor_data::put(char c)
{
if (skipping_)
return;
if (slowpath_) {
strings_.back() += c;
return;
}
int cond_linenum = c == '\n' ? linenum_ - 1 : linenum_;
if (unsigned diff = cond_linenum - target_.linenum_)
{
target_.linenum_ = cond_linenum;
if (diff <= target_.location_.size() + 11) {
target_.buffer_ << std::string(diff, '\n');
} else {
target_.buffer_ << "\376line " << target_.linenum_
<< ' ' << target_.location_ << '\n';
}
}
if (c == '\n')
++target_.linenum_;
target_.buffer_ << c;
}
void preprocessor_data::put(std::string const &s /*, int line_change*/)
{
if (skipping_)
return;
if (slowpath_) {
strings_.back() += s;
return;
}
target_.buffer_ << s;
// target_.linenum_ += line_change;
}
void preprocessor_data::conditional_skip(bool skip)
{
if (skip) ++skipping_;
push_token(skip ? token_desc::SKIP_ELSE : token_desc::PROCESS_IF);
}
bool preprocessor_data::get_chunk()
{
char c = in_.get();
token_desc &token = tokens_.back();
if (in_.eof()) {
// The end of file was reached.
// Make sure we don't have any incomplete tokens.
char const *s;
switch (token.type) {
case token_desc::START: return false; // everything is fine
case token_desc::PROCESS_IF:
case token_desc::SKIP_IF:
case token_desc::PROCESS_ELSE:
case token_desc::SKIP_ELSE: s = "#ifdef or #ifndef"; break;
case token_desc::STRING: s = "Quoted string"; break;
case token_desc::VERBATIM: s = "Verbatim string"; break;
case token_desc::MACRO_CHUNK:
case token_desc::MACRO_SPACE: s = "Macro substitution"; break;
case token_desc::MACRO_PARENS: s = "Macro argument"; break;
default: s = "???";
}
target_.error(std::string(s) + " not terminated", token.linenum);
}
if (c == '\n')
++linenum_;
if (c == '\376') {
std::string buffer(1, c);
for(;;) {
char d = in_.get();
if (in_.eof() || d == '\n')
break;
buffer += d;
}
buffer += '\n';
// line_change = 1-1 = 0
put(buffer);
} else if (token.type == token_desc::VERBATIM) {
put(c);
if (c == '>' && in_.peek() == '>') {
put(in_.get());
pop_token();
}
} else if (c == '<' && in_.peek() == '<') {
in_.get();
push_token(token_desc::VERBATIM);
put('<');
put('<');
} else if (c == '"') {
if (token.type == token_desc::STRING) {
target_.quoted_ = false;
put(c);
pop_token();
} else if (!target_.quoted_) {
target_.quoted_ = true;
push_token(token_desc::STRING);
put(c);
} else {
target_.error("Nested quoted string" , linenum_);
}
} else if (c == '{') {
push_token(token_desc::MACRO_SPACE);
++slowpath_;
} else if (c == ')' && token.type == token_desc::MACRO_PARENS) {
pop_token();
} else if (c == '#' && !target_.quoted_) {
std::string command = read_word();
bool comment = false;
if (command == "define") {
skip_spaces();
int linenum = linenum_;
std::vector< std::string > items = utils::split(read_line(), ' ');
if (items.empty()) {
target_.error("No macro name found after #define directive", linenum);
}
std::string symbol = items.front();
items.erase(items.begin());
int found_enddef = 0;
std::string buffer;
for(;;) {
if (in_.eof())
break;
char d = in_.get();
if (d == '\n')
++linenum_;
buffer += d;
if (d == '#')
found_enddef = 1;
else if (found_enddef > 0)
if (++found_enddef == 7) {
if (std::equal(buffer.end() - 6, buffer.end(), "enddef"))
break;
else
found_enddef = 0;
}
}
if (found_enddef != 7) {
target_.error("Unterminated preprocessor definition", linenum_);
}
if (!skipping_) {
buffer.erase(buffer.end() - 7, buffer.end());
(*target_.defines_)[symbol] = preproc_define(buffer, items, target_.textdomain_,
linenum + 1, target_.location_);
LOG_CF << "defining macro " << symbol << " (location " << get_location(target_.location_) << ")\n";
}
} else if (command == "ifdef") {
skip_spaces();
std::string const &symbol = read_word();
bool found = target_.defines_->count(symbol) != 0;
DBG_CF << "testing for macro " << symbol << ": "
<< (found ? "defined" : "not defined") << '\n';
conditional_skip(!found);
} else if (command == "ifndef") {
skip_spaces();
std::string const &symbol = read_word();
bool found = target_.defines_->count(symbol) != 0;
DBG_CF << "testing for macro " << symbol << ": "
<< (found ? "defined" : "not defined") << '\n';
conditional_skip(found);
} else if (command == "ifhave") {
skip_spaces();
std::string const &symbol = read_word();
bool found = !get_wml_location(symbol, directory_).empty();
DBG_CF << "testing for file or directory " << symbol << ": "
<< (found ? "found" : "not found") << '\n';
conditional_skip(!found);
} else if (command == "ifnhave") {
skip_spaces();
std::string const &symbol = read_word();
bool found = !get_wml_location(symbol, directory_).empty();
DBG_CF << "testing for file or directory " << symbol << ": "
<< (found ? "found" : "not found") << '\n';
conditional_skip(found);
} else if (command == "ifver" || command == "ifnver") {
skip_spaces();
std::string const& vsymstr = read_word();
skip_spaces();
std::string const& vopstr = read_word();
skip_spaces();
std::string const& vverstr = read_word();
const VERSION_COMP_OP vop = parse_version_op(vopstr);
if(vop == OP_INVALID) {
target_.error("Invalid #ifver/#ifnver operator", linenum_);
} else if(target_.defines_->count(vsymstr) != 0) {
preproc_define const& sym = (*target_.defines_)[vsymstr];
if(!sym.arguments.empty()) {
target_.error("First argument macro in #ifver/#ifnver should not require arguments", linenum_);
}
version_info const version1(sym.value);
version_info const version2(vverstr);
const bool found = do_version_check(version1, vop, version2);
DBG_CF << "testing version '" << version1.str() << "' against '" << version2.str() << "' (" << vopstr << "): "
<< (found ? "match" : "no match") << '\n';
conditional_skip(command == "ifver" ? !found : found);
} else {
target_.error("Undefined macro in #ifver/#ifnver first argument", linenum_);
}
} else if (command == "else") {
if (token.type == token_desc::SKIP_ELSE) {
pop_token();
--skipping_;
push_token(token_desc::PROCESS_ELSE);
} else if (token.type == token_desc::PROCESS_IF) {
pop_token();
++skipping_;
push_token(token_desc::SKIP_IF);
} else {
target_.error("Unexpected #else", linenum_);
}
} else if (command == "endif") {
switch (token.type) {
case token_desc::SKIP_IF:
case token_desc::SKIP_ELSE: --skipping_;
case token_desc::PROCESS_IF:
case token_desc::PROCESS_ELSE: break;
default:
target_.error("Unexpected #endif", linenum_);
}
pop_token();
} else if (command == "textdomain") {
skip_spaces();
std::string const &s = read_word();
if (s != target_.textdomain_) {
put("#textdomain ");
put(s);
target_.textdomain_ = s;
}
comment = true;
} else if (command == "enddef") {
target_.error("Unexpected #enddef", linenum_);
} else if (command == "undef") {
skip_spaces();
std::string const &symbol = read_word();
if (!skipping_) {
target_.defines_->erase(symbol);
LOG_CF << "undefine macro " << symbol << " (location " << get_location(target_.location_) << ")\n";
}
} else if (command == "error") {
if (!skipping_) {
skip_spaces();
std::ostringstream error;
error << "#error: \"" << read_rest_of_line() << '"';
target_.error(error.str(), linenum_);
} else
DBG_CF << "Skipped an error\n";
} else if (command == "warning") {
if (!skipping_) {
skip_spaces();
std::string message = read_rest_of_line();
WRN_CF << "#warning: \"" << message << "\" at "
<< linenum_ << ' ' << get_location(target_.location_) << '\n';
} else
DBG_CF << "Skipped a warning\n";
} else
comment = token.type != token_desc::MACRO_SPACE;
skip_eol();
if (comment)
put('\n');
} else if (token.type == token_desc::MACRO_SPACE || token.type == token_desc::MACRO_CHUNK) {
if (c == '(') {
// If a macro argument was started, it is implicitly ended.
token.type = token_desc::MACRO_SPACE;
push_token(token_desc::MACRO_PARENS);
} else if (utils::portable_isspace(c)) {
// If a macro argument was started, it is implicitly ended.
token.type = token_desc::MACRO_SPACE;
} else if (c == '}') {
--slowpath_;
if (skipping_) {
pop_token();
return true;
}