MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkTokenMatchPatterns

Method checkTokenMatchPatterns

lib/checkinternal.cpp:34–83  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

32#include <vector>
33
34void CheckInternalImpl::checkTokenMatchPatterns()
35{
36 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
37 for (const Scope *scope : symbolDatabase->functionScopes) {
38 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
39 if (!Token::simpleMatch(tok, "Token :: Match (") && !Token::simpleMatch(tok, "Token :: findmatch ("))
40 continue;
41
42 const std::string& funcname = tok->strAt(2);
43
44 // Get pattern string
45 const Token *patternTok = tok->tokAt(4)->nextArgument();
46 if (!patternTok || patternTok->tokType() != Token::eString)
47 continue;
48
49 const std::string pattern = patternTok->strValue();
50 if (pattern.empty()) {
51 simplePatternError(tok, pattern, funcname);
52 continue;
53 }
54
55 if (pattern.find("||") != std::string::npos || pattern.find(" | ") != std::string::npos || pattern[0] == '|' || (pattern[pattern.length() - 1] == '|' && pattern[pattern.length() - 2] == ' '))
56 orInComplexPattern(tok, pattern, funcname);
57
58 // Check for signs of complex patterns
59 if (pattern.find_first_of("[|") != std::string::npos)
60 continue;
61 if (pattern.find("!!") != std::string::npos)
62 continue;
63
64 bool complex = false;
65 size_t index = pattern.find('%');
66 while (index != std::string::npos) {
67 if (pattern.length() <= index + 2) {
68 complex = true;
69 break;
70 }
71 if (pattern[index + 1] == 'o' && pattern[index + 2] == 'r') // %or% or %oror%
72 index = pattern.find('%', index + 1);
73 else {
74 complex = true;
75 break;
76 }
77 index = pattern.find('%', index + 1);
78 }
79 if (!complex)
80 simplePatternError(tok, pattern, funcname);
81 }
82 }
83}
84
85void CheckInternalImpl::checkRedundantTokCheck()
86{

Callers 1

runChecksMethod · 0.80

Calls 7

nextMethod · 0.80
simpleMatchFunction · 0.70
nextArgumentMethod · 0.45
tokAtMethod · 0.45
strValueMethod · 0.45
emptyMethod · 0.45
findMethod · 0.45

Tested by

no test coverage detected