forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoutput.cpp
More file actions
1962 lines (1761 loc) · 51.6 KB
/
output.cpp
File metadata and controls
1962 lines (1761 loc) · 51.6 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 output.cpp
* Does all the output & comment formatting.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "uncrustify_version.h"
#include "prototypes.h"
#include "chunk_list.h"
#include "unc_ctype.h"
#include <cstdlib>
static void output_comment_multi(chunk_t *pc);
static void output_comment_multi_simple(chunk_t *pc, bool kw_subst);
static void do_kw_subst(chunk_t *pc);
struct cmt_reflow
{
chunk_t *pc;
int column; /* Column of the comment start */
int brace_col; /* Brace column (for indenting with tabs) */
int base_col; /* Base column (for indenting with tabs) */
int word_count; /* number of words on this line */
int xtra_indent; /* extra indent of non-first lines (0 or 1) */
unc_text cont_text; /* fixed text to output at the start of a line (0 to 3 chars) */
bool reflow; /* reflow the current line */
};
static chunk_t *output_comment_c(chunk_t *pc);
static chunk_t *output_comment_cpp(chunk_t *pc);
static void add_comment_text(const unc_text& text,
cmt_reflow& cmt, bool esc_close);
#define LOG_CONTTEXT() \
LOG_FMT(LCONTTEXT, "%s:%d set cont_text to '%s'\n", __func__, __LINE__, cmt.cont_text.c_str())
/**
* All output text is sent here, one char at a time.
*/
static void add_char(UINT32 ch)
{
/* If we did a '\r' and it isn't followed by a '\n', then output a newline */
if ((cpd.last_char == '\r') && (ch != '\n'))
{
write_string(cpd.newline);
cpd.column = 1;
cpd.did_newline = 1;
cpd.spaces = 0;
}
/* convert a newline into the LF/CRLF/CR sequence */
if (ch == '\n')
{
write_string(cpd.newline);
cpd.column = 1;
cpd.did_newline = 1;
cpd.spaces = 0;
}
else if (ch == '\r')
{
/* do not output '\r' */
cpd.column = 1;
cpd.did_newline = 1;
cpd.spaces = 0;
}
else if ((ch == '\t') && cpd.output_tab_as_space)
{
int endcol = next_tab_column(cpd.column);
while (cpd.column < endcol)
{
add_char(' ');
}
return;
}
else
{
/* Explicitly disallow a tab after a space */
if ((ch == '\t') && (cpd.last_char == ' '))
{
int endcol = next_tab_column(cpd.column);
while (cpd.column < endcol)
{
add_char(' ');
}
return;
}
else if ((ch == ' ') && !cpd.output_trailspace)
{
cpd.spaces++;
cpd.column++;
}
else
{
while (cpd.spaces > 0)
{
write_char(' ');
cpd.spaces--;
}
write_char(ch);
if (ch == '\t')
{
cpd.column = next_tab_column(cpd.column);
}
else
{
cpd.column++;
}
}
}
cpd.last_char = ch;
}
static void add_text(const char *ascii_text)
{
char ch;
while ((ch = *ascii_text) != 0)
{
ascii_text++;
add_char(ch);
}
}
static void add_text(const unc_text& text)
{
for (int idx = 0; idx < text.size(); idx++)
{
add_char(text[idx]);
}
}
/**
* Count the number of characters to the end of the next chunk of text.
* If it exceeds the limit, return true.
*/
static bool next_word_exceeds_limit(const unc_text& text, int idx)
{
int length = 0;
/* Count any whitespace */
while ((idx < text.size()) && unc_isspace(text[idx]))
{
idx++;
length++;
}
/* Count non-whitespace */
while ((idx < text.size()) && !unc_isspace(text[idx]))
{
idx++;
length++;
}
return((cpd.column + length - 1) > cpd.settings[UO_cmt_width].n);
}
/**
* Advance to a specific column
* cpd.column is the current column
*
* @param column The column to advance to
*/
static void output_to_column(int column, bool allow_tabs)
{
int nc;
cpd.did_newline = 0;
if (allow_tabs)
{
/* tab out as far as possible and then use spaces */
while ((nc = next_tab_column(cpd.column)) <= column)
{
add_text("\t");
}
}
/* space out the final bit */
while (cpd.column < column)
{
add_text(" ");
}
}
/**
* Output a comment to the column using indent_with_tabs and
* indent_cmt_with_tabs as the rules.
* base_col is the indent of the first line of the comment.
* On the first line, column == base_col.
* On subsequnet lines, column >= base_col.
*
* @param brace_col the brace-level indent of the comment
* @param base_col the indent of the start of the comment (multiline)
* @param column the column that we should end up in
*/
static void cmt_output_indent(int brace_col, int base_col, int column)
{
int iwt;
int tab_col;
iwt = cpd.settings[UO_indent_cmt_with_tabs].b ? 2 :
(cpd.settings[UO_indent_with_tabs].n ? 1 : 0);
tab_col = (iwt == 0) ? 0 : ((iwt == 1) ? brace_col : base_col);
//LOG_FMT(LSYS, "%s(brace=%d base=%d col=%d iwt=%d) tab=%d cur=%d\n",
// __func__, brace_col, base_col, column, iwt, tab_col, cpd.column);
cpd.did_newline = 0;
if ((iwt == 2) || ((cpd.column == 1) && (iwt == 1)))
{
/* tab out as far as possible and then use spaces */
while (next_tab_column(cpd.column) <= tab_col)
{
add_text("\t");
}
}
/* space out the rest */
while (cpd.column < column)
{
add_text(" ");
}
}
void output_parsed(FILE *pfile)
{
chunk_t *pc;
int cnt;
save_option_file(pfile, false);
fprintf(pfile, "# -=====-\n");
fprintf(pfile, "# Line Tag Parent Columns Br/Lvl/pp Flag Nl Text");
for (pc = chunk_get_head(); pc != NULL; pc = chunk_get_next(pc))
{
fprintf(pfile, "\n# %3d> %13.13s[%13.13s][%2d/%2d/%2d/%2d][%d/%d/%d][%10" PRIx64 "][%d-%d]",
pc->orig_line, get_token_name(pc->type),
get_token_name(pc->parent_type),
pc->column, pc->orig_col, pc->orig_col_end, pc->orig_prev_sp,
pc->brace_level, pc->level, pc->pp_level,
pc->flags, pc->nl_count, pc->after_tab);
if ((pc->type != CT_NEWLINE) && (pc->len() != 0))
{
for (cnt = 0; cnt < pc->column; cnt++)
{
fprintf(pfile, " ");
}
if (pc->type != CT_NL_CONT)
{
fprintf(pfile, "%s", pc->str.c_str());
}
else
{
fprintf(pfile, "\\");
}
}
}
fprintf(pfile, "\n# -=====-\n");
fflush(pfile);
}
/**
* This renders the chunk list to a file.
*/
void output_text(FILE *pfile)
{
chunk_t *pc;
chunk_t *prev;
int cnt;
int lvlcol;
bool allow_tabs;
cpd.fout = pfile;
cpd.did_newline = 1;
cpd.column = 1;
if (cpd.bom)
{
write_bom();
}
if (cpd.frag_cols > 0)
{
int indent = cpd.frag_cols - 1;
for (pc = chunk_get_head(); pc != NULL; pc = chunk_get_next(pc))
{
pc->column += indent;
pc->column_indent += indent;
}
cpd.frag_cols = 0;
}
for (pc = chunk_get_head(); pc != NULL; pc = chunk_get_next(pc))
{
cpd.output_tab_as_space = (cpd.settings[UO_cmt_convert_tab_to_spaces].b &&
chunk_is_comment(pc));
if (pc->type == CT_NEWLINE)
{
for (cnt = 0; cnt < pc->nl_count; cnt++)
{
add_char('\n');
}
cpd.did_newline = 1;
cpd.column = 1;
LOG_FMT(LOUTIND, " xx\n");
}
else if (pc->type == CT_NL_CONT)
{
/* FIXME: this really shouldn't be done here! */
if ((pc->flags & PCF_WAS_ALIGNED) == 0)
{
if (cpd.settings[UO_sp_before_nl_cont].a & AV_REMOVE)
{
pc->column = cpd.column + (cpd.settings[UO_sp_before_nl_cont].a == AV_FORCE);
}
else
{
/* Try to keep the same relative spacing */
prev = chunk_get_prev(pc);
while ((prev != NULL) && (prev->orig_col == 0) && (prev->nl_count == 0))
{
prev = chunk_get_prev(prev);
}
if ((prev != NULL) && (prev->nl_count == 0))
{
int orig_sp = (pc->orig_col - prev->orig_col_end);
pc->column = cpd.column + orig_sp;
if ((cpd.settings[UO_sp_before_nl_cont].a != AV_IGNORE) &&
(pc->column < (cpd.column + 1)))
{
pc->column = cpd.column + 1;
}
}
}
output_to_column(pc->column, false);
}
else
{
output_to_column(pc->column, (cpd.settings[UO_indent_with_tabs].n == 2));
}
add_char('\\');
add_char('\n');
cpd.did_newline = 1;
cpd.column = 1;
LOG_FMT(LOUTIND, " \\xx\n");
}
else if (pc->type == CT_COMMENT_MULTI)
{
if (cpd.settings[UO_cmt_indent_multi].b)
{
output_comment_multi(pc);
}
else
{
output_comment_multi_simple(pc, (pc->flags & PCF_INSERTED) != 0);
}
}
else if (pc->type == CT_COMMENT_CPP)
{
pc = output_comment_cpp(pc);
}
else if (pc->type == CT_COMMENT)
{
pc = output_comment_c(pc);
}
else if ((pc->type == CT_JUNK) || (pc->type == CT_IGNORED))
{
/* do not adjust the column for junk */
add_text(pc->str);
}
else if (pc->len() == 0)
{
/* don't do anything for non-visible stuff */
LOG_FMT(LOUTIND, " <%d> -", pc->column);
}
else
{
cpd.output_trailspace = (pc->type == CT_STRING_MULTI);
/* indent to the 'level' first */
if (cpd.did_newline)
{
if (cpd.settings[UO_indent_with_tabs].n == 1)
{
/* FIXME: it would be better to properly set column_indent in
* indent_text(), but this hack for '}' and ':' seems to work. */
if ((pc->type == CT_BRACE_CLOSE) ||
chunk_is_str(pc, ":", 1) ||
(pc->type == CT_PREPROC))
{
lvlcol = pc->column;
}
else
{
lvlcol = pc->column_indent;
if (lvlcol > pc->column)
{
lvlcol = pc->column;
}
}
if (lvlcol > 1)
{
output_to_column(lvlcol, true);
}
}
allow_tabs = (cpd.settings[UO_indent_with_tabs].n == 2) ||
(chunk_is_comment(pc) &&
(cpd.settings[UO_indent_with_tabs].n != 0));
LOG_FMT(LOUTIND, " %d> col %d/%d/%d - ", pc->orig_line, pc->column, pc->column_indent, cpd.column);
}
else
{
/**
* Reformatting multi-line comments can screw up the column.
* Make sure we don't mess up the spacing on this line.
* This has to be done here because comments are not formatted
* until the output phase.
*/
if (pc->column < cpd.column)
{
reindent_line(pc, cpd.column);
}
/* not the first item on a line */
prev = chunk_get_prev(pc);
allow_tabs = (cpd.settings[UO_align_with_tabs].b &&
((pc->flags & PCF_WAS_ALIGNED) != 0) &&
((prev->column + prev->len() + 1) != pc->column));
if (cpd.settings[UO_align_keep_tabs].b)
{
allow_tabs |= pc->after_tab;
}
LOG_FMT(LOUTIND, " %d(%d) -", pc->column, allow_tabs);
}
output_to_column(pc->column, allow_tabs);
add_text(pc->str);
cpd.did_newline = chunk_is_newline(pc);
cpd.output_trailspace = false;
}
}
}
/**
* Checks for and updates the lead chars.
*
* @param line the comment line
* @return 0=not present, >0=number of chars that are part of the lead
*/
static int cmt_parse_lead(const unc_text& line, int is_last)
{
int len = 0;
while ((len < 32) && (len < line.size()))
{
if ((len > 0) && (line[len] == '/'))
{
/* ignore combined comments */
int tmp = len + 1;
while ((tmp < line.size()) && unc_isspace(line[tmp]))
{
tmp++;
}
if ((tmp < line.size()) && (line[tmp] == '/'))
{
return 1;
}
break;
}
else if (strchr("*|\\#+", line[len]) == NULL)
{
break;
}
len++;
}
if (len > 30)
{
return 1;
}
if ((len > 0) && ((len >= line.size()) || unc_isspace(line[len])))
{
return len;
}
if ((len == 1) && (line[0] == '*'))
{
return len;
}
if (is_last && (len > 0))
{
return len;
}
return 0;
}
/**
* Scans a multiline comment to determine the following:
* - the extra indent of the non-first line (0 or 1)
* - the continuation text ('' or '* ')
*
* The decision is based on:
* - cmt_indent_multi
* - cmt_star_cont
* - the first line length
* - the second line leader length
* - the last line length
*
* If the first and last line are the same length and don't contain any alnum
* chars and (the first line len > 2 or the second leader is the same as the
* first line length), then the indent is 0.
*
* If the leader on the second line is 1 wide or missing, then the indent is 1.
*
* Otherwise, the indent is 0.
*
* @param str The comment string
* @param len Length of the comment
* @param start_col Starting column
* @return 0 or 1
*/
static void calculate_comment_body_indent(cmt_reflow &cmt, const unc_text& str)
{
int idx = 0;
int first_len = 0;
int last_len = 0;
int width = 0;
int len = str.size();
cmt.xtra_indent = 0;
if (!cpd.settings[UO_cmt_indent_multi].b)
{
return;
}
if (cpd.settings[UO_cmt_multi_check_last].b)
{
/* find the last line length */
for (idx = len - 1; idx > 0; idx--)
{
if ((str[idx] == '\n') || (str[idx] == '\r'))
{
idx++;
while ((idx < len) && ((str[idx] == ' ') || (str[idx] == '\t')))
{
idx++;
}
last_len = len - idx;
break;
}
}
}
/* find the first line length */
for (idx = 0; idx < len; idx++)
{
if ((str[idx] == '\n') || (str[idx] == '\r'))
{
first_len = idx;
while ((str[first_len - 1] == ' ') || (str[first_len - 1] == '\t'))
{
first_len--;
}
/* handle DOS endings */
if ((str[idx] == '\r') && (str[idx + 1] == '\n'))
{
idx++;
}
idx++;
break;
}
}
/* Scan the second line */
width = 0;
for (/* nada */; idx < len - 1; idx++)
{
if ((str[idx] == ' ') || (str[idx] == '\t'))
{
if (width > 0)
{
break;
}
continue;
}
if ((str[idx] == '\n') || (str[idx] == '\r'))
{
/* Done with second line */
break;
}
/* Count the leading chars */
if ((str[idx] == '*') ||
(str[idx] == '|') ||
(str[idx] == '\\') ||
(str[idx] == '#') ||
(str[idx] == '+'))
{
width++;
}
else
{
if ((width != 1) || (str[idx - 1] != '*'))
{
width = 0;
}
break;
}
}
//LOG_FMT(LSYS, "%s: first=%d last=%d width=%d\n", __func__, first_len, last_len, width);
/*TODO: make the first_len minimum (4) configurable? */
if ((first_len == last_len) && ((first_len > 4) || (first_len == width)))
{
return;
}
cmt.xtra_indent = ((width == 2) ? 0 : 1);
}
static chunk_t *get_next_function(chunk_t *pc)
{
while ((pc = chunk_get_next(pc)) != NULL)
{
if ((pc->type == CT_FUNC_DEF) ||
(pc->type == CT_OC_MSG_DECL) ||
(pc->type == CT_FUNC_PROTO))
{
return(pc);
}
}
return(NULL);
}
static chunk_t *get_next_class(chunk_t *pc)
{
while ((pc = chunk_get_next(pc)) != NULL)
{
if (pc->type == CT_CLASS)
{
return(chunk_get_next(pc));
}
}
return(NULL);
}
static int next_up(const unc_text& text, int idx, unc_text& tag)
{
int offs = 0;
while ((idx < text.size()) && unc_isspace(text[idx]))
{
idx++;
offs++;
}
if (text.startswith(tag, idx))
{
return(offs);
}
return(-1);
}
/**
* Outputs a comment. The initial opening '//' may be included in the text.
* Subsequent openings (if combining comments), should not be included.
* The closing (for C/D comments) should not be included.
*
* TODO:
* If reflowing text, the comment should be added one word (or line) at a time.
* A newline should only be sent if a blank line is encountered or if the next
* line is indented beyond the current line (optional?).
* If the last char on a line is a ':' or '.', then the next line won't be
* combined.
*/
static void add_comment_text(const unc_text& text,
cmt_reflow& cmt, bool esc_close)
{
bool was_star = false;
bool was_slash = false;
bool in_word = false;
int tmp;
int len = text.size();
int ch_cnt = 0; /* chars since newline */
/* If the '//' is included write it first else we may wrap an empty line */
int idx = 0;
if (text.startswith("//"))
{
add_text("//");
idx += 2;
while (unc_isspace(text[idx]))
{
add_char(text[idx++]);
}
}
for ( ; idx < len; idx++)
{
/* Split the comment */
if (text[idx] == '\n')
{
in_word = false;
add_char('\n');
cmt_output_indent(cmt.brace_col, cmt.base_col, cmt.column);
if (cmt.xtra_indent)
{
add_char(' ');
}
/* hack to get escaped newlines to align and not dup the leading '//' */
tmp = next_up(text, idx + 1, cmt.cont_text);
if (tmp < 0)
{
add_text(cmt.cont_text);
}
else
{
idx += tmp;
}
ch_cnt = 0;
}
else if (cmt.reflow &&
(text[idx] == ' ') &&
(cpd.settings[UO_cmt_width].n > 0) &&
((cpd.column > cpd.settings[UO_cmt_width].n) ||
((ch_cnt > 1) && next_word_exceeds_limit(text, idx))))
{
in_word = false;
add_char('\n');
cmt_output_indent(cmt.brace_col, cmt.base_col, cmt.column);
if (cmt.xtra_indent)
{
add_char(' ');
}
add_text(cmt.cont_text);
output_to_column(cmt.column + cpd.settings[UO_cmt_sp_after_star_cont].n,
false);
ch_cnt = 0;
}
else
{
/* Escape a C closure in a CPP comment */
if (esc_close &&
((was_star && (text[idx] == '/')) ||
(was_slash && (text[idx] == '*'))))
{
add_char(' ');
}
if (!in_word && !unc_isspace(text[idx]))
{
cmt.word_count++;
}
in_word = !unc_isspace(text[idx]);
add_char(text[idx]);
was_star = (text[idx] == '*');
was_slash = (text[idx] == '/');
ch_cnt++;
}
}
}
static void output_cmt_start(cmt_reflow& cmt, chunk_t *pc)
{
cmt.pc = pc;
cmt.column = pc->column;
cmt.brace_col = pc->column_indent;
cmt.base_col = pc->column_indent;
cmt.word_count = 0;
cmt.xtra_indent = 0;
cmt.cont_text.clear();
cmt.reflow = false;
if ((pc->flags & PCF_INSERTED))
{
do_kw_subst(pc);
}
if (cmt.brace_col == 0)
{
cmt.brace_col = 1 + (pc->brace_level * cpd.settings[UO_output_tab_size].n);
}
//LOG_FMT(LSYS, "%s: line %d, brace=%d base=%d col=%d orig=%d aligned=%x\n",
// __func__, pc->orig_line, cmt.brace_col, cmt.base_col, cmt.column, pc->orig_col,
// pc->flags & (PCF_WAS_ALIGNED | PCF_RIGHT_COMMENT));
if ((pc->parent_type == CT_COMMENT_START) ||
(pc->parent_type == CT_COMMENT_WHOLE))
{
if (!cpd.settings[UO_indent_col1_comment].b &&
(pc->orig_col == 1) &&
!(pc->flags & PCF_INSERTED))
{
cmt.column = 1;
cmt.base_col = 1;
cmt.brace_col = 1;
}
}
/* tab aligning code */
if (cpd.settings[UO_indent_cmt_with_tabs].b &&
((pc->parent_type == CT_COMMENT_END) ||
(pc->parent_type == CT_COMMENT_WHOLE)))
{
cmt.column = align_tab_column(cmt.column - 1);
//LOG_FMT(LSYS, "%s: line %d, orig:%d new:%d\n",
// __func__, pc->orig_line, pc->column, cmt.column);
pc->column = cmt.column;
}
cmt.base_col = cmt.column;
//LOG_FMT(LSYS, "%s: -- brace=%d base=%d col=%d\n",
// __func__, cmt.brace_col, cmt.base_col, cmt.column);
/* Bump out to the column */
cmt_output_indent(cmt.brace_col, cmt.base_col, cmt.column);
}
/**
* Checks to see if the current comment can be combined with the next comment.
* The two can be combined if:
* 1. They are the same type
* 2. There is exactly one newline between then
* 3. They are indented to the same level
*/
static bool can_combine_comment(chunk_t *pc, cmt_reflow& cmt)
{
/* We can't combine if there is something other than a newline next */
if (pc->parent_type == CT_COMMENT_START)
{
return(false);
}
/* next is a newline for sure, make sure it is a single newline */
chunk_t *next = chunk_get_next(pc);
if ((next != NULL) && (next->nl_count == 1))
{
/* Make sure the comment is the same type at the same column */
next = chunk_get_next(next);
if ((next != NULL) &&
(next->type == pc->type) &&
(((next->column == 1) && (pc->column == 1)) ||
((next->column == cmt.base_col) && (pc->column == cmt.base_col)) ||
((next->column > cmt.base_col) && (pc->parent_type == CT_COMMENT_END))))
{
return(true);
}
}
return(false);
}
/**
* Outputs the C comment at pc.
* C comment combining is done here
*
* @return the last chunk output'd
*/
static chunk_t *output_comment_c(chunk_t *first)
{
cmt_reflow cmt;
output_cmt_start(cmt, first);
cmt.reflow = (cpd.settings[UO_cmt_reflow_mode].n != 1);
/* See if we can combine this comment with the next comment */
if (!cpd.settings[UO_cmt_c_group].b ||
!can_combine_comment(first, cmt))
{
/* Just add the single comment */
cmt.cont_text = cpd.settings[UO_cmt_star_cont].b ? " * " : " ";
LOG_CONTTEXT();
add_comment_text(first->str, cmt, false);
return(first);
}
cmt.cont_text = cpd.settings[UO_cmt_star_cont].b ? " *" : " ";
LOG_CONTTEXT();
add_text("/*");
if (cpd.settings[UO_cmt_c_nl_start].b)
{
add_comment_text("\n", cmt, false);
}
chunk_t *pc = first;
unc_text tmp;
while (can_combine_comment(pc, cmt))
{
tmp.set(pc->str, 2, pc->len() - 4);
if ((cpd.last_char == '*') && (tmp[0] == '/'))
{
add_text(" ");
}
add_comment_text(tmp, cmt, false);
add_comment_text("\n", cmt, false);
pc = chunk_get_next(chunk_get_next(pc));
}
tmp.set(pc->str, 2, pc->len() - 4);
if ((cpd.last_char == '*') && (tmp[0] == '/'))
{
add_text(" ");
}
add_comment_text(tmp, cmt, false);
if (cpd.settings[UO_cmt_c_nl_end].b)
{
cmt.cont_text = " ";
LOG_CONTTEXT();
add_comment_text("\n", cmt, false);
}
add_comment_text("*/", cmt, false);
return(pc);
}
/**
* Outputs the CPP comment at pc.
* CPP comment combining is done here
*
* @return the last chunk output'd
*/
static chunk_t *output_comment_cpp(chunk_t *first)
{
cmt_reflow cmt;
unc_text tmp;
output_cmt_start(cmt, first);
cmt.reflow = (cpd.settings[UO_cmt_reflow_mode].n != 1);
/* CPP comments can't be grouped unless they are converted to C comments */
if (!cpd.settings[UO_cmt_cpp_to_c].b)
{
cmt.cont_text = (cpd.settings[UO_sp_cmt_cpp_start].a == AV_REMOVE) ? "//" : "// ";
LOG_CONTTEXT();
if (cpd.settings[UO_sp_cmt_cpp_start].a == AV_IGNORE)
{
add_comment_text(first->str, cmt, false);
}
else
{
unc_text tmp(first->str, 0, 2);
add_comment_text(tmp, cmt, false);
tmp.set(first->str, 2, first->len() - 2);
if (cpd.settings[UO_sp_cmt_cpp_start].a & AV_REMOVE)
{
while ((tmp.size() > 0) && unc_isspace(tmp[0]))
{
tmp.pop_front();
}
}
if (tmp.size() > 0)
{
if (cpd.settings[UO_sp_cmt_cpp_start].a & AV_ADD)
{
if (!unc_isspace(tmp[0]) && (tmp[0] != '/'))
{
add_comment_text(" ", cmt, false);
}
}
add_comment_text(tmp, cmt, false);
}
}
return(first);
}
/* We are going to convert the CPP comments to C comments */
cmt.cont_text = cpd.settings[UO_cmt_star_cont].b ? " * " : " ";
LOG_CONTTEXT();
/* See if we can combine this comment with the next comment */
if (!cpd.settings[UO_cmt_cpp_group].b ||
!can_combine_comment(first, cmt))
{