diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5fbca28aa..89dd7adc5 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2154,6 +2154,9 @@ bool Tokenizer::tokenize(std::istream &code, // remove Microsoft MFC.. simplifyMicrosoftMFC(); + // Remove Qt signals and slots + simplifyQtSignalsSlots(); + // remove Borland stuff.. simplifyBorland(); @@ -8714,4 +8717,46 @@ void Tokenizer::simplifyBorland() } } +// Remove Qt signals and slots +void Tokenizer::simplifyQtSignalsSlots() +{ + Token *tok = _tokens; + while ((tok = const_cast(Token::findmatch(tok, "class %var% :")))) + { + unsigned int indentlevel = 0; + for (Token *tok2 = tok; tok2; tok2 = tok2->next()) + { + if (tok2->str() == "{") + { + indentlevel++; + if (indentlevel == 1) + tok = tok2; + else + tok2 = tok2->link(); + } + else if (tok2->str() == "}") + { + indentlevel--; + if (indentlevel == 0) + break; + } + if (Token::simpleMatch(tok2->next(), "Q_OBJECT")) + { + tok2->deleteNext(); + } + else if (Token::Match(tok2->next(), "public|protected|private slots :")) + { + tok2 = tok2->next(); + tok2->str(tok2->str() + ":"); + tok2->deleteNext(); + tok2->deleteNext(); + } + else if (Token::simpleMatch(tok2->next(), "signals :")) + { + tok2->deleteNext(); + tok2->deleteNext(); + } + } + } +} diff --git a/lib/tokenize.h b/lib/tokenize.h index 8605398ce..57924a5b4 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -489,6 +489,11 @@ public: */ void simplifyBorland(); + /** + * Remove Qt signals and slots + */ + void simplifyQtSignalsSlots(); + /** * This will return a short name describing function parameters * e.g. parameters: (int a, char b) should get name "int,char,". diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 9c72889de..a080894ed 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -265,6 +265,8 @@ private: TEST_CASE(borland); + TEST_CASE(Qt); + TEST_CASE(sql); TEST_CASE(simplifyLogicalOperators); @@ -4770,6 +4772,37 @@ private: tokenizeAndStringify("class Fred { __property int x = { } };", false)); } + void Qt() + { + const char code[] = "class Counter : public QObject " + "{ " + " Q_OBJECT " + "public: " + " Counter() { m_value = 0; } " + " int value() const { return m_value; } " + "public slots: " + " void setValue(int value); " + "signals: " + " void valueChanged(int newValue); " + "private: " + " int m_value; " + "};"; + + const char result [] = "class Counter : public QObject " + "{ " + "public: " + "Counter ( ) { m_value = 0 ; } " + "int value ( ) const { return m_value ; } " + "public: " + "void setValue ( int value ) ; " + "void valueChanged ( int newValue ) ; " + "private: " + "int m_value ; " + "} ;"; + + ASSERT_EQUALS(result, tokenizeAndStringify(code,false)); + } + void sql() { // Oracle PRO*C extensions for inline SQL. Just replace the SQL with "asm()" to fix wrong error messages