Skip to content

Commit 719304a

Browse files
committed
Added internal check for || and | inside Token::Match patterns.
1 parent 62d0787 commit 719304a

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

lib/checkinternal.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ void CheckInternal::checkTokenMatchPatterns()
7777
}
7878
}
7979
}
80+
if (pattern.find("||") != std::string::npos || pattern.find(" | ") != std::string::npos || pattern[0] == '|' || (pattern[pattern.length() - 1] == '|' && pattern[pattern.length() - 2] == ' '))
81+
orInComplexPattern(tok, pattern, funcname);
8082

8183
// Check for signs of complex patterns
8284
if (pattern.find_first_of("[|%") != std::string::npos)
@@ -321,4 +323,10 @@ void CheckInternal::redundantNextPreviousError(const Token* tok, const std::stri
321323
"Call to 'Token::" + func1 + "()' followed by 'Token::" + func2 + "()' can be simplified.");
322324
}
323325

326+
void CheckInternal::orInComplexPattern(const Token* tok, const std::string& pattern, const std::string &funcname)
327+
{
328+
reportError(tok, Severity::error, "orInComplexPattern",
329+
"Token::" + funcname + "() pattern \"" + pattern + "\" contains \"||\" or \"|\". Replace it by \"%oror%\" or \"%or%\".");
330+
}
331+
324332
#endif // #ifdef CHECK_INTERNAL

lib/checkinternal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class CPPCHECKLIB CheckInternal : public Check {
7777
void missingPercentCharacterError(const Token *tok, const std::string &pattern, const std::string &funcname);
7878
void unknownPatternError(const Token* tok, const std::string& pattern);
7979
void redundantNextPreviousError(const Token* tok, const std::string& func1, const std::string& func2);
80+
void orInComplexPattern(const Token *tok, const std::string &pattern, const std::string &funcname);
8081

8182
void getErrorMessages(ErrorLogger *errorLogger, const Settings *settings) const {
8283
CheckInternal c(0, settings, errorLogger);
@@ -86,6 +87,7 @@ class CPPCHECKLIB CheckInternal : public Check {
8687
c.missingPercentCharacterError(0, "%num", "Match");
8788
c.unknownPatternError(0, "%typ");
8889
c.redundantNextPreviousError(0, "previous", "next");
90+
c.orInComplexPattern(0, "||", "Match");
8991
}
9092

9193
static std::string myName() {

test/testinternal.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestInternal : public TestFixture {
4141
TEST_CASE(redundantNextPrevious)
4242
TEST_CASE(internalError)
4343
TEST_CASE(invalidMultiCompare);
44+
TEST_CASE(orInComplexPattern);
4445
}
4546

4647
void check(const char code[]) {
@@ -342,6 +343,38 @@ class TestInternal : public TestFixture {
342343
"}");
343344
ASSERT_EQUALS("", errout.str());
344345
}
346+
347+
void orInComplexPattern() {
348+
check("void f() {\n"
349+
" Token::Match(tok, \"||\");\n"
350+
"}");
351+
ASSERT_EQUALS("[test.cpp:2]: (error) Token::Match() pattern \"||\" contains \"||\" or \"|\". Replace it by \"%oror%\" or \"%or%\".\n", errout.str());
352+
353+
check("void f() {\n"
354+
" Token::Match(tok, \"|\");\n"
355+
"}");
356+
ASSERT_EQUALS("[test.cpp:2]: (error) Token::Match() pattern \"|\" contains \"||\" or \"|\". Replace it by \"%oror%\" or \"%or%\".\n", errout.str());
357+
358+
check("void f() {\n"
359+
" Token::Match(tok, \"[|+-]\");\n"
360+
"}");
361+
ASSERT_EQUALS("", errout.str());
362+
363+
check("void f() {\n"
364+
" Token::Match(tok, \"foo | bar\");\n"
365+
"}");
366+
ASSERT_EQUALS("[test.cpp:2]: (error) Token::Match() pattern \"foo | bar\" contains \"||\" or \"|\". Replace it by \"%oror%\" or \"%or%\".\n", errout.str());
367+
368+
check("void f() {\n"
369+
" Token::Match(tok, \"foo |\");\n"
370+
"}");
371+
ASSERT_EQUALS("[test.cpp:2]: (error) Token::Match() pattern \"foo |\" contains \"||\" or \"|\". Replace it by \"%oror%\" or \"%or%\".\n", errout.str());
372+
373+
check("void f() {\n"
374+
" Token::Match(tok, \"bar foo|\");\n"
375+
"}");
376+
ASSERT_EQUALS("", errout.str());
377+
}
345378
};
346379

347380
REGISTER_TEST(TestInternal)

0 commit comments

Comments
 (0)