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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
|
/**
* @file chunk_list.cpp
* Manages and navigates the list of chunks.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "chunk_list.h"
#include "ListManager.h"
#include "prototypes.h"
#include "space.h"
#include "uncrustify.h"
#include "uncrustify_types.h"
#include <cstdlib>
#include <cstring>
typedef ListManager<chunk_t> ChunkList_t;
/**
* use this enum to define in what direction or location an
* operation shall be performed.
*/
enum class direction_e : unsigned int
{
FORWARD,
BACKWARD
};
/**
* @brief prototype for a function that checks a chunk to have a given type
*
* @note this typedef defines the function type "check_t"
* for a function pointer of type
* bool function(chunk_t *pc)
*/
typedef bool (*check_t)(chunk_t *pc);
/**
* @brief prototype for a function that searches through a chunk list
*
* @note this typedef defines the function type "search_t"
* for a function pointer of type
* chunk_t *function(chunk_t *cur, nav_t scope)
*/
typedef chunk_t * (*search_t)(chunk_t *cur, scope_e scope);
/**
* @brief search for a chunk that satisfies a condition in a chunk list
*
* A generic function that traverses a chunks list either
* in forward or reverse direction. The traversal continues until a
* chunk satisfies the condition defined by the compare function.
* Depending on the parameter cond the condition will either be
* checked to be true or false.
*
* Whenever a chunk list traversal is to be performed this function
* shall be used. This keeps the code clear and easy to understand.
*
* If there are performance issues this function might be worth to
* be optimized as it is heavily used.
*
* @param cur chunk to start search at
* @param check_fct compare function
* @param scope code parts to consider for search
* @param dir search direction
* @param cond success condition
*
* @retval nullptr no requested chunk was found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
static chunk_t *chunk_search(chunk_t *cur, const check_t check_fct, const scope_e scope = scope_e::ALL, const direction_e dir = direction_e::FORWARD, const bool cond = true);
/**
* @brief search for a chunk that satisfies a condition in a chunk list.
*
* This function is similar to chunk_search, except that it is tweaked to
* handle searches inside of preprocessor directives. Specifically, if the
* starting token is inside a preprocessor directive, it will ignore a line
* continuation, and will abort the search if it reaches the end of the
* directive. This function only searches forward.
*
* @param cur chunk to start search at
* @param check_fct compare function
* @param scope code parts to consider for search
* @param cond success condition
*
* @retval nullptr no requested chunk was found or invalid parameters provided
* @retval chunk_t pointer to the found chunk or pointer to the chunk at the
* end of the preprocessor directive
*/
static chunk_t *chunk_ppa_search(chunk_t *cur, const check_t check_fct, const bool cond = true);
static void chunk_log(chunk_t *pc, const char *text);
/*
* TODO: if we use C++ we can overload the following two functions
* and thus name them equally
*/
/**
* @brief search a chunk of a given category in a chunk list
*
* traverses a chunk list either in forward or backward direction.
* The traversal continues until a chunk of a given category is found.
*
* This function is a specialization of chunk_search.
*
* @param cur chunk to start search at
* @param type category to search for
* @param scope code parts to consider for search
* @param dir search direction
*
* @retval nullptr no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
static chunk_t *chunk_search_type(chunk_t *cur, const c_token_t type, const scope_e scope = scope_e::ALL, const direction_e dir = direction_e::FORWARD);
/**
* @brief search a chunk of a given type and level
*
* Traverses a chunk list in the specified direction until a chunk of a given type
* is found.
*
* This function is a specialization of chunk_search.
*
* @param cur chunk to start search at
* @param type category to search for
* @param scope code parts to consider for search
* @param dir search direction
* @param level nesting level to match or -1 / ANY_LEVEL
*
* @retval nullptr no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
static chunk_t *chunk_search_typelevel(chunk_t *cur, c_token_t type, scope_e scope = scope_e::ALL, direction_e dir = direction_e::FORWARD, int level = -1);
/**
* @brief searches a chunk that is non-NEWLINE, non-comment and non-preprocessor
*
* Traverses a chunk list either in forward or backward direction.
* The traversal continues until a chunk of a given category is found.
*
* @param cur chunk to start search at
* @param scope code parts to consider for search
* @param dir search direction
*
* @retval nullptr no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
static chunk_t *chunk_get_ncnlnp(chunk_t *cur, const scope_e scope = scope_e::ALL, const direction_e dir = direction_e::FORWARD);
/**
* @brief searches a chunk that holds a specific string
*
* Traverses a chunk list either in forward or backward direction until a chunk
* with the provided string was found. Additionally a nesting level can be
* provided to narrow down the search.
*
* @param cur chunk to start search at
* @param str string that searched chunk needs to have
* @param len length of the string
* @param scope code parts to consider for search
* @param dir search direction
* @param level nesting level of the searched chunk, ignored when negative
*
* @retval NULL no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
static chunk_t *chunk_search_str(chunk_t *cur, const char *str, size_t len, scope_e scope, direction_e dir, int level);
/**
* @brief Add a new chunk before/after the given position in a chunk list
*
* If ref is nullptr, add either at the head or tail based on the specified pos
*
* @param pc_in chunk to add to list
* @param ref insert position in list
* @param pos insert before or after
*
* @return chunk_t pointer to the added chunk
*/
static chunk_t *chunk_add(const chunk_t *pc_in, chunk_t *ref, const direction_e pos = direction_e::FORWARD);
/**
* @brief Determines which chunk search function to use
*
* Depending on the required search direction return a pointer
* to the corresponding chunk search function.
*
* @param dir search direction
*
* @return pointer to chunk search function
*/
static search_t select_search_fct(const direction_e dir = direction_e::FORWARD);
ChunkList_t g_cl; //! global chunk list
chunk_t *chunk_get_head(void)
{
return(g_cl.GetHead());
}
chunk_t *chunk_get_tail(void)
{
return(g_cl.GetTail());
}
static search_t select_search_fct(const direction_e dir)
{
return((dir == direction_e::FORWARD) ? chunk_get_next : chunk_get_prev);
}
chunk_t *chunk_search_prev_cat(chunk_t *pc, const c_token_t cat)
{
return(chunk_search_type(pc, cat, scope_e::ALL, direction_e::BACKWARD));
}
chunk_t *chunk_search_next_cat(chunk_t *pc, const c_token_t cat)
{
return(chunk_search_type(pc, cat, scope_e::ALL, direction_e::FORWARD));
}
bool are_chunks_in_same_line(chunk_t *start, chunk_t *end)
{
chunk_t *tmp;
if (start != nullptr)
{
tmp = chunk_get_next(start);
}
else
{
return(false);
}
while (tmp != nullptr && tmp != end)
{
if (chunk_is_token(tmp, CT_NEWLINE))
{
return(false);
}
tmp = chunk_get_next(tmp);
}
return(true);
}
static chunk_t *chunk_search_type(chunk_t *cur, const c_token_t type,
const scope_e scope, const direction_e dir)
{
/*
* Depending on the parameter dir the search function searches
* in forward or backward direction
*/
search_t search_function = select_search_fct(dir);
chunk_t *pc = cur;
do // loop over the chunk list
{
pc = search_function(pc, scope); // in either direction while
} while ( pc != nullptr // the end of the list was not reached yet
&& pc->type != type); // and the demanded chunk was not found either
return(pc); // the latest chunk is the searched one
}
static chunk_t *chunk_search_typelevel(chunk_t *cur, c_token_t type, scope_e scope, direction_e dir, int level)
{
/*
* Depending on the parameter dir the search function searches
* in forward or backward direction
*/
search_t search_function = select_search_fct(dir);
chunk_t *pc = cur;
do // loop over the chunk list
{
pc = search_function(pc, scope); // in either direction while
} while ( pc != nullptr // the end of the list was not reached yet
&& (is_expected_type_and_level(pc, type, level) == false));
return(pc); // the latest chunk is the searched one
}
static chunk_t *chunk_search_str(chunk_t *cur, const char *str, size_t len, scope_e scope, direction_e dir, int level)
{
/*
* Depending on the parameter dir the search function searches
* in forward or backward direction */
search_t search_function = select_search_fct(dir);
chunk_t *pc = cur;
do // loop over the chunk list
{
pc = search_function(pc, scope); // in either direction while
} while ( pc != nullptr // the end of the list was not reached yet
&& (is_expected_string_and_level(pc, str, level, len) == false));
return(pc); // the latest chunk is the searched one
}
static chunk_t *chunk_search(chunk_t *cur, const check_t check_fct, const scope_e scope,
const direction_e dir, const bool cond)
{
/*
* Depending on the parameter dir the search function searches
* in forward or backward direction */
search_t search_function = select_search_fct(dir);
chunk_t *pc = cur;
do // loop over the chunk list
{
pc = search_function(pc, scope); // in either direction while
} while ( pc != nullptr // the end of the list was not reached yet
&& (check_fct(pc) != cond)); // and the demanded chunk was not found either
return(pc); // the latest chunk is the searched one
}
static chunk_t *chunk_ppa_search(chunk_t *cur, const check_t check_fct, const bool cond)
{
if (cur && !cur->flags.test(PCF_IN_PREPROC))
{
// if not in preprocessor, do a regular search
return(chunk_search(cur, check_fct, scope_e::ALL,
direction_e::FORWARD, cond));
}
chunk_t *pc = cur;
while (pc != nullptr && (pc = pc->next) != nullptr)
{
if (!pc->flags.test(PCF_IN_PREPROC))
{
// Bail if we run off the end of the preprocessor directive, but
// return the next token, NOT nullptr, because the caller may need to
// know where the search ended
assert(chunk_is_token(pc, CT_NEWLINE));
return(pc);
}
if (chunk_is_token(pc, CT_NL_CONT))
{
// Skip line continuation
continue;
}
if (check_fct(pc) == cond)
{
// Requested token was found
return(pc);
}
}
// Ran out of tokens
return(nullptr);
}
/* @todo maybe it is better to combine chunk_get_next and chunk_get_prev
* into a common function However this should be done with the preprocessor
* to avoid addition check conditions that would be evaluated in the
* while loop of the calling function */
chunk_t *chunk_get_next(chunk_t *cur, scope_e scope)
{
if (cur == nullptr)
{
return(nullptr);
}
chunk_t *pc = g_cl.GetNext(cur);
if (pc == nullptr || scope == scope_e::ALL)
{
return(pc);
}
if (cur->flags.test(PCF_IN_PREPROC))
{
// If in a preproc, return nullptr if trying to leave
if (!pc->flags.test(PCF_IN_PREPROC))
{
return(nullptr);
}
return(pc);
}
// Not in a preproc, skip any preproc
while (pc != nullptr && pc->flags.test(PCF_IN_PREPROC))
{
pc = g_cl.GetNext(pc);
}
return(pc);
}
chunk_t *chunk_get_prev(chunk_t *cur, scope_e scope)
{
if (cur == nullptr)
{
return(nullptr);
}
chunk_t *pc = g_cl.GetPrev(cur);
if (pc == nullptr || scope == scope_e::ALL)
{
return(pc);
}
if (cur->flags.test(PCF_IN_PREPROC))
{
// If in a preproc, return NULL if trying to leave
if (!pc->flags.test(PCF_IN_PREPROC))
{
return(nullptr);
}
return(pc);
}
// Not in a preproc, skip any preproc
while (pc != nullptr && pc->flags.test(PCF_IN_PREPROC))
{
pc = g_cl.GetPrev(pc);
}
return(pc);
}
chunk_t *chunk_dup(const chunk_t *pc_in)
{
chunk_t *pc = new chunk_t; // Allocate a new chunk
if (pc == nullptr)
{
// @todo clean up properly before crashing
LOG_FMT(LERR, "Failed to allocate memory\n");
log_func_stack_inline(LSETFLG);
log_flush(true);
exit(EXIT_FAILURE);
}
// Copy all fields and then init the entry
*pc = *pc_in; // TODO: what happens if pc_in == nullptr?
g_cl.InitEntry(pc);
return(pc);
}
static void chunk_log_msg(chunk_t *chunk, const log_sev_t log, const char *str)
{
LOG_FMT(log, "%s orig_line is %zu, orig_col is %zu, ",
str, chunk->orig_line, chunk->orig_col);
if (chunk_is_token(chunk, CT_NEWLINE))
{
LOG_FMT(log, "<Newline>,");
}
else if (chunk_is_token(chunk, CT_VBRACE_OPEN))
{
LOG_FMT(log, "<VBRACE_OPEN>,");
}
else if (chunk_is_token(chunk, CT_VBRACE_CLOSE))
{
LOG_FMT(log, "<VBRACE_CLOSE>,");
}
else
{
LOG_FMT(log, "text() is '%s', type is %s,\n", chunk->text(), get_token_name(chunk->type));
}
}
static void chunk_log(chunk_t *pc, const char *text)
{
if ( pc != nullptr
&& (cpd.unc_stage != unc_stage_e::TOKENIZE)
&& (cpd.unc_stage != unc_stage_e::CLEANUP))
{
const log_sev_t log = LCHUNK;
chunk_t *prev = chunk_get_prev(pc);
chunk_t *next = chunk_get_next(pc);
chunk_log_msg(pc, log, text);
if (prev != nullptr && next != nullptr)
{
chunk_log_msg(prev, log, " @ between");
chunk_log_msg(next, log, " and");
}
else if (next != nullptr)
{
chunk_log_msg(next, log, " @ before");
}
else if (prev != nullptr)
{
chunk_log_msg(prev, log, " @ after");
}
LOG_FMT(log, " stage is %s",
get_unc_stage_name(cpd.unc_stage));
log_func_stack_inline(log);
}
}
chunk_t *chunk_add_after(const chunk_t *pc_in, chunk_t *ref)
{
return(chunk_add(pc_in, ref, direction_e::FORWARD));
}
chunk_t *chunk_add_before(const chunk_t *pc_in, chunk_t *ref)
{
return(chunk_add(pc_in, ref, direction_e::BACKWARD));
}
void chunk_del_2(chunk_t *pc)
{
g_cl.Pop(pc);
delete pc;
}
void chunk_move_after(chunk_t *pc_in, chunk_t *ref)
{
LOG_FUNC_ENTRY();
g_cl.Pop(pc_in);
g_cl.AddAfter(pc_in, ref);
// HACK: Adjust the original column
pc_in->column = ref->column + space_col_align(ref, pc_in);
pc_in->orig_col = pc_in->column;
pc_in->orig_col_end = pc_in->orig_col + pc_in->len();
}
chunk_t *chunk_get_next_nl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_newline, scope, direction_e::FORWARD, true));
}
chunk_t *chunk_get_prev_nl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_newline, scope, direction_e::BACKWARD, true));
}
chunk_t *chunk_get_next_nnl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_newline, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_prev_nnl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_newline, scope, direction_e::BACKWARD, false));
}
chunk_t *chunk_get_next_ncnl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment_or_newline, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_next_ncnlnp(chunk_t *cur, scope_e scope)
{
return(chunk_get_ncnlnp(cur, scope, direction_e::FORWARD));
}
chunk_t *chunk_ppa_get_next_ncnl(chunk_t *cur)
{
return(chunk_ppa_search(cur, chunk_is_comment_or_newline, false));
}
chunk_t *chunk_get_prev_ncnlnp(chunk_t *cur, scope_e scope)
{
return(chunk_get_ncnlnp(cur, scope, direction_e::BACKWARD));
}
chunk_t *chunk_get_next_nblank(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment_newline_or_blank, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_prev_nblank(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment_newline_or_blank, scope, direction_e::BACKWARD, false));
}
chunk_t *chunk_get_next_nc(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_next_nisq(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_balanced_square, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_prev_ncnl(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment_or_newline, scope, direction_e::BACKWARD, false));
}
chunk_t *chunk_get_prev_ncnlni(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment_or_newline_or_ignored, scope, direction_e::BACKWARD, false));
}
chunk_t *chunk_get_prev_nc(chunk_t *cur, scope_e scope)
{
return(chunk_search(cur, chunk_is_comment, scope, direction_e::BACKWARD, false));
}
chunk_t *chunk_get_next_type(chunk_t *cur, c_token_t type, int level, scope_e scope)
{
return(chunk_search_typelevel(cur, type, scope, direction_e::FORWARD, level));
}
chunk_t *chunk_get_next_str(chunk_t *cur, const char *str, size_t len, int level, scope_e scope)
{
return(chunk_search_str(cur, str, len, scope, direction_e::FORWARD, level));
}
chunk_t *chunk_get_prev_type(chunk_t *cur, c_token_t type, int level, scope_e scope)
{
return(chunk_search_typelevel(cur, type, scope, direction_e::BACKWARD, level));
}
chunk_t *chunk_get_prev_str(chunk_t *cur, const char *str, size_t len, int level, scope_e scope)
{
return(chunk_search_str(cur, str, len, scope, direction_e::BACKWARD, level));
}
bool chunk_is_newline_between(chunk_t *start, chunk_t *end)
{
for (chunk_t *pc = start; pc != end; pc = chunk_get_next(pc))
{
if (chunk_is_newline(pc))
{
return(true);
}
}
return(false);
}
void chunk_swap(chunk_t *pc1, chunk_t *pc2)
{
g_cl.Swap(pc1, pc2);
}
// TODO: the following function shall be made similar to the search functions
chunk_t *chunk_first_on_line(chunk_t *pc)
{
chunk_t *first = pc;
while ((pc = chunk_get_prev(pc)) != nullptr && !chunk_is_newline(pc))
{
first = pc;
}
return(first);
}
bool chunk_is_last_on_line(chunk_t &pc) //TODO: pc should be const here
{
// check if pc is the very last chunk of the file
const auto *end = chunk_get_tail();
if (&pc == end)
{
return(true);
}
// if the next chunk is a newline then pc is the last chunk on its line
const auto *next = chunk_get_next(&pc);
if (chunk_is_token(next, CT_NEWLINE))
{
return(true);
}
return(false);
}
// TODO: this function needs some cleanup
void chunk_swap_lines(chunk_t *pc1, chunk_t *pc2)
{
// to swap lines we need to find the first chunk of the lines
pc1 = chunk_first_on_line(pc1);
pc2 = chunk_first_on_line(pc2);
if ( pc1 == nullptr
|| pc2 == nullptr
|| pc1 == pc2)
{
return;
}
/*
* Example start:
* ? - start1 - a1 - b1 - nl1 - ? - ref2 - start2 - a2 - b2 - nl2 - ?
* ^- pc1 ^- pc2
*/
chunk_t *ref2 = chunk_get_prev(pc2);
// Move the line started at pc2 before pc1
while (pc2 != nullptr && !chunk_is_newline(pc2))
{
chunk_t *tmp = chunk_get_next(pc2);
g_cl.Pop(pc2);
g_cl.AddBefore(pc2, pc1);
pc2 = tmp;
}
/*
* Should now be:
* ? - start2 - a2 - b2 - start1 - a1 - b1 - nl1 - ? - ref2 - nl2 - ?
* ^- pc1 ^- pc2
*/
// Now move the line started at pc1 after ref2
while (pc1 != nullptr && !chunk_is_newline(pc1))
{
chunk_t *tmp = chunk_get_next(pc1);
g_cl.Pop(pc1);
if (ref2 != nullptr)
{
g_cl.AddAfter(pc1, ref2);
}
else
{
g_cl.AddHead(pc1);
}
ref2 = pc1;
pc1 = tmp;
}
/*
* Should now be:
* ? - start2 - a2 - b2 - nl1 - ? - ref2 - start1 - a1 - b1 - nl2 - ?
* ^- pc1 ^- pc2
*/
/*
* pc1 and pc2 should be the newlines for their lines.
* swap the chunks and the nl_count so that the spacing remains the same.
*/
if (pc1 != nullptr && pc2 != nullptr)
{
size_t nl_count = pc1->nl_count;
pc1->nl_count = pc2->nl_count;
pc2->nl_count = nl_count;
chunk_swap(pc1, pc2);
}
} // chunk_swap_lines
chunk_t *chunk_get_next_nvb(chunk_t *cur, const scope_e scope)
{
return(chunk_search(cur, chunk_is_vbrace, scope, direction_e::FORWARD, false));
}
chunk_t *chunk_get_prev_nvb(chunk_t *cur, const scope_e scope)
{
return(chunk_search(cur, chunk_is_vbrace, scope, direction_e::BACKWARD, false));
}
void chunk_flags_set_real(chunk_t *pc, pcf_flags_t clr_bits, pcf_flags_t set_bits)
{
if (pc != nullptr)
{
LOG_FUNC_ENTRY();
auto const nflags = (pc->flags & ~clr_bits) | set_bits;
if (pc->flags != nflags)
{
LOG_FMT(LSETFLG,
"%s(%d): %016llx^%016llx=%016llx "
"orig_line is %zu, orig_col is %zu, text() '%s', type is %s, ",
__func__, __LINE__,
static_cast<pcf_flags_t::int_t>(pc->flags),
static_cast<pcf_flags_t::int_t>(pc->flags ^ nflags),
static_cast<pcf_flags_t::int_t>(nflags),
pc->orig_line, pc->orig_col, pc->text(),
get_token_name(pc->type));
LOG_FMT(LSETFLG, "parent_type is %s",
get_token_name(get_chunk_parent_type(pc)));
log_func_stack_inline(LSETFLG);
pc->flags = nflags;
}
}
}
void set_chunk_type_real(chunk_t *pc, c_token_t token, const char *func, int line)
{
LOG_FUNC_ENTRY();
if ( pc == nullptr
|| pc->type == token)
{
return;
}
LOG_FMT(LSETTYP, "%s(%d): orig_line is %zu, orig_col is %zu, pc->text() ",
func, line, pc->orig_line, pc->orig_col);
if (token == CT_NEWLINE)
{
LOG_FMT(LSETTYP, "<Newline>\n");
}
else
{
LOG_FMT(LSETTYP, "'%s'\n", pc->text());
}
LOG_FMT(LSETTYP, " pc->type is %s, pc->parent_type is %s => *type is %s, *parent_type is %s\n",
get_token_name(pc->type), get_token_name(get_chunk_parent_type(pc)),
get_token_name(token), get_token_name(get_chunk_parent_type(pc)));
pc->type = token;
} // set_chunk_type_real
void set_chunk_parent_real(chunk_t *pc, c_token_t token, const char *func, int line)
{
LOG_FUNC_ENTRY();
if ( pc == nullptr
|| get_chunk_parent_type(pc) == token)
{
return;
}
LOG_FMT(LSETPAR, "%s(%d): orig_line is %zu, orig_col is %zu, pc->text() ",
func, line, pc->orig_line, pc->orig_col);
if (token == CT_NEWLINE)
{
LOG_FMT(LSETPAR, "<Newline>\n");
}
else
{
LOG_FMT(LSETPAR, "'%s'\n", pc->text());
}
LOG_FMT(LSETPAR, " pc->type is %s, pc->parent_type is %s => *type is %s, *parent_type is %s\n",
get_token_name(pc->type), get_token_name(get_chunk_parent_type(pc)),
get_token_name(token), get_token_name(get_chunk_parent_type(pc)));
pc->parent_type = token;
} // set_chunk_parent_real
c_token_t get_chunk_parent_type(chunk_t *pc)
{
LOG_FUNC_ENTRY();
if (pc == nullptr)
{
return(CT_NONE);
}
return(pc->parent_type);
} // get_chunk_parent_type
static chunk_t *chunk_get_ncnlnp(chunk_t *cur, const scope_e scope, const direction_e dir)
{
chunk_t *pc = cur;
pc = (chunk_is_preproc(pc) == true) ?
chunk_search(pc, chunk_is_comment_or_newline_in_preproc, scope, dir, false) :
chunk_search(pc, chunk_is_comment_newline_or_preproc, scope, dir, false);
return(pc);
}
static chunk_t *chunk_add(const chunk_t *pc_in, chunk_t *ref, const direction_e pos)
{
#ifdef DEBUG
if (pc_in->orig_line == 0)
{
fprintf(stderr, "%s(%d): no line number\n", __func__, __LINE__);
log_func_stack_inline(LSETFLG);
log_flush(true);
exit(EX_SOFTWARE);
}
if (pc_in->orig_col == 0)
{
fprintf(stderr, "%s(%d): no column number\n", __func__, __LINE__);
log_func_stack_inline(LSETFLG);
log_flush(true);
exit(EX_SOFTWARE);
}
#endif /* DEBUG */
chunk_t *pc = chunk_dup(pc_in);
if (pc != nullptr)
{
if (ref != nullptr) // ref is a valid chunk
{
(pos == direction_e::FORWARD) ? g_cl.AddAfter(pc, ref) : g_cl.AddBefore(pc, ref);
}
else // ref == NULL
{
(pos == direction_e::FORWARD) ? g_cl.AddHead(pc) : g_cl.AddTail(pc);
}
chunk_log(pc, "chunk_add(A):");
}
return(pc);
}
chunk_t *chunk_get_next_ssq(chunk_t *cur)
{
while (chunk_is_token(cur, CT_TSQUARE) || chunk_is_token(cur, CT_SQUARE_OPEN))
{
if (chunk_is_token(cur, CT_SQUARE_OPEN))
{
cur = chunk_skip_to_match(cur);
}
cur = chunk_get_next_ncnl(cur);
}
return(cur);
}
chunk_t *chunk_get_prev_ssq(chunk_t *cur)
{
while (chunk_is_token(cur, CT_TSQUARE) || chunk_is_token(cur, CT_SQUARE_CLOSE))
{
if (chunk_is_token(cur, CT_SQUARE_CLOSE))
{
cur = chunk_skip_to_match_rev(cur);
}
cur = chunk_get_prev_ncnl(cur);
}
return(cur);
}
chunk_t *chunk_get_pp_start(chunk_t *cur)
{
if (!chunk_is_preproc(cur))
{
return(nullptr);
}
while (!chunk_is_token(cur, CT_PREPROC))
{
cur = chunk_get_prev(cur, scope_e::PREPROC);
}
return(cur);
}
//! skip to the final word/type in a :: chain
static chunk_t *chunk_skip_dc_member(chunk_t *start, scope_e scope, direction_e dir)
{
LOG_FUNC_ENTRY();
if (start == nullptr)
{
return(nullptr);
}
const auto step_fcn = (dir == direction_e::FORWARD)
? chunk_get_next_ncnl : chunk_get_prev_ncnl;
chunk_t *pc = start;
chunk_t *next = chunk_is_token(pc, CT_DC_MEMBER) ? pc : step_fcn(pc, scope);
while (chunk_is_token(next, CT_DC_MEMBER))
{
pc = step_fcn(next, scope);
if (pc == nullptr)
{
return(nullptr);
}
next = step_fcn(pc, scope);
}
return(pc);
}
chunk_t *chunk_skip_dc_member(chunk_t *start, scope_e scope)
{
return(chunk_skip_dc_member(start, scope, direction_e::FORWARD));
}
chunk_t *chunk_skip_dc_member_rev(chunk_t *start, scope_e scope)
{
return(chunk_skip_dc_member(start, scope, direction_e::BACKWARD));
}
// set parent member
void chunk_set_parent(chunk_t *pc, chunk_t *parent)
{
if (pc == nullptr)
{
return;
}
if (parent == nullptr)
{
return;
}
if (pc == parent)
{
return;
}
pc->parent = parent;
}
c_token_t get_type_of_the_parent(chunk_t *pc)
{
if (pc == nullptr)
{
return(CT_UNKNOWN);
}
if (pc->parent == nullptr)
{
return(CT_PARENT_NOT_SET);
}
return(pc->parent->type);
}
|