forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine.cpp
4515 lines (4072 loc) · 120 KB
/
combine.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
/**
* @file combine.cpp
* Labels the chunks as needed.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "chunk_list.h"
#include "ChunkStack.h"
#include "prototypes.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include "unc_ctype.h"
#include <cassert>
static void fix_fcn_def_params(chunk_t *pc);
static void fix_typedef(chunk_t *pc);
static void fix_enum_struct_union(chunk_t *pc);
static void fix_casts(chunk_t *pc);
static void fix_type_cast(chunk_t *pc);
static chunk_t *fix_var_def(chunk_t *pc);
static void mark_function(chunk_t *pc);
static void mark_struct_union_body(chunk_t *start);
static chunk_t *mark_variable_definition(chunk_t *start);
static void mark_define_expressions(void);
static void process_returns(void);
static chunk_t *process_return(chunk_t *pc);
static void mark_class_ctor(chunk_t *pclass);
static void mark_namespace(chunk_t *pns);
static void mark_function_type(chunk_t *pc);
static void mark_cpp_constructor(chunk_t *pc);
static void mark_lvalue(chunk_t *pc);
static void mark_template_func(chunk_t *pc, chunk_t *pc_next);
static void mark_exec_sql(chunk_t *pc);
static void handle_oc_class(chunk_t *pc);
static void handle_oc_block(chunk_t *pc);
static void handle_oc_message_decl(chunk_t *pc);
static void handle_oc_message_send(chunk_t *pc);
static void handle_cs_square_stmt(chunk_t *pc);
static void handle_cs_property(chunk_t *pc);
static void handle_cpp_template(chunk_t *pc);
static void handle_d_template(chunk_t *pc);
static void handle_wrap(chunk_t *pc);
static void handle_proto_wrap(chunk_t *pc);
static bool is_oc_block(chunk_t *pc);
static void handle_java_assert(chunk_t *pc);
static chunk_t *get_d_template_types(ChunkStack& cs, chunk_t *open_paren);
static bool chunkstack_match(ChunkStack& cs, chunk_t *pc);
void make_type(chunk_t *pc)
{
if (pc != NULL)
{
if (pc->type == CT_WORD)
{
pc->type = CT_TYPE;
}
else if (chunk_is_star(pc))
{
pc->type = CT_PTR_TYPE;
}
else if (chunk_is_addr(pc))
{
pc->type = CT_BYREF;
}
}
}
void flag_series(chunk_t *start, chunk_t *end, UINT64 flags, chunk_nav_t nav)
{
while ((start != NULL) && (start != end))
{
start->flags |= flags;
start = chunk_get_next(start, nav);
}
if (end)
{
end->flags |= flags;
}
}
/**
* Flags everything from the open paren to the close paren.
*
* @param po Pointer to the open parenthesis
* @return The token after the close paren
*/
static chunk_t *flag_parens(chunk_t *po, UINT64 flags,
c_token_t opentype, c_token_t parenttype,
bool parent_all)
{
chunk_t *paren_close;
chunk_t *pc;
if (is_oc_block(po))
{
return(NULL);
}
paren_close = chunk_skip_to_match(po, CNAV_PREPROC);
if (paren_close == NULL)
{
LOG_FMT(LERR, "%s: no match for [%s] at [%d:%d]\n",
__func__, po->str.c_str(), po->orig_line, po->orig_col);
return(NULL);
}
if ((paren_close != NULL) && (po != paren_close))
{
if ((flags != 0) ||
(parent_all && (parenttype != CT_NONE)))
{
for (pc = chunk_get_next(po, CNAV_PREPROC);
pc != paren_close;
pc = chunk_get_next(pc, CNAV_PREPROC))
{
pc->flags |= flags;
if (parent_all)
{
pc->parent_type = parenttype;
}
}
}
if (opentype != CT_NONE)
{
po->type = opentype;
paren_close->type = (c_token_t)(opentype + 1);
}
if (parenttype != CT_NONE)
{
po->parent_type = parenttype;
paren_close->parent_type = parenttype;
}
}
return(chunk_get_next_ncnl(paren_close, CNAV_PREPROC));
}
/**
* Sets the parent of the open paren/brace/square/angle and the closing.
* Note - it is assumed that pc really does point to an open item and the
* close must be open + 1.
*
* @param start The open paren
* @param parent The type to assign as the parent
* @reutrn The chunk after the close paren
*/
chunk_t *set_paren_parent(chunk_t *start, c_token_t parent)
{
chunk_t *end;
end = chunk_skip_to_match(start, CNAV_PREPROC);
if (end != NULL)
{
start->parent_type = parent;
end->parent_type = parent;
}
return(chunk_get_next_ncnl(end, CNAV_PREPROC));
}
/* Scan backwards to see if we might be on a type declaration */
static bool chunk_ends_type(chunk_t *pc)
{
bool ret = false;
for (/* nada */; pc != NULL; pc = chunk_get_prev_ncnl(pc))
{
LOG_FMT(LFTYPE, "%s: [%s] %s flags %" PRIx64 " on line %d, col %d\n",
__func__, get_token_name(pc->type), pc->str.c_str(),
pc->flags, pc->orig_line, pc->orig_col);
if ((pc->type == CT_WORD) ||
(pc->type == CT_TYPE) ||
(pc->type == CT_STRUCT) ||
(pc->type == CT_DC_MEMBER) ||
(pc->type == CT_QUALIFIER))
{
continue;
}
if (chunk_is_semicolon(pc) ||
(pc->type == CT_BRACE_OPEN) ||
(pc->type == CT_BRACE_CLOSE))
{
ret = true;
}
break;
}
if (pc == NULL)
{
/* first token */
ret = true;
}
LOG_FMT(LFTYPE, "%s verdict: %s\n", __func__, ret ? "yes" : "no");
return(ret);
}
/* skip to the final word/type in a :: chain
* pc is either a word or a ::
*/
static chunk_t *skip_dc_member(chunk_t *start)
{
if (!start)
{
return NULL;
}
chunk_t *pc = start;
chunk_t *next = (pc->type == CT_DC_MEMBER) ? pc : chunk_get_next_ncnl(pc);
while (next && (next->type == CT_DC_MEMBER))
{
pc = chunk_get_next_ncnl(next);
next = chunk_get_next_ncnl(pc);
}
return pc;
}
/**
* This is called on every chunk.
* First on all non-preprocessor chunks and then on each preprocessor chunk.
* It does all the detection and classifying.
*/
void do_symbol_check(chunk_t *prev, chunk_t *pc, chunk_t *next)
{
chunk_t *tmp;
// LOG_FMT(LSYS, " %3d > ['%s' %s] ['%s' %s] ['%s' %s]\n",
// pc->orig_line,
// prev->str.c_str(), get_token_name(prev->type),
// pc->str.c_str(), get_token_name(pc->type),
// next->str.c_str(), get_token_name(next->type));
/* D stuff */
if ((cpd.lang_flags & LANG_D) &&
(pc->type == CT_QUALIFIER) &&
chunk_is_str(pc, "const", 5) &&
(next->type == CT_PAREN_OPEN))
{
pc->type = CT_D_CAST;
set_paren_parent(next, pc->type);
}
if ((next->type == CT_PAREN_OPEN) &&
((pc->type == CT_D_CAST) ||
(pc->type == CT_DELEGATE) ||
(pc->type == CT_ALIGN)))
{
/* mark the parenthesis parent */
tmp = set_paren_parent(next, pc->type);
/* For a D cast - convert the next item */
if ((pc->type == CT_D_CAST) && (tmp != NULL))
{
if (tmp->type == CT_STAR)
{
tmp->type = CT_DEREF;
}
else if (tmp->type == CT_AMP)
{
tmp->type = CT_ADDR;
}
else if (tmp->type == CT_MINUS)
{
tmp->type = CT_NEG;
}
else if (tmp->type == CT_PLUS)
{
tmp->type = CT_POS;
}
}
/* For a delegate, mark previous words as types and the item after the
* close paren as a variable def
*/
if (pc->type == CT_DELEGATE)
{
if (tmp != NULL)
{
tmp->parent_type = CT_DELEGATE;
if (tmp->level == tmp->brace_level)
{
tmp->flags |= PCF_VAR_1ST_DEF;
}
}
for (tmp = chunk_get_prev_ncnl(pc); tmp != NULL; tmp = chunk_get_prev_ncnl(tmp))
{
if (chunk_is_semicolon(tmp) ||
(tmp->type == CT_BRACE_OPEN) ||
(tmp->type == CT_VBRACE_OPEN))
{
break;
}
make_type(tmp);
}
}
if ((pc->type == CT_ALIGN) && (tmp != NULL))
{
if (tmp->type == CT_BRACE_OPEN)
{
set_paren_parent(tmp, pc->type);
}
else if (tmp->type == CT_COLON)
{
tmp->parent_type = pc->type;
}
}
} /* paren open + cast/align/delegate */
if (pc->type == CT_INVARIANT)
{
if (next->type == CT_PAREN_OPEN)
{
next->parent_type = pc->type;
tmp = chunk_get_next(next);
while (tmp != NULL)
{
if (tmp->type == CT_PAREN_CLOSE)
{
tmp->parent_type = pc->type;
break;
}
make_type(tmp);
tmp = chunk_get_next(tmp);
}
}
else
{
pc->type = CT_QUALIFIER;
}
}
/* Objective C stuff */
if (cpd.lang_flags & LANG_OC)
{
/* Check for message declarations */
if (pc->flags & PCF_STMT_START)
{
if ((chunk_is_str(pc, "-", 1) || chunk_is_str(pc, "+", 1)) &&
chunk_is_str(next, "(", 1))
{
handle_oc_message_decl(pc);
}
}
if (pc->flags & PCF_EXPR_START)
{
if (pc->type == CT_SQUARE_OPEN)
{
handle_oc_message_send(pc);
}
}
if ((pc->type == CT_OC_BLOCK_CARET) &&
(pc->parent_type != CT_OC_MSG_SPEC) &&
(pc->parent_type != CT_OC_MSG_DECL))
{
handle_oc_block(pc);
}
}
/* C# stuff */
if (cpd.lang_flags & LANG_CS)
{
/* '[assembly: xxx]' stuff */
if ((pc->flags & PCF_EXPR_START) &&
(pc->type == CT_SQUARE_OPEN))
{
handle_cs_square_stmt(pc);
}
if ((next != NULL) && (next->type == CT_BRACE_OPEN) &&
(next->parent_type == CT_NONE) &&
((pc->type == CT_SQUARE_CLOSE) ||
(pc->type == CT_WORD)))
{
handle_cs_property(next);
}
}
if ((pc->type == CT_ASSIGN) && (next->type == CT_SQUARE_OPEN))
{
set_paren_parent(next, CT_ASSIGN);
/* Mark one-liner assignment */
tmp = next;
while ((tmp = chunk_get_next_nc(tmp)) != NULL)
{
if (chunk_is_newline(tmp))
{
break;
}
if ((tmp->type == CT_SQUARE_CLOSE) && (next->level == tmp->level))
{
tmp->flags |= PCF_ONE_LINER;
next->flags |= PCF_ONE_LINER;
break;
}
}
}
if (pc->type == CT_ASSERT)
{
handle_java_assert(pc);
}
/* A [] in C# and D only follows a type */
if ((pc->type == CT_TSQUARE) &&
((cpd.lang_flags & (LANG_D | LANG_CS | LANG_VALA)) != 0))
{
if ((prev != NULL) && (prev->type == CT_WORD))
{
prev->type = CT_TYPE;
}
if ((next != NULL) && (next->type == CT_WORD))
{
next->flags |= PCF_VAR_1ST_DEF;
}
}
if ((pc->type == CT_SQL_EXEC) ||
(pc->type == CT_SQL_BEGIN) ||
(pc->type == CT_SQL_END))
{
mark_exec_sql(pc);
}
if ((pc->type == CT_FUNC_WRAP) ||
(pc->type == CT_TYPE_WRAP))
{
handle_wrap(pc);
next = chunk_get_next_ncnl(pc);
}
if (pc->type == CT_PROTO_WRAP)
{
handle_proto_wrap(pc);
}
/* Handle the typedef */
if (pc->type == CT_TYPEDEF)
{
fix_typedef(pc);
}
if ((pc->type == CT_ENUM) ||
(pc->type == CT_STRUCT) ||
(pc->type == CT_UNION))
{
if (prev->type != CT_TYPEDEF)
{
fix_enum_struct_union(pc);
}
}
if (pc->type == CT_EXTERN)
{
tmp = chunk_get_next_type(next, CT_BRACE_OPEN, next->level);
if (tmp != NULL)
{
set_paren_parent(tmp, CT_EXTERN);
}
}
if (pc->type == CT_TEMPLATE)
{
if (cpd.lang_flags & LANG_D)
{
handle_d_template(pc);
}
else
{
handle_cpp_template(pc);
}
}
if ((pc->type == CT_WORD) &&
(next->type == CT_ANGLE_OPEN) &&
(next->parent_type == CT_TEMPLATE))
{
mark_template_func(pc, next);
}
if ((pc->type == CT_SQUARE_CLOSE) &&
(next->type == CT_PAREN_OPEN))
{
flag_parens(next, 0, CT_FPAREN_OPEN, CT_NONE, false);
}
if (pc->type == CT_TYPE_CAST)
{
fix_type_cast(pc);
}
if (pc->type == CT_ASSIGN)
{
mark_lvalue(pc);
}
if ((pc->parent_type == CT_ASSIGN) &&
((pc->type == CT_BRACE_OPEN) ||
(pc->type == CT_SQUARE_OPEN)))
{
/* Mark everything in here as in assign */
flag_parens(pc, PCF_IN_ARRAY_ASSIGN, pc->type, CT_NONE, false);
}
if (pc->type == CT_D_TEMPLATE)
{
set_paren_parent(next, pc->type);
}
/**
* A word before an open paren is a function call or definition.
* CT_WORD => CT_FUNC_CALL or CT_FUNC_DEF
*/
if ((next->type == CT_PAREN_OPEN) &&
(next->next != NULL) && (next->next->type != CT_OC_BLOCK_CARET))
{
if ((pc->type == CT_WORD) || (pc->type == CT_OPERATOR_VAL))
{
pc->type = CT_FUNCTION;
}
else if (pc->type == CT_TYPE)
{
/**
* If we are on a type, then we are either on a C++ style cast, a
* function or we are on a function type.
* The only way to tell for sure is to find the close paren and see
* if it is followed by an open paren.
* "int(5.6)"
* "int()"
* "int(foo)(void)"
*
* FIXME: this check can be done better...
*/
tmp = chunk_get_next_type(next, CT_PAREN_CLOSE, next->level);
tmp = chunk_get_next(tmp);
if ((tmp != NULL) && (tmp->type == CT_PAREN_OPEN))
{
/* we have "TYPE(...)(" */
pc->type = CT_FUNCTION;
}
else
{
if ((pc->parent_type == CT_NONE) &&
((pc->flags & PCF_IN_TYPEDEF) == 0))
{
tmp = chunk_get_next_ncnl(next);
if ((tmp != NULL) && (tmp->type == CT_PAREN_CLOSE))
{
/* we have TYPE() */
pc->type = CT_FUNCTION;
}
else
{
/* we have TYPE(...) */
pc->type = CT_CPP_CAST;
set_paren_parent(next, CT_CPP_CAST);
}
}
}
}
else if (pc->type == CT_ATTRIBUTE)
{
flag_parens(next, 0, CT_FPAREN_OPEN, CT_ATTRIBUTE, false);
}
}
if ((cpd.lang_flags & LANG_PAWN) != 0)
{
if ((pc->type == CT_FUNCTION) && (pc->brace_level > 0))
{
pc->type = CT_FUNC_CALL;
}
if ((pc->type == CT_STATE) &&
(next != NULL) &&
(next->type == CT_PAREN_OPEN))
{
set_paren_parent(next, pc->type);
}
}
else
{
if ((pc->type == CT_FUNCTION) && !is_oc_block(pc))
{
mark_function(pc);
}
}
/* Detect C99 member stuff */
if ((pc->type == CT_MEMBER) &&
((prev->type == CT_COMMA) ||
(prev->type == CT_BRACE_OPEN)))
{
pc->type = CT_C99_MEMBER;
next->parent_type = CT_C99_MEMBER;
}
/* Mark function parens and braces */
if ((pc->type == CT_FUNC_DEF) ||
(pc->type == CT_FUNC_CALL) ||
(pc->type == CT_FUNC_CALL_USER) ||
(pc->type == CT_FUNC_PROTO))
{
tmp = next;
if (tmp->type == CT_SQUARE_OPEN)
{
tmp = set_paren_parent(tmp, pc->type);
}
else if ((tmp->type == CT_TSQUARE) ||
(tmp->parent_type == CT_OPERATOR))
{
tmp = chunk_get_next_ncnl(tmp);
}
tmp = flag_parens(tmp, 0, CT_FPAREN_OPEN, pc->type, false);
if (tmp != NULL)
{
if (tmp->type == CT_BRACE_OPEN)
{
if ((pc->flags & PCF_IN_CONST_ARGS) == 0)
{
set_paren_parent(tmp, pc->type);
}
}
else if (chunk_is_semicolon(tmp) && (pc->type == CT_FUNC_PROTO))
{
tmp->parent_type = pc->type;
}
}
}
/* Mark the parameters in catch() */
if ((pc->type == CT_CATCH) && (next->type == CT_SPAREN_OPEN))
{
fix_fcn_def_params(next);
}
if ((pc->type == CT_THROW) && (prev->type == CT_FPAREN_CLOSE))
{
pc->parent_type = prev->parent_type;
if (next->type == CT_PAREN_OPEN)
{
set_paren_parent(next, CT_THROW);
}
}
/* Mark the braces in: "for_each_entry(xxx) { }" */
if ((pc->type == CT_BRACE_OPEN) &&
(prev->type == CT_FPAREN_CLOSE) &&
((prev->parent_type == CT_FUNC_CALL) ||
(prev->parent_type == CT_FUNC_CALL_USER)) &&
((pc->flags & PCF_IN_CONST_ARGS) == 0))
{
set_paren_parent(pc, CT_FUNC_CALL);
}
/* Check for a close paren followed by an open paren, which means that
* we are on a function type declaration (C/C++ only?).
* Note that typedefs are already taken care of.
*/
if ((next != NULL) &&
((pc->flags & (PCF_IN_TYPEDEF | PCF_IN_TEMPLATE)) == 0) &&
(pc->parent_type != CT_CPP_CAST) &&
(pc->parent_type != CT_C_CAST) &&
((pc->flags & PCF_IN_PREPROC) == 0) &&
(!is_oc_block(pc)) &&
(pc->parent_type != CT_OC_MSG_DECL) &&
(pc->parent_type != CT_OC_MSG_SPEC) &&
chunk_is_str(pc, ")", 1) &&
chunk_is_str(next, "(", 1))
{
if ((cpd.lang_flags & LANG_D) != 0)
{
flag_parens(next, 0, CT_FPAREN_OPEN, CT_FUNC_CALL, false);
}
else
{
mark_function_type(pc);
}
}
if (((pc->type == CT_CLASS) ||
(pc->type == CT_STRUCT)) &&
(pc->level == pc->brace_level))
{
if ((pc->type != CT_STRUCT) || ((cpd.lang_flags & LANG_C) == 0))
{
mark_class_ctor(pc);
}
}
if (pc->type == CT_OC_CLASS)
{
handle_oc_class(pc);
}
if (pc->type == CT_NAMESPACE)
{
mark_namespace(pc);
}
/*TODO: Check for stuff that can only occur at the start of an statement */
if ((cpd.lang_flags & LANG_D) == 0)
{
/**
* Check a paren pair to see if it is a cast.
* Note that SPAREN and FPAREN have already been marked.
*/
if ((pc->type == CT_PAREN_OPEN) &&
((pc->parent_type == CT_NONE) ||
(pc->parent_type == CT_OC_MSG) ||
(pc->parent_type == CT_OC_BLOCK_EXPR)) &&
((next->type == CT_WORD) ||
(next->type == CT_TYPE) ||
(next->type == CT_STRUCT) ||
(next->type == CT_QUALIFIER) ||
(next->type == CT_MEMBER) ||
(next->type == CT_DC_MEMBER) ||
(next->type == CT_ENUM) ||
(next->type == CT_UNION)) &&
(prev->type != CT_SIZEOF) &&
(prev->parent_type != CT_OPERATOR) &&
((pc->flags & PCF_IN_TYPEDEF) == 0))
{
fix_casts(pc);
}
}
/* Check for stuff that can only occur at the start of an expression */
if ((pc->flags & PCF_EXPR_START) != 0)
{
/* Change STAR, MINUS, and PLUS in the easy cases */
if (pc->type == CT_STAR)
{
pc->type = (prev->type == CT_ANGLE_CLOSE) ? CT_PTR_TYPE : CT_DEREF;
}
if (pc->type == CT_MINUS)
{
pc->type = CT_NEG;
}
if (pc->type == CT_PLUS)
{
pc->type = CT_POS;
}
if (pc->type == CT_INCDEC_AFTER)
{
pc->type = CT_INCDEC_BEFORE;
//fprintf(stderr, "%s: %d> changed INCDEC_AFTER to INCDEC_BEFORE\n", __func__, pc->orig_line);
}
if (pc->type == CT_AMP)
{
//fprintf(stderr, "Changed AMP to ADDR on line %d\n", pc->orig_line);
pc->type = CT_ADDR;
}
}
/* Detect a variable definition that starts with struct/enum/union/class */
if (((pc->flags & PCF_IN_TYPEDEF) == 0) &&
(prev->parent_type != CT_CPP_CAST) &&
((prev->flags & PCF_IN_FCN_DEF) == 0) &&
((pc->type == CT_STRUCT) ||
(pc->type == CT_UNION) ||
(pc->type == CT_CLASS) ||
(pc->type == CT_ENUM)))
{
tmp = skip_dc_member(next);
if (tmp && ((tmp->type == CT_TYPE) || (tmp->type == CT_WORD)))
{
tmp->parent_type = pc->type;
tmp->type = CT_TYPE;
tmp = chunk_get_next_ncnl(tmp);
}
if ((tmp != NULL) && (tmp->type == CT_BRACE_OPEN))
{
tmp = chunk_skip_to_match(tmp);
tmp = chunk_get_next_ncnl(tmp);
}
if ((tmp != NULL) && (chunk_is_star(tmp) || chunk_is_addr(tmp) || (tmp->type == CT_WORD)))
{
mark_variable_definition(tmp);
}
}
/**
* Change the paren pair after a function/macrofunc.
* CT_PAREN_OPEN => CT_FPAREN_OPEN
*/
if (pc->type == CT_MACRO_FUNC)
{
flag_parens(next, PCF_IN_FCN_CALL, CT_FPAREN_OPEN, CT_MACRO_FUNC, false);
}
if ((pc->type == CT_MACRO_OPEN) ||
(pc->type == CT_MACRO_ELSE) ||
(pc->type == CT_MACRO_CLOSE))
{
if (next->type == CT_PAREN_OPEN)
{
flag_parens(next, 0, CT_FPAREN_OPEN, pc->type, false);
}
}
if ((pc->type == CT_DELETE) && (next->type == CT_TSQUARE))
{
next->parent_type = CT_DELETE;
}
/* Change CT_STAR to CT_PTR_TYPE or CT_ARITH or CT_DEREF */
if (pc->type == CT_STAR)
{
if (chunk_is_paren_close(next) || (next->type == CT_COMMA))
{
pc->type = CT_PTR_TYPE;
}
else if ((cpd.lang_flags & LANG_OC) && (next->type == CT_STAR))
{
/* Change pointer-to-pointer types in OC_MSG_DECLs
* from ARITH <===> DEREF to PTR_TYPE <===> PTR_TYPE */
pc->type = CT_PTR_TYPE;
pc->parent_type = prev->parent_type;
next->type = CT_PTR_TYPE;
next->parent_type = pc->parent_type;
}
else if ((prev->type == CT_SIZEOF) || (prev->type == CT_DELETE))
{
pc->type = CT_DEREF;
}
else if (((prev->type == CT_WORD) && chunk_ends_type(prev)) ||
(prev->type == CT_DC_MEMBER) || (prev->type == CT_PTR_TYPE))
{
pc->type = CT_PTR_TYPE;
}
else if (next->type == CT_SQUARE_OPEN)
{
pc->type = CT_PTR_TYPE;
}
else
{
/* most PCF_PUNCTUATOR chunks except a paren close would make this
* a deref. A paren close may end a cast or may be part of a macro fcn.
*/
pc->type = ((prev->flags & PCF_PUNCTUATOR) &&
(!chunk_is_paren_close(prev) ||
(prev->parent_type == CT_MACRO_FUNC)) &&
(prev->type != CT_SQUARE_CLOSE) &&
(prev->type != CT_DC_MEMBER)) ? CT_DEREF : CT_ARITH;
}
}
if (pc->type == CT_AMP)
{
if (prev->type == CT_DELETE)
{
pc->type = CT_ADDR;
}
else if (prev->type == CT_TYPE)
{
pc->type = CT_BYREF;
}
else
{
pc->type = CT_ARITH;
if (prev->type == CT_WORD)
{
tmp = chunk_get_prev_ncnl(prev);
if ((tmp != NULL) &&
(chunk_is_semicolon(tmp) ||
(tmp->type == CT_BRACE_OPEN) ||
(tmp->type == CT_QUALIFIER)))
{
prev->type = CT_TYPE;
pc->type = CT_ADDR;
next->flags |= PCF_VAR_1ST;
}
}
}
}
if ((pc->type == CT_MINUS) ||
(pc->type == CT_PLUS))
{
if ((prev->type == CT_POS) || (prev->type == CT_NEG))
{
pc->type = (pc->type == CT_MINUS) ? CT_NEG : CT_POS;
}
else if (prev->type == CT_OC_CLASS)
{
pc->type = (pc->type == CT_MINUS) ? CT_NEG : CT_POS;
}
else
{
pc->type = CT_ARITH;
}
}
}
/**
* Change CT_INCDEC_AFTER + WORD to CT_INCDEC_BEFORE
* Change number/word + CT_ADDR to CT_ARITH
* Change number/word + CT_STAR to CT_ARITH
* Change number/word + CT_NEG to CT_ARITH
* Change word + ( to a CT_FUNCTION
* Cahnge struct/union/enum + CT_WORD => CT_TYPE
* Force parens on return.
*
* TODO: This could be done earlier.
*
* Patterns detected:
* STRUCT/ENUM/UNION + WORD :: WORD => TYPE
* WORD + '(' :: WORD => FUNCTION
*/
void fix_symbols(void)
{
chunk_t *pc;
chunk_t *next;
chunk_t *prev;
chunk_t dummy;
mark_define_expressions();
pc = chunk_get_head();
if (chunk_is_newline(pc) || chunk_is_comment(pc))
{
pc = chunk_get_next_ncnl(pc);
}
while (pc != NULL)
{
prev = chunk_get_prev_ncnl(pc, CNAV_PREPROC);
if (prev == NULL)
{
prev = &dummy;
}
next = chunk_get_next_ncnl(pc, CNAV_PREPROC);
if (next == NULL)
{
next = &dummy;
}
do_symbol_check(prev, pc, next);
pc = chunk_get_next_ncnl(pc);
}
pawn_add_virtual_semicolons();
process_returns();
/**
* 2nd pass - handle variable definitions
* REVISIT: We need function params marked to do this (?)
*/
pc = chunk_get_head();
int square_level = -1;
while (pc != NULL)
{
/* Can't have a variable definition inside [ ] */
if (square_level < 0)
{
if (pc->type == CT_SQUARE_OPEN)
{
square_level = pc->level;
}
}
else
{
if (pc->level <= square_level)
{
square_level = -1;
}
}
/**
* A variable definition is possible after at the start of a statement
* that starts with: QUALIFIER, TYPE, or WORD
*/
if ((square_level < 0) &&
((pc->flags & PCF_STMT_START) != 0) &&
((pc->type == CT_QUALIFIER) ||
(pc->type == CT_TYPE) ||
(pc->type == CT_WORD)) &&
(pc->parent_type != CT_ENUM) &&
((pc->flags & PCF_IN_ENUM) == 0))
{
pc = fix_var_def(pc);
}