Debian Codegraph @

kwsCheckTemplate.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckTemplate.cxx
5
6 Copyright (c) Kitware, Inc. All rights reserved.
7 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
8
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the above copyright notices for more information.
12
13 =========================================================================*/
14 #include "kwsParser.h"
15
16 namespace kws {
17
18
19 /** Check if the template definition of the class follows
20 * a particular regular expression */
21 bool Parser::CheckTemplate(const char* regEx)
22 {
23 m_TestsDone[TEMPLATE] = true;
24 m_TestsDescription[TEMPLATE] = "Template should match the regex: ";
25 m_TestsDescription[TEMPLATE] += regEx;
26
27 kwssys::RegularExpression regex(regEx);
28
29 bool hasErrors = false;
30 // We check all the templates in the file
31 // Maybe we should separate the main class from the templated function
32 // at some point.
33
34 auto templatePos =
35 static_cast<long int>(m_BufferNoComment.find("template", 0));
36 while(templatePos != -1 )
37 {
38 bool valid = true;
39
40 if(m_BufferNoComment[templatePos-1]!=' '
41 && m_BufferNoComment[templatePos-1]!='\n')
42 {
43 valid = false;
44 }
45 else if(m_BufferNoComment[templatePos+8]!=' '
46 && m_BufferNoComment[templatePos+8]!='\n')
47 {
48 valid = false;
49 }
50
51 // Definition is template <whatever name,whatever name2 = test, ...>
52 auto inf =
53 static_cast<long int>(m_BufferNoComment.find("<", templatePos));
54 auto sup = static_cast<long int>(m_BufferNoComment.find(">", inf));
55
56 if (inf == -1 || sup == -1) {
57 // std::cout << "CheckTemplate(): There is a problem parsing the file"
58 // << std::endl;
59 valid = false;
60 }
61 else
62 {
63 for(long int p=templatePos+8;p<inf;p++)
64 {
65 if(m_BufferNoComment[p]!=' ' && m_BufferNoComment[p]!='\n'
66 && m_BufferNoComment[p]!='\r')
67 {
68 valid = false;
69 break;
70 }
71 }
72 }
73
74
75
76 if(!valid)
77 {
78 templatePos = static_cast<long int>(m_BufferNoComment.find("template",templatePos+1));
79 continue;
80 }
81
82 long int i = inf+1;
83 bool inWord = false;
84 bool afterEqual = false;
85 int level = 0;
86 std::string currentWord = "";
87
88 while(i<=sup)
89 {
90 if(m_BufferNoComment[i] == '<')
91 {
92 level++;
93 }
94
95 // If we have a space
96 if(m_BufferNoComment[i] == ' ')
97 {
98 // do nothing
99 inWord = false;
100 }
101 else if(m_BufferNoComment[i] == ',' || m_BufferNoComment[i] == '>' || m_BufferNoComment[i] == '=')
102 {
103 inWord = false;
104
105 if (!currentWord.empty() && !afterEqual && !regex.find(currentWord) &&
106 level == 0) {
107 Error error;
108 error.line = this->GetLineNumber(i,true);
109 error.line2 = error.line;
110 error.number = TEMPLATE;
111 error.description = "Template definition (" + currentWord + ") doesn't match regular expression";
112 m_ErrorList.push_back(error);
113 hasErrors = true;
114 }
115
116 if(m_BufferNoComment[i] == '=')
117 {
118 afterEqual = true;
119 }
120 else
121 {
122 afterEqual = false;
123 }
124 }
125 else
126 {
127 if(!inWord)
128 {
129 currentWord = "";
130 }
131 currentWord += m_BufferNoComment[i];
132 inWord = true;
133 }
134
135 if(m_BufferNoComment[i] == '>')
136 {
137 level--;
138 }
139 i++;
140 }
141
142 templatePos = static_cast<long int>(m_BufferNoComment.find("template",templatePos+1));
143 }
144
145 return !hasErrors;
146 }
147
148 } // end namespace kws