Debian Codegraph @

kwsCheckRelativePathInInclude.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
16 namespace kws {
17
18 /** Check if the file contains includes with paths that refer to parent directory (ie: #include "../folder/file.h" ) */
19 bool Parser::CheckRelativePathInInclude(bool forbidRelativePaths)
20 {
21 if(!forbidRelativePaths)
22 return true;
23
24 bool hasError = false;
25 size_t includePos = m_BufferNoComment.find("#include", 0);
26 while( (includePos = m_BufferNoComment.find("#include", includePos)) != std::string::npos)
27 {
28 if(this->IsBetweenQuote(includePos, false))
29 {
30 includePos += 8;
31 continue;
32 }
33
34 size_t newlinePos = m_BufferNoComment.find('\n', includePos);
35 size_t parentDirPos = m_BufferNoComment.find("..", includePos);
36
37 if(parentDirPos != std::string::npos &&
38 ( (newlinePos != std::string::npos && parentDirPos < newlinePos) || newlinePos == std::string::npos )
39 )
40 {
41 hasError = true;
42 Error error;
43 error.line = this->GetLineNumber(includePos, true);
44 error.line2 = error.line;
45 error.description = "#include refers to a parent directory in the path.";
46 error.number = RELATIVE_PATH_IN_INCLUDE;
47 m_ErrorList.push_back(error);
48 }
49 includePos += 8;
50 }
51 return !hasError;
52 }
53
54 } // end namespace kws