File: kwsCheckNameOfClass.cxx

package info (click to toggle)
kwstyle 1.0.1+git3224cf2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 45,404 kB
  • ctags: 145,645
  • sloc: cpp: 423,059; ansic: 9,347; xml: 588; sh: 102; php: 87; perl: 35; makefile: 7
file content (180 lines) | stat: -rw-r--r-- 4,560 bytes parent folder | download
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
/*=========================================================================

  Program:   KWStyle - Kitware Style Checker
  Module:    kwsCheckNameOfClass.cxx

  Copyright (c) Kitware, Inc.  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "kwsParser.h"

namespace kws {


/** Check if the name of the class 
 *  The class name is the one that is not between anything and does not have ;
 *  before the } */
bool Parser::CheckNameOfClass(const char* name,const char* prefix)
{
  m_TestsDone[NAMEOFCLASS] = true;
  m_TestsDescription[NAMEOFCLASS] = "The name of the class should match ";
  if(prefix)
    {
    m_TestsDescription[NAMEOFCLASS] += prefix;
    }
  m_TestsDescription[NAMEOFCLASS] += name;
  
  bool gotMatch = false;
  bool gotAtLeastOne = false;

  long int classpos = static_cast<long int>(this->GetClassPosition(0));
  std::string nameOfClass = "";
  while(classpos!=-1)
    {
    nameOfClass = this->FindPreviousWord(classpos);
        
    if(m_Filename == "")
      {
      std::cout << "CheckNameOfClass() : m_Filename shoud be set" << std::endl;
      return false;
      }

    long int point = static_cast<long int>(m_Filename.find_last_of("."));
    long int slash = static_cast<long int>(m_Filename.find_last_of("/"));

    if(slash == -1)
      {
      slash = 0;
      }

    std::string nameofclass = m_Filename.substr(slash+1,point-slash-1);
    std::string extension = m_Filename.substr(point+1,m_Filename.size()-point-1);

    // construct the string
    std::string toMatch = name;
    long int p = static_cast<long int>(toMatch.find("[NameOfClass]"));
    if(p != -1)
      {
      toMatch.replace(p,13,nameofclass);
      }
    p = static_cast<long int>(toMatch.find("[Extension]"));
    if(p != -1)
      {
      toMatch.replace(p,11,extension);
      }
        
    if(prefix)
      {
      nameOfClass = prefix+nameOfClass;
      }

    gotAtLeastOne = true;
    if(nameOfClass == toMatch)
      {
      gotMatch = true;
      break; 
      }
    classpos = static_cast<long int>(this->GetClassPosition(classpos+1));
    }

  if(!gotMatch && gotAtLeastOne && (classpos!=-1))
    {
    Error error;
    error.line = this->GetLineNumber(classpos,true);
    error.line2 = error.line;
    error.number = NAMEOFCLASS;
    error.description = "classname is not defined correctly";
    m_ErrorList.push_back(error);
    return false;
    }

  return true;
}


/** Returns the position of a class within the file.
 *  This function checks that we are in the class definition and not somewhere else 
 *  The return position is a position right after the name of the class (meaning before a : or a { 
 *  Returns std::string::npos if not found
 */
size_t Parser::GetClassPosition(size_t position,std::string buffer) const
{
  if(position == std::string::npos)
   {
   position = 0;
   } 
 
  if(buffer.size() == 0)
    {
    buffer = m_BufferNoComment;
    }

  size_t pos = buffer.find("class",position);

  std::string nameOfClass = "";
  while(pos!=std::string::npos)
    {
    if(!this->IsBetweenCharsFast('<','>',pos,false,buffer))
      {
      bool valid = true;      
      // We check that the word class is alone
      if(pos>1)
        {
        if(buffer[pos-1] != ' ' && buffer[pos-1] != '/' && buffer[pos-1] != '\n')
          {
          valid = false;
          }
        }
      if(pos<buffer.size()-2)
        {
        if(buffer[pos+5] != ' ' && buffer[pos+5] != '/' && buffer[pos+5] != '\r')
          {
          valid = false;
          } 
        }
      
      size_t i = pos+4;
      // We should get a { before a ;
      size_t brac = buffer.find('{',pos);
      size_t sem = buffer.find(';',pos);

      if(sem<=brac)
        {
        valid = false;
        }
      else
        {
        while((buffer[i] != '{')
          && (i<buffer.size())
          )
          {
          if(buffer[i] == ';')
            {
            valid = false;
            break;
            }
          else if(buffer[i] == ':')
            {
            break;
            }
          i++;
          }
        }
      
      if(valid)
        {
        return i;
        }
      }
    pos = buffer.find("class",pos+1);
    }

  return std::string::npos;
}

} // end namespace kws