forked from uncrustify/uncrustify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindent.cpp
More file actions
2130 lines (1955 loc) · 70.6 KB
/
indent.cpp
File metadata and controls
2130 lines (1955 loc) · 70.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 indent.cpp
* Does all the indenting stuff.
*
* @author Ben Gardner
* @license GPL v2+
*/
#include "uncrustify_types.h"
#include "chunk_list.h"
#include "prototypes.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include "unc_ctype.h"
/**
* General indenting approach:
* Indenting levels are put into a stack.
*
* The stack entries contain:
* - opening type
* - brace column
* - continuation column
*
* Items that start a new stack item:
* - preprocessor (new parse frame)
* - Brace Open (Virtual brace also)
* - Paren, Square, Angle open
* - Assignments
* - C++ '<<' operator (ie, cout << "blah")
* - case
* - class colon
* - return
* - types
* - any other continued statement
*
* Note that the column of items marked 'PCF_WAS_ALIGNED' is not changed.
*
* For an open brace:
* - indent increases by indent_columns
* - if part of if/else/do/while/switch/etc, an extra indent may be applied
* - if in a paren, then cont-col is set to column + 1, ie "({ some code })"
*
* Open paren/square/angle:
* cont-col is set to the column of the item after the open paren, unless
* followed by a newline, then it is set to (brace-col + indent_columns).
* Examples:
* a_really_long_funcion_name(
* param1, param2);
* a_really_long_funcion_name(param1,
* param2);
*
* Assignments:
* Assignments are continued aligned with the first item after the assignment,
* unless the assign is followed by a newline.
* Examples:
* some.variable = asdf + asdf +
* asdf;
* some.variable =
* asdf + asdf + asdf;
*
* C++ << operator:
* Handled the same as assignment.
* Examples:
* cout << "this is test number: "
* << test_number;
*
* case:
* Started with case or default.
* Terminated with close brace at level or another case or default.
* Special indenting according to various rules.
* - indent of case label
* - indent of case body
* - how to handle optional braces
* Examples:
* {
* case x: {
* a++;
* break;
* }
* case y:
* b--;
* break;
* default:
* c++;
* break;
* }
*
* Class colon:
* Indent continuation by indent_columns:
* class my_class :
* baseclass1,
* baseclass2
* {
*
* Return: same as assignemts
* If the return statement is not fully paren'd, then the indent continues at
* the column of the item after the return. If it is paren'd, then the paren
* rules apply.
* return somevalue +
* othervalue;
*
* Type: pretty much the same as assignments
* Examples:
* int foo,
* bar,
* baz;
*
* Any other continued item:
* There shouldn't be anything not covered by the above cases, but any other
* continued item is indented by indent_columns:
* Example:
* somereallycrazylongname.with[lotsoflongstuff].
* thatreallyannoysme.whenIhavetomaintain[thecode] = 3;
*/
static void indent_comment(chunk_t *pc, int col);
static bool ifdef_over_whole_file();
void indent_to_column(chunk_t *pc, int column)
{
LOG_FUNC_ENTRY();
if (column < pc->column)
{
column = pc->column;
}
reindent_line(pc, column);
}
enum align_mode
{
ALMODE_SHIFT, /* shift relative to the current column */
ALMODE_KEEP_ABS, /* try to keep the original absolute column */
ALMODE_KEEP_REL, /* try to keep the original gap */
};
/* Same as indent_to_column, except we can move both ways */
void align_to_column(chunk_t *pc, int column)
{
LOG_FUNC_ENTRY();
if (column == pc->column)
{
return;
}
LOG_FMT(LINDLINE, "%s: %d] col %d on %s [%s] => %d\n",
__func__, pc->orig_line, pc->column, pc->str.c_str(),
get_token_name(pc->type), column);
int col_delta = column - pc->column;
int min_col = column;
int min_delta;
pc->column = column;
do
{
chunk_t *next = chunk_get_next(pc);
chunk_t *prev;
align_mode almod = ALMODE_SHIFT;
if (next == NULL)
{
break;
}
min_delta = space_col_align(pc, next);
min_col += min_delta;
prev = pc;
pc = next;
if (chunk_is_comment(pc) && (pc->parent_type != CT_COMMENT_EMBED))
{
almod = (chunk_is_single_line_comment(pc) &&
cpd.settings[UO_indent_relative_single_line_comments].b) ?
ALMODE_KEEP_REL : ALMODE_KEEP_ABS;
}
if (almod == ALMODE_KEEP_ABS)
{
/* Keep same absolute column */
pc->column = pc->orig_col;
if (pc->column < min_col)
{
pc->column = min_col;
}
}
else if (almod == ALMODE_KEEP_REL)
{
/* Keep same relative column */
int orig_delta = pc->orig_col - prev->orig_col;
if (orig_delta < min_delta)
{
orig_delta = min_delta;
}
pc->column = prev->column + orig_delta;
}
else /* ALMODE_SHIFT */
{
/* Shift by the same amount */
pc->column += col_delta;
if (pc->column < min_col)
{
pc->column = min_col;
}
}
LOG_FMT(LINDLINED, " %s set column of %s on line %d to col %d (orig %d)\n",
(almod == ALMODE_KEEP_ABS) ? "abs" :
(almod == ALMODE_KEEP_REL) ? "rel" : "sft",
get_token_name(pc->type), pc->orig_line, pc->column, pc->orig_col);
} while ((pc != NULL) && (pc->nl_count == 0));
}
/**
* Changes the initial indent for a line to the given column
*
* @param pc The chunk at the start of the line
* @param column The desired column
*/
void reindent_line(chunk_t *pc, int column)
{
LOG_FUNC_ENTRY();
LOG_FMT(LINDLINE, "%s: %d] col %d on '%s' [%s/%s] => %d",
__func__, pc->orig_line, pc->column, pc->str.c_str(),
get_token_name(pc->type), get_token_name(pc->parent_type),
column);
log_func_stack_inline(LINDLINE);
if (column == pc->column)
{
return;
}
int col_delta = column - pc->column;
int min_col = column;
pc->column = column;
do
{
chunk_t *next = chunk_get_next(pc);
if (next == NULL)
{
break;
}
if (pc->nl_count)
{
min_col = 0;
col_delta = 0;
}
min_col += space_col_align(pc, next);
pc = next;
bool is_comment = chunk_is_comment(pc);
bool keep = is_comment && chunk_is_single_line_comment(pc) &&
cpd.settings[UO_indent_relative_single_line_comments].b;
if (is_comment && (pc->parent_type != CT_COMMENT_EMBED) && !keep)
{
pc->column = pc->orig_col;
if (pc->column < min_col)
{
pc->column = min_col; // + 1;
}
LOG_FMT(LINDLINE, "%s: set comment on line %d to col %d (orig %d)\n",
__func__, pc->orig_line, pc->column, pc->orig_col);
}
else
{
pc->column += col_delta;
if (pc->column < min_col)
{
pc->column = min_col;
}
LOG_FMT(LINDLINED, " set column of '%s' to %d (orig %d)\n",
pc->str.c_str(), pc->column, pc->orig_col);
}
} while ((pc != NULL) && (pc->nl_count == 0));
}
/**
* Starts a new entry
*
* @param frm The parse frame
* @param pc The chunk causing the push
*/
static void indent_pse_push(struct parse_frame& frm, chunk_t *pc)
{
LOG_FUNC_ENTRY();
static int ref = 0;
/* check the stack depth */
if (frm.pse_tos < ((int)ARRAY_SIZE(frm.pse) - 1))
{
/* Bump up the index and initialize it */
frm.pse_tos++;
memset(&frm.pse[frm.pse_tos], 0, sizeof(frm.pse[frm.pse_tos]));
LOG_FMT(LINDPSE, "%4d] (pp=%d) OPEN [%d,%s] level=%d\n",
pc->orig_line, cpd.pp_level, frm.pse_tos, get_token_name(pc->type), pc->level);
frm.pse[frm.pse_tos].pc = pc;
frm.pse[frm.pse_tos].type = pc->type;
frm.pse[frm.pse_tos].level = pc->level;
frm.pse[frm.pse_tos].open_line = pc->orig_line;
frm.pse[frm.pse_tos].ref = ++ref;
frm.pse[frm.pse_tos].in_preproc = (pc->flags & PCF_IN_PREPROC) != 0;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab;
frm.pse[frm.pse_tos].indent_cont = frm.pse[frm.pse_tos - 1].indent_cont;
frm.pse[frm.pse_tos].non_vardef = false;
frm.pse[frm.pse_tos].ns_cnt = frm.pse[frm.pse_tos - 1].ns_cnt;
memcpy(&frm.pse[frm.pse_tos].ip, &frm.pse[frm.pse_tos - 1].ip, sizeof(frm.pse[frm.pse_tos].ip));
}
}
/**
* Removes the top entry
*
* @param frm The parse frame
* @param pc The chunk causing the push
*/
static void indent_pse_pop(struct parse_frame& frm, chunk_t *pc)
{
LOG_FUNC_ENTRY();
/* Bump up the index and initialize it */
if (frm.pse_tos > 0)
{
if (pc != NULL)
{
LOG_FMT(LINDPSE, "%4d] (pp=%d) CLOSE [%d,%s] on %s, started on line %d, level=%d/%d\n",
pc->orig_line, cpd.pp_level, frm.pse_tos,
get_token_name(frm.pse[frm.pse_tos].type),
get_token_name(pc->type),
frm.pse[frm.pse_tos].open_line,
frm.pse[frm.pse_tos].level,
pc->level);
}
else
{
LOG_FMT(LINDPSE, " EOF] CLOSE [%d,%s], started on line %d\n",
frm.pse_tos, get_token_name(frm.pse[frm.pse_tos].type),
frm.pse[frm.pse_tos].open_line);
}
/* Don't clear the stack entry because some code 'cheats' and uses the
* just-popped indent values
*/
frm.pse_tos--;
}
}
static int token_indent(c_token_t type)
{
switch (type)
{
case CT_IF:
case CT_DO:
return(3);
case CT_FOR:
case CT_ELSE: // wacky, but that's what is wanted
return(4);
case CT_WHILE:
case CT_USING_STMT:
return(6);
case CT_SWITCH:
return(7);
case CT_ELSEIF:
return(8);
default:
return(0);
}
}
#define indent_column_set(X) \
do { \
indent_column = (X); \
LOG_FMT(LINDENT2, "[line %d] indent_column = %d\n", \
__LINE__, indent_column); \
} while (0)
static int calc_indent_continue(struct parse_frame& frm, int pse_tos)
{
int ic = cpd.settings[UO_indent_continue].n;
if ((ic < 0) && frm.pse[pse_tos].indent_cont)
{
return frm.pse[pse_tos].indent;
}
return frm.pse[pse_tos].indent + abs(ic);
}
/**
* We are on a '{' that has parent = OC_BLOCK_EXPR
* find the column of the param tag
*/
static chunk_t *oc_msg_block_indent(chunk_t *pc, bool from_brace,
bool from_caret, bool from_colon,
bool from_keyword)
{
LOG_FUNC_ENTRY();
chunk_t *tmp = chunk_get_prev_nc(pc);
if (from_brace)
{
return pc;
}
if (chunk_is_paren_close(tmp))
{
tmp = chunk_get_prev_nc(chunk_skip_to_match_rev(tmp));
}
if (!tmp || (tmp->type != CT_OC_BLOCK_CARET))
{
return NULL;
}
if (from_caret)
{
return tmp;
}
tmp = chunk_get_prev_nc(tmp);
if (!tmp || (tmp->type != CT_OC_COLON))
{
return NULL;
}
if (from_colon)
{
return tmp;
}
tmp = chunk_get_prev_nc(tmp);
if (!tmp || ((tmp->type != CT_OC_MSG_NAME) && (tmp->type != CT_OC_MSG_FUNC)))
{
return NULL;
}
if (from_keyword)
{
return tmp;
}
return NULL;
}
/**
* We are on a '{' that has parent = OC_BLOCK_EXPR
*/
static chunk_t *oc_msg_prev_colon(chunk_t *pc)
{
return chunk_get_prev_type(pc, CT_OC_COLON, pc->level, CNAV_ALL);
}
/**
* Change the top-level indentation only by changing the column member in
* the chunk structures.
* The level indicator must already be set.
*/
void indent_text(void)
{
LOG_FUNC_ENTRY();
chunk_t *pc;
chunk_t *next;
chunk_t *prev = NULL;
bool did_newline = true;
int idx;
int vardefcol = 0;
int indent_size = cpd.settings[UO_indent_columns].n;
int tmp;
struct parse_frame frm;
bool in_preproc = false;
int indent_column;
int parent_token_indent = 0;
int xml_indent = 0;
bool token_used;
int sql_col = 0;
int sql_orig_col = 0;
bool in_func_def = false;
c_token_t memtype;
memset(&frm, 0, sizeof(frm));
cpd.frame_count = 0;
/* dummy top-level entry */
frm.pse[0].indent = 1;
frm.pse[0].indent_tmp = 1;
frm.pse[0].indent_tab = 1;
frm.pse[0].type = CT_EOF;
pc = chunk_get_head();
while (pc != NULL)
{
/* Handle preprocessor transitions */
in_preproc = (pc->flags & PCF_IN_PREPROC) != 0;
if (cpd.settings[UO_indent_brace_parent].b)
{
parent_token_indent = token_indent(pc->parent_type);
}
/* Handle "force indentation of function definition to start in column 1" */
if (cpd.settings[UO_indent_func_def_force_col1].b)
{
if (!in_func_def)
{
next = chunk_get_next_ncnl(pc);
if ((pc->parent_type == CT_FUNC_DEF) ||
((pc->type == CT_COMMENT) &&
(next != NULL) &&
(next->parent_type == CT_FUNC_DEF)))
{
in_func_def = true;
indent_pse_push(frm, pc);
frm.pse[frm.pse_tos].indent_tmp = 1;
frm.pse[frm.pse_tos].indent = 1;
frm.pse[frm.pse_tos].indent_tab = 1;
}
}
else
{
prev = chunk_get_prev(pc);
if ((prev->type == CT_BRACE_CLOSE) &&
(prev->parent_type == CT_FUNC_DEF))
{
in_func_def = false;
indent_pse_pop(frm, pc);
}
}
}
/* Clean up after a #define, etc */
if (!in_preproc)
{
while ((frm.pse_tos > 0) && frm.pse[frm.pse_tos].in_preproc)
{
c_token_t type = frm.pse[frm.pse_tos].type;
indent_pse_pop(frm, pc);
/* If we just removed an #endregion, then check to see if a
* PP_REGION_INDENT entry is right below it
*/
if ((type == CT_PP_ENDREGION) &&
(frm.pse[frm.pse_tos].type == CT_PP_REGION_INDENT))
{
indent_pse_pop(frm, pc);
}
}
}
else if (pc->type == CT_PREPROC)
{
/* Close out PP_IF_INDENT before playing with the parse frames */
if ((frm.pse[frm.pse_tos].type == CT_PP_IF_INDENT) &&
((pc->parent_type == CT_PP_ENDIF) ||
(pc->parent_type == CT_PP_ELSE)))
{
indent_pse_pop(frm, pc);
}
pf_check(&frm, pc);
/* Indent the body of a #region here */
if (cpd.settings[UO_pp_region_indent_code].b &&
(pc->parent_type == CT_PP_REGION))
{
next = chunk_get_next(pc);
/* Hack to get the logs to look right */
set_chunk_type(next, CT_PP_REGION_INDENT);
indent_pse_push(frm, next);
set_chunk_type(next, CT_PP_REGION);
/* Indent one level */
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent + indent_size;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab + indent_size;
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos].in_preproc = false;
}
/* Indent the body of a #if here */
if (cpd.settings[UO_pp_if_indent_code].b &&
((pc->parent_type == CT_PP_IF) ||
(pc->parent_type == CT_PP_ELSE)))
{
next = chunk_get_next(pc);
/* Hack to get the logs to look right */
memtype = next->type;
set_chunk_type(next, CT_PP_IF_INDENT);
indent_pse_push(frm, next);
set_chunk_type(next, memtype);
/* Indent one level except if the #if is a #include guard */
int extra = ((pc->pp_level == 0) && ifdef_over_whole_file()) ? 0 : indent_size;
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent + extra;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos - 1].indent_tab + extra;
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos].in_preproc = false;
}
/* Transition into a preproc by creating a dummy indent */
frm.level++;
indent_pse_push(frm, chunk_get_next(pc));
if (pc->parent_type == CT_PP_DEFINE)
{
frm.pse[frm.pse_tos].indent_tmp = cpd.settings[UO_pp_define_at_level].b ?
frm.pse[frm.pse_tos - 1].indent_tmp : 1;
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos].indent_tmp + indent_size;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos].indent;
}
else
{
if ((frm.pse[frm.pse_tos - 1].type == CT_PP_REGION_INDENT) ||
((frm.pse[frm.pse_tos - 1].type == CT_PP_IF_INDENT) &&
(frm.pse[frm.pse_tos].type != CT_PP_ENDIF)))
{
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 2].indent;
}
else
{
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent;
}
if ((pc->parent_type == CT_PP_REGION) ||
(pc->parent_type == CT_PP_ENDREGION))
{
int val = cpd.settings[UO_pp_indent_region].n;
if (val > 0)
{
frm.pse[frm.pse_tos].indent = val;
}
else
{
frm.pse[frm.pse_tos].indent += val;
}
}
else if ((pc->parent_type == CT_PP_IF) ||
(pc->parent_type == CT_PP_ELSE) ||
(pc->parent_type == CT_PP_ENDIF))
{
int val = cpd.settings[UO_pp_indent_if].n;
if (val > 0)
{
frm.pse[frm.pse_tos].indent = val;
}
else
{
frm.pse[frm.pse_tos].indent += val;
}
}
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
}
}
/* Check for close XML tags "</..." */
if (cpd.settings[UO_indent_xml_string].n > 0)
{
if (pc->type == CT_STRING)
{
if ((pc->len() > 4) &&
(xml_indent > 0) &&
(pc->str[1] == '<') &&
(pc->str[2] == '/'))
{
xml_indent -= cpd.settings[UO_indent_xml_string].n;
}
}
else
{
if (!chunk_is_comment(pc) && !chunk_is_newline(pc))
{
xml_indent = 0;
}
}
}
/**
* Handle non-brace closures
*/
token_used = false;
int old_pse_tos;
do
{
old_pse_tos = frm.pse_tos;
/* End anything that drops a level */
if (!chunk_is_newline(pc) &&
!chunk_is_comment(pc) &&
(frm.pse[frm.pse_tos].level > pc->level))
{
indent_pse_pop(frm, pc);
}
if (frm.pse[frm.pse_tos].level >= pc->level)
{
/* process virtual braces closes (no text output) */
if ((pc->type == CT_VBRACE_CLOSE) &&
(frm.pse[frm.pse_tos].type == CT_VBRACE_OPEN))
{
indent_pse_pop(frm, pc);
frm.level--;
pc = chunk_get_next(pc);
}
/* End any assign operations with a semicolon on the same level */
if (((frm.pse[frm.pse_tos].type == CT_ASSIGN_NL) ||
(frm.pse[frm.pse_tos].type == CT_ASSIGN)) &&
(chunk_is_semicolon(pc) ||
(pc->type == CT_COMMA) ||
(pc->type == CT_BRACE_OPEN) ||
(pc->type == CT_SPAREN_CLOSE) ||
((pc->type == CT_SQUARE_OPEN) && (pc->parent_type == CT_OC_AT)) ||
((pc->type == CT_SQUARE_OPEN) && (pc->parent_type == CT_ASSIGN))) &&
(pc->parent_type != CT_CPP_LAMBDA))
{
indent_pse_pop(frm, pc);
}
/* End any assign operations with a semicolon on the same level */
if (chunk_is_semicolon(pc) &&
((frm.pse[frm.pse_tos].type == CT_IMPORT) ||
(frm.pse[frm.pse_tos].type == CT_USING)))
{
indent_pse_pop(frm, pc);
}
/* End any custom macro-based open/closes */
if (!token_used &&
(frm.pse[frm.pse_tos].type == CT_MACRO_OPEN) &&
(pc->type == CT_MACRO_CLOSE))
{
token_used = true;
indent_pse_pop(frm, pc);
}
/* End any CPP/ObjC class colon stuff */
if (((frm.pse[frm.pse_tos].type == CT_CLASS_COLON) ||
(frm.pse[frm.pse_tos].type == CT_CONSTR_COLON)) &&
((pc->type == CT_BRACE_OPEN) ||
(pc->type == CT_OC_END) ||
(pc->type == CT_OC_SCOPE) ||
(pc->type == CT_OC_PROPERTY) ||
chunk_is_semicolon(pc)))
{
indent_pse_pop(frm, pc);
}
/* a case is ended with another case or a close brace */
if ((frm.pse[frm.pse_tos].type == CT_CASE) &&
((pc->type == CT_BRACE_CLOSE) ||
(pc->type == CT_CASE)))
{
indent_pse_pop(frm, pc);
}
/* a class scope is ended with another class scope or a close brace */
if (cpd.settings[UO_indent_access_spec_body].b &&
(frm.pse[frm.pse_tos].type == CT_PRIVATE) &&
((pc->type == CT_BRACE_CLOSE) ||
(pc->type == CT_PRIVATE)))
{
indent_pse_pop(frm, pc);
}
/* return & throw are ended with a semicolon */
if (chunk_is_semicolon(pc) &&
((frm.pse[frm.pse_tos].type == CT_RETURN) ||
(frm.pse[frm.pse_tos].type == CT_THROW)))
{
indent_pse_pop(frm, pc);
}
/* an OC SCOPE ('-' or '+') ends with a semicolon or brace open */
if ((frm.pse[frm.pse_tos].type == CT_OC_SCOPE) &&
(chunk_is_semicolon(pc) ||
(pc->type == CT_BRACE_OPEN)))
{
indent_pse_pop(frm, pc);
}
/* a typedef and an OC SCOPE ('-' or '+') ends with a semicolon or
* brace open */
if ((frm.pse[frm.pse_tos].type == CT_TYPEDEF) &&
(chunk_is_semicolon(pc) ||
chunk_is_paren_open(pc) ||
(pc->type == CT_BRACE_OPEN)))
{
indent_pse_pop(frm, pc);
}
/* an SQL EXEC is ended with a semicolon */
if ((frm.pse[frm.pse_tos].type == CT_SQL_EXEC) &&
chunk_is_semicolon(pc))
{
indent_pse_pop(frm, pc);
}
/* an CLASS is ended with a semicolon or brace open */
if ((frm.pse[frm.pse_tos].type == CT_CLASS) &&
((pc->type == CT_CLASS_COLON) ||
(pc->type == CT_BRACE_OPEN) ||
chunk_is_semicolon(pc)))
{
indent_pse_pop(frm, pc);
}
/* Close out parens and squares */
if ((frm.pse[frm.pse_tos].type == (pc->type - 1)) &&
((pc->type == CT_PAREN_CLOSE) ||
(pc->type == CT_SPAREN_CLOSE) ||
(pc->type == CT_FPAREN_CLOSE) ||
(pc->type == CT_SQUARE_CLOSE) ||
(pc->type == CT_ANGLE_CLOSE)))
{
indent_pse_pop(frm, pc);
frm.paren_count--;
}
}
} while (old_pse_tos > frm.pse_tos);
/* Grab a copy of the current indent */
indent_column_set(frm.pse[frm.pse_tos].indent_tmp);
if (!chunk_is_newline(pc) && !chunk_is_comment(pc) && log_sev_on(LINDPC))
{
LOG_FMT(LINDPC, " -=[ %d:%d %s ]=-\n",
pc->orig_line, pc->orig_col, pc->str.c_str());
for (int ttidx = frm.pse_tos; ttidx > 0; ttidx--)
{
LOG_FMT(LINDPC, " [%d %d:%d %s/%s tmp=%d ind=%d bri=%d tab=%d cont=%d lvl=%d blvl=%d]\n",
ttidx,
frm.pse[ttidx].pc->orig_line,
frm.pse[ttidx].pc->orig_col,
get_token_name(frm.pse[ttidx].type),
get_token_name(frm.pse[ttidx].pc->parent_type),
frm.pse[ttidx].indent_tmp,
frm.pse[ttidx].indent,
frm.pse[ttidx].brace_indent,
frm.pse[ttidx].indent_tab,
frm.pse[ttidx].indent_cont,
frm.pse[ttidx].level,
frm.pse[ttidx].pc->brace_level);
}
}
/**
* Handle stuff that can affect the current indent:
* - brace close
* - vbrace open
* - brace open
* - case (immediate)
* - labels (immediate)
* - class colons (immediate)
*
* And some stuff that can't
* - open paren
* - open square
* - assignment
* - return
*/
bool brace_indent = false;
if ((pc->type == CT_BRACE_CLOSE) || (pc->type == CT_BRACE_OPEN))
{
brace_indent = (cpd.settings[UO_indent_braces].b &&
(!cpd.settings[UO_indent_braces_no_func].b ||
(pc->parent_type != CT_FUNC_DEF)) &&
(!cpd.settings[UO_indent_braces_no_func].b ||
(pc->parent_type != CT_FUNC_CLASS_DEF)) &&
(!cpd.settings[UO_indent_braces_no_class].b ||
(pc->parent_type != CT_CLASS)) &&
(!cpd.settings[UO_indent_braces_no_struct].b ||
(pc->parent_type != CT_STRUCT)));
}
if (pc->type == CT_BRACE_CLOSE)
{
if (frm.pse[frm.pse_tos].type == CT_BRACE_OPEN)
{
/* Indent the brace to match the open brace */
indent_column_set(frm.pse[frm.pse_tos].brace_indent);
if (frm.pse[frm.pse_tos].ip.ref)
{
pc->indent.ref = frm.pse[frm.pse_tos].ip.ref;
pc->indent.delta = 0;
}
indent_pse_pop(frm, pc);
frm.level--;
}
}
else if (pc->type == CT_VBRACE_OPEN)
{
frm.level++;
indent_pse_push(frm, pc);
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent + indent_size;
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos].indent;
/* Always indent on virtual braces */
indent_column_set(frm.pse[frm.pse_tos].indent_tmp);
}
else if (pc->type == CT_BRACE_OPEN)
{
frm.level++;
indent_pse_push(frm, pc);
/* any '{' that is inside of a '(' overrides the '(' indent */
if (!cpd.settings[UO_indent_paren_open_brace].b &&
chunk_is_paren_open(frm.pse[frm.pse_tos - 1].pc) &&
chunk_is_newline(chunk_get_next_nc(pc)))
{
/* FIXME: I don't know how much of this is necessary, but it seems to work */
frm.pse[frm.pse_tos].brace_indent = 1 + (pc->brace_level * indent_size);
indent_column = frm.pse[frm.pse_tos].brace_indent;
frm.pse[frm.pse_tos].indent = indent_column + indent_size;
frm.pse[frm.pse_tos].indent_tab = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos].indent_tmp = frm.pse[frm.pse_tos].indent;
frm.pse[frm.pse_tos - 1].indent_tmp = frm.pse[frm.pse_tos].indent_tmp;
}
else if (frm.paren_count != 0)
{
if (frm.pse[frm.pse_tos].pc->parent_type == CT_OC_BLOCK_EXPR)
{
if ((pc->flags & PCF_IN_OC_MSG) &&
cpd.settings[UO_indent_oc_block_msg].n)
{
frm.pse[frm.pse_tos].ip.ref = oc_msg_block_indent(pc, false, false, false, true);
frm.pse[frm.pse_tos].ip.delta = cpd.settings[UO_indent_oc_block_msg].n;
}
if (cpd.settings[UO_indent_oc_block].b ||
cpd.settings[UO_indent_oc_block_msg_xcode_style].b)
{
bool in_oc_msg = (pc->flags & PCF_IN_OC_MSG);
bool indent_from_keyword = cpd.settings[UO_indent_oc_block_msg_from_keyword].b && in_oc_msg;
bool indent_from_colon = cpd.settings[UO_indent_oc_block_msg_from_colon].b && in_oc_msg;
bool indent_from_caret = cpd.settings[UO_indent_oc_block_msg_from_caret].b && in_oc_msg;
bool indent_from_brace = cpd.settings[UO_indent_oc_block_msg_from_brace].b && in_oc_msg;
// In "Xcode indent mode", we want to indent:
// - if the colon is aligned (namely, if a newline has been
// added before it), indent_from_brace
// - otherwise, indent from previous block (the "else" statement here)
if (cpd.settings[UO_indent_oc_block_msg_xcode_style].b)
{
chunk_t *colon = oc_msg_prev_colon(pc);
chunk_t *param_name = chunk_get_prev(colon);
chunk_t *before_param = chunk_get_prev(param_name);
if (before_param && (before_param->type == CT_NEWLINE))
{
indent_from_keyword = true;
indent_from_colon = false;
indent_from_caret = false;
indent_from_brace = false;
}
else
{
indent_from_brace = false;
indent_from_colon = false;
indent_from_caret = false;
indent_from_keyword = false;
}
}
chunk_t *ref = oc_msg_block_indent(pc, indent_from_brace,
indent_from_caret,
indent_from_colon,
indent_from_keyword);
if (ref)
{
frm.pse[frm.pse_tos].indent = indent_size + ref->column;
indent_column_set(frm.pse[frm.pse_tos].indent - indent_size);
}
else
{
frm.pse[frm.pse_tos].indent = 1 + ((pc->brace_level + 1) * indent_size);
indent_column_set(frm.pse[frm.pse_tos].indent - indent_size);
}
}
else
{
frm.pse[frm.pse_tos].indent = frm.pse[frm.pse_tos - 1].indent_tmp + indent_size;
}
}
else
{