File: kwsCheckTemplate.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 (146 lines) | stat: -rw-r--r-- 3,853 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
/*=========================================================================

  Program:   KWStyle - Kitware Style Checker
  Module:    kwsCheckTemplate.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 template definition of the class follows
 *  a particular regular expression */
bool Parser::CheckTemplate(const char* regEx)
{
  m_TestsDone[TEMPLATE] = true;
  m_TestsDescription[TEMPLATE] = "Template should match the regex: ";
  m_TestsDescription[TEMPLATE] += regEx;

  kwssys::RegularExpression regex(regEx);

  bool hasErrors = false;
  // We check all the templates in the file
  // Maybe we should separate the main class from the templated function
  // at some point.

  long int templatePos = static_cast<long int>(m_BufferNoComment.find("template",0));
  while(templatePos != -1 ) 
    {
    bool valid = true;

    if(m_BufferNoComment[templatePos-1]!=' ' 
      && m_BufferNoComment[templatePos-1]!='\n')
      {
      valid = false;
      }
    else if(m_BufferNoComment[templatePos+8]!=' ' 
         && m_BufferNoComment[templatePos+8]!='\n')
      {
      valid = false;
      }

    // Definition is template <whatever name,whatever name2 = test, ...>
    long int inf = static_cast<long int>(m_BufferNoComment.find("<",templatePos));
    long int sup = static_cast<long int>(m_BufferNoComment.find(">",inf));

    if(inf == -1 || sup == -1)
      {
      //std::cout << "CheckTemplate(): There is a problem parsing the file" << std::endl;
      valid = false;
      }
    else
      {
      for(long int p=templatePos+8;p<inf;p++)
        {
        if(m_BufferNoComment[p]!=' ' && m_BufferNoComment[p]!='\n'
           && m_BufferNoComment[p]!='\r')
          {
          valid = false;
          break;
          }
        }
      }



    if(!valid)
      {
      templatePos = static_cast<long int>(m_BufferNoComment.find("template",templatePos+1));
      continue;
      }

    long int i = inf+1;
    bool inWord = false;
    bool afterEqual = false;
    int level = 0;
    std::string currentWord = "";

    while(i<=sup)
      {
      if(m_BufferNoComment[i] == '<')
        {
        level++;
        }

      // If we have a space
      if(m_BufferNoComment[i] == ' ')
        {
        // do nothing
        inWord = false;
        }
      else if(m_BufferNoComment[i] == ',' || m_BufferNoComment[i] == '>' || m_BufferNoComment[i] == '=')
        {
        inWord = false;

        if(currentWord.size()>0 && !afterEqual && !regex.find(currentWord) && level==0)
          {
          Error error;
          error.line = this->GetLineNumber(i,true);
          error.line2 = error.line;
          error.number = TEMPLATE;
          error.description = "Template definition (" + currentWord + ") doesn't match regular expression";
          m_ErrorList.push_back(error);
          hasErrors = true;
          }

        if(m_BufferNoComment[i] == '=')
          {
          afterEqual = true;
          }
        else
          {
          afterEqual = false;
          }
        }
      else
        {
        if(!inWord)
          {
          currentWord = "";
          }
        currentWord += m_BufferNoComment[i];
        inWord = true;
        }

       if(m_BufferNoComment[i] == '>')
        {
        level--;
        }
      i++;
      }

    templatePos = static_cast<long int>(m_BufferNoComment.find("template",templatePos+1));  
    }

  return !hasErrors;
}

} // end namespace kws