diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 4dbd696f36a..c9e13d2e9e9 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -9004,6 +9004,9 @@ void Tokenizer::findGarbageCode() const else if (Token::Match(tok, "[`\\@]")) syntaxError(tok); + else if (Token::Match(tok, ".|->") && !Token::Match(tok->next(), "%name%|~|*|::") && !Token::simpleMatch(tok->previous(), "operator")) + syntaxError(tok, tok->str() + tok->strAt(1)); + // UNKNOWN_MACRO(return) if (tok->isKeyword() && Token::Match(tok, "throw|return )") && Token::Match(tok->linkAt(1)->previous(), "%name% (")) unknownMacroError(tok->linkAt(1)->previous()); diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index c798950b8e3..07570ff0630 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -870,7 +870,7 @@ class TestGarbage : public TestFixture { ASSERT_THROW_INTERNAL_EQUALS(checkCode("{ xs :: i(:) ! ! x/5 ! !\n" "i, :: a :: b integer, } foo2(x) :: j(:)\n" "b type(*), d(:), a x :: end d(..), foo end\n" - "foo4 b d(..), a a x type(*), b foo2 b\n"), SYNTAX, "syntax error"); + "foo4 b d(..), a a x type(*), b foo2 b\n"), SYNTAX, "syntax error: .."); } void garbageCode100() { // #6840 diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index dcd1534a84f..c256b99dcd8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -467,6 +467,7 @@ class TestTokenizer : public TestFixture { // Make sure the Tokenizer::findGarbageCode() does not have false positives // The TestGarbage ensures that there are true positives TEST_CASE(findGarbageCode); + TEST_CASE(memberAccessWithoutName); TEST_CASE(checkEnableIf); TEST_CASE(checkTemplates); TEST_CASE(checkNamespaces); @@ -8068,6 +8069,16 @@ class TestTokenizer : public TestFixture { ignore_errout(); } + void memberAccessWithoutName() { + const char code[] = "void f(void) {\n" + "\ti = x->(a, b);\n" + "}\n"; + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify(code, dinit(TokenizeOptions, $.cpp = false)), SYNTAX, "syntax error: ->("); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("void f(P *p) { p->1; }\n"), SYNTAX, "syntax error: ->1"); + ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("void f(P p) { p.; }\n"), SYNTAX, "syntax error: .;"); + ASSERT_NO_THROW(tokenizeAndStringify("void f(P *p) { p.operator->(); }\n")); + } + void findGarbageCode() { // Test Tokenizer::findGarbageCode() // C++ try/catch in global scope ASSERT_THROW_INTERNAL_EQUALS(tokenizeAndStringify("try { }\n"), SYNTAX, "syntax error: keyword 'try' is not allowed in global scope");