forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoutput.cpp
More file actions
2227 lines (1957 loc) · 59.9 KB
/
output.cpp
File metadata and controls
2227 lines (1957 loc) · 59.9 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
* @author Guy Maurel since version 0.62 for uncrustify4Qt
* October 2015, 2016
* @license GPL v2+
*/
#include "output.h"
#include "uncrustify_types.h"
#include "prototypes.h"
#include "chunk_list.h"
#include "unc_ctype.h"
#include "uncrustify.h"
#include "indent.h"
#include "braces.h"
#include "unicode.h"
#include "helper_for_print.h"
#include <cstdlib>
struct cmt_reflow
{
chunk_t *pc;
size_t column; //! Column of the comment start
size_t brace_col; //! Brace column (for indenting with tabs)
size_t base_col; //! Base column (for indenting with tabs)
size_t word_count; //! number of words on this line
size_t 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
};
/**
* A multiline comment
* The only trick here is that we have to trim out whitespace characters
* to get the comment to line up.
*/
static void output_comment_multi(chunk_t *pc);
static bool kw_fcn_filename(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_class(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_message(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_category(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_scope(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_function(chunk_t *cmt, unc_text &out_txt);
/**
* Adds the javadoc-style @param and @return stuff, based on the params and
* return value for pc.
* If the arg list is '()' or '(void)', then no @params are added.
* Likewise, if the return value is 'void', then no @return is added.
*/
static bool kw_fcn_javaparam(chunk_t *cmt, unc_text &out_txt);
static bool kw_fcn_fclass(chunk_t *cmt, unc_text &out_txt);
/**
* Output a multiline comment without any reformatting other than shifting
* it left or right to get the column right.
* Trim trailing whitespace and do keyword substitution.
*/
static void output_comment_multi_simple(chunk_t *pc, bool kw_subst);
/**
* This renders the #if condition to a string buffer.
*
* @param[out] dst unc_text buffer to be filled
* @param[in] ifdef if conditional as chunk list
*/
static void generate_if_conditional_as_text(unc_text &dst, chunk_t *ifdef);
/**
* Do keyword substitution on a comment.
* NOTE: it is assumed that a comment will contain at most one of each type
* of keyword.
*/
static void do_kw_subst(chunk_t *pc);
//! All output text is sent here, one char at a time.
static void add_char(UINT32 ch);
static void add_text(const char *ascii_text);
static void add_text(const unc_text &text, bool is_ignored);
/**
* 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, size_t idx);
/**
* 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 subsequent 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(size_t brace_col, size_t base_col, size_t column);
/**
* 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 size_t cmt_parse_lead(const unc_text &line, bool is_last);
/**
* 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
* - cmt_multi_first_len_minimum
* - the first line length
* - the second line leader length
* - the last line length (without leading space/tab)
*
* 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 cmt.xtra_indent is set to 0 or 1
*/
static void calculate_comment_body_indent(cmt_reflow &cmt, const unc_text &str);
static int next_up(const unc_text &text, size_t idx, unc_text &tag);
/**
* 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 *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 *pc);
static void cmt_trim_whitespace(unc_text &line, bool in_preproc);
/**
* 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);
static void output_cmt_start(cmt_reflow &cmt, chunk_t *pc);
/**
* 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);
#define LOG_CONTTEXT() \
LOG_FMT(LCONTTEXT, "%s:%d set cont_text to '%s'\n", __func__, __LINE__, cmt.cont_text.c_str())
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 the CARRIAGERETURN
{
// do not output '\r'
cpd.column = 1;
cpd.did_newline = 1;
cpd.spaces = 0;
}
else if ((ch == '\t') && cpd.output_tab_as_space)
{
size_t 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 == ' ')
{
size_t endcol = next_tab_column(cpd.column);
while (cpd.column < endcol)
{
add_char(' ');
}
return;
}
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;
} // add_char
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, bool is_ignored = false)
{
for (size_t idx = 0; idx < text.size(); idx++)
{
int ch = text[idx];
if (is_ignored)
{
write_char(ch);
}
else
{
add_char(ch);
}
}
}
static bool next_word_exceeds_limit(const unc_text &text, size_t idx)
{
size_t 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].u);
}
/**
* Advance to a specific column
* cpd.column is the current column
*
* @param column The column to advance to
*/
static void output_to_column(size_t column, bool allow_tabs)
{
cpd.did_newline = 0;
if (allow_tabs)
{
// tab out as far as possible and then use spaces
size_t next_column = next_tab_column(cpd.column);
while (next_column <= column)
{
add_text("\t");
next_column = next_tab_column(cpd.column);
}
}
// space out the final bit
while (cpd.column < column)
{
add_text(" ");
}
}
static void cmt_output_indent(size_t brace_col, size_t base_col, size_t column)
{
size_t iwt = cpd.settings[UO_indent_cmt_with_tabs].b ? 2 :
(cpd.settings[UO_indent_with_tabs].u ? 1 : 0);
size_t 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(" ");
}
} // cmt_output_indent
void output_parsed(FILE *pfile)
{
// save_option_file(pfile, false);
save_option_file_kernel(pfile, false, true);
fprintf(pfile, "# -=====-\n");
fprintf(pfile, "# Line Tag Parent Columns Br/Lvl/pp Flag Nl Text");
for (chunk_t *pc = chunk_get_head(); pc != nullptr; pc = chunk_get_next(pc))
{
char *outputMessage;
outputMessage = make_message("\n# %3zu> %16.16s[%16.16s][%3zu/%3zu/%3zu/%3d][%zu/%zu/%zu][%10" PRIx64 "][%zu-%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);
fprintf(pfile, "%s", outputMessage);
free(outputMessage);
if (pc->type != CT_NEWLINE && (pc->len() != 0))
{
for (size_t cnt = 0; cnt < pc->column; cnt++)
{
fprintf(pfile, " ");
}
if (pc->type != CT_NL_CONT)
{
fprintf(pfile, "%s", pc->text());
}
else
{
fprintf(pfile, "\\");
}
}
}
fprintf(pfile, "\n# -=====-\n");
fflush(pfile);
} // output_parsed
void output_text(FILE *pfile)
{
cpd.fout = pfile;
cpd.did_newline = 1;
cpd.column = 1;
if (cpd.bom)
{
write_bom();
}
chunk_t *pc;
if (cpd.frag_cols > 0)
{
size_t indent = cpd.frag_cols - 1;
// loop over the whole chunk list
for (pc = chunk_get_head(); pc != nullptr; pc = chunk_get_next(pc))
{
pc->column += indent;
pc->column_indent += indent;
}
cpd.frag_cols = 0;
}
// loop over the whole chunk list
for (pc = chunk_get_head(); pc != nullptr; pc = chunk_get_next(pc))
{
LOG_FMT(LOUTIND, "%s(%d): text() is %s, type is %s, orig_col is %zu, column is %zu, nl is %zu\n",
__func__, __LINE__, pc->text(), get_token_name(pc->type), pc->orig_col, pc->column, pc->nl_count);
cpd.output_tab_as_space = ( cpd.settings[UO_cmt_convert_tab_to_spaces].b
&& chunk_is_comment(pc));
if (pc->type == CT_NEWLINE)
{
for (size_t 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
chunk_t *prev = chunk_get_prev(pc);
if (prev && prev->type == CT_PP_IGNORE)
{
/*
* Want to completely leave alone PP_IGNORE'd blocks because
* they likely have special column aligned newline
* continuations (common in multiline macros)
*/
pc->column = pc->orig_col;
}
else
{
// Try to keep the same relative spacing
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);
if ((int)(cpd.column + orig_sp) < 0)
{
char *outputMessage;
outputMessage = make_message("FATAL: negative value.\n pc->orig_col=%zu prev->orig_col_end=%zu\n",
pc->orig_col, prev->orig_col_end);
fprintf(stderr, "%s", outputMessage);
free(outputMessage);
log_flush(true);
exit(EX_SOFTWARE);
}
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].u == 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); // forcing value to bool
}
}
else if (pc->type == CT_COMMENT_CPP)
{
bool tmp = cpd.output_trailspace;
/*
* keep trailing spaces if they are still present in a chunk;
* note that tokenize() already strips spaces in comments,
* so if they made it up to here, they are to stay
*/
cpd.output_trailspace = true;
pc = output_comment_cpp(pc);
cpd.output_trailspace = tmp;
}
else if (pc->type == CT_COMMENT)
{
pc = output_comment_c(pc);
}
else if (pc->type == CT_JUNK || pc->type == CT_IGNORED)
{
LOG_FMT(LOUTIND, "%s(%d): orig_line is %zu, orig_col is %zu,\npc->text() >%s<, pc->str.size() is %zu\n",
__func__, __LINE__, pc->orig_line, pc->orig_col, pc->text(), pc->str.size());
// do not adjust the column for junk
add_text(pc->str, true);
}
else if (pc->len() == 0)
{
// don't do anything for non-visible stuff
LOG_FMT(LOUTIND, " <%zu> -", pc->column);
}
else
{
bool allow_tabs;
cpd.output_trailspace = (pc->type == CT_STRING_MULTI);
// indent to the 'level' first
if (cpd.did_newline)
{
if (cpd.settings[UO_indent_with_tabs].u == 1)
{
size_t lvlcol;
/*
* 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
|| pc->type == CT_CASE_COLON
|| 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].u == 2)
|| ( chunk_is_comment(pc)
&& cpd.settings[UO_indent_with_tabs].u != 0);
LOG_FMT(LOUTIND, "%s(%d): orig_line is %zu, column is %zu, column_indent is %zu, cpd.column is %zu\n",
__func__, __LINE__, 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
chunk_t *prev = chunk_get_prev(pc);
allow_tabs = ( cpd.settings[UO_align_with_tabs].b
&& (pc->flags & PCF_WAS_ALIGNED)
&& ((prev->column + prev->len() + 1) != pc->column));
if (cpd.settings[UO_align_keep_tabs].b)
{
allow_tabs |= pc->after_tab;
}
LOG_FMT(LOUTIND, " %s(%d): at column %zu(%s)\n",
__func__, __LINE__, pc->column, (allow_tabs ? "true" : "FALSE"));
}
output_to_column(pc->column, allow_tabs);
add_text(pc->str);
if (pc->type == CT_PP_DEFINE) // Issue #876
{
if (cpd.settings[UO_force_tab_after_define].b)
{
add_char('\t');
}
}
cpd.did_newline = chunk_is_newline(pc);
cpd.output_trailspace = false;
}
}
} // output_text
static size_t cmt_parse_lead(const unc_text &line, bool is_last)
{
size_t len = 0;
while (len < 32 && len < line.size()) // TODO what is the meaning of 32?
{
if (len > 0 && line[len] == '/')
{
// ignore combined comments
size_t 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]) == nullptr)
{
break; // none of the characters '*|\#+' found in line
}
len++;
}
if (len > 30) // TODO: what is the meaning of 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);
} // cmt_parse_lead
static void calculate_comment_body_indent(cmt_reflow &cmt, const unc_text &str)
{
cmt.xtra_indent = 0;
if (!cpd.settings[UO_cmt_indent_multi].b)
{
return;
}
size_t idx = 0;
size_t len = str.size();
size_t last_len = 0;
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
size_t first_len = 0;
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
size_t width = 0;
for ( ; idx < len - 1; idx++)
{
if (str[idx] == ' ' || str[idx] == '\t')
{
if (width > 0)
{
break;
}
continue;
}
if (str[idx] == '\n' || str[idx] == '\r')
{
break; // Done with second line
}
// 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);
/*
* If the first and last line are the same length and don't contain any
* alphanumeric chars and (the first line len > cmt_multi_first_len_minimum
* or the second leader is the same as the first line length), then the
* indent is 0.
*/
if ( first_len == last_len
&& ( first_len > cpd.settings[UO_cmt_multi_first_len_minimum].u
|| first_len == width))
{
return;
}
cmt.xtra_indent = (width == 2) ? 0 : 1;
} // calculate_comment_body_indent
// TODO: can we use search_next_chunk here?
static chunk_t *get_next_function(chunk_t *pc)
{
while ((pc = chunk_get_next(pc)) != nullptr)
{
if ( pc->type == CT_FUNC_DEF
|| pc->type == CT_FUNC_PROTO
|| pc->type == CT_FUNC_CLASS_DEF
|| pc->type == CT_FUNC_CLASS_PROTO
|| pc->type == CT_OC_MSG_DECL)
{
return(pc);
}
}
return(nullptr);
}
static chunk_t *get_next_class(chunk_t *pc)
{
return(chunk_get_next(chunk_search_next_cat(pc, CT_CLASS)));
}
static chunk_t *get_prev_category(chunk_t *pc)
{
return(chunk_search_prev_cat(pc, CT_OC_CATEGORY));
}
static chunk_t *get_next_scope(chunk_t *pc)
{
return(chunk_search_next_cat(pc, CT_OC_SCOPE));
}
static chunk_t *get_prev_oc_class(chunk_t *pc)
{
return(chunk_search_prev_cat(pc, CT_OC_CLASS));
}
static int next_up(const unc_text &text, size_t idx, unc_text &tag)
{
size_t offs = 0;
while (idx < text.size() && unc_isspace(text[idx]))
{
idx++;
offs++;
}
if (text.startswith(tag, idx))
{
return(offs);
}
return(-1);
}
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;
size_t len = text.size();
size_t ch_cnt = 0; // chars since newline
// If the '//' is included write it first else we may wrap an empty line
size_t idx = 0;
if (text.startswith("//"))
{
add_text("//");
idx += 2;
while (unc_isspace(text[idx]))
{
add_char(text[idx++]);
}
}
for ( ; idx < len; idx++) // TODO: avoid modifying idx in loop
{
// 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 > 0)
{
add_char(' ');
}
// hack to get escaped newlines to align and not duplicate the leading '//'
int 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].u > 0
&& ( cpd.column > cpd.settings[UO_cmt_width].u
|| (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 > 0)
{
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++;
}
}
} // add_comment_text
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);