Skip to content

Commit 3fb6a01

Browse files
committed
Merge branch 'main' into chr_13218
2 parents 070ff0d + aa5262d commit 3fb6a01

File tree

7 files changed

+70
-4
lines changed

7 files changed

+70
-4
lines changed

lib/checkclass.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,9 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
856856
if (!Token::Match(ftok->next(), "::| %name%") &&
857857
!Token::Match(ftok->next(), "*| this . %name%") &&
858858
!Token::Match(ftok->next(), "* %name% =") &&
859-
!Token::Match(ftok->next(), "( * this ) . %name%"))
859+
!Token::Match(ftok->next(), "( * this ) . %name%") &&
860+
!Token::Match(ftok->next(), "( * %name% ) =") &&
861+
!Token::Match(ftok->next(), "* ( %name% ) ="))
860862
continue;
861863

862864
// Goto the first token in this statement..
@@ -1060,6 +1062,10 @@ void CheckClass::initializeVarList(const Function &func, std::list<const Functio
10601062
// Assignment of array item of member variable?
10611063
else if (Token::Match(ftok, "* %name% =")) {
10621064
assignVar(usage, ftok->next()->varId());
1065+
} else if (Token::Match(ftok, "( * %name% ) =")) {
1066+
assignVar(usage, ftok->tokAt(2)->varId());
1067+
} else if (Token::Match(ftok, "* ( %name% ) =")) {
1068+
assignVar(usage, ftok->tokAt(2)->varId());
10631069
} else if (Token::Match(ftok, "* this . %name% =")) {
10641070
assignVar(usage, ftok->tokAt(3)->varId());
10651071
} else if (astIsRangeBasedForDecl(ftok)) {

lib/tokenize.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8530,6 +8530,9 @@ void Tokenizer::findGarbageCode() const
85308530
else if (Token::Match(tok, "[({<] %assign%"))
85318531
syntaxError(tok);
85328532

8533+
else if (Token::Match(tok, "%assign% >"))
8534+
syntaxError(tok);
8535+
85338536
else if (Token::Match(tok, "[`\\@]"))
85348537
syntaxError(tok);
85358538

lib/tokenlist.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,10 @@ void TokenList::validateAst(bool print) const
18851885
throw InternalError(tok, "Syntax Error: AST broken, binary operator '" + tok->str() + "' doesn't have two operands.", InternalError::AST);
18861886
}
18871887

1888+
if (Token::Match(tok, "++|--") && !tok->astOperand1()) {
1889+
throw InternalError(tok, "Syntax Error: AST broken, operator '" + tok->str() + "' doesn't have an operand.", InternalError::AST);
1890+
}
1891+
18881892
// Check control blocks and asserts
18891893
if (Token::Match(tok->previous(), "if|while|for|switch|assert|ASSERT (")) {
18901894
if (!tok->astOperand1() || !tok->astOperand2())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C(){o y=>::*}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assert(a~--)

test/testclass.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class TestClass : public TestFixture {
8080
TEST_CASE(operatorEqToSelf8); // ticket #2179
8181
TEST_CASE(operatorEqToSelf9); // ticket #2592
8282

83+
TEST_CASE(operatorEqPtrAssign); // ticket #13203
84+
8385
TEST_CASE(memsetOnStruct);
8486
TEST_CASE(memsetVector);
8587
TEST_CASE(memsetOnClass);
@@ -2589,6 +2591,55 @@ class TestClass : public TestFixture {
25892591
ASSERT_EQUALS("", errout_str());
25902592
}
25912593

2594+
#define checkConstructors(code) checkConstructors_(code, __FILE__, __LINE__)
2595+
template<size_t size>
2596+
void checkConstructors_(const char (&code)[size], const char* file, int line) {
2597+
const Settings settings = settingsBuilder().severity(Severity::warning).build();
2598+
2599+
// Tokenize..
2600+
SimpleTokenizer tokenizer(settings, *this);
2601+
ASSERT_LOC(tokenizer.tokenize(code), file, line);
2602+
2603+
// Check..
2604+
CheckClass checkClass(&tokenizer, &settings, this);
2605+
(checkClass.constructors)();
2606+
}
2607+
2608+
void operatorEqPtrAssign() { // ticket #13203
2609+
checkConstructors("struct S {\n"
2610+
" int* m_data;\n"
2611+
" S() : m_data(new int()) {}\n"
2612+
" S& operator=(const S& s) {\n"
2613+
" if (&s != this) {\n"
2614+
" *(m_data) = *(s.m_data);\n"
2615+
" }\n"
2616+
" return *this;\n"
2617+
" }\n"
2618+
"};\n");
2619+
ASSERT_EQUALS("", errout_str());
2620+
2621+
checkConstructors("struct S {\n"
2622+
" int* m_data;\n"
2623+
" S() : m_data(new int()) {}\n"
2624+
" S& operator=(const S& s) {\n"
2625+
" if (&s != this) {\n"
2626+
" (*m_data) = *(s.m_data);\n"
2627+
" }\n"
2628+
" return *this;\n"
2629+
" }\n"
2630+
"};\n");
2631+
ASSERT_EQUALS("", errout_str());
2632+
2633+
checkConstructors("struct S {\n"
2634+
" int* m_data;\n"
2635+
" S() : m_data(new int()) {}\n"
2636+
" S& operator=(const S& s) {\n"
2637+
" return *this;\n"
2638+
" }\n"
2639+
"};\n");
2640+
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'S::m_data' is not assigned a value in 'S::operator='.\n", errout_str());
2641+
}
2642+
25922643
// Check that base classes have virtual destructors
25932644
#define checkVirtualDestructor(...) checkVirtualDestructor_(__FILE__, __LINE__, __VA_ARGS__)
25942645
template<size_t size>

test/testpostfixoperator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ class TestPostfixOperator : public TestFixture {
305305
}
306306

307307
void test2168() {
308-
check("--> declare allocator lock here\n"
309-
"int main(){}");
310-
ASSERT_EQUALS("", errout_str());
308+
ASSERT_THROW_INTERNAL(check("--> declare allocator lock here\n"
309+
"int main(){}"),
310+
AST);
311311
}
312312

313313
void pointerSimplest() {

0 commit comments

Comments
 (0)