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

#include "change_int_types.h"

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


using namespace uncrustify;


static bool is_storage_keyword(const Chunk *pc)
{
   return(  strcmp(pc->Text(), "auto") == 0
         || strcmp(pc->Text(), "const") == 0
         || strcmp(pc->Text(), "extern") == 0
         || strcmp(pc->Text(), "mutable") == 0
         || strcmp(pc->Text(), "register") == 0
         || strcmp(pc->Text(), "static") == 0
         || strcmp(pc->Text(), "thread_local") == 0
         || strcmp(pc->Text(), "typedef") == 0
         || strcmp(pc->Text(), "volatile") == 0
         || strcmp(pc->Text(), "_Atomic") == 0
         || strcmp(pc->Text(), "_Thread_local") == 0);
}


static bool is_non_integer(const Chunk *pc)
{
   return(  strcmp(pc->Text(), "char") == 0
         || strcmp(pc->Text(), "double") == 0);
}


static bool find_non_storage_siblings(const Chunk *pc, Chunk * &prev, Chunk * &next)
{
   prev = pc->GetPrevNcNnl();
   next = pc->GetNextNcNnl();

   // Find the last token that was not a storage keyword
   while (is_storage_keyword(prev))
   {
      prev = prev->GetPrevNcNnl();
   }

   // Return false if the last token indicates that this is not an integer type
   if (is_non_integer(prev))
   {
      return(false);
   }

   // Find the next token that is not a storage keyword
   while (is_storage_keyword(next))
   {
      next = next->GetNextNcNnl();
   }

   // Return false if the next token indicates that this is not an integer type
   if (is_non_integer(next))
   {
      return(false);
   }
   // Return true if this is indeed an integer type
   return(true);
}


static void add_or_remove_int_keyword(Chunk *pc, Chunk *sibling, iarf_e action, E_Direction dir, Chunk * &int_keyword)
{
   if (strcmp(sibling->Text(), "int") == 0)
   {
      if (action == IARF_REMOVE)
      {
         if (sibling == int_keyword)
         {
            int_keyword = Chunk::NullChunkPtr;
         }
         Chunk::Delete(sibling);
      }
      else if (  int_keyword->IsNotNullChunk()
              && int_keyword != sibling)
      {
         // We added an int keyword, but now we see that there already was one.
         // Keep one or the other but not both.
         if (options::mod_int_prefer_int_on_left())
         {
            Chunk::Delete(sibling);
         }
         else
         {
            Chunk::Delete(int_keyword);
            int_keyword = sibling;
         }
      }
      else
      {
         int_keyword = sibling;
      }
   }
   else
   {
      if (  action == IARF_ADD
         || action == IARF_FORCE)
      {
         if (int_keyword->IsNotNullChunk())
         {
            // There was already an int keyword. Either keep it and don't add a
            // new one or delete it to make way for the new one.
            if (options::mod_int_prefer_int_on_left())
            {
               return;
            }
            else
            {
               Chunk::Delete(int_keyword);
            }
         }

         if (dir == E_Direction::BACKWARD)
         {
            int_keyword = pc->CopyAndAddBefore(pc);
         }
         else
         {
            int_keyword = pc->CopyAndAddAfter(pc);
         }
         int_keyword->Str() = "int";
      }
   }
} // add_or_remove_int_keyword


void change_int_types()
{
   LOG_FUNC_ENTRY();

   Chunk *prev;
   Chunk *next;
   Chunk *int_keyword = Chunk::NullChunkPtr;

   for (Chunk *pc = Chunk::GetHead(); pc->IsNotNullChunk(); pc = pc->GetNextNcNnl())
   {
      if (strcmp(pc->Text(), "short") == 0)
      {
         if (find_non_storage_siblings(pc, prev, next))
         {
            add_or_remove_int_keyword(pc, prev, options::mod_int_short(), E_Direction::BACKWARD, int_keyword);
            add_or_remove_int_keyword(pc, next, options::mod_short_int(), E_Direction::FORWARD, int_keyword);
         }
      }
      else if (strcmp(pc->Text(), "long") == 0)
      {
         if (find_non_storage_siblings(pc, prev, next))
         {
            add_or_remove_int_keyword(pc, prev, options::mod_int_long(), E_Direction::BACKWARD, int_keyword);
            add_or_remove_int_keyword(pc, next, options::mod_long_int(), E_Direction::FORWARD, int_keyword);
         }
      }
      else if (strcmp(pc->Text(), "signed") == 0)
      {
         if (find_non_storage_siblings(pc, prev, next))
         {
            add_or_remove_int_keyword(pc, prev, options::mod_int_signed(), E_Direction::BACKWARD, int_keyword);
            add_or_remove_int_keyword(pc, next, options::mod_signed_int(), E_Direction::FORWARD, int_keyword);
         }
      }
      else if (strcmp(pc->Text(), "unsigned") == 0)
      {
         if (find_non_storage_siblings(pc, prev, next))
         {
            add_or_remove_int_keyword(pc, prev, options::mod_int_unsigned(), E_Direction::BACKWARD, int_keyword);
            add_or_remove_int_keyword(pc, next, options::mod_unsigned_int(), E_Direction::FORWARD, int_keyword);
         }
      }
      else if (  strcmp(pc->Text(), "int") != 0
              && !is_storage_keyword(pc))
      {
         int_keyword = Chunk::NullChunkPtr; // We are no longer in a variable declaration
      }
   }
} // change_int_types