1 /**
2 * @file align.cpp
3 * Does all the aligning stuff.
4 *
5 * @author Ben Gardner
6 * @author Guy Maurel since version 0.62 for uncrustify4Qt
7 * October 2015, 2016
8 * @license GPL v2+
9 */
10
11 #include "align.h"
12
13 #include "align_asm_colon.h"
14 #include "align_assign.h"
15 #include "align_eigen_comma_init.h"
16 #include "align_func_params.h"
17 #include "align_func_proto.h"
18 #include "align_init_brace.h"
19 #include "align_left_shift.h"
20 #include "align_oc_decl_colon.h"
21 #include "align_oc_msg_colons.h"
22 #include "align_oc_msg_spec.h"
23 #include "align_preprocessor.h"
24 #include "align_same_func_call_params.h"
25 #include "align_stack.h"
26 #include "align_struct_initializers.h"
27 #include "align_trailing_comments.h"
28 #include "align_typedefs.h"
29 #include "align_var_def_brace.h"
30 #include "log_rules.h"
31 #include "quick_align_again.h"
32
33 constexpr static auto LCURRENT = LALIGN;
34
35 using namespace uncrustify;
36
37
38 /*
39 * Here are the items aligned:
40 *
41 * - enum value assignments
42 * enum {
43 * cat = 1,
44 * fred = 2,
45 * };
46 *
47 * - struct/union variable & bit definitions
48 * struct foo {
49 * char cat;
50 * int id : 5;
51 * int name_len : 6;
52 * int height : 12;
53 * };
54 *
55 * - variable definitions & assignments in normal code
56 * const char *cat = "feline";
57 * int id = 4;
58 * a = 5;
59 * bat = 14;
60 *
61 * - simple array initializers
62 * int a[] = {
63 * 1, 2, 3, 4, 5,
64 * 6, 7, 8, 9, 10
65 * };
66 *
67 * - c99 array initializers
68 * const char *name[] = {
69 * [FRED] = "fred",
70 * [JOE] = "joe",
71 * [PETER] = "peter",
72 * };
73 * struct foo b[] = {
74 * { .id = 1, .name = "text 1" },
75 * { .id = 567, .name = "text 2" },
76 * };
77 * struct foo_t bars[] =
78 * {
79 * [0] = { .name = "bar",
80 * .age = 21 },
81 * [1] = { .name = "barley",
82 * .age = 55 },
83 * };
84 *
85 * - compact array initializers
86 * struct foo b[] = {
87 * { 3, "dog" }, { 6, "spider" },
88 * { 8, "elephant" }, { 3, "cat" },
89 * };
90 *
91 * - multiline array initializers (2nd line indented, not aligned)
92 * struct foo b[] = {
93 * { AD_NOT_ALLOWED, "Sorry, you failed to guess the password.",
94 * "Try again?", "Yes", "No" },
95 * { AD_SW_ERROR, "A software error has occured.", "Bye!", NULL, NULL },
96 * };
97 *
98 * - Trailing comments
99 *
100 * - Back-slash newline groups
101 *
102 * - Function prototypes
103 * int foo();
104 * void bar();
105 *
106 * - Preprocessors
107 * #define FOO_VAL 15
108 * #define MAX_TIMEOUT 60
109 * #define FOO(x) ((x) * 65)
110 *
111 * - typedefs
112 * typedef uint8_t BYTE;
113 * typedef int32_t INT32;
114 * typedef uint32_t UINT32;
115 */
align_all(void)116 void align_all(void)
117 {
118 LOG_FUNC_ENTRY();
119
120 log_rule_B("align_typedef_span");
121
122 if (options::align_typedef_span() > 0)
123 {
124 align_typedefs(options::align_typedef_span());
125 }
126 log_rule_B("align_left_shift");
127
128 if (options::align_left_shift())
129 {
130 align_left_shift();
131 }
132 log_rule_B("align_eigen_comma_init");
133
134 if (options::align_eigen_comma_init())
135 {
136 align_eigen_comma_init();
137 }
138 log_rule_B("align_oc_msg_colon_span");
139
140 if (options::align_oc_msg_colon_span() > 0)
141 {
142 align_oc_msg_colons();
143 }
144 // Align variable definitions
145 log_rule_B("align_var_def_span");
146 log_rule_B("align_var_struct_span");
147 log_rule_B("align_var_class_span");
148
149 if ( (options::align_var_def_span() > 0)
150 || (options::align_var_struct_span() > 0)
151 || (options::align_var_class_span() > 0))
152 {
153 align_var_def_brace(chunk_get_head(), options::align_var_def_span(), nullptr);
154 }
155 // Align assignments
156 log_rule_B("align_enum_equ_span");
157 log_rule_B("align_assign_span");
158 log_rule_B("align_assign_thresh");
159
160 if ( (options::align_enum_equ_span() > 0)
161 || (options::align_assign_span() > 0))
162 {
163 align_assign(chunk_get_head(),
164 options::align_assign_span(),
165 options::align_assign_thresh(),
166 nullptr);
167 }
168 // Align structure initializers
169 log_rule_B("align_struct_init_span");
170
171 if (options::align_struct_init_span() > 0)
172 {
173 align_struct_initializers();
174 }
175 // Align function prototypes
176 log_rule_B("align_func_proto_span");
177 log_rule_B("align_mix_var_proto");
178
179 if ( (options::align_func_proto_span() > 0)
180 && !options::align_mix_var_proto())
181 {
182 align_func_proto(options::align_func_proto_span());
183 }
184 // Align function prototypes
185 log_rule_B("align_oc_msg_spec_span");
186
187 if (options::align_oc_msg_spec_span() > 0)
188 {
189 align_oc_msg_spec(options::align_oc_msg_spec_span());
190 }
191 // Align OC colons
192 log_rule_B("align_oc_decl_colon");
193
194 if (options::align_oc_decl_colon())
195 {
196 align_oc_decl_colon();
197 }
198 log_rule_B("align_asm_colon");
199
200 if (options::align_asm_colon())
201 {
202 align_asm_colon();
203 }
204 // Align variable definitions in function prototypes
205 log_rule_B("align_func_params");
206 log_rule_B("align_func_params_span");
207
208 if ( options::align_func_params()
209 || options::align_func_params_span() > 0)
210 {
211 align_func_params();
212 }
213 log_rule_B("align_same_func_call_params");
214
215 if (options::align_same_func_call_params())
216 {
217 align_same_func_call_params();
218 }
219 // Just in case something was aligned out of order... do it again
220 quick_align_again();
221 } // align_all
222