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

Method checkTokenSimpleMatchPatterns

lib/checkinternal.cpp:126–188  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

124}
125
126void CheckInternalImpl::checkTokenSimpleMatchPatterns()
127{
128 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
129 for (const Scope* scope : symbolDatabase->functionScopes) {
130 for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
131 if (!Token::simpleMatch(tok, "Token :: simpleMatch (") && !Token::simpleMatch(tok, "Token :: findsimplematch ("))
132 continue;
133
134 const std::string& funcname = tok->strAt(2);
135
136 // Get pattern string
137 const Token *patternTok = tok->tokAt(4)->nextArgument();
138 if (!patternTok || patternTok->tokType() != Token::eString)
139 continue;
140
141 const std::string pattern = patternTok->strValue();
142 if (pattern.empty()) {
143 complexPatternError(tok, pattern, funcname);
144 continue;
145 }
146
147 // Check for [xyz] usage - but exclude standalone square brackets
148 unsigned int char_count = 0;
149 for (const char c : pattern) {
150 if (c == ' ') {
151 char_count = 0;
152 } else if (c == ']') {
153 if (char_count > 0) {
154 complexPatternError(tok, pattern, funcname);
155 continue;
156 }
157 } else {
158 ++char_count;
159 }
160 }
161
162 // Check | usage: Count characters before the symbol
163 char_count = 0;
164 for (const char c : pattern) {
165 if (c == ' ') {
166 char_count = 0;
167 } else if (c == '|') {
168 if (char_count > 0) {
169 complexPatternError(tok, pattern, funcname);
170 continue;
171 }
172 } else {
173 ++char_count;
174 }
175 }
176
177 // Check for real errors
178 if (pattern.length() > 1) {
179 for (size_t j = 0; j < pattern.length() - 1; j++) {
180 if (pattern[j] == '%' && pattern[j + 1] != ' ')
181 complexPatternError(tok, pattern, funcname);
182 else if (pattern[j] == '!' && pattern[j + 1] == '!')
183 complexPatternError(tok, pattern, funcname);

Callers 1

runChecksMethod · 0.80

Calls 6

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

Tested by

no test coverage detected