forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewlines.cpp
More file actions
3487 lines (3172 loc) · 95.7 KB
/
newlines.cpp
File metadata and controls
3487 lines (3172 loc) · 95.7 KB
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 newlines.cpp
* Adds or removes newlines.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "chunk_list.h"
#include "prototypes.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cerrno>
#include "unc_ctype.h"
static void newlines_double_space_struct_enum_union(chunk_t *open_brace);
static bool one_liner_nl_ok(chunk_t *pc);
static void undo_one_liner(chunk_t *pc);
static void nl_handle_define(chunk_t *pc);
static void newline_iarf_pair(chunk_t *before, chunk_t *after, argval_t av);
#define MARK_CHANGE() mark_change(__func__, __LINE__)
static void mark_change(const char *func, int line)
{
LOG_FUNC_ENTRY();
cpd.changes++;
if (cpd.pass_count == 0)
{
LOG_FMT(LCHANGE, "%s: change %d on %s:%d\n", __func__, cpd.changes, func, line);
}
}
/**
* Check to see if we are allowed to increase the newline count.
* We can't increase the nl count:
* - if nl_squeeze_ifdef and a preproc is after the newline.
* - if eat_blanks_before_close_brace and the next is '}'
* - if eat_blanks_after_open_brace and the prev is '{'
*/
static bool can_increase_nl(chunk_t *nl)
{
LOG_FUNC_ENTRY();
chunk_t *prev = chunk_get_prev_nc(nl);
chunk_t *pcmt = chunk_get_prev(nl);
chunk_t *next = chunk_get_next(nl);
if (cpd.settings[UO_nl_squeeze_ifdef].b)
{
if (prev && (prev->type == CT_PREPROC) &&
(prev->parent_type == CT_PP_ENDIF))
{
LOG_FMT(LBLANKD, "%s: nl_squeeze_ifdef %d (prev)\n", __func__, nl->orig_line);
return(false);
}
if (next && (next->type == CT_PREPROC) &&
(next->parent_type == CT_PP_ENDIF))
{
LOG_FMT(LBLANKD, "%s: nl_squeeze_ifdef %d (next)\n", __func__, nl->orig_line);
return(false);
}
}
if (cpd.settings[UO_eat_blanks_before_close_brace].b)
{
if (next && (next->type == CT_BRACE_CLOSE))
{
LOG_FMT(LBLANKD, "%s: eat_blanks_before_close_brace %d\n", __func__, nl->orig_line);
return(false);
}
}
if (cpd.settings[UO_eat_blanks_after_open_brace].b)
{
if (prev && (prev->type == CT_BRACE_OPEN))
{
LOG_FMT(LBLANKD, "%s: eat_blanks_after_open_brace %d\n", __func__, nl->orig_line);
return(false);
}
}
if (!pcmt && (cpd.settings[UO_nl_start_of_file].a != AV_IGNORE))
{
LOG_FMT(LBLANKD, "%s: no prev %d\n", __func__, nl->orig_line);
return(false);
}
if (!next && (cpd.settings[UO_nl_end_of_file].a != AV_IGNORE))
{
LOG_FMT(LBLANKD, "%s: no next %d\n", __func__, nl->orig_line);
return(false);
}
return(true);
}
/**
* Double the newline, if allowed.
*/
static void double_newline(chunk_t *nl)
{
LOG_FUNC_ENTRY();
chunk_t *prev = chunk_get_prev(nl);
LOG_FMT(LNEWLINE, "%s: add newline after %s on line %d",
__func__, prev->str.c_str(), prev->orig_line);
if (!can_increase_nl(nl))
{
LOG_FMT(LNEWLINE, " - denied\n");
return;
}
LOG_FMT(LNEWLINE, " - done\n");
if (nl->nl_count != 2)
{
nl->nl_count = 2;
MARK_CHANGE();
}
}
/*
* Basic approach:
* 1. Find next open brace
* 2. Find next close brace
* 3. Determine why the braces are there
* a. struct/union/enum "enum [name] {"
* c. assignment "= {"
* b. if/while/switch/for/etc ") {"
* d. else "} else {"
*/
//#define DEBUG_NEWLINES
static void setup_newline_add(chunk_t *prev, chunk_t *nl, chunk_t *next)
{
LOG_FUNC_ENTRY();
if (!prev || !nl || !next)
{
return;
}
undo_one_liner(prev);
nl->orig_line = prev->orig_line;
nl->level = prev->level;
nl->brace_level = prev->brace_level;
nl->pp_level = prev->pp_level;
nl->nl_count = 1;
nl->flags = (prev->flags & PCF_COPY_FLAGS) & ~PCF_IN_PREPROC;
if ((prev->flags & PCF_IN_PREPROC) && (next->flags & PCF_IN_PREPROC))
{
nl->flags |= PCF_IN_PREPROC;
}
if ((nl->flags & PCF_IN_PREPROC) != 0)
{
set_chunk_type(nl, CT_NL_CONT);
nl->str = "\\\n";
}
else
{
set_chunk_type(nl, CT_NEWLINE);
nl->str = "\n";
}
}
/**
* Add a newline before the chunk if there isn't already a newline present.
* Virtual braces are skipped, as they do not contribute to the output.
*/
chunk_t *newline_add_before(chunk_t *pc)
{
LOG_FUNC_ENTRY();
chunk_t nl;
chunk_t *prev;
prev = chunk_get_prev_nvb(pc);
if (chunk_is_newline(prev))
{
/* Already has a newline before this chunk */
return(prev);
}
LOG_FMT(LNEWLINE, "%s: '%s' on line %d",
__func__, pc->str.c_str(), pc->orig_line);
log_func_stack_inline(LSETTYP);
setup_newline_add(prev, &nl, pc);
MARK_CHANGE();
return(chunk_add_before(&nl, pc));
}
chunk_t *newline_force_before(chunk_t *pc)
{
LOG_FUNC_ENTRY();
chunk_t *nl = newline_add_before(pc);
if (nl && (nl->nl_count > 1))
{
nl->nl_count = 1;
MARK_CHANGE();
}
return nl;
}
/**
* Add a newline after the chunk if there isn't already a newline present.
* Virtual braces are skipped, as they do not contribute to the output.
*/
chunk_t *newline_add_after(chunk_t *pc)
{
LOG_FUNC_ENTRY();
chunk_t nl;
chunk_t *next;
if (!pc)
{
return(NULL);
}
next = chunk_get_next_nvb(pc);
if (chunk_is_newline(next))
{
/* Already has a newline after this chunk */
return(next);
}
LOG_FMT(LNEWLINE, "%s: '%s' on line %d",
__func__, pc->str.c_str(), pc->orig_line);
log_func_stack_inline(LNEWLINE);
setup_newline_add(pc, &nl, next);
MARK_CHANGE();
return(chunk_add_after(&nl, pc));
}
chunk_t *newline_force_after(chunk_t *pc)
{
LOG_FUNC_ENTRY();
chunk_t *nl = newline_add_after(pc);
if (nl && (nl->nl_count > 1))
{
nl->nl_count = 1;
MARK_CHANGE();
}
return nl;
}
/**
* Ensure that the next non-comment token after close brace is a nl
*/
static void newline_end_newline(chunk_t *br_close)
{
LOG_FUNC_ENTRY();
chunk_t *next = chunk_get_next(br_close);
chunk_t nl;
if (!chunk_is_newline(next) && !chunk_is_comment(next))
{
nl.orig_line = br_close->orig_line;
nl.nl_count = 1;
nl.flags = (br_close->flags & PCF_COPY_FLAGS) & ~PCF_IN_PREPROC;
if ((br_close->flags & PCF_IN_PREPROC) &&
(next != NULL) && (next->flags & PCF_IN_PREPROC))
{
nl.flags |= PCF_IN_PREPROC;
}
if ((nl.flags & PCF_IN_PREPROC) != 0)
{
nl.type = CT_NL_CONT;
nl.str = "\\\n";
}
else
{
nl.type = CT_NEWLINE;
nl.str = "\n";
}
MARK_CHANGE();
chunk_add_after(&nl, br_close);
}
}
static void newline_min_after(chunk_t *ref, INT32 count, UINT64 flag)
{
LOG_FUNC_ENTRY();
chunk_t *pc = ref;
chunk_t *next;
LOG_FMT(LNEWLINE, "%s: '%s' line %d - count=%d flg=0x%" PRIx64 ":",
__func__, ref->str.c_str(), ref->orig_line, count, flag);
log_func_stack_inline(LNEWLINE);
do
{
pc = chunk_get_next(pc);
} while ((pc != NULL) && !chunk_is_newline(pc));
//LOG_FMT(LNEWLINE, "%s: on %s, line %d, col %d\n",
// __func__, get_token_name(pc->type), pc->orig_line, pc->orig_col);
next = chunk_get_next(pc);
if (chunk_is_comment(next) && (next->nl_count == 1) &&
chunk_is_comment(chunk_get_prev(pc)))
{
newline_min_after(next, count, flag);
return;
}
else
{
pc->flags |= flag;
if (chunk_is_newline(pc) && can_increase_nl(pc))
{
if (pc->nl_count < count)
{
pc->nl_count = count;
MARK_CHANGE();
}
}
}
}
/**
* Add a newline between two tokens.
* If there is already a newline between then, nothing is done.
* Otherwise a newline is inserted.
*
* If end is CT_BRACE_OPEN and a comment and newline follow, then
* the brace open is moved instead of inserting a newline.
*
* In this situation:
* if (...) { //comment
*
* you get:
* if (...) //comment
* {
*/
chunk_t *newline_add_between(chunk_t *start, chunk_t *end)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
if ((start == NULL) || (end == NULL))
{
return(NULL);
}
LOG_FMT(LNEWLINE, "%s: '%s'[%s] line %d:%d and '%s' line %d:%d :",
__func__, start->str.c_str(), get_token_name(start->type),
start->orig_line, start->orig_col,
end->str.c_str(), end->orig_line, end->orig_col);
log_func_stack_inline(LNEWLINE);
/* Back-up check for one-liners (should never be true!) */
if (!one_liner_nl_ok(start))
{
return(NULL);
}
/* Scan for a line break */
for (pc = start; pc != end; pc = chunk_get_next(pc))
{
if (chunk_is_newline(pc))
{
return(pc);
}
}
/* If the second one is a brace open, then check to see
* if a comment + newline follows
*/
if (end->type == CT_BRACE_OPEN)
{
pc = chunk_get_next(end);
if (chunk_is_comment(pc))
{
pc = chunk_get_next(pc);
if (chunk_is_newline(pc))
{
/* Move the open brace to after the newline */
chunk_move_after(end, pc);
return(pc);
}
}
}
return(newline_add_before(end));
}
/**
* Removes any CT_NEWLINE or CT_NL_CONT between start and end.
* Start must be before end on the chunk list.
* If the 'PCF_IN_PREPROC' status differs between two tags, we can't remove
* the newline.
*
* @param start The starting chunk (cannot be a newline)
* @param end The ending chunk (cannot be a newline)
* @return true/false - removed something
*/
void newline_del_between(chunk_t *start, chunk_t *end)
{
LOG_FUNC_ENTRY();
chunk_t *next;
chunk_t *prev;
chunk_t *pc = start;
LOG_FMT(LNEWLINE, "%s: '%s' line %d:%d and '%s' line %d:%d : preproc=%d/%d ",
__func__, start->str.c_str(), start->orig_line, start->orig_col,
end->str.c_str(), end->orig_line, end->orig_col,
((start->flags & PCF_IN_PREPROC) != 0),
((end->flags & PCF_IN_PREPROC) != 0));
log_func_stack_inline(LNEWLINE);
/* Can't remove anything if the preproc status differs */
if (!chunk_same_preproc(start, end))
{
return;
}
do
{
next = chunk_get_next(pc);
if (chunk_is_newline(pc))
{
prev = chunk_get_prev(pc);
if ((!chunk_is_comment(prev) && !chunk_is_comment(next)) ||
chunk_is_newline(prev) ||
chunk_is_newline(next))
{
if (chunk_safe_to_del_nl(pc))
{
chunk_del(pc);
MARK_CHANGE();
if (prev != NULL)
{
align_to_column(next, prev->column + space_col_align(prev, next));
}
}
}
else
{
if (pc->nl_count > 1)
{
pc->nl_count = 1;
MARK_CHANGE();
}
}
}
pc = next;
} while (pc != end);
if (chunk_is_str(end, "{", 1) &&
(chunk_is_str(start, ")", 1) ||
(start->type == CT_DO) ||
(start->type == CT_ELSE)))
{
if (chunk_get_prev_nl(end) != start)
{
chunk_move_after(end, start);
}
}
}
/**
* Add or remove a newline between the closing paren and opening brace.
* Also uncuddles anything on the closing brace. (may get fixed later)
*
* "if (...) { \n" or "if (...) \n { \n"
*
* For virtual braces, we can only add a newline after the vbrace open.
* If we do so, also add a newline after the vbrace close.
*/
static bool newlines_if_for_while_switch(chunk_t *start, argval_t nl_opt)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *close_paren;
chunk_t *brace_open;
bool retval = false;
if ((nl_opt == AV_IGNORE) ||
(((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b))
{
return(false);
}
pc = chunk_get_next_ncnl(start);
if ((pc != NULL) && (pc->type == CT_SPAREN_OPEN))
{
close_paren = chunk_get_next_type(pc, CT_SPAREN_CLOSE, pc->level);
brace_open = chunk_get_next_ncnl(close_paren);
if ((brace_open != NULL) &&
((brace_open->type == CT_BRACE_OPEN) ||
(brace_open->type == CT_VBRACE_OPEN)) &&
one_liner_nl_ok(brace_open))
{
if (cpd.settings[UO_nl_multi_line_cond].b)
{
while ((pc = chunk_get_next(pc)) != close_paren)
{
if (chunk_is_newline(pc))
{
nl_opt = AV_ADD;
break;
}
}
}
if (brace_open->type == CT_VBRACE_OPEN)
{
/* Can only add - we don't want to create a one-line here */
if (nl_opt & AV_ADD)
{
newline_iarf_pair(close_paren, chunk_get_next_ncnl(brace_open), nl_opt);
pc = chunk_get_next_type(brace_open, CT_VBRACE_CLOSE, brace_open->level);
if (!chunk_is_newline(chunk_get_prev_nc(pc)) &&
!chunk_is_newline(chunk_get_next_nc(pc)))
{
newline_add_after(pc);
retval = true;
}
}
}
else
{
newline_iarf_pair(close_paren, brace_open, nl_opt);
newline_add_between(brace_open, chunk_get_next_ncnl(brace_open));
/* Make sure nothing is cuddled with the closing brace */
pc = chunk_get_next_type(brace_open, CT_BRACE_CLOSE, brace_open->level);
newline_add_between(pc, chunk_get_next_nblank(pc));
retval = true;
}
}
}
return(retval);
}
/**
* Add or remove extra newline before the chunk.
* Adds before comments
* Doesn't do anything if open brace before it
* "code\n\ncomment\nif (...)" or "code\ncomment\nif (...)"
*/
static void newlines_if_for_while_switch_pre_blank_lines(chunk_t *start, argval_t nl_opt)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *prev;
chunk_t *next;
chunk_t *last_nl = NULL;
int level = start->level;
bool do_add = nl_opt & AV_ADD;
if ((nl_opt == AV_IGNORE) ||
(((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b))
{
return;
}
/*
* look backwards until we find
* open brace (don't add or remove)
* 2 newlines in a row (don't add)
* something else (don't remove)
*/
for (pc = chunk_get_prev(start); pc != NULL; pc = chunk_get_prev(pc))
{
if (chunk_is_newline(pc))
{
last_nl = pc;
/* if we found 2 or more in a row */
if ((pc->nl_count > 1) || chunk_is_newline(chunk_get_prev_nvb(pc)))
{
/* need to remove */
if ((nl_opt & AV_REMOVE) && ((pc->flags & PCF_VAR_DEF) == 0))
{
/* if we're also adding, take care of that here */
int nl_count = do_add ? 2 : 1;
if (nl_count != pc->nl_count)
{
pc->nl_count = nl_count;
MARK_CHANGE();
}
/* can keep using pc because anything other than newline stops loop, and we delete if newline */
while (chunk_is_newline(prev = chunk_get_prev_nvb(pc)))
{
/* Make sure we don't combine a preproc and non-preproc */
if (!chunk_safe_to_del_nl(prev))
{
break;
}
chunk_del(prev);
MARK_CHANGE();
}
}
return;
}
}
else if (chunk_is_opening_brace(pc) || (pc->level < level))
{
return;
}
else if (chunk_is_comment(pc))
{
/* vbrace close is ok because it won't go into output, so we should skip it */
last_nl = NULL;
continue;
}
else
{
if (do_add) /* we found something previously besides a comment or a new line */
{
/* if we have run across a newline */
if (last_nl != NULL)
{
if (last_nl->nl_count < 2)
{
double_newline(last_nl);
}
}
else
{
/* we didn't run into a nl, so we need to add one */
if (((next = chunk_get_next(pc)) != NULL) &&
chunk_is_comment(next))
{
pc = next;
}
if ((last_nl = newline_add_after(pc)) != NULL)
{
double_newline(last_nl);
}
}
}
return;
}
}
}
static chunk_t *get_closing_brace(chunk_t *start)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
int level = start->level;
for (pc = start; (pc = chunk_get_next(pc)) != NULL; )
{
if (((pc->type == CT_BRACE_CLOSE) || (pc->type == CT_VBRACE_CLOSE)) && (pc->level == level))
{
return(pc);
}
/* for some reason, we can have newlines between if and opening brace that are lower level than either */
if (!chunk_is_newline(pc) && (pc->level < level))
{
return(NULL);
}
}
return(NULL);
}
/**
* remove any consecutive newlines following this chunk
* skip vbraces
*/
static void remove_next_newlines(chunk_t *start)
{
LOG_FUNC_ENTRY();
chunk_t *next;
while ((next = chunk_get_next(start)) != NULL)
{
if (chunk_is_newline(next) && chunk_safe_to_del_nl(next))
{
chunk_del(next);
MARK_CHANGE();
}
else if (chunk_is_vbrace(next))
{
start = next;
}
else
{
break;
}
}
}
/**
* Add or remove extra newline after end of the block started in chunk.
* Doesn't do anything if close brace after it
* Interesting issue is that at this point, nls can be before or after vbraces
* VBraces will stay VBraces, conversion to real ones should have already happened
* "if (...)\ncode\ncode" or "if (...)\ncode\n\ncode"
*/
static void newlines_if_for_while_switch_post_blank_lines(chunk_t *start, argval_t nl_opt)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *next;
chunk_t *prev;
bool have_pre_vbrace_nl = false;
int nl_count;
if ((nl_opt == AV_IGNORE) ||
(((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b))
{
return;
}
/* first find ending brace */
if ((pc = get_closing_brace(start)) == NULL)
{
return;
}
/* if we're dealing with an if, we actually want to add or remove blank lines after any elses */
if (start->type == CT_IF)
{
while (true)
{
next = chunk_get_next_ncnl(pc);
if ((next != NULL) && ((next->type == CT_ELSE) || (next->type == CT_ELSEIF)))
{
/* point to the closing brace of the else */
if ((pc = get_closing_brace(next)) == NULL)
{
return;
}
}
else
{
break;
}
}
}
/* if we're dealing with a do/while, we actually want to add or remove blank lines after while and its condition */
if (start->type == CT_DO)
{
/* point to the next semicolon */
if ((pc = chunk_get_next_type(pc, CT_SEMICOLON, start->level)) == NULL)
{
return;
}
}
bool isVBrace = pc->type == CT_VBRACE_CLOSE;
if ((prev = chunk_get_prev_nvb(pc)) == NULL)
{
return;
}
have_pre_vbrace_nl = isVBrace && chunk_is_newline(prev);
if (nl_opt & AV_REMOVE)
{
/* if vbrace, have to check before and after */
/* if chunk before vbrace, remove any nls after vbrace */
if (have_pre_vbrace_nl)
{
if (prev->nl_count != 1)
{
prev->nl_count = 1;
MARK_CHANGE();
}
remove_next_newlines(pc);
}
else if ((chunk_is_newline(next = chunk_get_next_nvb(pc))) &&
!(next->flags & PCF_VAR_DEF))
{
/* otherwise just deal with nls after brace */
if (next->nl_count != 1)
{
next->nl_count = 1;
MARK_CHANGE();
}
remove_next_newlines(next);
}
}
/* may have a nl before and after vbrace */
/* don't do anything with it if the next non nl chunk is a closing brace */
if (nl_opt & AV_ADD)
{
if ((next = chunk_get_next_nnl(pc)) == NULL)
{
return;
}
if (next->type != CT_BRACE_CLOSE)
{
/* if vbrace, have to check before and after */
/* if chunk before vbrace, check its count */
nl_count = have_pre_vbrace_nl ? prev->nl_count : 0;
if (chunk_is_newline(next = chunk_get_next_nvb(pc)))
{
nl_count += next->nl_count;
}
/* if we have no newlines, add one and make it double */
if (nl_count == 0)
{
if (((next = chunk_get_next(pc)) != NULL) &&
chunk_is_comment(next))
{
pc = next;
}
if ((next = newline_add_after(pc)) == NULL)
{
return;
}
double_newline(next);
}
else if (nl_count == 1) /* if we don't have enough newlines */
{
/* if we have one before vbrace, need to add one after */
if (have_pre_vbrace_nl)
{
next = newline_add_after(pc);
}
else
{
prev = chunk_get_prev_nnl(next);
pc = chunk_get_next_nl(next);
//LOG_FMT(LSYS, " -- pc1=%s [%s]\n", pc->str.c_str(), get_token_name(pc->type));
pc = chunk_get_next(pc);
//LOG_FMT(LSYS, " -- pc2=%s [%s]\n", pc->str.c_str(), get_token_name(pc->type));
if ((pc != NULL) && (pc->type == CT_PREPROC) &&
(pc->parent_type == CT_PP_ENDIF) &&
cpd.settings[UO_nl_squeeze_ifdef].b)
{
LOG_FMT(LNEWLINE, "%s: cannot add newline after line %d due to nl_squeeze_ifdef\n",
__func__, prev->orig_line);
}
else
{
/* make nl after double */
double_newline(next);
}
}
}
}
}
}
/**
* Adds or removes a newline between the keyword and the open brace.
* If there is something after the '{' on the same line, then
* the newline is removed unconditionally.
* If there is a '=' between the keyword and '{', do nothing.
*
* "struct [name] {" or "struct [name] \n {"
*/
static void newlines_struct_enum_union(chunk_t *start, argval_t nl_opt, bool leave_trailing)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *next;
if ((nl_opt == AV_IGNORE) ||
(((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b))
{
return;
}
/* step past any junk between the keyword and the open brace
* Quit if we hit a semicolon or '=', which are not expected.
*/
int level = start->level;
pc = start;
while (((pc = chunk_get_next_ncnl(pc)) != NULL) && (pc->level >= level))
{
if ((pc->level == level) &&
((pc->type == CT_BRACE_OPEN) ||
chunk_is_semicolon(pc) ||
(pc->type == CT_ASSIGN)))
{
break;
}
start = pc;
}
/* If we hit a brace open, then we need to toy with the newlines */
if ((pc != NULL) && (pc->type == CT_BRACE_OPEN))
{
/* Skip over embedded C comments */
next = chunk_get_next(pc);
while ((next != NULL) && (next->type == CT_COMMENT))
{
next = chunk_get_next(next);
}
if (leave_trailing &&
!chunk_is_comment(next) &&
!chunk_is_newline(next))
{
nl_opt = AV_IGNORE;
}
newline_iarf_pair(start, pc, nl_opt);
}
}
/**
* Cuddles or un-cuddles a chunk with a previous close brace
*
* "} while" vs "} \n while"
* "} else" vs "} \n else"
*
* @param start The chunk - should be CT_ELSE or CT_WHILE_OF_DO
*/
static void newlines_cuddle_uncuddle(chunk_t *start, argval_t nl_opt)
{
LOG_FUNC_ENTRY();
chunk_t *br_close;
if (((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b)
{
return;
}
br_close = chunk_get_prev_ncnl(start);
if ((br_close != NULL) && (br_close->type == CT_BRACE_CLOSE))
{
newline_iarf_pair(br_close, start, nl_opt);
}
}
/**
* Adds/removes a newline between else and '{'.
* "else {" or "else \n {"
*/
static void newlines_do_else(chunk_t *start, argval_t nl_opt)
{
LOG_FUNC_ENTRY();
chunk_t *next;
chunk_t *tmp;
if ((nl_opt == AV_IGNORE) ||
(((start->flags & PCF_IN_PREPROC) != 0) &&
!cpd.settings[UO_nl_define_macro].b))
{
return;
}
next = chunk_get_next_ncnl(start);
if ((next != NULL) &&
((next->type == CT_BRACE_OPEN) ||
(next->type == CT_VBRACE_OPEN)))
{
if (!one_liner_nl_ok(next))
{
return;
}
if (next->type == CT_VBRACE_OPEN)
{
/* Can only add - we don't want to create a one-line here */
if (nl_opt & AV_ADD)
{
newline_iarf_pair(start, chunk_get_next_ncnl(next), nl_opt);
tmp = chunk_get_next_type(next, CT_VBRACE_CLOSE, next->level);
if (!chunk_is_newline(chunk_get_next_nc(tmp)) &&
!chunk_is_newline(chunk_get_prev_nc(tmp)))
{
newline_add_after(tmp);
}
}
}
else