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
|
/**
* @file chunk_list.h
* Manages and navigates the list of chunks.
*
* @author Ben Gardner
* @license GPL v2+
*/
#ifndef CHUNK_LIST_H_INCLUDED
#define CHUNK_LIST_H_INCLUDED
#include "uncrustify_types.h"
// necessary to not sort it
#include "char_table.h"
#include "language_tools.h"
/*
* TODO: better use a namespace for all chunk related operations.
* The function "chunk_is_comment()" would for instance
* become "chunk::is_comment()". This makes the usage of the chunks easier
* and more intuitive.
*/
static constexpr int ANY_LEVEL = -1;
/**
* Specifies which chunks should/should not be found.
* ALL (default)
* - return the true next/prev
*
* PREPROC
* - If not in a preprocessor, skip over any encountered preprocessor stuff
* - If in a preprocessor, fail to leave (return nullptr)
*/
enum class scope_e : unsigned int
{
ALL, //! search in all kind of chunks
PREPROC, //! search only in preprocessor chunks
};
/**
* duplicate a chunk in a chunk list
*
* @param pc_in chunk to duplicate
*/
chunk_t *chunk_dup(const chunk_t *pc_in);
/**
* @brief Add a copy of a chunk to a chunk list after the given position.
*
* @note If ref is nullptr, add at the tail of the chunk list
*
* @todo is ref=nullptr really useful ?
*
* @param pc_in pointer to chunk to add to list
* @param ref position where insertion takes place
*
* @return pointer to the added chunk
*/
chunk_t *chunk_add_after(const chunk_t *pc_in, chunk_t *ref);
/**
* @brief Add a copy of a chunk to a chunk list before the given position
*
* @note If ref is nullptr, add at the head of the chunk list
*
* @todo is ref=nullptr really useful ?
*
* \bug code adds it before the tail, either code or comment is wrong
*
* @param pc_in pointer to chunk to add to list
* @param ref position where insertion takes place
*
* @retval pointer to the added chunk
*/
chunk_t *chunk_add_before(const chunk_t *pc_in, chunk_t *ref);
/**
* delete a chunk from a chunk list
*
* @param pc chunk to delete
*/
void chunk_del(chunk_t * &pc);
/**
* move a chunk to after the reference position in a chunk list
*
* @param pc_in chunk to move
* @param ref chunk after which to move
*/
void chunk_move_after(chunk_t *pc_in, chunk_t *ref);
/**
* @brief returns the head of a chunk list
*
* @return pointer to the first chunk
*/
chunk_t *chunk_get_head(void);
//! get the last chunk in a chunk list
chunk_t *chunk_get_tail(void);
/**
* @brief returns the next chunk in a list of chunks
*
* @param cur chunk to use as start point
* @param scope code region to search in
*
* @return pointer to next chunk or nullptr if no chunk was found
*/
chunk_t *chunk_get_next(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* @brief returns the previous chunk in a list of chunks
*
* @param cur chunk to use as start point
* @param scope code region to search in
*
* @return pointer to previous chunk or nullptr if no chunk was found
*/
chunk_t *chunk_get_prev(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Swaps two chunks
*
* @param pc1 The first chunk
* @param pc2 The second chunk
*/
void chunk_swap(chunk_t *pc1, chunk_t *pc2);
/**
* Swaps two lines that are started with the specified chunks.
*
* @param pc1 The first chunk of line 1
* @param pc2 The first chunk of line 2
*/
void chunk_swap_lines(chunk_t *pc1, chunk_t *pc2);
/**
* Finds the first chunk on the line that pc is on.
* This just backs up until a newline or nullptr is hit.
*
* given: [ a - b - c - n1 - d - e - n2 ]
* input: [ a | b | c | n1 ] => a
* input: [ d | e | n2 ] => d
*
* @param pc chunk to start with
*/
chunk_t *chunk_first_on_line(chunk_t *pc);
//! check if a given chunk is the last on its line
bool chunk_is_last_on_line(chunk_t &pc);
/**
* Gets the next NEWLINE chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_nl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-comment chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_nc(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-NEWLINE
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_nnl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-NEWLINE and non-comment chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_ncnnl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-NEWLINE and non-comment chunk, non-preprocessor chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_ncnnlnp(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-NEWLINE and non-comment chunk inside a preprocessor block
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_ncnnl_in_pp(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-NEWLINE and non-comment chunk (preprocessor aware).
* Unlike chunk_get_next_ncnnl, this will also ignore a line continuation if
* the starting chunk is in a preprocessor directive, and may return a newline
* if the search reaches the end of a preprocessor directive.
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_ppa_get_next_ncnnl(chunk_t *cur);
/**
* Gets the next chunk not in or part of balanced square
* brackets. This handles stacked [] instances to accommodate
* multi-dimensional array declarations
*
* @param cur chunk to use as start point
* @param scope code region to search in
*
* @return nullptr or the next chunk not in or part of square brackets
*/
chunk_t *chunk_get_next_nisq(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the next non-blank chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_next_nblank(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-blank chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_nblank(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev NEWLINE chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_nl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-comment chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_nc(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-NEWLINE chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_nnl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-NEWLINE and non-comment chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_ncnnl(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-NEWLINE and non-comment chunk inside a preprocessor block
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_ncnnl_in_pp(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-NEWLINE and non-comment and non-ignored chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_ncnnlni(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Gets the prev non-NEWLINE and non-comment chunk, non-preprocessor chunk
*
* @param cur chunk to use as start point
* @param scope code region to search in
*/
chunk_t *chunk_get_prev_ncnnlnp(chunk_t *cur, scope_e scope = scope_e::ALL);
/**
* Grabs the next chunk of the given type at the level.
*
* @param cur chunk to use as start point
* @param type the type to look for
* @param level -1 or ANY_LEVEL (any level) or the level to match
* @param scope code region to search in
*
* @return nullptr or the match
*/
chunk_t *chunk_get_next_type(chunk_t *cur, c_token_t type, int level, scope_e scope = scope_e::ALL);
/**
* Grabs the prev chunk of the given type at the level.
*
* @param cur chunk to use as start point
* @param type The type to look for
* @param level -1 or ANY_LEVEL (any level) or the level to match
* @param scope code region to search in
*
* @return nullptr or the match
*/
chunk_t *chunk_get_prev_type(chunk_t *cur, c_token_t type, int level, scope_e scope = scope_e::ALL);
/**
* @brief find a chunk that holds a given string
*
* Traverses a chunk list in forward direction until a chunk of a given category is found.
*
* @param cur chunk to use as start point
* @param str string to search for
* @param len length of string
* @param level the level to match or -1 or ANY_LEVEL
* @param scope code region to search in
*
* @retval nullptr no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
chunk_t *chunk_get_next_str(chunk_t *cur, const char *str, size_t len, int level, scope_e scope = scope_e::ALL);
/**
* @brief find a chunk that holds a given string
*
* Traverses a chunk list in backward direction until a chunk of a given category is found.
*
* @param cur chunk to use as start point
* @param str string to search for
* @param len length of string
* @param level the level to match or -1 or ANY_LEVEL
* @param scope code region to search in
*
* @retval nullptr no chunk found or invalid parameters provided
* @retval chunk_t pointer to the found chunk
*/
chunk_t *chunk_get_prev_str(chunk_t *cur, const char *str, size_t len, int level, scope_e scope = scope_e::ALL);
/**
* @brief Gets the next non-vbrace chunk
*
* @param cur chunk to start search
* @param scope chunk section to consider
*
* @return pointer to found chunk or nullptr if no chunk was found
*/
chunk_t *chunk_get_next_nvb(chunk_t *cur, const scope_e scope = scope_e::ALL);
/**
* @brief Gets the previous non-vbrace chunk
*
* @param cur chunk to start search
* @param scope chunk section to consider
*
* @return pointer to found chunk or nullptr if no chunk was found
*/
chunk_t *chunk_get_prev_nvb(chunk_t *cur, const scope_e scope = scope_e::ALL);
/**
* Gets the next chunk not in or part of balanced square
* brackets.This handles stacked[] instances to accommodate
* multi - dimensional array declarations
*
* @param cur chunk to use as start point
*
* @return nullptr or the next chunk not in or part of square brackets
*/
chunk_t *chunk_get_next_ssq(chunk_t *cur);
/**
* Gets the prev chunk not in or part of balanced square
* brackets.This handles stacked[] instances to accommodate
* multi - dimensional array declarations
*
* @param cur chunk to use as start point
*
* @return nullptr or the prev chunk not in or part of square brackets
*/
chunk_t *chunk_get_prev_ssq(chunk_t *cur);
/**
* Gets the corresponding start chunk if the given chunk is within a
* preprocessor directive, or nullptr otherwise.
*
* @param cur chunk to use as start point
*
* @return nullptr or start chunk of the preprocessor directive
*/
chunk_t *chunk_get_pp_start(chunk_t *cur);
/**
* @brief reverse search a chunk of a given category in a chunk list
*
* @param pc chunk list to search in
* @param cat category to search for
*
* @retval nullptr no object found, or invalid parameters provided
* @retval chunk_t pointer to the found object
*/
chunk_t *chunk_search_prev_cat(chunk_t *pc, const c_token_t cat);
/**
* @brief forward search a chunk of a given category in a chunk list
*
* @param pc chunk list to search in
* @param cat category to search for
*
* @retval nullptr no object found, or invalid parameters provided
* @retval chunk_t pointer to the found object
*/
chunk_t *chunk_search_next_cat(chunk_t *pc, const c_token_t cat);
/**
* @brief checks wether two chunks are in same line
*
* @param start
* @param end
*
* @return true if there is no newline between start and end chunks
*/
bool are_chunks_in_same_line(chunk_t *start, chunk_t *end);
/*
* TODO: better move the function implementations to the source file.
* No need to make the implementation public.
*/
/*
* TODO: I doubt that inline is required for the functions below.
* The compiler should know how to optimize the code itself.
* To clarify do a profiling run with and without inline
*/
static inline bool is_expected_type_and_level(chunk_t *pc, c_token_t type, int level)
{
// we don't care if the pointer is invalid or about the level (if it is negative),
// or it is as expected and the type is as expected
return( pc == nullptr
|| ( ( level < 0
|| pc->level == static_cast<size_t>(level))
&& pc->type == type));
}
static inline bool is_expected_string_and_level(chunk_t *pc, const char *str, int level, size_t len)
{
// we don't care if the pointer is invalid or about the level (if it is negative) or it is as expected
return( pc == nullptr
|| ( ( level < 0
|| pc->level == static_cast<size_t>(level))
&& pc->len() == len // and the length is as expected
&& memcmp(str, pc->text(), len) == 0)); // and the strings are equal
}
static inline bool chunk_is_token(const chunk_t *pc, c_token_t c_token)
{
return( pc != nullptr
&& pc->type == c_token);
}
static inline bool chunk_is_not_token(const chunk_t *pc, c_token_t c_token)
{
return( pc != nullptr
&& pc->type != c_token);
}
/**
* Skips to the closing match for the current paren/brace/square.
*
* @param cur The opening or closing paren/brace/square
* @param scope chunk section to consider
*
* @return nullptr or the matching paren/brace/square
*/
static inline chunk_t *chunk_skip_to_match(chunk_t *cur, scope_e scope = scope_e::ALL)
{
if ( cur != nullptr
&& ( chunk_is_token(cur, CT_PAREN_OPEN)
|| chunk_is_token(cur, CT_SPAREN_OPEN)
|| chunk_is_token(cur, CT_FPAREN_OPEN)
|| chunk_is_token(cur, CT_TPAREN_OPEN)
|| chunk_is_token(cur, CT_BRACE_OPEN)
|| chunk_is_token(cur, CT_VBRACE_OPEN)
|| chunk_is_token(cur, CT_ANGLE_OPEN)
|| chunk_is_token(cur, CT_SQUARE_OPEN)))
{
return(chunk_get_next_type(cur, (c_token_t)(cur->type + 1), cur->level, scope));
}
return(cur);
}
static inline chunk_t *chunk_skip_to_match_rev(chunk_t *cur, scope_e scope = scope_e::ALL)
{
if ( cur != nullptr
&& ( chunk_is_token(cur, CT_PAREN_CLOSE)
|| chunk_is_token(cur, CT_SPAREN_CLOSE)
|| chunk_is_token(cur, CT_FPAREN_CLOSE)
|| chunk_is_token(cur, CT_TPAREN_CLOSE)
|| chunk_is_token(cur, CT_BRACE_CLOSE)
|| chunk_is_token(cur, CT_VBRACE_CLOSE)
|| chunk_is_token(cur, CT_ANGLE_CLOSE)
|| chunk_is_token(cur, CT_SQUARE_CLOSE)))
{
return(chunk_get_prev_type(cur, (c_token_t)(cur->type - 1), cur->level, scope));
}
return(cur);
}
//! skip to the final word/type in a :: chain
chunk_t *chunk_skip_dc_member(chunk_t *start, scope_e scope = scope_e::ALL);
chunk_t *chunk_skip_dc_member_rev(chunk_t *start, scope_e scope = scope_e::ALL);
/**
* checks if a chunk is valid and is a comment
*
* comment means any kind of
* - single line comment
* - multiline comment
* - C comment
* - C++ comment
*/
static inline bool chunk_is_comment(chunk_t *pc)
{
return( chunk_is_token(pc, CT_COMMENT)
|| chunk_is_token(pc, CT_COMMENT_MULTI)
|| chunk_is_token(pc, CT_COMMENT_CPP));
}
/**
* Returns true if the chunk under test is an inheritance access specifier
*/
static inline bool chunk_is_cpp_inheritance_access_specifier(chunk_t *pc)
{
return( language_is_set(LANG_CPP)
&& pc != nullptr
&& ( chunk_is_token(pc, CT_ACCESS)
|| chunk_is_token(pc, CT_QUALIFIER))
&& ( std::strncmp(pc->str.c_str(), "private", 7) == 0
|| std::strncmp(pc->str.c_str(), "protected", 9) == 0
|| std::strncmp(pc->str.c_str(), "public", 6) == 0));
} // chunk_is_cpp_inheritance_access_specifier
static inline bool chunk_is_colon(chunk_t *pc)
{
return( chunk_is_token(pc, CT_ACCESS_COLON)
|| chunk_is_token(pc, CT_ASM_COLON)
|| chunk_is_token(pc, CT_BIT_COLON)
|| chunk_is_token(pc, CT_CASE_COLON)
|| chunk_is_token(pc, CT_CLASS_COLON)
|| chunk_is_token(pc, CT_COLON)
|| chunk_is_token(pc, CT_COND_COLON)
|| chunk_is_token(pc, CT_CONSTR_COLON)
|| chunk_is_token(pc, CT_CS_SQ_COLON)
|| chunk_is_token(pc, CT_D_ARRAY_COLON)
|| chunk_is_token(pc, CT_FOR_COLON)
|| chunk_is_token(pc, CT_LABEL_COLON)
|| chunk_is_token(pc, CT_OC_COLON)
|| chunk_is_token(pc, CT_OC_DICT_COLON)
|| chunk_is_token(pc, CT_TAG_COLON)
|| chunk_is_token(pc, CT_WHERE_COLON));
}
static inline bool chunk_is_single_line_comment(chunk_t *pc)
{
return( chunk_is_token(pc, CT_COMMENT)
|| chunk_is_token(pc, CT_COMMENT_CPP));
}
static inline bool chunk_is_newline(chunk_t *pc)
{
return( chunk_is_token(pc, CT_NEWLINE)
|| chunk_is_token(pc, CT_NL_CONT));
}
static inline bool chunk_is_semicolon(chunk_t *pc)
{
return( chunk_is_token(pc, CT_SEMICOLON)
|| chunk_is_token(pc, CT_VSEMICOLON));
}
/**
* checks if a chunk is valid and is a blank character
*
* @note check compares if len == 0
*
* @todo rename function: blank is a space not an empty string
*/
static inline bool chunk_is_blank(chunk_t *pc)
{
return( pc != nullptr
&& (pc->len() == 0));
}
//! checks if a chunk is valid and either a comment or newline
static inline bool chunk_is_comment_or_newline(chunk_t *pc)
{
return( chunk_is_comment(pc)
|| chunk_is_newline(pc));
}
//! checks if a chunk is valid and either a comment or newline or ignored
static inline bool chunk_is_comment_or_newline_or_ignored(chunk_t *pc)
{
return( chunk_is_comment(pc)
|| chunk_is_newline(pc)
|| chunk_is_token(pc, CT_IGNORED));
}
static inline bool chunk_is_balanced_square(chunk_t *pc)
{
return( chunk_is_token(pc, CT_SQUARE_OPEN)
|| chunk_is_token(pc, CT_TSQUARE)
|| chunk_is_token(pc, CT_SQUARE_CLOSE));
}
static inline bool chunk_is_preproc(chunk_t *pc)
{
return( pc != nullptr
&& pc->flags.test(PCF_IN_PREPROC));
}
static inline bool chunk_is_comment_or_newline_in_preproc(chunk_t *pc)
{
return( pc != nullptr
&& chunk_is_preproc(pc)
&& ( chunk_is_comment(pc)
|| chunk_is_newline(pc)));
}
static inline bool chunk_is_comment_newline_or_preproc(chunk_t *pc)
{
return( chunk_is_comment(pc)
|| chunk_is_newline(pc)
|| chunk_is_preproc(pc));
}
static inline bool chunk_is_comment_newline_or_blank(chunk_t *pc)
{
return( chunk_is_comment_or_newline(pc)
|| chunk_is_blank(pc));
}
static inline bool chunk_is_Doxygen_comment(chunk_t *pc)
{
if ( pc == nullptr
|| !chunk_is_comment(pc))
{
return(false);
}
// check the third character
const char *sComment = pc->text();
const size_t len = strlen(sComment);
if (len < 3)
{
return(false);
}
return( (sComment[2] == '/')
|| (sComment[2] == '!')
|| (sComment[2] == '@'));
}
static inline bool chunk_is_type(chunk_t *pc)
{
return( chunk_is_token(pc, CT_TYPE)
|| chunk_is_token(pc, CT_PTR_TYPE)
|| chunk_is_token(pc, CT_BYREF)
|| chunk_is_token(pc, CT_DC_MEMBER)
|| chunk_is_token(pc, CT_QUALIFIER)
|| chunk_is_token(pc, CT_STRUCT)
|| chunk_is_token(pc, CT_ENUM)
|| chunk_is_token(pc, CT_UNION));
}
static inline bool chunk_is_str(chunk_t *pc, const char *str, size_t len)
{
return( pc != nullptr // valid pc pointer
&& (pc->len() == len) // token size equals size parameter
&& (memcmp(pc->text(), str, len) == 0)); // token name is the same as str parameter
/*
* TODO: possible access beyond array for memcmp, check this
* why not use strncmp here?
*/
}
static inline bool chunk_is_str_case(chunk_t *pc, const char *str, size_t len)
{
return( pc != nullptr
&& (pc->len() == len)
&& (strncasecmp(pc->text(), str, len) == 0));
}
static inline bool chunk_is_word(chunk_t *pc)
{
return( pc != nullptr
&& (pc->len() >= 1)
&& CharTable::IsKw1(pc->str[0]));
}
static inline bool chunk_is_star(chunk_t *pc)
{
return( pc != nullptr
&& (pc->len() == 1)
&& (pc->str[0] == '*')
&& pc->type != CT_OPERATOR_VAL);
}
static inline bool chunk_is_nullable(chunk_t *pc)
{
return( language_is_set(LANG_CS)
&& (pc != nullptr)
&& (pc->len() == 1)
&& (pc->str[0] == '?'));
}
static inline bool chunk_is_addr(chunk_t *pc)
{
if ( pc != nullptr
&& ( chunk_is_token(pc, CT_BYREF)
|| ( (pc->len() == 1)
&& (pc->str[0] == '&')
&& pc->type != CT_OPERATOR_VAL)))
{
chunk_t *prev = chunk_get_prev(pc);
if ( pc->flags.test(PCF_IN_TEMPLATE)
&& ( chunk_is_token(prev, CT_COMMA)
|| chunk_is_token(prev, CT_ANGLE_OPEN)))
{
return(false);
}
return(true);
}
return(false);
}
static inline bool chunk_is_msref(chunk_t *pc) // ms compilers for C++/CLI and WinRT use '^' instead of '*' for marking up reference types vs pointer types
{
return( language_is_set(LANG_CPP)
&& ( pc != nullptr
&& (pc->len() == 1)
&& (pc->str[0] == '^')
&& pc->type != CT_OPERATOR_VAL));
}
static inline bool chunk_is_ptr_operator(chunk_t *pc)
{
return( ( chunk_is_star(pc)
|| chunk_is_addr(pc)
|| chunk_is_msref(pc))
|| chunk_is_nullable(pc));
}
static inline bool chunk_is_pointer_or_reference(chunk_t *pc)
{
return( chunk_is_ptr_operator(pc)
|| chunk_is_token(pc, CT_BYREF));
}
//! Check to see if there is a newline between the two chunks
bool chunk_is_newline_between(chunk_t *start, chunk_t *end);
static inline bool chunk_is_closing_brace(chunk_t *pc)
{
return( chunk_is_token(pc, CT_BRACE_CLOSE)
|| chunk_is_token(pc, CT_VBRACE_CLOSE));
}
static inline bool chunk_is_opening_brace(chunk_t *pc)
{
return( chunk_is_token(pc, CT_BRACE_OPEN)
|| chunk_is_token(pc, CT_VBRACE_OPEN));
}
static inline bool chunk_is_vbrace(chunk_t *pc)
{
return( chunk_is_token(pc, CT_VBRACE_CLOSE)
|| chunk_is_token(pc, CT_VBRACE_OPEN));
}
static inline bool chunk_is_paren_open(chunk_t *pc)
{
return( chunk_is_token(pc, CT_PAREN_OPEN)
|| chunk_is_token(pc, CT_SPAREN_OPEN)
|| chunk_is_token(pc, CT_TPAREN_OPEN)
|| chunk_is_token(pc, CT_FPAREN_OPEN)
|| chunk_is_token(pc, CT_LPAREN_OPEN));
}
static inline bool chunk_is_paren_close(chunk_t *pc)
{
return( chunk_is_token(pc, CT_PAREN_CLOSE)
|| chunk_is_token(pc, CT_SPAREN_CLOSE)
|| chunk_is_token(pc, CT_TPAREN_CLOSE)
|| chunk_is_token(pc, CT_FPAREN_CLOSE));
}
/**
* Returns true if either chunk is null or both have the same preproc flags.
* If this is true, you can remove a newline/nl_cont between the two.
*/
static inline bool chunk_same_preproc(chunk_t *pc1, chunk_t *pc2)
{
return( pc1 == nullptr
|| pc2 == nullptr
|| ((pc1->flags & PCF_IN_PREPROC) == (pc2->flags & PCF_IN_PREPROC)));
}
/**
* Returns true if it is safe to delete the newline token.
* The prev and next chunks must have the same PCF_IN_PREPROC flag AND
* the newline can't be after a C++ comment.
*/
static inline bool chunk_safe_to_del_nl(chunk_t *nl)
{
chunk_t *tmp = chunk_get_prev(nl);
if (chunk_is_token(tmp, CT_COMMENT_CPP))
{
return(false);
}
return(chunk_same_preproc(chunk_get_prev(nl), chunk_get_next(nl)));
}
/**
* Checks if a chunk points to the opening parenthese of a
* for(...in...) loop in Objective-C.
*
* @return true - the chunk is the opening parentheses of a for in loop
*/
static inline bool chunk_is_forin(chunk_t *pc)
{
if ( language_is_set(LANG_OC)
&& chunk_is_token(pc, CT_SPAREN_OPEN))
{
chunk_t *prev = chunk_get_prev_ncnnl(pc);
if (chunk_is_token(prev, CT_FOR))
{
chunk_t *next = pc;
while ( next != nullptr
&& next->type != CT_SPAREN_CLOSE
&& next->type != CT_IN)
{
next = chunk_get_next_ncnnl(next);
}
if (chunk_is_token(next, CT_IN))
{
return(true);
}
}
}
return(false);
}
/**
* Returns true if pc is an CT_ATTRIBUTE or CT_DECLSPEC
*/
bool chunk_is_attribute_or_declspec(chunk_t *pc);
/**
* Returns true if pc is one of CT_CLASS, CT_ENUM, CT_ENUM_CLASS, CT_STRUCT or CT_UNION
*/
bool chunk_is_class_enum_struct_union(chunk_t *pc);
/**
* Returns true if pc is a CT_CLASS or CT_STRUCT
*/
bool chunk_is_class_or_struct(chunk_t *pc);
/**
* Returns true if pc is one of CT_CLASS, CT_STRUCT or CT_UNION
*/
bool chunk_is_class_struct_union(chunk_t *pc);
/**
* Returns true if pc is a CT_ENUM or CT_ENUM_CLASS
*/
bool chunk_is_enum(chunk_t *pc);
void set_chunk_type_real(chunk_t *pc, c_token_t tt, const char *func, int line);
void set_chunk_parent_real(chunk_t *pc, c_token_t tt, const char *func, int line);
#define set_chunk_type(pc, tt) set_chunk_type_real((pc), (tt), __unqualified_func__, __LINE__)
#define set_chunk_parent(pc, tt) set_chunk_parent_real((pc), (tt), __unqualified_func__, __LINE__)
c_token_t get_chunk_parent_type(chunk_t *pc);
void chunk_flags_set_real(chunk_t *pc, pcf_flags_t clr_bits, pcf_flags_t set_bits);
#define chunk_flags_upd(pc, cc, ss) chunk_flags_set_real((pc), (cc), (ss))
#define chunk_flags_set(pc, ss) chunk_flags_set_real((pc), {}, (ss))
#define chunk_flags_clr(pc, cc) chunk_flags_set_real((pc), (cc), {})
void chunk_set_parent(chunk_t *pc, chunk_t *parent);
c_token_t get_type_of_the_parent(chunk_t *pc);
/**
* @brief compare the positions of two tokens in a file.
*
* The function compares the two positions of two tokens.
*
* @param A_token
* @param B_token
*
* @return returns an integer less than, equal to, or greater than zero
* if A_token is found, respectively, to be less/before than, to
* match, or be greater/after than B_token.
*/
int chunk_compare_position(const chunk_t *A_token, const chunk_t *B_token);
#endif /* CHUNK_LIST_H_INCLUDED */
|