summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.76.0/src/rewrite_infinite_loops.cpp
blob: 6b97ea7d1947fa0454d3dfc1289d4929977e6282 (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
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
/**
 * @file rewrite_infinite_loops.cpp
 *
 * @author  Alex Henrie
 * @license GPL v2+
 */

#include "rewrite_infinite_loops.h"

#include "chunk.h"
#include "newlines.h"
#include "uncrustify.h"


using namespace uncrustify;


static bool for_needs_rewrite(Chunk *pc, E_Token desired_type)
{
   // The 'for' statement needs to be rewritten if `for(;;)` is not the
   // preferred syntax for infinite loops and this 'for' is an infinite loop
   // with no extra tokens (such as inline comments).

   if (desired_type == CT_FOR)
   {
      return(false);
   }
   pc = pc->GetNext();

   if (!pc->Is(CT_SPAREN_OPEN))
   {
      return(false);
   }
   pc = pc->GetNext();

   if (!pc->Is(CT_SEMICOLON))
   {
      return(false);
   }
   pc = pc->GetNext();

   if (!pc->Is(CT_SEMICOLON))
   {
      return(false);
   }
   pc = pc->GetNext();

   if (!pc->Is(CT_SPAREN_CLOSE))
   {
      return(false);
   }
   return(true);
}


static bool while_needs_rewrite(Chunk *keyword, E_Token desired_type, const char *desired_condition)
{
   // The 'while' statement needs to be rewritten if it has only the tokens that
   // are strictly necessary (keyword, condition, two parentheses, and semicolon
   // if do-while) and either the keyword or the condition needs to be changed.

   Chunk *oparen    = keyword->GetNext();
   Chunk *condition = oparen->GetNext();
   Chunk *cparen    = condition->GetNext();

   if (!oparen->Is(CT_SPAREN_OPEN))
   {
      return(false);
   }

   if (  strcmp(condition->Text(), "true") != 0
      && strcmp(condition->Text(), "1") != 0)
   {
      return(false);
   }

   if (!cparen->Is(CT_SPAREN_CLOSE))
   {
      return(false);
   }

   if (keyword->Is(CT_WHILE_OF_DO))
   {
      Chunk *semicolon = cparen->GetNext();

      if (!semicolon->Is(CT_SEMICOLON))
      {
         return(false);
      }
   }

   if (!keyword->Is(desired_type))
   {
      return(true);
   }

   if (  strcmp(condition->Text(), "true") == 0
      && strcmp(desired_condition, "true") != 0)
   {
      return(true);
   }

   if (  strcmp(condition->Text(), "1") == 0
      && strcmp(desired_condition, "1") != 0)
   {
      return(true);
   }
   return(false);
} // while_needs_rewrite


void rewrite_loop_keyword(Chunk *keyword, E_Token new_type)
{
   keyword->SetType(new_type);

   switch (new_type)
   {
   case CT_DO:
      keyword->SetOrigColEnd(keyword->GetOrigColEnd() + strlen("do") - keyword->Len());
      keyword->Str() = "do";
      break;

   case CT_WHILE:
   case CT_WHILE_OF_DO:
      keyword->SetOrigColEnd(keyword->GetOrigColEnd() + strlen("while") - keyword->Len());
      keyword->Str() = "while";
      break;

   case CT_FOR:
      keyword->SetOrigColEnd(keyword->GetOrigColEnd() + strlen("for") - keyword->Len());
      keyword->Str() = "for";
      break;

   default:
      break;
   }
}


static void move_one_token(Chunk * &source, Chunk * &destination, E_Token parent_type)
{
   Chunk *next_source = source->GetNext();

   // Place the source token immediately after the destination token, without
   // any whitespace.

   source->MoveAfter(destination);
   source->SetColumn(destination->GetColumn() + destination->Len());
   source->SetOrigCol(destination->GetOrigCol() + destination->Len());
   source->SetOrigColEnd(source->GetOrigColEnd() + source->Len());
   source->SetOrigPrevSp(0);
   source->SetParentType(parent_type);

   destination = source;
   source      = next_source;
}


static void rewrite_loop_condition(Chunk * &source, Chunk * &destination,
                                   E_Token desired_type, const char *desired_condition)
{
   // Move the opening parenthesis
   move_one_token(source, destination, desired_type);

   // Move the condition
   if (desired_type == CT_FOR)
   {
      source->SetType(CT_SEMICOLON);
      source->SetParentType(CT_FOR);
      source->Str() = ";";
      move_one_token(source, destination, desired_type);
      destination = (destination)->CopyAndAddAfter(destination);
   }
   else
   {
      source->SetType(CT_WORD);
      source->Str() = desired_condition;
      move_one_token(source, destination, desired_type);
   }

   // If converting a 'for' to a 'while', delete the second semicolon
   if (source->Is(CT_SEMICOLON))
   {
      Chunk *next_source = source->GetNext();
      Chunk::Delete(source);
      source = next_source;
   }
   // Move the closing parenthesis
   move_one_token(source, destination, desired_type);
}


void rewrite_loop_in_place(Chunk *keyword, E_Token desired_type, const char *desired_condition)
{
   Chunk *top    = keyword->GetNext();
   Chunk *bottom = keyword;

   rewrite_loop_keyword(keyword, desired_type);
   rewrite_loop_condition(top, bottom, desired_type, desired_condition);
}


static Chunk *find_start_brace(Chunk *pc)
{
   while (!pc->IsBraceOpen())
   {
      pc = pc->GetNextNcNnl();
   }
   return(pc);
}


void rewrite_infinite_loops()
{
   LOG_FUNC_ENTRY();

   E_Token    desired_type;
   const char *desired_condition;

   switch (options::mod_infinite_loop())
   {
   case 1: // for(;;)
      desired_type      = CT_FOR;
      desired_condition = nullptr;
      break;

   case 2: // while(true)
      desired_type      = CT_WHILE;
      desired_condition = "true";
      break;

   case 3: // do...while(true)
      desired_type      = CT_WHILE_OF_DO;
      desired_condition = "true";
      break;

   case 4: // while(1)
      desired_type      = CT_WHILE;
      desired_condition = "1";
      break;

   case 5: // do...while(1)
      desired_type      = CT_WHILE_OF_DO;
      desired_condition = "1";
      break;

   default:
      return;
   }

   for (Chunk *pc = Chunk::GetHead(); pc->IsNotNullChunk(); pc = pc->GetNextNcNnl())
   {
      if (pc->Is(CT_DO))
      {
         Chunk *start_brace   = find_start_brace(pc);
         Chunk *end_brace     = start_brace->GetClosingParen();
         Chunk *while_keyword = end_brace->GetNextNcNnl();

         if (  !while_keyword->Is(CT_WHILE_OF_DO)
            || !while_needs_rewrite(while_keyword, desired_type, desired_condition))
         {
            continue;
         }

         if (desired_type == CT_WHILE_OF_DO)
         {
            // Change the loop condition
            rewrite_loop_in_place(while_keyword, desired_type, desired_condition);

            // Update the braces' parent types
            start_brace->SetParentType(CT_DO);
            end_brace->SetParentType(CT_DO);
         }
         else
         {
            Chunk *top    = pc;
            Chunk *bottom = while_keyword->GetNext();

            // Change the 'do' at the top of the loop to a 'for' or a 'while'
            rewrite_loop_keyword(top, desired_type);

            // Delete the 'while' at the bottom of the loop
            Chunk::Delete(while_keyword);

            // Move the rest of the tokens from the bottom to the top
            rewrite_loop_condition(bottom, top, desired_type, desired_condition);

            // Delete the final semicolon
            Chunk::Delete(bottom);

            // Update the braces' parent types
            start_brace->SetParentType(desired_type);
            end_brace->SetParentType(desired_type);
         }
      }
      else if (  (  pc->Is(CT_WHILE)
                 && while_needs_rewrite(pc, desired_type, desired_condition))
              || (  pc->Is(CT_FOR)
                 && for_needs_rewrite(pc, desired_type)))
      {
         Chunk *start_brace = find_start_brace(pc);
         Chunk *end_brace   = start_brace->GetClosingParen();

         if (desired_type == CT_WHILE_OF_DO)
         {
            Chunk *top    = pc;
            Chunk *bottom = end_brace;

            if (bottom->Is(CT_VBRACE_CLOSE))
            {
               // Insert a new line before the new 'while' keyword
               newline_add_before(bottom);
            }
            // Add a 'while' at the bottom of the loop
            bottom = top->CopyAndAddAfter(bottom);
            rewrite_loop_keyword(bottom, CT_WHILE_OF_DO);

            // Change the 'while' at the top of the loop to a 'do'
            rewrite_loop_keyword(top, CT_DO);
            top = top->GetNext();

            // Move the tokens from the top to the bottom
            rewrite_loop_condition(top, bottom, desired_type, desired_condition);

            // Add the final semicolon
            bottom = bottom->CopyAndAddAfter(bottom);
            bottom->SetType(CT_SEMICOLON);
            bottom->Str() = ";";

            // Update the braces' parent types
            start_brace->SetParentType(CT_DO);
            end_brace->SetParentType(CT_DO);
         }
         else
         {
            // Change 'for' to 'while' or vice-versa
            rewrite_loop_in_place(pc, desired_type, desired_condition);

            // Update the braces' parent types
            start_brace->SetParentType(desired_type);
            end_brace->SetParentType(desired_type);
         }
      }
   }
} // rewrite_infinite_loops