Debian Codegraph @

kwsCheckUsingDirectives.cxx

raw
1 /*=========================================================================
2
3 Program: KWStyle - Kitware Style Checker
4 Module: kwsCheckUsingDirectives.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 #include <cctype>
16
17 namespace kws {
18
19 /** Check if the file contains "using namespace .* ;" */
20 bool Parser::CheckUsingDirectives(bool forbidUsingDirectives)
21 {
22 if(!forbidUsingDirectives)
23 return true;
24
25 bool hasError = false;
26
27 size_t usingPos = m_BufferNoComment.find("using");
28 while( usingPos != std::string::npos )
29 {
30 size_t tempIndex = usingPos + 5;//points right after "using"
31 if(this->IsBetweenQuote(usingPos, false))
32 {
33 usingPos = m_BufferNoComment.find("using", tempIndex);
34 continue;
35 }
36
37 //Munch whitespace (at least one character)
38 int whitespaceCharacterCount = 0;
39 while(tempIndex < m_BufferNoComment.size() && isspace(m_BufferNoComment[tempIndex]))
40 {
41 tempIndex++;
42 whitespaceCharacterCount++;
43 }
44 if( tempIndex >= m_BufferNoComment.size() || whitespaceCharacterCount < 1 )//file ended or not enough whitespace characters
45 return true;
46
47 //Munch "namespace"
48 if(m_BufferNoComment.substr(tempIndex, 9) != "namespace")
49 {
50 usingPos = m_BufferNoComment.find("using", tempIndex);
51 continue;
52 }
53 tempIndex += 9;//points right after "namespace"
54
55 //Munch whitespace (at least one character)
56 whitespaceCharacterCount = 0;
57 while(tempIndex < m_BufferNoComment.size() && isspace(m_BufferNoComment[tempIndex]))
58 {
59 tempIndex++;
60 whitespaceCharacterCount++;
61 }
62 if( tempIndex >= m_BufferNoComment.size() || whitespaceCharacterCount < 1 )//file ended or not enough whitespace characters
63 return true;
64
65 //Munch identifier
66 while(tempIndex < m_BufferNoComment.size() && (isalpha(m_BufferNoComment[tempIndex]) || m_BufferNoComment[tempIndex] == '_') )
67 tempIndex++;
68 if( tempIndex >= m_BufferNoComment.size() )//file ended
69 return true;
70
71 //Munch whitespace
72 while(tempIndex < m_BufferNoComment.size() && isspace(m_BufferNoComment[tempIndex]))
73 tempIndex++;
74 if( tempIndex >= m_BufferNoComment.size() )//file ended
75 return true;
76
77 if(m_BufferNoComment[tempIndex] == ';')//Match found for the full "using namespace [A-Za-z_]+;"
78 {
79 hasError = true;
80 Error error;
81 error.line = this->GetLineNumber(usingPos, true);
82 error.line2 = error.line;
83 error.description = "Using namespace directive used.";
84 error.number = USING_DIRECTIVES;
85 m_ErrorList.push_back(error);
86 }
87
88 usingPos = m_BufferNoComment.find("using", tempIndex);
89 }
90
91 return !hasError;
92 }
93
94 } // end namespace kws