summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.76.0/src/remove_duplicate_include.cpp
blob: e06029f1cbd315565b4bd8bd47e4cd52d660c46c (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
/**
 * @file remove_duplicate_include.cpp
 *
 * @author  Guy Maurel
 *          October 2015, 2016
 * @license GPL v2+
 */

#include "remove_duplicate_include.h"

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

using std::vector;


void remove_duplicate_include()
{
   LOG_FUNC_ENTRY();

   vector<Chunk *> includes;

   Chunk           *preproc = Chunk::NullChunkPtr;
   Chunk           *pc      = Chunk::GetHead();

   while (pc->IsNotNullChunk())
   {
      //LOG_FMT(LRMRETURN, "%s(%d): orig line is %zu, orig col is %zu, Text() is '%s', type is %s, parent type is %s\n",
      //        __func__, __LINE__, pc->GetOrigLine(), pc->GetOrigCol(), pc->Text(),
      //        get_token_name(pc->GetType()), get_token_name(pc->GetParentType()));

      if (pc->Is(CT_PREPROC))
      {
         preproc = pc;
      }
      else if (pc->Is(CT_PP_INCLUDE))
      {
         Chunk *next = pc->GetNext();

         //LOG_FMT(LRMRETURN, "%s(%d): orig line is %zu, orig col is %zu, Text() is '%s', type is %s, parent type is %s\n",
         //        __func__, __LINE__, next->GetOrigLine(), next->GetOrigCol(), next->Text(),
         //        get_token_name(next->GetType()), get_token_name(next->GetParentType()));
         if (includes.empty())
         {
            includes.push_back(next);
            // goto next newline
            pc = next->GetNextNl();
         }
         else
         {
            //LOG_FMT(LRMRETURN, "%s(%d): size is %zu\n",
            //        __func__, __LINE__, includes.size());
            // look for duplicate
            auto ite = includes.end();

            for (auto itc = includes.begin(); itc != ite; ++itc)
            {
               Chunk *current = *itc;

               //LOG_FMT(LRMRETURN, "%s(%d): next->Text()    is '%s'\n",
               //        __func__, __LINE__, next->Text());
               //LOG_FMT(LRMRETURN, "%s(%d): current->Text() is '%s'\n",
               //        __func__, __LINE__, current->Text());
               if (std::strcmp(next->Text(), current->Text()) == 0)
               {
                  // erase the statement
                  Chunk *temp    = pc;
                  Chunk *comment = next->GetNext();
                  Chunk *eol     = next->GetNextNl();
                  pc = preproc->GetPrev();
                  Chunk::Delete(preproc);
                  Chunk::Delete(temp);
                  Chunk::Delete(next);

                  if (comment != eol)
                  {
                     Chunk::Delete(comment);
                  }
                  Chunk::Delete(eol);
                  break;
               }
               else
               {
                  // goto next newline
                  pc = next->GetNextNl();
                  // and still look for duplicate
               }
            } // for (auto itc = includes.begin();
         } // if (includes.empty())
      } // else if (pc->Is(CT_PP_INCLUDE))
      // get the next token
      pc = pc->GetNext();
   }
} // remove_duplicate_include