summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.72.0/src/enum_cleanup.cpp
blob: f3d43eb56e9318707b3a71895c3c768fedeee8b9 (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
/**
 * @file enum_cleanup.cpp
 * works on the last comma withing enum
 *
 * @author  Guy Maurel Juli 2018
 * @license GPL v2+
 */

#include "enum_cleanup.h"

#include "chunk_list.h"
#include "log_rules.h"
#include "logger.h"
#include "uncrustify.h"
#include "uncrustify_types.h"

using namespace uncrustify;


void enum_cleanup(void)
{
   LOG_FUNC_ENTRY();

   log_rule_B("mod_enum_last_comma");

   if (options::mod_enum_last_comma() == IARF_IGNORE)
   {
      // nothing to do
      return;
   }
   chunk_t *pc = chunk_get_head();  // Issue #858

   while (pc != nullptr)
   {
      if (  get_chunk_parent_type(pc) == CT_ENUM
         && chunk_is_token(pc, CT_BRACE_CLOSE))
      {
         LOG_FMT(LTOK, "%s(%d): orig_line is %zu, type is %s\n",
                 __func__, __LINE__, pc->orig_line, get_token_name(pc->type));
         chunk_t *prev = chunk_get_prev_ncnlnp(pc);

         // test of (prev == nullptr) is not necessary
         if (chunk_is_token(prev, CT_COMMA))
         {
            log_rule_B("mod_enum_last_comma");

            if (options::mod_enum_last_comma() == IARF_REMOVE)
            {
               chunk_del(prev);
            }
         }
         else
         {
            if (chunk_is_token(prev, CT_BRACE_OPEN))                // Issue #2902
            {
               // nothing betwen CT_BRACE_OPEN and CT_BRACE_CLOSE
            }
            else
            {
               log_rule_B("mod_enum_last_comma");

               if (  options::mod_enum_last_comma() == IARF_ADD
                  || options::mod_enum_last_comma() == IARF_FORCE)
               {
                  // create a comma
                  chunk_t comma;
                  set_chunk_type(&comma, CT_COMMA);
                  comma.orig_line = prev->orig_line;
                  comma.orig_col  = prev->orig_col + 1;
                  comma.nl_count  = 0;
                  comma.flags     = PCF_NONE;
                  comma.str       = ",";
                  chunk_add_after(&comma, prev);
               }
            }
         }
      }
      pc = chunk_get_next(pc);
   }
} // enum_cleanup