forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestpreprocessor.cpp
3613 lines (3020 loc) · 128 KB
/
testpreprocessor.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
/*
* Cppcheck - A tool for static C/C++ code analysis
* Copyright (C) 2007-2012 Daniel Marjamäki and Cppcheck team.
*
* 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 3 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; 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
// The preprocessor that Cppcheck uses is a bit special. Instead of generating
// the code for a known configuration, it generates the code for each configuration.
#include "testsuite.h"
#include "preprocessor.h"
#include "tokenize.h"
#include "token.h"
#include "settings.h"
#include <map>
#include <string>
#include <sstream>
extern std::ostringstream errout;
extern std::ostringstream output;
#ifdef _MSC_VER
// Visual Studio complains about truncated values for '(char)0xff' and '(char)0xfe'
// TODO: Is there any nice way to fix these warnings?
#pragma warning( disable : 4310 )
#endif
class TestPreprocessor : public TestFixture {
public:
TestPreprocessor() : TestFixture("TestPreprocessor") {
Preprocessor::macroChar = '$';
}
class OurPreprocessor : public Preprocessor {
public:
static std::string replaceIfDefined(const std::string &str) {
return Preprocessor::replaceIfDefined(str);
}
static std::string expandMacros(const std::string& code, ErrorLogger *errorLogger = 0) {
return Preprocessor::expandMacros(code, "file.cpp", "", errorLogger);
}
static int getHeaderFileName(std::string &str) {
return Preprocessor::getHeaderFileName(str);
}
};
private:
void run() {
// Just read the code into a string. Perform simple cleanup of the code
TEST_CASE(readCode1);
TEST_CASE(readCode2);
// reading utf-16 file
TEST_CASE(utf16);
// The bug that started the whole work with the new preprocessor
TEST_CASE(Bug2190219);
TEST_CASE(test1);
TEST_CASE(test2);
TEST_CASE(test3);
TEST_CASE(test4);
TEST_CASE(test5);
TEST_CASE(test6);
TEST_CASE(test7);
TEST_CASE(test7a);
TEST_CASE(test7b);
TEST_CASE(test7c);
TEST_CASE(test7d);
TEST_CASE(test7e);
TEST_CASE(test8); // #if A==1 => cfg: A=1
// #error => don't extract any code
TEST_CASE(error1);
// #error with extended chars
TEST_CASE(error2);
TEST_CASE(error3);
TEST_CASE(error4); // #2919 - wrong filename is reported
TEST_CASE(if0_exclude);
TEST_CASE(if0_whitespace);
TEST_CASE(if0_else);
TEST_CASE(if0_elif);
// Don't handle include in a #if 0 block
TEST_CASE(if0_include_1);
TEST_CASE(if0_include_2);
// Handling include guards (don't create extra configuration for it)
TEST_CASE(includeguard1);
TEST_CASE(includeguard2);
TEST_CASE(newlines);
TEST_CASE(comments1);
TEST_CASE(if0);
TEST_CASE(if1);
TEST_CASE(elif);
// Test the Preprocessor::match_cfg_def
TEST_CASE(match_cfg_def);
TEST_CASE(if_cond1);
TEST_CASE(if_cond2);
TEST_CASE(if_cond3);
TEST_CASE(if_cond4);
TEST_CASE(if_cond5);
TEST_CASE(if_cond6);
TEST_CASE(if_cond8);
TEST_CASE(if_cond9);
TEST_CASE(if_cond10);
TEST_CASE(if_cond11);
TEST_CASE(if_cond12);
TEST_CASE(if_cond13);
TEST_CASE(if_cond14);
TEST_CASE(if_or_1);
TEST_CASE(if_or_2);
TEST_CASE(if_macro_eq_macro); // #3536
TEST_CASE(ticket_3675);
TEST_CASE(ticket_3699);
TEST_CASE(multiline1);
TEST_CASE(multiline2);
TEST_CASE(multiline3);
TEST_CASE(multiline4);
TEST_CASE(multiline5);
TEST_CASE(remove_asm);
TEST_CASE(if_defined); // "#if defined(AAA)" => "#ifdef AAA"
TEST_CASE(if_not_defined); // "#if !defined(AAA)" => "#ifndef AAA"
// Macros..
TEST_CASE(macro_simple1);
TEST_CASE(macro_simple2);
TEST_CASE(macro_simple3);
TEST_CASE(macro_simple4);
TEST_CASE(macro_simple5);
TEST_CASE(macro_simple6);
TEST_CASE(macro_simple7);
TEST_CASE(macro_simple8);
TEST_CASE(macro_simple9);
TEST_CASE(macro_simple10);
TEST_CASE(macro_simple11);
TEST_CASE(macro_simple12);
TEST_CASE(macro_simple13);
TEST_CASE(macro_simple14);
TEST_CASE(macro_simple15);
TEST_CASE(macroInMacro1);
TEST_CASE(macroInMacro2);
TEST_CASE(macro_mismatch);
TEST_CASE(macro_linenumbers);
TEST_CASE(macro_nopar);
TEST_CASE(macro_switchCase);
TEST_CASE(string1);
TEST_CASE(string2);
TEST_CASE(string3);
TEST_CASE(preprocessor_undef);
TEST_CASE(defdef); // Defined multiple times
TEST_CASE(preprocessor_doublesharp);
TEST_CASE(preprocessor_include_in_str);
TEST_CASE(va_args_1);
TEST_CASE(va_args_2);
TEST_CASE(va_args_3);
TEST_CASE(va_args_4);
TEST_CASE(multi_character_character);
TEST_CASE(stringify);
TEST_CASE(stringify2);
TEST_CASE(stringify3);
TEST_CASE(stringify4);
TEST_CASE(stringify5);
TEST_CASE(ifdefwithfile);
TEST_CASE(pragma);
TEST_CASE(pragma_asm_1);
TEST_CASE(pragma_asm_2);
TEST_CASE(endifsemicolon);
TEST_CASE(missing_doublequote);
TEST_CASE(handle_error);
TEST_CASE(dup_defines);
TEST_CASE(unicodeInCode);
TEST_CASE(unicodeInComment);
TEST_CASE(unicodeInString);
TEST_CASE(define_part_of_func);
TEST_CASE(conditionalDefine);
TEST_CASE(multiline_comment);
TEST_CASE(macro_parameters);
TEST_CASE(newline_in_macro);
TEST_CASE(includes);
TEST_CASE(ifdef_ifdefined);
// define and then ifdef
TEST_CASE(define_if1);
TEST_CASE(define_if2);
TEST_CASE(define_if3);
TEST_CASE(define_ifdef);
TEST_CASE(define_ifndef1);
TEST_CASE(define_ifndef2);
TEST_CASE(ifndef_define);
TEST_CASE(undef_ifdef);
TEST_CASE(endfile);
TEST_CASE(redundant_config);
TEST_CASE(testPreprocessorRead1);
TEST_CASE(testPreprocessorRead2);
TEST_CASE(testPreprocessorRead3);
TEST_CASE(testPreprocessorRead4);
TEST_CASE(invalid_define); // #2605 - hang for: '#define ='
// Show 'missing include' warnings
TEST_CASE(missingInclude);
// inline suppression, missingInclude
TEST_CASE(inline_suppression_for_missing_include);
// Using -D to predefine symbols
TEST_CASE(predefine1);
TEST_CASE(predefine2);
TEST_CASE(predefine3);
TEST_CASE(predefine4);
TEST_CASE(predefine5); // automatically define __cplusplus
// Test Preprocessor::simplifyCondition
TEST_CASE(simplifyCondition);
TEST_CASE(invalidElIf); // #2942 segfault
// Defines are given: test Preprocessor::handleIncludes
TEST_CASE(def_handleIncludes);
TEST_CASE(def_missingInclude);
TEST_CASE(def_handleIncludes_ifelse); // problems in handleIncludes for #else
TEST_CASE(def_valueWithParenthesis); // #3531
// Using -U to undefine symbols
TEST_CASE(undef1);
TEST_CASE(undef2);
TEST_CASE(undef3);
TEST_CASE(undef4);
TEST_CASE(undef5);
TEST_CASE(undef6);
TEST_CASE(undef7);
TEST_CASE(undef8);
TEST_CASE(undef9);
TEST_CASE(undef10);
TEST_CASE(handleUndef);
TEST_CASE(macroChar);
TEST_CASE(validateCfg);
}
void readCode1() {
const char code[] = " \t a //\n"
" #aa\t /* remove this */\tb \r\n";
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream istr(code);
std::string codestr(preprocessor.read(istr,"test.c"));
ASSERT_EQUALS("a\n#aa b\n", codestr);
}
void readCode2() {
const char code[] = "R\"( \" /* abc */ \n)\"";
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream istr(code);
std::string codestr(preprocessor.read(istr,"test.c"));
ASSERT_EQUALS("\" \\\" /* abc */ \\n\"\n", codestr);
}
void utf16() {
Settings settings;
Preprocessor preprocessor(&settings, this);
// a => a
{
const char code[] = { (char)0xff, (char)0xfe, 'a', '\0' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
ASSERT_EQUALS("a", preprocessor.read(istr, "test.c"));
}
{
const char code[] = { (char)0xfe, (char)0xff, '\0', 'a' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
ASSERT_EQUALS("a", preprocessor.read(istr, "test.c"));
}
// extended char => 0xff
{
const char code[] = { (char)0xff, (char)0xfe, 'a', 'a' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
const char expected[] = { (char)0xff, 0 };
ASSERT_EQUALS(expected, preprocessor.read(istr, "test.c"));
}
{
const char code[] = { (char)0xfe, (char)0xff, 'a', 'a' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
const char expected[] = { (char)0xff, 0 };
ASSERT_EQUALS(expected, preprocessor.read(istr, "test.c"));
}
// \r\n => \n
{
const char code[] = { (char)0xff, (char)0xfe, '\r', '\0', '\n', '\0' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
ASSERT_EQUALS("\n", preprocessor.read(istr, "test.c"));
}
{
const char code[] = { (char)0xfe, (char)0xff, '\0', '\r', '\0', '\n' };
std::string s(code, sizeof(code));
std::istringstream istr(s);
ASSERT_EQUALS("\n", preprocessor.read(istr, "test.c"));
}
}
void Bug2190219() {
const char filedata[] = "int main()\n"
"{\n"
"#ifdef __cplusplus\n"
" int* flags = new int[10];\n"
"#else\n"
" int* flags = (int*)malloc((10)*sizeof(int));\n"
"#endif\n"
"\n"
"#ifdef __cplusplus\n"
" delete [] flags;\n"
"#else\n"
" free(flags);\n"
"#endif\n"
"}\n";
// Expected result..
std::map<std::string, std::string> expected;
expected[""] = "int main()\n"
"{\n"
"\n"
"\n"
"\n"
"int* flags = (int*)malloc((10)*sizeof(int));\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"free(flags);\n"
"\n"
"}\n";
expected["__cplusplus"] = "int main()\n"
"{\n"
"\n"
"int* flags = new int[10];\n"
"\n"
"\n"
"\n"
"\n"
"\n"
"delete [] flags;\n"
"\n"
"\n"
"\n"
"}\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(expected[""], actual[""]);
ASSERT_EQUALS(expected["__cplusplus"], actual["__cplusplus"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test1() {
const char filedata[] = "#ifdef WIN32 \n"
" abcdef\n"
"#else \n"
" qwerty\n"
"#endif \n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\nqwerty\n\n", actual[""]);
ASSERT_EQUALS("\nabcdef\n\n\n\n", actual["WIN32"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test2() {
const char filedata[] = "# ifndef WIN32\n"
" \" # ifdef WIN32\" // a comment\n"
" # else \n"
" qwerty\n"
" # endif \n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\" # ifdef WIN32\"\n\n\n\n", actual[""]);
ASSERT_EQUALS("\n\n\nqwerty\n\n", actual["WIN32"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test3() {
const char filedata[] = "#ifdef ABC\n"
"a\n"
"#ifdef DEF\n"
"b\n"
"#endif\n"
"c\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\na\n\n\n\nc\n\n", actual["ABC"]);
ASSERT_EQUALS("\na\n\nb\n\nc\n\n", actual["ABC;DEF"]);
ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));
}
void test4() {
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#endif\t\n"
"#ifdef ABC\n"
"A\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nA\n\n\nA\n\n", actual["ABC"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test5() {
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#else\n"
"B\n"
"#ifdef DEF\n"
"C\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\nB\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nA\n\n\n\n\n\n\n", actual["ABC"]);
ASSERT_EQUALS("\n\n\nB\n\nC\n\n\n", actual["DEF"]);
ASSERT_EQUALS(3, static_cast<unsigned int>(actual.size()));
}
void test6() {
const char filedata[] = "#if(A)\n"
"#if ( A ) \n"
"#if A\n"
"#if defined((A))\n"
"#elif defined (A)\n";
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
const std::string actual(preprocessor.read(istr, "test.c"));
// Compare results..
ASSERT_EQUALS("#if A\n#if A\n#if A\n#if defined(A)\n#elif defined(A)\n", actual);
}
void test7() {
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#ifdef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
"",
errout.str());
// Compare results..
ASSERT_EQUALS("\n\n\n\n\n\n", actual[""]);
ASSERT_EQUALS("\nA\n\nB\n\n\n", actual["ABC"]);
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
test7a();
test7b();
test7c();
test7d();
}
void test7a() {
const char filedata[] = "#ifndef ABC\n"
"A\n"
"#ifndef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed NOT to be defined\n",
"", errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7b() {
const char filedata[] = "#ifndef ABC\n"
"A\n"
"#ifdef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed NOT to be defined\n",
"", errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7c() {
const char filedata[] = "#ifdef ABC\n"
"A\n"
"#ifndef ABC\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
"",
errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7d() {
const char filedata[] = "#if defined(ABC)\n"
"A\n"
"#if defined(ABC)\n"
"B\n"
"#endif\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
TODO_ASSERT_EQUALS("[file.c:3]: (error) ABC is already guaranteed to be defined\n",
"",
errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test7e() {
const char filedata[] = "#ifdef ABC\n"
"#file \"test.h\"\n"
"#ifndef test_h\n"
"#define test_h\n"
"#ifdef ABC\n"
"#endif\n"
"#endif\n"
"#endfile\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Make sure an error message is written..
ASSERT_EQUALS("",
errout.str());
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void test8() {
const char filedata[] = "#if A == 1\n"
"1\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// No error..
ASSERT_EQUALS("", errout.str());
// Compare results..
ASSERT_EQUALS(2U, actual.size());
ASSERT_EQUALS("\n\n\n", actual[""]);
ASSERT_EQUALS("\n1\n\n", actual["A=1"]);
}
void error1() {
const char filedata[] = "#ifdef A\n"
";\n"
"#else\n"
"#error abcd\n"
"#endif\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
errout.str("");
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
ASSERT_EQUALS("", actual[""]);
ASSERT_EQUALS("\n;\n\n\n\n", actual["A"]);
}
void error2() {
errout.str("");
const char filedata[] = "#error ê\n"
"#warning ê\n"
"123";
// Read string..
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("#error\n\n123", preprocessor.read(istr,"test.c"));
}
void error3() {
errout.str("");
Settings settings;
settings.userDefines = "__cplusplus";
Preprocessor preprocessor(&settings, this);
const std::string code("#error hello world!\n");
preprocessor.getcode(code, "X", "test.c");
ASSERT_EQUALS("[test.c:1]: (error) #error hello world!\n", errout.str());
}
// Ticket #2919 - wrong filename reported for #error
void error4() {
// In included file
{
errout.str("");
Settings settings;
settings.userDefines = "TEST";
Preprocessor preprocessor(&settings, this);
const std::string code("#file \"ab.h\"\n#error hello world!\n#endfile");
preprocessor.getcode(code, "TEST", "test.c");
ASSERT_EQUALS("[ab.h:1]: (error) #error hello world!\n", errout.str());
}
// After including a file
{
errout.str("");
Settings settings;
settings.userDefines = "TEST";
Preprocessor preprocessor(&settings, this);
const std::string code("#file \"ab.h\"\n\n#endfile\n#error aaa");
preprocessor.getcode(code, "TEST", "test.c");
ASSERT_EQUALS("[test.c:2]: (error) #error aaa\n", errout.str());
}
}
void if0_exclude() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"A\n"
"#endif\n"
"B\n");
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code,""));
std::istringstream code2("#if (0)\n"
"A\n"
"#endif\n"
"B\n");
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code2,""));
}
void if0_whitespace() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code(" # if 0 \n"
"A\n"
" # endif \n"
"B\n");
ASSERT_EQUALS("#if 0\n\n#endif\nB\n", preprocessor.read(code,""));
}
void if0_else() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"A\n"
"#else\n"
"B\n"
"#endif\n"
"C\n");
ASSERT_EQUALS("#if 0\n\n#else\nB\n#endif\nC\n", preprocessor.read(code,""));
std::istringstream code2("#if 1\n"
"A\n"
"#else\n"
"B\n"
"#endif\n"
"C\n");
TODO_ASSERT_EQUALS("#if 1\nA\n#else\n\n#endif\nC\n",
"#if 1\nA\n#else\nB\n#endif\nC\n", preprocessor.read(code2,""));
}
void if0_elif() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"A\n"
"#elif 1\n"
"B\n"
"#endif\n"
"C\n");
ASSERT_EQUALS("#if 0\n\n#elif 1\nB\n#endif\nC\n", preprocessor.read(code,""));
}
void if0_include_1() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"#include \"a.h\"\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("#if 0\n\n#endif\nAB\n", preprocessor.read(code,""));
}
void if0_include_2() {
Settings settings;
Preprocessor preprocessor(&settings, this);
std::istringstream code("#if 0\n"
"#include \"a.h\"\n"
"#ifdef WIN32\n"
"#else\n"
"#endif\n"
"#endif\n"
"AB\n");
ASSERT_EQUALS("#if 0\n\n#ifdef WIN32\n#else\n#endif\n#endif\nAB\n", preprocessor.read(code,""));
}
void includeguard1() {
// Handling include guards..
const char filedata[] = "#file \"abc.h\"\n"
"#ifndef abcH\n"
"#define abcH\n"
"#endif\n"
"#endfile\n"
"#ifdef ABC\n"
"#endif";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Expected configurations: "" and "ABC"
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
}
void includeguard2() {
// Handling include guards..
const char filedata[] = "#file \"abc.h\"\n"
"foo\n"
"#ifdef ABC\n"
"\n"
"#endif\n"
"#endfile\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Expected configurations: "" and "ABC"
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
ASSERT_EQUALS(true, actual.find("") != actual.end());
ASSERT_EQUALS(true, actual.find("ABC") != actual.end());
}
void ifdefwithfile() {
// Handling include guards..
const char filedata[] = "#ifdef ABC\n"
"#file \"abc.h\"\n"
"class A{};/*\n\n\n\n\n\n\n*/\n"
"#endfile\n"
"#endif\n"
"int main() {}\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Expected configurations: "" and "ABC"
ASSERT_EQUALS(2, static_cast<unsigned int>(actual.size()));
ASSERT_EQUALS("\n#file \"abc.h\"\n\n\n\n\n\n\n\n\n#endfile\n\nint main() {}\n", actual[""]);
ASSERT_EQUALS("\n#file \"abc.h\"\nclass A{};\n\n\n\n\n\n\n\n#endfile\n\nint main() {}\n", actual["ABC"]);
}
void newlines() {
const char filedata[] = "\r\r\n\n";
// Preprocess
std::istringstream istr(filedata);
Settings settings;
Preprocessor preprocessor(&settings, this);
ASSERT_EQUALS("\n\n\n", preprocessor.read(istr, "test.c"));
}
void comments1() {
{
const char filedata[] = "/*\n"
"#ifdef WIN32\n"
"#endif\n"
"*/\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n", actual[""]);
ASSERT_EQUALS(1, static_cast<unsigned int>(actual.size()));
}
{
const char filedata[] = "/*\n"
"\x080 #ifdef WIN32\n"
"#endif\n"
"*/\n";
// Preprocess => actual result..
std::istringstream istr(filedata);
std::map<std::string, std::string> actual;
Settings settings;
Preprocessor preprocessor(&settings, this);
preprocessor.preprocess(istr, actual, "file.c");
// Compare results..
ASSERT_EQUALS("\n\n\n\n", actual[""]);
ASSERT_EQUALS(1, static_cast<unsigned int>(actual.size()));
}
{
const char filedata[] = "void f()\n"
"{\n"
" *p = a / *b / *c;\n"
"}\n";