summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.73.0/src/align.cpp
blob: 09e45dc8c4692d7f3e9ee2fbb926e70050da0997 (plain)
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
/**
 * @file align.cpp
 * Does all the aligning stuff.
 *
 * @author  Ben Gardner
 * @author  Guy Maurel since version 0.62 for uncrustify4Qt
 *          October 2015, 2016
 * @license GPL v2+
 */

#include "align.h"

#include "align_asm_colon.h"
#include "align_assign.h"
#include "align_eigen_comma_init.h"
#include "align_func_params.h"
#include "align_func_proto.h"
#include "align_init_brace.h"
#include "align_left_shift.h"
#include "align_oc_decl_colon.h"
#include "align_oc_msg_colons.h"
#include "align_oc_msg_spec.h"
#include "align_preprocessor.h"
#include "align_same_func_call_params.h"
#include "align_stack.h"
#include "align_struct_initializers.h"
#include "align_trailing_comments.h"
#include "align_typedefs.h"
#include "align_var_def_brace.h"
#include "log_rules.h"
#include "quick_align_again.h"

constexpr static auto LCURRENT = LALIGN;

using namespace uncrustify;


/*
 *   Here are the items aligned:
 *
 *   - enum value assignments
 *     enum {
 *        cat  = 1,
 *        fred = 2,
 *     };
 *
 *   - struct/union variable & bit definitions
 *     struct foo {
 *        char cat;
 *        int  id       : 5;
 *        int  name_len : 6;
 *        int  height   : 12;
 *     };
 *
 *   - variable definitions & assignments in normal code
 *     const char *cat = "feline";
 *     int        id   = 4;
 *     a   = 5;
 *     bat = 14;
 *
 *   - simple array initializers
 *     int a[] = {
 *        1, 2, 3, 4, 5,
 *        6, 7, 8, 9, 10
 *     };
 *
 *   - c99 array initializers
 *     const char *name[] = {
 *        [FRED]  = "fred",
 *        [JOE]   = "joe",
 *        [PETER] = "peter",
 *     };
 *     struct foo b[] = {
 *        { .id = 1,   .name = "text 1" },
 *        { .id = 567, .name = "text 2" },
 *     };
 *     struct foo_t bars[] =
 *     {
 *        [0] = { .name = "bar",
 *                .age  = 21 },
 *        [1] = { .name = "barley",
 *                .age  = 55 },
 *     };
 *
 *   - compact array initializers
 *     struct foo b[] = {
 *        { 3, "dog" },      { 6, "spider" },
 *        { 8, "elephant" }, { 3, "cat" },
 *     };
 *
 *   - multiline array initializers (2nd line indented, not aligned)
 *     struct foo b[] = {
 *        { AD_NOT_ALLOWED, "Sorry, you failed to guess the password.",
 *          "Try again?", "Yes", "No" },
 *        { AD_SW_ERROR,    "A software error has occured.", "Bye!", NULL, NULL },
 *     };
 *
 *   - Trailing comments
 *
 *   - Back-slash newline groups
 *
 *   - Function prototypes
 *     int  foo();
 *     void bar();
 *
 *   - Preprocessors
 *     #define FOO_VAL        15
 *     #define MAX_TIMEOUT    60
 *     #define FOO(x)         ((x) * 65)
 *
 *   - typedefs
 *     typedef uint8_t     BYTE;
 *     typedef int32_t     INT32;
 *     typedef uint32_t    UINT32;
 */
void align_all(void)
{
   LOG_FUNC_ENTRY();

   log_rule_B("align_typedef_span");

   if (options::align_typedef_span() > 0)
   {
      align_typedefs(options::align_typedef_span());
   }
   log_rule_B("align_left_shift");

   if (options::align_left_shift())
   {
      align_left_shift();
   }
   log_rule_B("align_eigen_comma_init");

   if (options::align_eigen_comma_init())
   {
      align_eigen_comma_init();
   }
   log_rule_B("align_oc_msg_colon_span");

   if (options::align_oc_msg_colon_span() > 0)
   {
      align_oc_msg_colons();
   }
   // Align variable definitions
   log_rule_B("align_var_def_span");
   log_rule_B("align_var_struct_span");
   log_rule_B("align_var_class_span");

   if (  (options::align_var_def_span() > 0)
      || (options::align_var_struct_span() > 0)
      || (options::align_var_class_span() > 0))
   {
      align_var_def_brace(chunk_get_head(), options::align_var_def_span(), nullptr);
   }
   // Align assignments
   log_rule_B("align_enum_equ_span");
   log_rule_B("align_assign_span");
   log_rule_B("align_assign_thresh");

   if (  (options::align_enum_equ_span() > 0)
      || (options::align_assign_span() > 0))
   {
      align_assign(chunk_get_head(),
                   options::align_assign_span(),
                   options::align_assign_thresh(),
                   nullptr);
   }
   // Align structure initializers
   log_rule_B("align_struct_init_span");

   if (options::align_struct_init_span() > 0)
   {
      align_struct_initializers();
   }
   // Align function prototypes
   log_rule_B("align_func_proto_span");
   log_rule_B("align_mix_var_proto");

   if (  (options::align_func_proto_span() > 0)
      && !options::align_mix_var_proto())
   {
      align_func_proto(options::align_func_proto_span());
   }
   // Align function prototypes
   log_rule_B("align_oc_msg_spec_span");

   if (options::align_oc_msg_spec_span() > 0)
   {
      align_oc_msg_spec(options::align_oc_msg_spec_span());
   }
   // Align OC colons
   log_rule_B("align_oc_decl_colon");

   if (options::align_oc_decl_colon())
   {
      align_oc_decl_colon();
   }
   log_rule_B("align_asm_colon");

   if (options::align_asm_colon())
   {
      align_asm_colon();
   }
   // Align variable definitions in function prototypes
   log_rule_B("align_func_params");
   log_rule_B("align_func_params_span");

   if (  options::align_func_params()
      || options::align_func_params_span() > 0)
   {
      align_func_params();
   }
   log_rule_B("align_same_func_call_params");

   if (options::align_same_func_call_params())
   {
      align_same_func_call_params();
   }
   // Just in case something was aligned out of order... do it again
   quick_align_again();
} // align_all