From affd0ffdfda4c55269fdcb00333f19820f2300a2 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Wed, 24 Sep 2014 13:23:44 +0200 Subject: [PATCH] Splitted large groups of tests out of testsimplifytokens.cpp and testtokenize.cpp --- test/testrunner.vcxproj | 3 + test/testrunner.vcxproj.filters | 9 + test/testsimplifytemplate.cpp | 1062 +++++++++ test/testsimplifytokens.cpp | 3924 ------------------------------- test/testsimplifytypedef.cpp | 2983 +++++++++++++++++++++++ test/testtokenize.cpp | 2331 +----------------- test/testvarid.cpp | 2383 +++++++++++++++++++ 7 files changed, 6442 insertions(+), 6253 deletions(-) create mode 100644 test/testsimplifytemplate.cpp create mode 100644 test/testsimplifytypedef.cpp create mode 100644 test/testvarid.cpp diff --git a/test/testrunner.vcxproj b/test/testrunner.vcxproj index 19ee8072b..672017f98 100644 --- a/test/testrunner.vcxproj +++ b/test/testrunner.vcxproj @@ -63,7 +63,9 @@ + + @@ -86,6 +88,7 @@ + diff --git a/test/testrunner.vcxproj.filters b/test/testrunner.vcxproj.filters index 85adeec95..e75af2a1d 100644 --- a/test/testrunner.vcxproj.filters +++ b/test/testrunner.vcxproj.filters @@ -190,6 +190,15 @@ Source Files + + Source Files + + + Source Files + + + Source Files + diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp new file mode 100644 index 000000000..30d6be6cc --- /dev/null +++ b/test/testsimplifytemplate.cpp @@ -0,0 +1,1062 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "testsuite.h" +#include "tokenize.h" +#include "token.h" +#include "settings.h" +#include "templatesimplifier.h" + +#include + +extern std::ostringstream errout; + + +class TestSimplifyTemplate : public TestFixture { +public: + TestSimplifyTemplate() : TestFixture("TestSimplifyTemplate") { + } + +private: + + void run() { + TEST_CASE(template1); + TEST_CASE(template2); + TEST_CASE(template3); + TEST_CASE(template4); + TEST_CASE(template5); + TEST_CASE(template6); + TEST_CASE(template7); + TEST_CASE(template8); + TEST_CASE(template9); + TEST_CASE(template10); + TEST_CASE(template11); + TEST_CASE(template12); + TEST_CASE(template13); + TEST_CASE(template14); + TEST_CASE(template15); + TEST_CASE(template16); + TEST_CASE(template17); + TEST_CASE(template18); + TEST_CASE(template19); + TEST_CASE(template20); + TEST_CASE(template21); + TEST_CASE(template22); + TEST_CASE(template23); + TEST_CASE(template24); // #2648 - using sizeof in template parameter + TEST_CASE(template25); // #2648 - another test for sizeof template parameter + TEST_CASE(template26); // #2721 - passing 'char[2]' as template parameter + TEST_CASE(template27); // #3350 - removing unused template in macro call + TEST_CASE(template28); + TEST_CASE(template29); // #3449 - don't crash for garbage code + TEST_CASE(template30); // #3529 - template < template < .. + TEST_CASE(template31); // #4010 - reference type + TEST_CASE(template32); // #3818 - mismatching template not handled well + TEST_CASE(template33); // #3818,#4544 - inner templates in template instantiation not handled well + TEST_CASE(template34); // #3706 - namespace => hang + TEST_CASE(template35); // #4074 - A<'x'> a; + TEST_CASE(template36); // #4310 - passing unknown template instantiation as template argument + TEST_CASE(template37); // #4544 - A a; + TEST_CASE(template38); // #4832 - crash on C++11 right angle brackets + TEST_CASE(template39); // #4742 - freeze + TEST_CASE(template40); // #5055 - template specialization outside struct + TEST_CASE(template41); // #4710 - const in instantiation not handled perfectly + TEST_CASE(template42); // #4878 - variadic templates + TEST_CASE(template43); // #5097 - assert due to '>>' not treated as end of template instantiation + TEST_CASE(template44); // #5297 - TemplateSimplifier::simplifyCalculations not eager enough + TEST_CASE(template45); // #5814 - syntax error reported for valid code + TEST_CASE(template46); // #5816 - syntax error reported for valid code + TEST_CASE(template47); // #6023 - syntax error reported for valid code + TEST_CASE(template48); // #6134 - 100% CPU upon invalid code + TEST_CASE(template_unhandled); + TEST_CASE(template_default_parameter); + TEST_CASE(template_default_type); + TEST_CASE(template_typename); + TEST_CASE(template_constructor); // #3152 - template constructor is removed + + // Test TemplateSimplifier::templateParameters + TEST_CASE(templateParameters); + TEST_CASE(templateParameters1); // #4169 - segmentation fault + } + + std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) { + errout.str(""); + + Settings settings; + settings.addEnabled("portability"); + settings.platform(type); + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + if (simplify) + tokenizer.simplifyTokenList2(); + + return tokenizer.tokens()->stringifyList(0, !simplify); + } + + std::string tok(const char code[], const char filename[]) { + errout.str(""); + + Settings settings; + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, filename); + tokenizer.simplifyTokenList2(); + + return tokenizer.tokens()->stringifyList(0, false); + } + + void template1() { + const char code[] = "template void f(T val) { T a; }\n" + "f(10);"; + + const std::string expected("f ( 10 ) ; " + "void f ( int val ) { }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template2() { + const char code[] = "template class Fred { T a; };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "class Fred { int a ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template3() { + const char code[] = "template class Fred { T data[sz]; };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "class Fred { float data [ 4 ] ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template4() { + const char code[] = "template class Fred { Fred(); };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "class Fred { Fred ( ) ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template5() { + const char code[] = "template class Fred { };\n" + "template Fred::Fred() { }\n" + "Fred fred;"; + + const std::string expected("template < classname T > Fred < T > :: Fred ( ) { } " // <- TODO: this should be removed + "Fred fred ; " + "class Fred { } ; " + "Fred :: Fred ( ) { }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template6() { + const char code[] = "template class Fred { };\n" + "Fred fred1;\n" + "Fred fred2;"; + + const std::string expected("Fred fred1 ; " + "Fred fred2 ; " + "class Fred { } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template7() { + // A template class that is not used => no simplification + { + const char code[] = "template \n" + "class ABC\n" + "{\n" + "public:\n" + " typedef ABC m;\n" + "};\n"; + + const std::string expected("template < class T > class ABC { public: } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "template class ABC {\n" + "public:\n" + " typedef std::vector type;\n" + "};\n" + "int main() {\n" + " ABC::type v;\n" + " v.push_back(4);\n" + " return 0;\n" + "}\n"; + + const std::string wanted("template < typename T > class ABC { public: } ; " + "int main ( ) { " + "std :: vector < int > v ; " + "v . push_back ( 4 ) ; " + "return 0 ; " + "}"); + + const std::string current("template < typename T > class ABC { public: } ; " + "int main ( ) { " + "ABC < int > :: type v ; " + "v . push_back ( 4 ) ; " + "return 0 ; " + "}"); + + TODO_ASSERT_EQUALS(wanted, current, tok(code)); + } + + { + const char code[] = "template class ABC {\n" + "public:\n" + " typedef std::vector type;\n" + " void f()\n" + " {\n" + " ABC::type v;\n" + " v.push_back(4);\n" + " }\n" + "};\n"; + + const std::string expected("template < typename T > class ABC { " + "public: void f ( ) { " + "ABC < int > :: type v ; " + "v . push_back ( 4 ) ; " + "} " + "} ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + } + + // Template definitions but no usage => no expansion + void template8() { + const char code[] = "template class A;\n" + "template class B;\n" + "\n" + "typedef A x;\n" + "typedef B y;\n" + "\n" + "template class A {\n" + " void f() {\n" + " B a = B::g();\n" + " T b = 0;\n" + " if (b)\n" + " b = 0;\n" + " }\n" + "};\n" + "\n" + "template inline B h() { return B(); }\n"; + + ASSERT_EQUALS("template < typename T > class A ; " + "template < typename T > class B ; " + "template < typename T > class A { void f ( ) { B < T > a ; a = B < T > :: g ( ) ; T b ; b = 0 ; } } ; " + "template < typename T > B < T > h ( ) { return B < T > ( ) ; }", tok(code)); + + ASSERT_EQUALS("class A { template < typename T > int foo ( T d ) ; } ;", tok("class A{ template int foo(T d);};")); + } + + void template9() { + const char code[] = "template < typename T > class A { } ;\n" + "\n" + "void f ( ) {\n" + " A a ;\n" + "}\n" + "\n" + "template < typename T >\n" + "class B {\n" + " void g ( ) {\n" + " A < T > b = A < T > :: h ( ) ;\n" + " }\n" + "} ;\n"; + + // The expected result.. + std::string expected("void f ( ) { A a ; } " + "template < typename T > class B { void g ( ) { A b ; b = A :: h ( ) ; } } ; " + "class A { } ; class A { } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template10() { + const char code[] = "template T * foo()\n" + "{ return new T[ui]; }\n" + "\n" + "void f ( )\n" + "{\n" + " foo<3,int>();\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " foo<3,int> ( ) ; " + "} " + "int * foo<3,int> ( ) { return new int [ 3 ] ; }"); + ASSERT_EQUALS(expected, tok(code)); + } + + void template11() { + const char code[] = "template T * foo()\n" + "{ return new T[ui]; }\n" + "\n" + "void f ( )\n" + "{\n" + " char * p = foo<3,char>();\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " char * p ; p = foo<3,char> ( ) ; " + "} " + "char * foo<3,char> ( ) { return new char [ 3 ] ; }"); + ASSERT_EQUALS(expected, tok(code)); + } + + void template12() { + const char code[] = "template \n" + "class A : public B\n" + "{ };\n" + "\n" + "void f()\n" + "{\n" + " A<12,12,11> a;\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " A<12,12,11> a ; " + "} " + "class A<12,12,11> : public B < 12 , 12 , 0 > " + "{ } ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + void template13() { + const char code[] = "class BB {};\n" + "\n" + "template \n" + "class AA\n" + "{\n" + "public:\n" + " static AA create(T* newObject);\n" + " static int size();\n" + "};\n" + "\n" + "class CC { public: CC(AA, int) {} };\n" + "\n" + "class XX {\n" + " AA y;\n" + "public:\n" + " XX();\n" + "};\n" + "\n" + "XX::XX():\n" + " y(AA::create(new CC(AA(), 0)))\n" + " {}\n" + "\n" + "int yy[AA::size()];"; + + // Just run it and check that there are not assertions. + tok(code); + } + + void template14() { + const char code[] = "template <> void foo()\n" + "{ x(); }\n" + "\n" + "int main()\n" + "{\n" + "foo();\n" + "}\n"; + + // The expected result.. + const std::string expected("void foo ( ) " + "{ x ( ) ; } " + "int main ( ) " + "{ foo ( ) ; }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template15() { + const char code[] = "template void a()\n" + "{\n" + " a();\n" + "}\n" + "\n" + "template <> void a<0>()\n" + "{ }\n" + "\n" + "int main()\n" + "{\n" + " a<2>();\n" + " return 0;\n" + "}\n"; + + // The expected result.. + const std::string expected("void a<0> ( ) { } " + "int main ( ) " + "{ a<2> ( ) ; return 0 ; } " + "void a<2> ( ) { a<1> ( ) ; } " + "void a<1> ( ) { a<0> ( ) ; }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template16() { + const char code[] = "template void a()\n" + "{ }\n" + "\n" + "template void b()\n" + "{ a(); }\n" + "\n" + "int main()\n" + "{\n" + " b<2>();\n" + " return 0;\n" + "}\n"; + + const std::string expected("int main ( ) { b<2> ( ) ; return 0 ; } " + "void b<2> ( ) { a<2> ( ) ; } " + "void a ( ) { } " + "void a<2> ( ) { }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template17() { + const char code[] = "template\n" + "class Fred\n" + "{\n" + " template\n" + " static shared_ptr< Fred > CreateFred()\n" + " {\n" + " }\n" + "};\n" + "\n" + "shared_ptr i;\n"; + + // Assert that there is no segmentation fault.. + tok(code); + } + + void template18() { + const char code[] = "template class foo { T a; };\n" + "foo *f;"; + + const std::string expected("foo * f ; " + "class foo { int a ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template19() { + const char code[] = "template T & foo()\n" + "{ static T temp; return temp; }\n" + "\n" + "void f ( )\n" + "{\n" + " char p = foo();\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " char p ; p = foo ( ) ; " + "} " + "char & foo ( ) { static char temp ; return temp ; }"); + ASSERT_EQUALS(expected, tok(code)); + } + + void template20() { + // Ticket #1788 - the destructor implementation is lost + const char code[] = "template class A\n" + "{\n" + "public:\n" + " ~A();\n" + "};\n" + "\n" + "template A::~A()\n" + "{\n" + "}\n" + "\n" + "A a;\n"; + + // The expected result.. + const std::string expected("template < class T > A < T > :: ~ A ( ) { } " // <- TODO: this should be removed + "A a ; " + "class A { public: ~ A ( ) ; } ; " + "A :: ~ A ( ) { }"); + ASSERT_EQUALS(expected, tok(code)); + } + + void template21() { + { + const char code[] = "template struct Fred { T a; };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "struct Fred { int a ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "template struct Fred { T data[sz]; };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "struct Fred { float data [ 4 ] ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "template struct Fred { Fred(); };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "struct Fred { Fred ( ) ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "template struct Fred { };\n" + "Fred fred1;\n" + "Fred fred2;"; + + const std::string expected("Fred fred1 ; " + "Fred fred2 ; " + "struct Fred { } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + } + + void template22() { + const char code[] = "template struct Fred { T a; };\n" + "Fred fred;"; + + const std::string expected("Fred fred ; " + "struct Fred { std :: string a ; } ;"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template23() { + const char code[] = "template void foo() { }\n" + "void bar() {\n" + " std::cout << (foo());\n" + "}"; + + const std::string expected("void bar ( ) {" + " std :: cout << ( foo ( ) ) ; " + "} " + "void foo ( ) { }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void template24() { + // #2648 + const char code[] = "template struct B\n" + "{\n" + " int a[n];\n" + "};\n" + "\n" + "template class bitset: B\n" + "{};\n" + "\n" + "bitset<1> z;"; + const char expected[] = "bitset<1> z ; " + "class bitset<1> : B<4> { } ; " + "struct B<4> { int a [ 4 ] ; } ;"; + ASSERT_EQUALS(expected, tok(code)); + } + + void template25() { + const char code[] = "template struct B\n" + "{\n" + " int a[n];\n" + "};\n" + "\n" + "template class bitset: B<((sizeof(int)) ? : 1)>\n" + "{};\n" + "\n" + "bitset<1> z;"; + + const char actual[] = "template < int n > struct B { int a [ n ] ; } ; " + "bitset<1> z ; " + "class bitset<1> : B < 4 > { } ;"; + + const char expected[] = "bitset<1> z ; " + "class bitset<1> : B<4> { } ; " + "struct B<4> { int a [ 4 ] ; } ;"; + + TODO_ASSERT_EQUALS(expected, actual, tok(code)); + + } + + void template26() { + // #2721 + const char code[] = "template\n" + "class A { public: T x; };\n" + "\n" + "template\n" + "class C: public A {};\n" + "\n" + "C<2> a;\n"; + // TODO: expand A also + ASSERT_EQUALS("template < class T > class A { public: T x ; } ; C<2> a ; class C<2> : public A < char [ 2 ] > { } ;", tok(code)); + } + + void template27() { + // #3350 - template inside macro call + const char code[] = "X(template class Fred);"; + ASSERT_EQUALS("X ( template < class T > class Fred ) ;", tok(code)); + } + + void template28() { + // #3226 - inner template + const char code[] = "template class Fred {};\n" + "Fred > x;\n"; + ASSERT_EQUALS("Fred> x ; class Fred { } ; class Fred> { } ;", tok(code)); + } + + void template29() { + // #3449 - garbage code (don't segfault) + const char code[] = "template struct A;\n" + "struct B { template struct C };\n" + "{};"; + ASSERT_EQUALS("template < typename T > struct A ; struct B { template < typename T > struct C } ; { } ;", tok(code)); + } + + void template30() { + // #3529 - template < template < .. + const char code[] = "template class A, class B> void f(){}"; + ASSERT_EQUALS("template < template < class > class A , class B > void f ( ) { }", tok(code)); + } + + void template31() { + // #4010 - template reference type + const char code[] = "template struct A{}; A a;"; + ASSERT_EQUALS("A a ; struct A { } ;", tok(code)); + } + + void template32() { + // #3818 - mismatching template not handled well + const char code[] = "template struct A { };\n" + "\n" + "template \n" + "struct B\n" + "{\n" + " public:\n" + " A < int, Pair, int > a;\n" // mismatching parameters => don't instantiate + "};\n" + "\n" + "B b;\n"; + ASSERT_EQUALS("template < class T1 , class T2 , class T3 , class T4 > struct A { } ; " + "B b ; " + "struct B { public: A < int , Pair < int , int > , int > a ; } ;", tok(code)); + } + + void template33() { + { + // #3818 - inner templates in template instantiation not handled well + const char code[] = "template struct A { };\n" + "template struct B { };\n" + "template struct C { A > > ab; };\n" + "C c;"; + ASSERT_EQUALS("C c ; " + "struct C { A>> ab ; } ; " + "struct B> { } ; " // <- redundant.. but nevermind + "struct A>> { } ; " // <- redundant.. but nevermind + "struct A>> { } ;", tok(code)); + } + + { + // #4544 + const char code[] = "struct A { };\n" + "template struct B { };\n" + "template struct C { };\n" + "C< B > c;"; + ASSERT_EQUALS("struct A { } ; " + "template < class T > struct B { } ; " // <- redundant.. but nevermind + "C> c ; struct C> { } ;", + tok(code)); + } + } + + void template34() { + // #3706 - namespace => hang + const char code[] = "namespace abc {\n" + "template struct X { void f(X &x) {} };\n" + "}\n" + "template <> int X::Y(0);"; + ASSERT_EQUALS("namespace abc { " + "template < typename T > struct X { void f ( X < T > & x ) { } } ; " + "} " + "template < > int X < int > :: Y ( 0 ) ;", tok(code)); + } + + void template35() { // #4074 - "A<'x'> a;" is not recognized as template instantiation + const char code[] = "template class A {};\n" + "A<'x'> a;"; + ASSERT_EQUALS("A<'x'> a ; class A<'x'> { } ;", tok(code)); + } + + void template36() { // #4310 - Passing unknown template instantiation as template argument + const char code[] = "template struct X { T t; };\n" + "template struct Y { Foo < X< Bar > > _foo; };\n" // <- Bar is unknown + "Y bar;"; + ASSERT_EQUALS("Y bar ; " + "struct Y { Foo < X> > _foo ; } ; " + "struct X> { Bar < int > t ; } ;", + tok(code)); + } + + void template37() { // #4544 - A a; + const char code[] = "class A { };\n" + "template class B {};\n" + "B b1;\n" + "B b2;"; + ASSERT_EQUALS("class A { } ; B b1 ; B b2 ; class B { } ;", + tok(code)); + } + + void template_unhandled() { + // An unhandled template usage should be simplified.. + ASSERT_EQUALS("x ( ) ;", tok("x();")); + } + + void template38() { // #4832 - Crash on C++11 right angle brackets + const char code[] = "template class A {\n" + " T mT;\n" + "public:\n" + " void foo() {}\n" + "};\n" + "\n" + "int main() {\n" + " A> gna1;\n" + " A gna2;\n" + "}\n"; + tok(code); // Don't crash or freeze + } + + void template39() { // #4742 - Used to freeze in 1.60 + const char code[] = "template struct vector {" + " operator T() const;" + "};" + "void f() {" + " vector> v;" + " const vector vi = static_cast>(v);" + "}"; + tok(code); + } + + void template40() { // #5055 - false negatives when there is template specialization outside struct + const char code[] = "struct A {" + " template struct X { T t; };" + "};" + "template<> struct A::X { int *t; };"; + ASSERT_EQUALS("struct A { template < typename T > struct X { T t ; } ; } ;", tok(code)); + } + + void template41() { // #4710 - const in template instantiation not handled perfectly + const char code1[] = "template struct X { };\n" + "void f(const X x) { }"; + ASSERT_EQUALS("void f ( const X x ) { } struct X { } ;", tok(code1)); + + const char code2[] = "template T f(T t) { return t; }\n" + "int x() { return f(123); }"; + ASSERT_EQUALS("int x ( ) { return f ( 123 ) ; } int f ( int t ) { return t ; }", tok(code2)); + } + + void template42() { // #4878 cpcheck aborts in ext-blocks.cpp (clang testcode) + const char code[] = "template\n" + "int f0(Args ...args) {\n" + " return ^ {\n" + " return sizeof...(Args);\n" + " }() + ^ {\n" + " return sizeof...(args);\n" + " }();\n" + "}"; + ASSERT_THROW(tok(code), InternalError); + } + + void template43() { // #5097 - Assert due to '>>' in 'B>' not being treated as end of template instantation + const char code[] = "template struct C { };" + "template struct D { static int f() { return C::f(); } };" + "template inline int f2() { return D::f(); }" + "template int f1(int x, T *) { int id = f2(); return id; }" + "template <> struct C < B < A >> {" + " static int f() {" + " return f1 < B < A >> (0, reinterpret_cast< B *>(E::Int(-1)));" + " }" + "};"; + tok(code); // Don't assert + } + + void template44() { // #5297 + tok("template struct StackContainer {" + " void foo(int i) {" + " if (0 >= 1 && i<0) {}" + " }" + "};" + "template class ZContainer : public StackContainer {};" + "struct FGSTensor {};" + "class FoldedZContainer : public ZContainer {};"); + } + + void template45() { // #5814 + tok("namespace Constants { const int fourtytwo = 42; } " + "template struct TypeMath { " + " static const int mult = sizeof(T) * U; " + "}; " + "template struct FOO { " + " enum { value = TypeMath::something }; " + "};"); + ASSERT_EQUALS("", errout.str()); + } + + void template46() { // #5816 + tok("template struct A { static const int value = 0; }; " + "template struct B { " + " enum { value = A::value }; " + "};"); + ASSERT_EQUALS("", errout.str()); + tok("template struct A {}; " + "enum { e = sizeof(A) }; " + "template struct B {};"); + ASSERT_EQUALS("", errout.str()); + tok("template struct A { static const int value = 0; }; " + "template struct B { typedef int type; }; " + "template struct C { " + " enum { value = A::type, int>::value }; " + "};"); + ASSERT_EQUALS("", errout.str()); + } + + void template47() { // #6023 + tok("template > class C1 {}; " + "class C2 : public C1 {};"); + ASSERT_EQUALS("", errout.str()); + } + + void template48() { // #6134 + tok("template int f( { } ); " + "int foo = f<1>(0);"); + ASSERT_EQUALS("", errout.str()); + } + + void template_default_parameter() { + { + const char code[] = "template \n" + "class A\n" + "{ T ar[n]; };\n" + "\n" + "void f()\n" + "{\n" + " A a1;\n" + " A a2;\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " A a1 ;" + " A a2 ; " + "} " + "class A " + "{ int ar [ 2 ] ; } ; " + "class A " + "{ int ar [ 3 ] ; } ;"); + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template \n" + "class A\n" + "{ T ar[n1+n2]; };\n" + "\n" + "void f()\n" + "{\n" + " A a1;\n" + " A a2;\n" + "}\n"; + + // The expected result.. + const std::string expected("void f ( ) " + "{" + " A a1 ;" + " A a2 ; " + "} " + "class A " + "{ int ar [ 5 ] ; } ;"); + ASSERT_EQUALS(expected, tok(code)); + } + { + const char code[] = "template \n" + "class A\n" + "{ T ar[n]; };\n" + "\n" + "void f()\n" + "{\n" + " A a1;\n" + " A a2;\n" + "}\n"; + + const std::string wanted("template < class T , int n >" + " class A" + " { T ar [ n ] ; } ;" + " void f ( )" + " {" + " A a1 ;" + " A a2 ;" + " }" + " class A" + " { int ar [ 2 ] ; }" + " class A" + " { int ar [ 3 ] ; }"); + + const std::string current("void f ( ) " + "{ " + "A < int , ( int ) 2 > a1 ; " + "A a2 ; " + "} " + "class A " + "{ int ar [ 3 ] ; } ;" + ); + TODO_ASSERT_EQUALS(wanted, current, tok(code)); + } + { + const char code[] = "template> class B {};\n" + "template> class C {};\n" + "template class D { };\n"; + ASSERT_EQUALS("template < class T , class T2 > class B { } ; " + "template < class B , typename C > class C { } ; " + "template < class B , typename C > class D { } ;", tok(code)); + } + } + + void template_default_type() { + const char code[] = "template \n" + "class A\n" + "{\n" + "public:\n" + " void foo() {\n" + " int a;\n" + " a = static_cast(a);\n" + " }\n" + "};\n" + "\n" + "template \n" + "class B\n" + "{\n" + "protected:\n" + " A a;\n" + "};\n" + "\n" + "class C\n" + " : public B\n" + "{\n" + "};\n"; + + tok(code); + + //ASSERT_EQUALS("[file1.cpp:15]: (error) Internal error: failed to instantiate template. The checking continues anyway.\n", errout.str()); + ASSERT_EQUALS("", errout.str()); + } + + void template_typename() { + { + const char code[] = "template \n" + "void foo(typename T::t *)\n" + "{ }"; + + // The expected result.. + const std::string expected("template < class T > void foo ( T :: t * ) { }"); + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "void f() {\n" + " x(sizeof typename);\n" + " type = 0;\n" + "}"; + + ASSERT_EQUALS("void f ( ) { x ( sizeof ( typename ) ) ; type = 0 ; }", tok(code)); + } + } + + void template_constructor() { + // #3152 - if template constructor is removed then there might be + // "no constructor" false positives + const char code[] = "class Fred {\n" + " template explicit Fred(T t) { }\n" + "}"; + ASSERT_EQUALS("class Fred { template < class T > explicit Fred ( T t ) { } }", tok(code)); + + // #3532 + const char code2[] = "class Fred {\n" + " template Fred(T t) { }\n" + "}"; + ASSERT_EQUALS("class Fred { template < class T > Fred ( T t ) { } }", tok(code2)); + } + + unsigned int templateParameters(const char code[]) { + Settings settings; + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp", "", true); + + return TemplateSimplifier::templateParameters(tokenizer.tokens()); + } + + void templateParameters() { + // Test that the function TemplateSimplifier::templateParameters works + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + ASSERT_EQUALS(1U, templateParameters(" x;")); + } + + void templateParameters1() { + // #4169 - segmentation fault (invalid code) + const char code[] = "volatile true , test < test < #ifdef __ppc__ true ,"; + // do not crash on invalid code + ASSERT_EQUALS(0, templateParameters(code)); + } +}; + +REGISTER_TEST(TestSimplifyTemplate) diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 761c39b6a..dc5261612 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -16,17 +16,12 @@ * along with this program. If not, see . */ - - #include "testsuite.h" #include "tokenize.h" #include "token.h" #include "settings.h" -#include "templatesimplifier.h" -#include "path.h" #include -#include extern std::ostringstream errout; @@ -91,64 +86,6 @@ private: TEST_CASE(strlen1); TEST_CASE(strlen2); - TEST_CASE(template1); - TEST_CASE(template2); - TEST_CASE(template3); - TEST_CASE(template4); - TEST_CASE(template5); - TEST_CASE(template6); - TEST_CASE(template7); - TEST_CASE(template8); - TEST_CASE(template9); - TEST_CASE(template10); - TEST_CASE(template11); - TEST_CASE(template12); - TEST_CASE(template13); - TEST_CASE(template14); - TEST_CASE(template15); - TEST_CASE(template16); - TEST_CASE(template17); - TEST_CASE(template18); - TEST_CASE(template19); - TEST_CASE(template20); - TEST_CASE(template21); - TEST_CASE(template22); - TEST_CASE(template23); - TEST_CASE(template24); // #2648 - using sizeof in template parameter - TEST_CASE(template25); // #2648 - another test for sizeof template parameter - TEST_CASE(template26); // #2721 - passing 'char[2]' as template parameter - TEST_CASE(template27); // #3350 - removing unused template in macro call - TEST_CASE(template28); - TEST_CASE(template29); // #3449 - don't crash for garbage code - TEST_CASE(template30); // #3529 - template < template < .. - TEST_CASE(template31); // #4010 - reference type - TEST_CASE(template32); // #3818 - mismatching template not handled well - TEST_CASE(template33); // #3818,#4544 - inner templates in template instantiation not handled well - TEST_CASE(template34); // #3706 - namespace => hang - TEST_CASE(template35); // #4074 - A<'x'> a; - TEST_CASE(template36); // #4310 - passing unknown template instantiation as template argument - TEST_CASE(template37); // #4544 - A a; - TEST_CASE(template38); // #4832 - crash on C++11 right angle brackets - TEST_CASE(template39); // #4742 - freeze - TEST_CASE(template40); // #5055 - template specialization outside struct - TEST_CASE(template41); // #4710 - const in instantiation not handled perfectly - TEST_CASE(template42); // #4878 - variadic templates - TEST_CASE(template43); // #5097 - assert due to '>>' not treated as end of template instantiation - TEST_CASE(template44); // #5297 - TemplateSimplifier::simplifyCalculations not eager enough - TEST_CASE(template45); // #5814 - syntax error reported for valid code - TEST_CASE(template46); // #5816 - syntax error reported for valid code - TEST_CASE(template47); // #6023 - syntax error reported for valid code - TEST_CASE(template48); // #6134 - 100% CPU upon invalid code - TEST_CASE(template_unhandled); - TEST_CASE(template_default_parameter); - TEST_CASE(template_default_type); - TEST_CASE(template_typename); - TEST_CASE(template_constructor); // #3152 - template constructor is removed - - // Test TemplateSimplifier::templateParameters - TEST_CASE(templateParameters); - TEST_CASE(templateParameters1); // #4169 - segmentation fault - TEST_CASE(namespaces); // Assignment in condition.. @@ -196,127 +133,6 @@ private: TEST_CASE(simplifyAtol) - TEST_CASE(simplifyTypedef1) - TEST_CASE(simplifyTypedef2) - TEST_CASE(simplifyTypedef3) - TEST_CASE(simplifyTypedef4) - TEST_CASE(simplifyTypedef5) - TEST_CASE(simplifyTypedef6) - TEST_CASE(simplifyTypedef7); - TEST_CASE(simplifyTypedef8); - TEST_CASE(simplifyTypedef9); - TEST_CASE(simplifyTypedef10); - TEST_CASE(simplifyTypedef11); - TEST_CASE(simplifyTypedef12); - TEST_CASE(simplifyTypedef13); - TEST_CASE(simplifyTypedef14); - TEST_CASE(simplifyTypedef15); - TEST_CASE(simplifyTypedef16); - TEST_CASE(simplifyTypedef17); - TEST_CASE(simplifyTypedef18); // typedef vector a; - TEST_CASE(simplifyTypedef19); - TEST_CASE(simplifyTypedef20); - TEST_CASE(simplifyTypedef21); - TEST_CASE(simplifyTypedef22); - TEST_CASE(simplifyTypedef23); - TEST_CASE(simplifyTypedef24); - TEST_CASE(simplifyTypedef25); - TEST_CASE(simplifyTypedef26); - TEST_CASE(simplifyTypedef27); - TEST_CASE(simplifyTypedef28); - TEST_CASE(simplifyTypedef29); - TEST_CASE(simplifyTypedef30); - TEST_CASE(simplifyTypedef31); - TEST_CASE(simplifyTypedef32); - TEST_CASE(simplifyTypedef33); - TEST_CASE(simplifyTypedef34); // ticket #1411 - TEST_CASE(simplifyTypedef35); - TEST_CASE(simplifyTypedef36); // ticket #1434 - TEST_CASE(simplifyTypedef37); // ticket #1449 - TEST_CASE(simplifyTypedef38); - TEST_CASE(simplifyTypedef39); - TEST_CASE(simplifyTypedef40); - TEST_CASE(simplifyTypedef41); // ticket #1488 - TEST_CASE(simplifyTypedef42); // ticket #1506 - TEST_CASE(simplifyTypedef43); // ticket #1588 - TEST_CASE(simplifyTypedef44); - TEST_CASE(simplifyTypedef45); // ticket #1613 - TEST_CASE(simplifyTypedef46); - TEST_CASE(simplifyTypedef47); - TEST_CASE(simplifyTypedef48); // ticket #1673 - TEST_CASE(simplifyTypedef49); // ticket #1691 - TEST_CASE(simplifyTypedef50); - TEST_CASE(simplifyTypedef51); - TEST_CASE(simplifyTypedef52); // ticket #1782 - TEST_CASE(simplifyTypedef53); // ticket #1801 - TEST_CASE(simplifyTypedef54); // ticket #1814 - TEST_CASE(simplifyTypedef55); - TEST_CASE(simplifyTypedef56); // ticket #1829 - TEST_CASE(simplifyTypedef57); // ticket #1846 - TEST_CASE(simplifyTypedef58); // ticket #1963 - TEST_CASE(simplifyTypedef59); // ticket #2011 - TEST_CASE(simplifyTypedef60); // ticket #2035 - TEST_CASE(simplifyTypedef61); // ticket #2074 and 2075 - TEST_CASE(simplifyTypedef62); // ticket #2082 - TEST_CASE(simplifyTypedef63); // ticket #2175 'typedef float x[3];' - TEST_CASE(simplifyTypedef64); - TEST_CASE(simplifyTypedef65); // ticket #2314 - TEST_CASE(simplifyTypedef66); // ticket #2341 - TEST_CASE(simplifyTypedef67); // ticket #2354 - TEST_CASE(simplifyTypedef68); // ticket #2355 - TEST_CASE(simplifyTypedef69); // ticket #2348 - TEST_CASE(simplifyTypedef70); // ticket #2348 - TEST_CASE(simplifyTypedef71); // ticket #2348 - TEST_CASE(simplifyTypedef72); // ticket #2375 - TEST_CASE(simplifyTypedef73); // ticket #2412 - TEST_CASE(simplifyTypedef74); // ticket #2414 - TEST_CASE(simplifyTypedef75); // ticket #2426 - TEST_CASE(simplifyTypedef76); // ticket #2453 - TEST_CASE(simplifyTypedef77); // ticket #2554 - TEST_CASE(simplifyTypedef78); // ticket #2568 - TEST_CASE(simplifyTypedef79); // ticket #2348 - TEST_CASE(simplifyTypedef80); // ticket #2587 - TEST_CASE(simplifyTypedef81); // ticket #2603 - TEST_CASE(simplifyTypedef82); // ticket #2403 - TEST_CASE(simplifyTypedef83); // ticket #2620 - TEST_CASE(simplifyTypedef84); // ticket #2630 - TEST_CASE(simplifyTypedef85); // ticket #2651 - TEST_CASE(simplifyTypedef86); // ticket #2581 - TEST_CASE(simplifyTypedef87); // ticket #2651 - TEST_CASE(simplifyTypedef88); // ticket #2675 - TEST_CASE(simplifyTypedef89); // ticket #2717 - TEST_CASE(simplifyTypedef90); // ticket #2718 - TEST_CASE(simplifyTypedef91); // ticket #2716 - TEST_CASE(simplifyTypedef92); // ticket #2736 - TEST_CASE(simplifyTypedef93); // ticket #2738 - TEST_CASE(simplifyTypedef94); // ticket #1982 - TEST_CASE(simplifyTypedef95); // ticket #2844 - TEST_CASE(simplifyTypedef96); // ticket #2886 - TEST_CASE(simplifyTypedef97); // ticket #2983 (segmentation fault) - TEST_CASE(simplifyTypedef98); // ticket #2963 - TEST_CASE(simplifyTypedef99); // ticket #2999 - TEST_CASE(simplifyTypedef100); // ticket #3000 - TEST_CASE(simplifyTypedef101); // ticket #3003 (segmentation fault) - TEST_CASE(simplifyTypedef102); // ticket #3004 - TEST_CASE(simplifyTypedef103); // ticket #3007 - TEST_CASE(simplifyTypedef104); // ticket #3070 - TEST_CASE(simplifyTypedef105); // ticket #3616 - TEST_CASE(simplifyTypedef106); // ticket #3619 - TEST_CASE(simplifyTypedef107); // ticket #3963 - bad code => segmentation fault - TEST_CASE(simplifyTypedef108); // ticket #4777 - TEST_CASE(simplifyTypedef109); // ticket #1823 - rvalue reference - - TEST_CASE(simplifyTypedefFunction1); - TEST_CASE(simplifyTypedefFunction2); // ticket #1685 - TEST_CASE(simplifyTypedefFunction3); - TEST_CASE(simplifyTypedefFunction4); - TEST_CASE(simplifyTypedefFunction5); - TEST_CASE(simplifyTypedefFunction6); - TEST_CASE(simplifyTypedefFunction7); - TEST_CASE(simplifyTypedefFunction8); - - TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable - TEST_CASE(simplifyOperator1); TEST_CASE(reverseArraySyntax) @@ -1681,941 +1497,6 @@ private: } - - void template1() { - const char code[] = "template void f(T val) { T a; }\n" - "f(10);"; - - const std::string expected("f ( 10 ) ; " - "void f ( int val ) { }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template2() { - const char code[] = "template class Fred { T a; };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "class Fred { int a ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template3() { - const char code[] = "template class Fred { T data[sz]; };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "class Fred { float data [ 4 ] ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template4() { - const char code[] = "template class Fred { Fred(); };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "class Fred { Fred ( ) ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template5() { - const char code[] = "template class Fred { };\n" - "template Fred::Fred() { }\n" - "Fred fred;"; - - const std::string expected("template < classname T > Fred < T > :: Fred ( ) { } " // <- TODO: this should be removed - "Fred fred ; " - "class Fred { } ; " - "Fred :: Fred ( ) { }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template6() { - const char code[] = "template class Fred { };\n" - "Fred fred1;\n" - "Fred fred2;"; - - const std::string expected("Fred fred1 ; " - "Fred fred2 ; " - "class Fred { } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template7() { - // A template class that is not used => no simplification - { - const char code[] = "template \n" - "class ABC\n" - "{\n" - "public:\n" - " typedef ABC m;\n" - "};\n"; - - const std::string expected("template < class T > class ABC { public: } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "template class ABC {\n" - "public:\n" - " typedef std::vector type;\n" - "};\n" - "int main() {\n" - " ABC::type v;\n" - " v.push_back(4);\n" - " return 0;\n" - "}\n"; - - const std::string wanted("template < typename T > class ABC { public: } ; " - "int main ( ) { " - "std :: vector < int > v ; " - "v . push_back ( 4 ) ; " - "return 0 ; " - "}"); - - const std::string current("template < typename T > class ABC { public: } ; " - "int main ( ) { " - "ABC < int > :: type v ; " - "v . push_back ( 4 ) ; " - "return 0 ; " - "}"); - - TODO_ASSERT_EQUALS(wanted, current, tok(code)); - } - - { - const char code[] = "template class ABC {\n" - "public:\n" - " typedef std::vector type;\n" - " void f()\n" - " {\n" - " ABC::type v;\n" - " v.push_back(4);\n" - " }\n" - "};\n"; - - const std::string expected("template < typename T > class ABC { " - "public: void f ( ) { " - "ABC < int > :: type v ; " - "v . push_back ( 4 ) ; " - "} " - "} ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - } - - // Template definitions but no usage => no expansion - void template8() { - const char code[] = "template class A;\n" - "template class B;\n" - "\n" - "typedef A x;\n" - "typedef B y;\n" - "\n" - "template class A {\n" - " void f() {\n" - " B a = B::g();\n" - " T b = 0;\n" - " if (b)\n" - " b = 0;\n" - " }\n" - "};\n" - "\n" - "template inline B h() { return B(); }\n"; - - ASSERT_EQUALS("template < typename T > class A ; " - "template < typename T > class B ; " - "template < typename T > class A { void f ( ) { B < T > a ; a = B < T > :: g ( ) ; T b ; b = 0 ; } } ; " - "template < typename T > B < T > h ( ) { return B < T > ( ) ; }", tok(code)); - - ASSERT_EQUALS("class A { template < typename T > int foo ( T d ) ; } ;", tok("class A{ template int foo(T d);};")); - } - - void template9() { - const char code[] = "template < typename T > class A { } ;\n" - "\n" - "void f ( ) {\n" - " A a ;\n" - "}\n" - "\n" - "template < typename T >\n" - "class B {\n" - " void g ( ) {\n" - " A < T > b = A < T > :: h ( ) ;\n" - " }\n" - "} ;\n"; - - // The expected result.. - std::string expected("void f ( ) { A a ; } " - "template < typename T > class B { void g ( ) { A b ; b = A :: h ( ) ; } } ; " - "class A { } ; class A { } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template10() { - const char code[] = "template T * foo()\n" - "{ return new T[ui]; }\n" - "\n" - "void f ( )\n" - "{\n" - " foo<3,int>();\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " foo<3,int> ( ) ; " - "} " - "int * foo<3,int> ( ) { return new int [ 3 ] ; }"); - ASSERT_EQUALS(expected, tok(code)); - } - - void template11() { - const char code[] = "template T * foo()\n" - "{ return new T[ui]; }\n" - "\n" - "void f ( )\n" - "{\n" - " char * p = foo<3,char>();\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " char * p ; p = foo<3,char> ( ) ; " - "} " - "char * foo<3,char> ( ) { return new char [ 3 ] ; }"); - ASSERT_EQUALS(expected, tok(code)); - } - - void template12() { - const char code[] = "template \n" - "class A : public B\n" - "{ };\n" - "\n" - "void f()\n" - "{\n" - " A<12,12,11> a;\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " A<12,12,11> a ; " - "} " - "class A<12,12,11> : public B < 12 , 12 , 0 > " - "{ } ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - void template13() { - const char code[] = "class BB {};\n" - "\n" - "template \n" - "class AA\n" - "{\n" - "public:\n" - " static AA create(T* newObject);\n" - " static int size();\n" - "};\n" - "\n" - "class CC { public: CC(AA, int) {} };\n" - "\n" - "class XX {\n" - " AA y;\n" - "public:\n" - " XX();\n" - "};\n" - "\n" - "XX::XX():\n" - " y(AA::create(new CC(AA(), 0)))\n" - " {}\n" - "\n" - "int yy[AA::size()];"; - - // Just run it and check that there are not assertions. - tok(code); - } - - void template14() { - const char code[] = "template <> void foo()\n" - "{ x(); }\n" - "\n" - "int main()\n" - "{\n" - "foo();\n" - "}\n"; - - // The expected result.. - const std::string expected("void foo ( ) " - "{ x ( ) ; } " - "int main ( ) " - "{ foo ( ) ; }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template15() { - const char code[] = "template void a()\n" - "{\n" - " a();\n" - "}\n" - "\n" - "template <> void a<0>()\n" - "{ }\n" - "\n" - "int main()\n" - "{\n" - " a<2>();\n" - " return 0;\n" - "}\n"; - - // The expected result.. - const std::string expected("void a<0> ( ) { } " - "int main ( ) " - "{ a<2> ( ) ; return 0 ; } " - "void a<2> ( ) { a<1> ( ) ; } " - "void a<1> ( ) { a<0> ( ) ; }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template16() { - const char code[] = "template void a()\n" - "{ }\n" - "\n" - "template void b()\n" - "{ a(); }\n" - "\n" - "int main()\n" - "{\n" - " b<2>();\n" - " return 0;\n" - "}\n"; - - const std::string expected("int main ( ) { b<2> ( ) ; return 0 ; } " - "void b<2> ( ) { a<2> ( ) ; } " - "void a ( ) { } " - "void a<2> ( ) { }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template17() { - const char code[] = "template\n" - "class Fred\n" - "{\n" - " template\n" - " static shared_ptr< Fred > CreateFred()\n" - " {\n" - " }\n" - "};\n" - "\n" - "shared_ptr i;\n"; - - // Assert that there is no segmentation fault.. - tok(code); - } - - void template18() { - const char code[] = "template class foo { T a; };\n" - "foo *f;"; - - const std::string expected("foo * f ; " - "class foo { int a ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template19() { - const char code[] = "template T & foo()\n" - "{ static T temp; return temp; }\n" - "\n" - "void f ( )\n" - "{\n" - " char p = foo();\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " char p ; p = foo ( ) ; " - "} " - "char & foo ( ) { static char temp ; return temp ; }"); - ASSERT_EQUALS(expected, tok(code)); - } - - void template20() { - // Ticket #1788 - the destructor implementation is lost - const char code[] = "template class A\n" - "{\n" - "public:\n" - " ~A();\n" - "};\n" - "\n" - "template A::~A()\n" - "{\n" - "}\n" - "\n" - "A a;\n"; - - // The expected result.. - const std::string expected("template < class T > A < T > :: ~ A ( ) { } " // <- TODO: this should be removed - "A a ; " - "class A { public: ~ A ( ) ; } ; " - "A :: ~ A ( ) { }"); - ASSERT_EQUALS(expected, tok(code)); - } - - void template21() { - { - const char code[] = "template struct Fred { T a; };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "struct Fred { int a ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "template struct Fred { T data[sz]; };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "struct Fred { float data [ 4 ] ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "template struct Fred { Fred(); };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "struct Fred { Fred ( ) ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "template struct Fred { };\n" - "Fred fred1;\n" - "Fred fred2;"; - - const std::string expected("Fred fred1 ; " - "Fred fred2 ; " - "struct Fred { } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - } - - void template22() { - const char code[] = "template struct Fred { T a; };\n" - "Fred fred;"; - - const std::string expected("Fred fred ; " - "struct Fred { std :: string a ; } ;"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template23() { - const char code[] = "template void foo() { }\n" - "void bar() {\n" - " std::cout << (foo());\n" - "}"; - - const std::string expected("void bar ( ) {" - " std :: cout << ( foo ( ) ) ; " - "} " - "void foo ( ) { }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void template24() { - // #2648 - const char code[] = "template struct B\n" - "{\n" - " int a[n];\n" - "};\n" - "\n" - "template class bitset: B\n" - "{};\n" - "\n" - "bitset<1> z;"; - const char expected[] = "bitset<1> z ; " - "class bitset<1> : B<4> { } ; " - "struct B<4> { int a [ 4 ] ; } ;"; - ASSERT_EQUALS(expected, tok(code)); - } - - void template25() { - const char code[] = "template struct B\n" - "{\n" - " int a[n];\n" - "};\n" - "\n" - "template class bitset: B<((sizeof(int)) ? : 1)>\n" - "{};\n" - "\n" - "bitset<1> z;"; - - const char actual[] = "template < int n > struct B { int a [ n ] ; } ; " - "bitset<1> z ; " - "class bitset<1> : B < 4 > { } ;"; - - const char expected[] = "bitset<1> z ; " - "class bitset<1> : B<4> { } ; " - "struct B<4> { int a [ 4 ] ; } ;"; - - TODO_ASSERT_EQUALS(expected, actual, tok(code)); - - } - - void template26() { - // #2721 - const char code[] = "template\n" - "class A { public: T x; };\n" - "\n" - "template\n" - "class C: public A {};\n" - "\n" - "C<2> a;\n"; - // TODO: expand A also - ASSERT_EQUALS("template < class T > class A { public: T x ; } ; C<2> a ; class C<2> : public A < char [ 2 ] > { } ;", tok(code)); - } - - void template27() { - // #3350 - template inside macro call - const char code[] = "X(template class Fred);"; - ASSERT_EQUALS("X ( template < class T > class Fred ) ;", tok(code)); - } - - void template28() { - // #3226 - inner template - const char code[] = "template class Fred {};\n" - "Fred > x;\n"; - ASSERT_EQUALS("Fred> x ; class Fred { } ; class Fred> { } ;", tok(code)); - } - - void template29() { - // #3449 - garbage code (don't segfault) - const char code[] = "template struct A;\n" - "struct B { template struct C };\n" - "{};"; - ASSERT_EQUALS("template < typename T > struct A ; struct B { template < typename T > struct C } ; { } ;", tok(code)); - } - - void template30() { - // #3529 - template < template < .. - const char code[] = "template class A, class B> void f(){}"; - ASSERT_EQUALS("template < template < class > class A , class B > void f ( ) { }", tok(code)); - } - - void template31() { - // #4010 - template reference type - const char code[] = "template struct A{}; A a;"; - ASSERT_EQUALS("A a ; struct A { } ;", tok(code)); - } - - void template32() { - // #3818 - mismatching template not handled well - const char code[] = "template struct A { };\n" - "\n" - "template \n" - "struct B\n" - "{\n" - " public:\n" - " A < int, Pair, int > a;\n" // mismatching parameters => don't instantiate - "};\n" - "\n" - "B b;\n"; - ASSERT_EQUALS("template < class T1 , class T2 , class T3 , class T4 > struct A { } ; " - "B b ; " - "struct B { public: A < int , Pair < int , int > , int > a ; } ;", tok(code)); - } - - void template33() { - { - // #3818 - inner templates in template instantiation not handled well - const char code[] = "template struct A { };\n" - "template struct B { };\n" - "template struct C { A > > ab; };\n" - "C c;"; - ASSERT_EQUALS("C c ; " - "struct C { A>> ab ; } ; " - "struct B> { } ; " // <- redundant.. but nevermind - "struct A>> { } ; " // <- redundant.. but nevermind - "struct A>> { } ;", tok(code)); - } - - { - // #4544 - const char code[] = "struct A { };\n" - "template struct B { };\n" - "template struct C { };\n" - "C< B > c;"; - ASSERT_EQUALS("struct A { } ; " - "template < class T > struct B { } ; " // <- redundant.. but nevermind - "C> c ; struct C> { } ;", - tok(code)); - } - } - - void template34() { - // #3706 - namespace => hang - const char code[] = "namespace abc {\n" - "template struct X { void f(X &x) {} };\n" - "}\n" - "template <> int X::Y(0);"; - ASSERT_EQUALS("namespace abc { " - "template < typename T > struct X { void f ( X < T > & x ) { } } ; " - "} " - "template < > int X < int > :: Y ( 0 ) ;", tok(code)); - } - - void template35() { // #4074 - "A<'x'> a;" is not recognized as template instantiation - const char code[] = "template class A {};\n" - "A<'x'> a;"; - ASSERT_EQUALS("A<'x'> a ; class A<'x'> { } ;", tok(code)); - } - - void template36() { // #4310 - Passing unknown template instantiation as template argument - const char code[] = "template struct X { T t; };\n" - "template struct Y { Foo < X< Bar > > _foo; };\n" // <- Bar is unknown - "Y bar;"; - ASSERT_EQUALS("Y bar ; " - "struct Y { Foo < X> > _foo ; } ; " - "struct X> { Bar < int > t ; } ;", - tok(code)); - } - - void template37() { // #4544 - A a; - const char code[] = "class A { };\n" - "template class B {};\n" - "B b1;\n" - "B b2;"; - ASSERT_EQUALS("class A { } ; B b1 ; B b2 ; class B { } ;", - tok(code)); - } - - void template_unhandled() { - // An unhandled template usage should be simplified.. - ASSERT_EQUALS("x ( ) ;", tok("x();")); - } - - void template38() { // #4832 - Crash on C++11 right angle brackets - const char code[] = "template class A {\n" - " T mT;\n" - "public:\n" - " void foo() {}\n" - "};\n" - "\n" - "int main() {\n" - " A> gna1;\n" - " A gna2;\n" - "}\n"; - tok(code); // Don't crash or freeze - } - - void template39() { // #4742 - Used to freeze in 1.60 - const char code[] = "template struct vector {" - " operator T() const;" - "};" - "void f() {" - " vector> v;" - " const vector vi = static_cast>(v);" - "}"; - tok(code); - } - - void template40() { // #5055 - false negatives when there is template specialization outside struct - const char code[] = "struct A {" - " template struct X { T t; };" - "};" - "template<> struct A::X { int *t; };"; - ASSERT_EQUALS("struct A { template < typename T > struct X { T t ; } ; } ;", tok(code)); - } - - void template41() { // #4710 - const in template instantiation not handled perfectly - const char code1[] = "template struct X { };\n" - "void f(const X x) { }"; - ASSERT_EQUALS("void f ( const X x ) { } struct X { } ;", tok(code1)); - - const char code2[] = "template T f(T t) { return t; }\n" - "int x() { return f(123); }"; - ASSERT_EQUALS("int x ( ) { return f ( 123 ) ; } int f ( int t ) { return t ; }", tok(code2)); - } - - void template42() { // #4878 cpcheck aborts in ext-blocks.cpp (clang testcode) - const char code[] = "template\n" - "int f0(Args ...args) {\n" - " return ^ {\n" - " return sizeof...(Args);\n" - " }() + ^ {\n" - " return sizeof...(args);\n" - " }();\n" - "}"; - ASSERT_THROW(tok(code), InternalError); - } - - void template43() { // #5097 - Assert due to '>>' in 'B>' not being treated as end of template instantation - const char code[] = "template struct C { };" - "template struct D { static int f() { return C::f(); } };" - "template inline int f2() { return D::f(); }" - "template int f1(int x, T *) { int id = f2(); return id; }" - "template <> struct C < B < A >> {" - " static int f() {" - " return f1 < B < A >> (0, reinterpret_cast< B *>(E::Int(-1)));" - " }" - "};"; - tok(code); // Don't assert - } - - void template44() { // #5297 - tok("template struct StackContainer {" - " void foo(int i) {" - " if (0 >= 1 && i<0) {}" - " }" - "};" - "template class ZContainer : public StackContainer {};" - "struct FGSTensor {};" - "class FoldedZContainer : public ZContainer {};"); - } - - void template45() { // #5814 - tok("namespace Constants { const int fourtytwo = 42; } " - "template struct TypeMath { " - " static const int mult = sizeof(T) * U; " - "}; " - "template struct FOO { " - " enum { value = TypeMath::something }; " - "};"); - ASSERT_EQUALS("", errout.str()); - } - - void template46() { // #5816 - tok("template struct A { static const int value = 0; }; " - "template struct B { " - " enum { value = A::value }; " - "};"); - ASSERT_EQUALS("", errout.str()); - tok("template struct A {}; " - "enum { e = sizeof(A) }; " - "template struct B {};"); - ASSERT_EQUALS("", errout.str()); - tok("template struct A { static const int value = 0; }; " - "template struct B { typedef int type; }; " - "template struct C { " - " enum { value = A::type, int>::value }; " - "};"); - ASSERT_EQUALS("", errout.str()); - } - - void template47() { // #6023 - tok("template > class C1 {}; " - "class C2 : public C1 {};"); - ASSERT_EQUALS("", errout.str()); - } - - void template48() { // #6134 - tok("template int f( { } ); " - "int foo = f<1>(0);"); - ASSERT_EQUALS("", errout.str()); - } - - void template_default_parameter() { - { - const char code[] = "template \n" - "class A\n" - "{ T ar[n]; };\n" - "\n" - "void f()\n" - "{\n" - " A a1;\n" - " A a2;\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " A a1 ;" - " A a2 ; " - "} " - "class A " - "{ int ar [ 2 ] ; } ; " - "class A " - "{ int ar [ 3 ] ; } ;"); - ASSERT_EQUALS(expected, tok(code)); - } - { - const char code[] = "template \n" - "class A\n" - "{ T ar[n1+n2]; };\n" - "\n" - "void f()\n" - "{\n" - " A a1;\n" - " A a2;\n" - "}\n"; - - // The expected result.. - const std::string expected("void f ( ) " - "{" - " A a1 ;" - " A a2 ; " - "} " - "class A " - "{ int ar [ 5 ] ; } ;"); - ASSERT_EQUALS(expected, tok(code)); - } - { - const char code[] = "template \n" - "class A\n" - "{ T ar[n]; };\n" - "\n" - "void f()\n" - "{\n" - " A a1;\n" - " A a2;\n" - "}\n"; - - const std::string wanted("template < class T , int n >" - " class A" - " { T ar [ n ] ; } ;" - " void f ( )" - " {" - " A a1 ;" - " A a2 ;" - " }" - " class A" - " { int ar [ 2 ] ; }" - " class A" - " { int ar [ 3 ] ; }"); - - const std::string current("void f ( ) " - "{ " - "A < int , ( int ) 2 > a1 ; " - "A a2 ; " - "} " - "class A " - "{ int ar [ 3 ] ; } ;" - ); - TODO_ASSERT_EQUALS(wanted, current, tok(code)); - } - { - const char code[] = "template> class B {};\n" - "template> class C {};\n" - "template class D { };\n"; - ASSERT_EQUALS("template < class T , class T2 > class B { } ; " - "template < class B , typename C > class C { } ; " - "template < class B , typename C > class D { } ;", tok(code)); - } - } - - void template_default_type() { - const char code[] = "template \n" - "class A\n" - "{\n" - "public:\n" - " void foo() {\n" - " int a;\n" - " a = static_cast(a);\n" - " }\n" - "};\n" - "\n" - "template \n" - "class B\n" - "{\n" - "protected:\n" - " A a;\n" - "};\n" - "\n" - "class C\n" - " : public B\n" - "{\n" - "};\n"; - - tok(code); - - //ASSERT_EQUALS("[file1.cpp:15]: (error) Internal error: failed to instantiate template. The checking continues anyway.\n", errout.str()); - ASSERT_EQUALS("", errout.str()); - } - - void template_typename() { - { - const char code[] = "template \n" - "void foo(typename T::t *)\n" - "{ }"; - - // The expected result.. - const std::string expected("template < class T > void foo ( T :: t * ) { }"); - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "void f() {\n" - " x(sizeof typename);\n" - " type = 0;\n" - "}"; - - ASSERT_EQUALS("void f ( ) { x ( sizeof ( typename ) ) ; type = 0 ; }", tok(code)); - } - } - - void template_constructor() { - // #3152 - if template constructor is removed then there might be - // "no constructor" false positives - const char code[] = "class Fred {\n" - " template explicit Fred(T t) { }\n" - "}"; - ASSERT_EQUALS("class Fred { template < class T > explicit Fred ( T t ) { } }", tok(code)); - - // #3532 - const char code2[] = "class Fred {\n" - " template Fred(T t) { }\n" - "}"; - ASSERT_EQUALS("class Fred { template < class T > Fred ( T t ) { } }", tok(code2)); - } - - unsigned int templateParameters(const char code[]) { - Settings settings; - Tokenizer tokenizer(&settings, this); - - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp", "", true); - - return TemplateSimplifier::templateParameters(tokenizer.tokens()); - } - - void templateParameters() { - // Test that the function TemplateSimplifier::templateParameters works - ASSERT_EQUALS(1U, templateParameters(" x;")); - ASSERT_EQUALS(1U, templateParameters(" x;")); - ASSERT_EQUALS(1U, templateParameters(" x;")); - ASSERT_EQUALS(1U, templateParameters(" x;")); - ASSERT_EQUALS(1U, templateParameters(" x;")); - } - - void templateParameters1() { - // #4169 - segmentation fault (invalid code) - const char code[] = "volatile true , test < test < #ifdef __ppc__ true ,"; - // do not crash on invalid code - ASSERT_EQUALS(0, templateParameters(code)); - } - void namespaces() { { const char code[] = "namespace std { }"; @@ -3523,2811 +2404,6 @@ private: ASSERT_EQUALS("a = 10 ;", tok("a = atol(\"0xa\");")); } - std::string simplifyTypedef(const char code[]) { - errout.str(""); - - Settings settings; - Tokenizer tokenizer(&settings, this); - - std::istringstream istr(code); - tokenizer.list.createTokens(istr); - tokenizer.createLinks(); - tokenizer.simplifyTypedef(); - - return tokenizer.tokens()->stringifyList(0, false); - } - - - - void simplifyTypedef1() { - const char code[] = "class A\n" - "{\n" - "public:\n" - " typedef wchar_t duplicate;\n" - " void foo() {}\n" - "};\n" - "typedef A duplicate;\n" - "int main()\n" - "{\n" - " duplicate a;\n" - " a.foo();\n" - " A::duplicate c = 0;\n" - "}\n"; - - const std::string expected = - "class A " - "{ " - "public: " - "" - "void foo ( ) { } " - "} ; " - "int main ( ) " - "{ " - "A a ; " - "a . foo ( ) ; " - "wchar_t c ; c = 0 ; " - "}"; - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef2() { - const char code[] = "class A;\n" - "typedef A duplicate;\n" - "class A\n" - "{\n" - "public:\n" - "typedef wchar_t duplicate;\n" - "duplicate foo() { wchar_t b; return b; }\n" - "};\n"; - - const std::string expected = - "class A ; " - "class A " - "{ " - "public: " - "" - "wchar_t foo ( ) { wchar_t b ; return b ; } " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef3() { - const char code[] = "class A {};\n" - "typedef A duplicate;\n" - "wchar_t foo()\n" - "{\n" - "typedef wchar_t duplicate;\n" - "duplicate b;\n" - "return b;\n" - "}\n" - "int main()\n" - "{\n" - "duplicate b;\n" - "}\n"; - - const std::string expected = - "class A { } ; " - "wchar_t foo ( ) " - "{ " - "" - "wchar_t b ; " - "return b ; " - "} " - "int main ( ) " - "{ " - "A b ; " - "}"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef4() { - const char code[] = "typedef int s32;\n" - "typedef unsigned int u32;\n" - "void f()\n" - "{\n" - " s32 ivar = -2;\n" - " u32 uvar = 2;\n" - " return uvar / ivar;\n" - "}\n"; - - const std::string expected = - "void f ( ) " - "{ " - "int ivar ; ivar = -2 ; " - "unsigned int uvar ; uvar = 2 ; " - "return uvar / ivar ; " - "}"; - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef5() { - // ticket #780 - const char code[] = - "typedef struct yy_buffer_state *YY_BUFFER_STATE;\n" - "void f()\n" - "{\n" - " YY_BUFFER_STATE state;\n" - "}\n"; - - const char expected[] = - "void f ( ) " - "{ " - "struct yy_buffer_state * state ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef6() { - // ticket #983 - const char code[] = - "namespace VL {\n" - " typedef float float_t ;\n" - " inline VL::float_t fast_atan2(VL::float_t y, VL::float_t x){}\n" - "}\n"; - - const char expected[] = - "namespace VL { " - "" - "float fast_atan2 ( float y , float x ) { } " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef7() { - const char code[] = "typedef int abc ; " - "Fred :: abc f ;"; - const char expected[] = "Fred :: abc f ;"; - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef8() { - const char code[] = "typedef int INT;\n" - "typedef unsigned int UINT;\n" - "typedef int * PINT;\n" - "typedef unsigned int * PUINT;\n" - "typedef int & RINT;\n" - "typedef unsigned int & RUINT;\n" - "typedef const int & RCINT;\n" - "typedef const unsigned int & RCUINT;\n" - "INT ti;\n" - "UINT tui;\n" - "PINT tpi;\n" - "PUINT tpui;\n" - "RINT tri;\n" - "RUINT trui;\n" - "RCINT trci;\n" - "RCUINT trcui;"; - - const char expected[] = - "int ti ; " - "unsigned int tui ; " - "int * tpi ; " - "unsigned int * tpui ; " - "int & tri ; " - "unsigned int & trui ; " - "const int & trci ; " - "const unsigned int & trcui ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef9() { - const char code[] = "typedef struct s S, * PS;\n" - "typedef struct t { int a; } T, *TP;\n" - "typedef struct { int a; } U;\n" - "typedef struct { int a; } * V;\n" - "S s;\n" - "PS ps;\n" - "T t;\n" - "TP tp;\n" - "U u;\n" - "V v;"; - - const char expected[] = - "struct t { int a ; } ; " - "struct U { int a ; } ; " - "struct Unnamed0 { int a ; } ; " - "struct s s ; " - "struct s * ps ; " - "struct t t ; " - "struct t * tp ; " - "struct U u ; " - "struct Unnamed0 * v ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef10() { - const char code[] = "typedef union s S, * PS;\n" - "typedef union t { int a; float b ; } T, *TP;\n" - "typedef union { int a; float b; } U;\n" - "typedef union { int a; float b; } * V;\n" - "S s;\n" - "PS ps;\n" - "T t;\n" - "TP tp;\n" - "U u;\n" - "V v;"; - - const char expected[] = - "union t { int a ; float b ; } ; " - "union U { int a ; float b ; } ; " - "union Unnamed1 { int a ; float b ; } ; " - "union s s ; " - "union s * ps ; " - "union t t ; " - "union t * tp ; " - "union U u ; " - "union Unnamed1 * v ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef11() { - const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc;\n" - "typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\n" - "abc e1;\n" - "XYZ e2;"; - - const char expected[] = "int e1 ; " - "int e2 ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef12() { - const char code[] = "typedef vector V1;\n" - "typedef std::vector V2;\n" - "typedef std::vector > V3;\n" - "typedef std::list::iterator IntListIterator;\n" - "V1 v1;\n" - "V2 v2;\n" - "V3 v3;\n" - "IntListIterator iter;"; - - const char expected[] = - "vector < int > v1 ; " - "std :: vector < int > v2 ; " - "std :: vector < std :: vector < int > > v3 ; " - "std :: list < int > :: iterator iter ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef13() { - // ticket # 1167 - const char code[] = "typedef std::pair Func;" - "typedef std::vector CallQueue;" - "int main() {}"; - - // Tokenize and check output.. - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef14() { - // ticket # 1232 - const char code[] = "template struct E" - "{" - " typedef E0)?(N-1):0> v;" - " typedef typename add::val val;" - " FP_M(val);" - "};" - "template struct E " - "{" - " typedef typename D<1>::val val;" - " FP_M(val);" - "};"; - - // Tokenize and check output.. - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef15() { - { - const char code[] = "typedef char frame[10];\n" - "frame f;"; - - const char expected[] = "char f [ 10 ] ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "typedef unsigned char frame[10];\n" - "frame f;"; - - const char expected[] = "unsigned char f [ 10 ] ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef16() { - // ticket # 1252 - const char code[] = "typedef char MOT8;\n" - "typedef MOT8 CHFOO[4096];\n" - "typedef struct {\n" - " CHFOO freem;\n" - "} STRFOO;"; - - // Tokenize and check output.. - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef17() { - const char code[] = "typedef char * PCHAR, CHAR;\n" - "PCHAR pc;\n" - "CHAR c;"; - - const char expected[] = - "char * pc ; " - "char c ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef18() { - const char code[] = "typedef vector a;\n" - "a b;\n"; - - // Clear the error buffer.. - errout.str(""); - - Settings settings; - Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); - - tokenizer.simplifyTokenList2(); - - tokenizer.validate(); - } - - void simplifyTypedef19() { - { - // ticket #1275 - const char code[] = "typedef struct {} A, *B, **C;\n" - "A a;\n" - "B b;\n" - "C c;"; - - const char expected[] = - "struct A { } ; " - "struct A a ; " - "struct A * b ; " - "struct A * * c ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "typedef struct {} A, *********B;\n" - "A a;\n" - "B b;"; - - const char expected[] = - "struct A { } ; " - "struct A a ; " - "struct A * * * * * * * * * b ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "typedef struct {} **********A, *B, C;\n" - "A a;\n" - "B b;\n" - "C c;"; - - const char expected[] = - "struct Unnamed2 { } ; " - "struct Unnamed2 * * * * * * * * * * a ; " - "struct Unnamed2 * b ; " - "struct Unnamed2 c ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef20() { - // ticket #1284 - const char code[] = "typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *);"; - - // Clear the error buffer.. - errout.str(""); - - Settings settings; - Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); - - tokenizer.simplifyTokenList2(); - - tokenizer.validate(); - } - - void simplifyTypedef21() { - const char code[] = "typedef void (* PF)();\n" - "typedef void * (* PFV)(void *);\n" - "PF pf;\n" - "PFV pfv;"; - - const char expected[] = - "" - "" - "void ( * pf ) ( ) ; " - "void * ( * pfv ) ( void * ) ;"; - - ASSERT_EQUALS(expected, simplifyTypedef(code)); - } - - void simplifyTypedef22() { - { - const char code[] = "class Fred {\n" - " typedef void (*testfp)();\n" - " testfp get() { return test; }\n" - " static void test() { }\n" - "};"; - - const char expected[] = - "class Fred { " - "" - "void * get ( ) { return test ; } " - "static void test ( ) { } " - "} ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "class Fred {\n" - " typedef void * (*testfp)(void *);\n" - " testfp get() { return test; }\n" - " static void * test(void * p) { return p; }\n" - "};\n"; - - const char expected[] = - "class Fred { " - "" - "void * * get ( ) { return test ; } " - "static void * test ( void * p ) { return p ; } " - "} ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "class Fred {\n" - " typedef unsigned int * (*testfp)(unsigned int *);\n" - " testfp get() { return test; }\n" - " static unsigned int * test(unsigned int * p) { return p; }\n" - "};\n"; - - const char expected[] = - "class Fred { " - "" - "unsigned int * * get ( ) { return test ; } " - "static unsigned int * test ( unsigned int * p ) { return p ; } " - "} ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "class Fred {\n" - " typedef const unsigned int * (*testfp)(const unsigned int *);\n" - " testfp get() { return test; }\n" - " static const unsigned int * test(const unsigned int * p) { return p; }\n" - "};\n"; - - // static const gets changed to const static - const char expected[] = - "class Fred { " - "" - "const unsigned int * * get ( ) { return test ; } " - "const static unsigned int * test ( const unsigned int * p ) { return p ; } " - "} ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "class Fred {\n" - " typedef void * (*testfp)(void *);\n" - " testfp get(int i) { return test; }\n" - " static void * test(void * p) { return p; }\n" - "};\n"; - - const char expected[] = - "class Fred { " - "" - "void * * get ( int i ) { return test ; } " - "static void * test ( void * p ) { return p ; } " - "} ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef23() { - const char code[] = "typedef bool (*Callback) (int i);\n" - "void addCallback(Callback callback) { }\n" - "void addCallback1(Callback callback, int j) { }"; - - const char expected[] = - "void addCallback ( bool * callback ) { } " - "void addCallback1 ( bool * callback , int j ) { }"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef24() { - { - const char code[] = "typedef int (*fp)();\n" - "void g( fp f )\n" - "{\n" - " fp f2 = (fp)f;\n" - "}"; - - const char expected[] = - "void g ( int * f ) " - "{ " - "int * f2 ; f2 = ( int * ) f ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "typedef int (*fp)();\n" - "void g( fp f )\n" - "{\n" - " fp f2 = static_cast(f);\n" - "}"; - - const char expected[] = - "void g ( int * f ) " - "{ " - "int * f2 ; f2 = static_cast < int * > ( f ) ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef25() { - { - // ticket #1298 - const char code[] = "typedef void (*fill_names_f) (const char *);\n" - "struct vfs_class {\n" - " void (*fill_names) (struct vfs_class *me, fill_names_f);\n" - "}"; - - const char expected[] = - "struct vfs_class { " - "void ( * fill_names ) ( struct vfs_class * me , void ( * ) ( const char * ) ) ; " - "}"; - - ASSERT_EQUALS(expected, simplifyTypedef(code)); - } - - { - const char code[] = "typedef void (*fill_names_f) (const char *);\n" - "struct vfs_class {\n" - " void (*fill_names) (fill_names_f, struct vfs_class *me);\n" - "}"; - - const char expected[] = - "struct vfs_class { " - "void ( * fill_names ) ( void ( * ) ( const char * ) , struct vfs_class * me ) ; " - "}"; - - ASSERT_EQUALS(expected, simplifyTypedef(code)); - } - } - - void simplifyTypedef26() { - { - const char code[] = "typedef void (*Callback) ();\n" - "void addCallback(Callback (*callback)());"; - - const char expected[] = "void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - // ticket # 1307 - const char code[] = "typedef void (*pc_video_update_proc)(bitmap_t *bitmap,\n" - "struct mscrtc6845 *crtc);\n" - "\n" - "struct mscrtc6845 *pc_video_start(pc_video_update_proc (*choosevideomode)(running_machine *machine, int *width, int *height, struct mscrtc6845 *crtc));"; - - const char expected[] = "struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef27() { - // ticket #1316 - const char code[] = "int main()\n" - "{\n" - " typedef int (*func_ptr)(float, double);\n" - " VERIFY((is_same::type, int>::value));\n" - "}"; - - const char expected[] = - "int main ( ) " - "{ " - "" - "VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef28() { - const char code[] = "typedef std::pair (*F)(double);\n" - "F f;"; - - const char expected[] = "std :: pair < double , double > ( * f ) ( double ) ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef29() { - const char code[] = "typedef int array [ice_or::value, is_int::value>::value ? 1 : -1];\n" - "typedef int array1 [N];\n" - "typedef int array2 [N][M];\n" - "typedef int int_t, int_array[N];\n" - "array a;\n" - "array1 a1;\n" - "array2 a2;\n" - "int_t t;\n" - "int_array ia;"; - - const char expected[] = - "int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; " - "int a1 [ N ] ; " - "int a2 [ N ] [ M ] ; " - "int t ; " - "int ia [ N ] ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef30() { - const char code[] = "typedef ::std::list int_list;\n" - "typedef ::std::list::iterator int_list_iterator;\n" - "typedef ::std::list int_list_array[10];\n" - "int_list il;\n" - "int_list_iterator ili;\n" - "int_list_array ila;"; - - const char expected[] = - ":: std :: list < int > il ; " - ":: std :: list < int > :: iterator ili ; " - ":: std :: list < int > ila [ 10 ] ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef31() { - { - const char code[] = "class A {\n" - "public:\n" - " typedef int INT;\n" - " INT get() const;\n" - " void put(INT x) { a = x; }\n" - " INT a;\n" - "};\n" - "A::INT A::get() const { return a; }\n" - "A::INT i = A::a;"; - - const char expected[] = "class A { " - "public: " - "" - "int get ( ) const ; " - "void put ( int x ) { a = x ; } " - "int a ; " - "} ; " - "int A :: get ( ) const { return a ; } " - "int i ; i = A :: a ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - { - const char code[] = "struct A {\n" - " typedef int INT;\n" - " INT get() const;\n" - " void put(INT x) { a = x; }\n" - " INT a;\n" - "};\n" - "A::INT A::get() const { return a; }\n" - "A::INT i = A::a;"; - - const char expected[] = "struct A { " - "" - "int get ( ) const ; " - "void put ( int x ) { a = x ; } " - "int a ; " - "} ; " - "int A :: get ( ) const { return a ; } " - "int i ; i = A :: a ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef32() { - const char code[] = "typedef char CHAR;\n" - "typedef CHAR * LPSTR;\n" - "typedef const CHAR * LPCSTR;\n" - "CHAR c;\n" - "LPSTR cp;\n" - "LPCSTR ccp;"; - - const char expected[] = - "char c ; " - "char * cp ; " - "const char * ccp ;"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef33() { - const char code[] = "class A {\n" - "public:\n" - " typedef char CHAR_A;\n" - " CHAR_A funA();\n" - " class B {\n" - " public:\n" - " typedef short SHRT_B;\n" - " SHRT_B funB();\n" - " class C {\n" - " public:\n" - " typedef int INT_C;\n" - " INT_C funC();\n" - " struct D {\n" - " typedef long LONG_D;\n" - " LONG_D funD();\n" - " LONG_D d;\n" - " };\n" - " INT_C c;\n" - " };\n" - " SHRT_B b;\n" - " };\n" - " CHAR_A a;\n" - "};\n" - "A::CHAR_A A::funA() { return a; }\n" - "A::B::SHRT_B A::B::funB() { return b; }\n" - "A::B::C::INT_C A::B::C::funC() { return c; }" - "A::B::C::D::LONG_D A::B::C::D::funD() { return d; }"; - - const char expected[] = - "class A { " - "public: " - "" - "char funA ( ) ; " - "class B { " - "public: " - "" - "short funB ( ) ; " - "class C { " - "public: " - "" - "int funC ( ) ; " - "struct D { " - "" - "long funD ( ) ; " - "long d ; " - "} ; " - "int c ; " - "} ; " - "short b ; " - "} ; " - "char a ; " - "} ; " - "char A :: funA ( ) { return a ; } " - "short A :: B :: funB ( ) { return b ; } " - "int A :: B :: C :: funC ( ) { return c ; } " - "long A :: B :: C :: D :: funD ( ) { return d ; }"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef34() { - // ticket #1411 - const char code[] = "class X { };\n" - "typedef X (*foofunc)(const X&);\n" - "int main()\n" - "{\n" - " foofunc *Foo = new foofunc[2];\n" - "}"; - const char expected[] = - "class X { } ; " - "int main ( ) " - "{ " - "X ( * * Foo ) ( const X & ) ; Foo = new X ( * ) ( const X & ) [ 2 ] ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - } - - // Check simplifyTypedef - void checkSimplifyTypedef(const char code[]) { - errout.str(""); - // Tokenize.. - Settings settings; - settings.inconclusive = true; - settings.addEnabled("style"); - settings.debugwarnings = true; // show warnings about unhandled typedef - Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - tokenizer.tokenize(istr, "test.cpp"); - } - - void simplifyTypedef35() { - const char code[] = "typedef int A;\n" - "class S\n" - "{\n" - "public:\n" - " typedef float A;\n" - " A a;\n" - " virtual void fun(A x);\n" - "};\n" - "void S::fun(S::A) { };\n" - "class S1 : public S\n" - "{\n" - "public:\n" - " void fun(S::A) { }\n" - "};\n" - "struct T\n" - "{\n" - " typedef A B;\n" - " B b;\n" - "};\n" - "float fun1(float A) { return A; }\n" - "float fun2(float a) { float A = a++; return A; }\n" - "float fun3(int a)\n" - "{\n" - " typedef struct { int a; } A;\n" - " A s; s.a = a;\n" - " return s.a;\n" - "}\n" - "int main()\n" - "{\n" - " A a = 0;\n" - " S::A s = fun1(a) + fun2(a) - fun3(a);\n" - " return a + s;\n" - "}"; - - const char expected[] = "class S " - "{ " - "public: " - "" - "float a ; " - "virtual void fun ( float x ) ; " - "} ; " - "void S :: fun ( float ) { } ; " - "class S1 : public S " - "{ " - "public: " - "void fun ( float ) { } " - "} ; " - "struct T " - "{ " - "" - "int b ; " - "} ; " - "float fun1 ( float A ) { return A ; } " - "float fun2 ( float a ) { float A ; A = a ++ ; return A ; } " - "float fun3 ( int a ) " - "{ " - "struct A { int a ; } ; " - "struct A s ; s . a = a ; " - "return s . a ; " - "} " - "int main ( ) " - "{ " - "int a ; a = 0 ; " - "float s ; s = fun1 ( a ) + fun2 ( a ) - fun3 ( a ) ; " - "return a + s ; " - "}"; - - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" - "[test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name.\n" - "[test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name.\n" - "[test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" - "[test.cpp:24]: (debug) ValueFlow bailout: parameter a, at '{'\n", errout.str()); - } - - void simplifyTypedef36() { - // ticket #1434 - const char code[] = "typedef void (*TIFFFaxFillFunc)();\n" - "void f(va_list ap)\n" - "{\n" - " *va_arg(ap, TIFFFaxFillFunc*) = 0;\n" - "}"; - const char expected[] = "void f ( va_list ap ) " - "{ " - "* va_arg ( ap , void ( * * ) ( ) ) = 0 ; " - "}"; - ASSERT_EQUALS(expected, tok(code, false)); - } - - void simplifyTypedef37() { - { - // ticket #1449 - const char code[] = "template class V {};\n" - "typedef V A;\n" - "typedef int B;\n" - "typedef V A;\n" - "typedef int B;"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" - "[test.cpp:5] -> [test.cpp:3]: (style, inconclusive) The typedef 'B' hides a typedef with the same name.\n", errout.str()); - } - - { - const char code[] = "typedef int INT;\n" - "void f()\n" - "{\n" - " INT i; { }\n" - "}"; - const char expected[] = "void f ( ) " - "{ " - "int i ; { } " - "}"; - ASSERT_EQUALS(expected, tok(code, false)); - } - } - - void simplifyTypedef38() { - const char code[] = "typedef C A;\n" - "struct AB : public A, public B { };"; - const char expected[] = "struct AB : public C , public B { } ;"; - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef39() { - const char code[] = "typedef int A;\n" - "template ::value;"; - const char expected[] = "template < const int , int > :: value ;"; - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef40() { - const char code[] = "typedef int A;\n" - "typedef int B;\n" - "template class C { };"; - const char expected[] = "template < class A , class B > class C { } ;"; - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) The template parameter 'A' hides a typedef with the same name.\n" - "[test.cpp:3] -> [test.cpp:2]: (style, inconclusive) The template parameter 'B' hides a typedef with the same name.\n", errout.str()); - - checkSimplifyTypedef("typedef tuple t2;\n" - "void ordering_test()\n" - "{\n" - " tuple t2(5, 3.3f);\n" - " BOOST_CHECK(t3 > t2);\n" - "}"); - ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (style, inconclusive) The template instantiation 't2' hides a typedef with the same name.\n", errout.str()); - - checkSimplifyTypedef("class MyOverflowingUnsigned\n" - "{\n" - "public:\n" - " typedef unsigned self_type::* bool_type;\n" - " operator bool_type() const { return this->v_ ? &self_type::v_ : 0; }\n" - "}"); - ASSERT_EQUALS("", errout.str()); - - checkSimplifyTypedef("typedef int (*fptr_type)(int, int);\n" - "struct which_one {\n" - " typedef fptr_type (*result_type)(bool x);\n" - "}"); - ASSERT_EQUALS("", errout.str()); - - checkSimplifyTypedef("class my_configuration\n" - "{\n" - "public:\n" - " template < typename T >\n" - " class hook\n" - " {\n" - " public:\n" - " typedef ::boost::rational rational_type;\n" - " public:\n" - " rational_type ( &r_ )[ 9 ];\n" - " };\n" - "}"); - ASSERT_EQUALS("", errout.str()); - - checkSimplifyTypedef("class A\n" - "{\n" - " typedef B b;\n" - " friend b;\n" - "};"); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef41() { - // ticket #1488 - checkSimplifyTypedef("class Y;\n" - "class X\n" - "{\n" - " typedef Y type;\n" - " friend class type;\n" - "};"); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef42() { - // ticket #1506 - checkSimplifyTypedef("typedef struct A { } A;\n" - "struct A;"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The struct 'A' forward declaration is unnecessary. Type struct is already declared earlier.\n", errout.str()); - - checkSimplifyTypedef("typedef union A { int i; float f; } A;\n" - "union A;"); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The union 'A' forward declaration is unnecessary. Type union is already declared earlier.\n", errout.str()); - - const char code [] = "typedef std::map A;\n" - "class A;"; - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The class 'A' forward declaration is unnecessary. Type class is already declared earlier.\n", errout.str()); - TODO_ASSERT_EQUALS("class A ;", "class std :: map < std :: string , int > ;", tok(code)); - } - - void simplifyTypedef43() { - // ticket #1588 - { - const char code[] = "typedef struct foo A;\n" - "struct A\n" - "{\n" - " int alloclen;\n" - "};\n"; - - // The expected result.. - const std::string expected("struct A " - "{ " - "int alloclen ; " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The struct 'A' hides a typedef with the same name.\n", errout.str()); - } - - { - const char code[] = "typedef union foo A;\n" - "union A\n" - "{\n" - " int alloclen;\n" - "};\n"; - - // The expected result.. - const std::string expected("union A " - "{ " - "int alloclen ; " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The union 'A' hides a typedef with the same name.\n", errout.str()); - } - - { - const char code[] = "typedef class foo A;\n" - "class A\n" - "{\n" - " int alloclen;\n" - "};\n"; - - // The expected result.. - const std::string expected("class A " - "{ " - "int alloclen ; " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The class 'A' hides a typedef with the same name.\n", errout.str()); - } - } - - void simplifyTypedef44() { - { - const char code[] = "typedef std::map Map;\n" - "class MyMap : public Map\n" - "{\n" - "};\n"; - - // The expected result.. - const std::string expected("class MyMap : public std :: map < std :: string , int > " - "{ " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef std::map Map;\n" - "class MyMap : protected Map\n" - "{\n" - "};\n"; - - // The expected result.. - const std::string expected("class MyMap : protected std :: map < std :: string , int > " - "{ " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef std::map Map;\n" - "class MyMap : private Map\n" - "{\n" - "};\n"; - - // The expected result.. - const std::string expected("class MyMap : private std :: map < std :: string , int > " - "{ " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef struct foo { } A;\n" - "struct MyA : public A\n" - "{\n" - "};\n"; - - // The expected result.. - const std::string expected("struct foo { } ; " - "struct MyA : public foo " - "{ " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef class foo { } A;\n" - "class MyA : public A\n" - "{\n" - "};\n"; - - // The expected result.. - const std::string expected("class foo { } ; " - "class MyA : public foo " - "{ " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedef45() { - // ticket # 1613 - const char code[] = "void fn() {\n" - " typedef foo<> bar;\n" - " while (0 > bar(1)) {}\n" - "}"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef46() { - const char code[] = "typedef const struct A { int a; } * AP;\n" - "AP ap;\n"; - - // The expected result.. - const std::string expected("struct A { int a ; } ; " - "const struct A * ap ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef47() { - { - const char code[] = "typedef std::pair const I;\n" - "I i;"; - - // The expected result.. - const std::string expected("std :: pair < int , int > const i ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - { - const char code[] = "typedef void (X:: *F)();\n" - "F f;"; - - // The expected result.. - const std::string expected("void * f ;"); - ASSERT_EQUALS(expected, tok(code)); - } - } - - void simplifyTypedef48() { // ticket #1673 - const char code[] = "typedef struct string { } string;\n" - "void foo (LIST *module_name)\n" - "{\n" - " bar(module_name ? module_name->string : 0);\n" - "}\n"; - - // The expected result.. - const std::string expected("struct string { } ; " - "void foo ( LIST * module_name ) " - "{ " - "bar ( module_name ? module_name . string : 0 ) ; " - "}"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef49() { // ticket #1691 - const char code[] = "class Class2 {\n" - "typedef const Class & Const_Reference;\n" - "void some_method (Const_Reference x) const {}\n" - "void another_method (Const_Reference x) const {}\n" - "}"; - - // The expected result.. - const std::string expected("class Class2 { " - "" - "void some_method ( const Class & x ) const { } " - "void another_method ( const Class & x ) const { } " - "}"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef50() { - const char code[] = "typedef char (* type1)[10];\n" - "typedef char (& type2)[10];\n" - "typedef char (& type3)[x];\n" - "typedef char (& type4)[x + 2];\n" - "type1 t1;\n" - "type1 (*tp1)[2];\n" - "type2 t2;\n" - "type3 t3;\n" - "type4 t4;"; - - // The expected result.. - const std::string expected("char * t1 [ 10 ] ; " - "char ( * ( * tp1 ) [ 2 ] ) [ 10 ] ; " - "char ( & t2 ) [ 10 ] ; " - "char ( & t3 ) [ x ] ; " - "char ( & t4 ) [ x + 2 ] ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef51() { - const char code[] = "class A { public: int i; };\n" - "typedef const char (A :: * type1);\n" - "type1 t1 = &A::i;"; - - // The expected result.. - const std::string expected("class A { public: int i ; } ; " - "const char ( A :: * t1 ) ; t1 = & A :: i ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef52() { // ticket #1782 - { - const char code[] = "typedef char (* type1)[10];\n" - "type1 foo() { }"; - - // The expected result.. - const std::string expected("char ( * foo ( ) ) [ 10 ] { }"); - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef char (* type1)[10];\n" - "LOCAL(type1) foo() { }"; - - // this is invalid C so just make sure it doesn't generate an internal error - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedef53() { // ticket #1801 - { - const char code[] = "typedef int ( * int ( * ) ( ) ) ( ) ;"; - - // this is invalid C so just make sure it doesn't crash - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:1]: (debug) Failed to parse 'typedef int ( * int ( * ) ( ) ) ( ) ;'. The checking continues anyway.\n", errout.str()); - } - - { - const char code[] = "typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);\n" - "typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);\n"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'PPDMarkOption' hides a typedef with the same name.\n", errout.str()); - } - - { - const char code[] = "typedef int * A;\n" - "typedef int * A;\n"; - checkSimplifyTypedef(code); - ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n", errout.str()); - } - } - - void simplifyTypedef54() { // ticket #1814 - const char code[] = "void foo()\n" - "{\n" - " typedef std::basic_string string_type;\n" - " try\n" - " {\n" - " throw string_type(\"leak\");\n" - " }\n" - " catch (const string_type&)\n" - " {\n" - " pthread_exit (0);\n" - " }\n" - "}"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef55() { - const char code[] = "typedef volatile unsigned long * const hwreg_t ;\n" - "typedef void *const t1[2];\n" - "typedef int*const *_Iterator;\n" - "hwreg_t v1;\n" - "t1 v2;\n" - "_Iterator v3;\n"; - - // The expected result.. - const std::string expected("long * v1 ; " - "void * v2 [ 2 ] ; " - "int * * v3 ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef56() { // ticket #1829 - const char code[] = "struct C {\n" - " typedef void (*fptr)();\n" - " const fptr pr;\n" - " operator const fptr& () { return pr; }\n" - "};\n"; - - // The expected result.. - const std::string expected("struct C { " - "" - "const void * pr ; " // this gets simplified to a regular pointer - "operatorconstvoid(*)()& ( ) { return pr ; } " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef57() { // ticket #1846 - const char code[] = "void foo {\n" - " typedef int A;\n" - " A a = A(1) * A(2);\n" - "};\n"; - - // The expected result.. - const std::string expected("void foo { " - "" - "int a ; a = int ( 1 ) * int ( 2 ) ; " - "} ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef58() { // ticket #1963 - { - const char code[] = "typedef int vec2_t[2];\n" - "vec2_t coords[4] = {1,2,3,4,5,6,7,8};\n"; - - // The expected result.. - const std::string expected("int coords [ 4 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef int vec2_t[2];\n" - "vec2_t coords[4][5][6+1] = {1,2,3,4,5,6,7,8};\n"; - - // The expected result.. - const std::string expected("int coords [ 4 ] [ 5 ] [ 7 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedef59() { // ticket #2011 - const char code[] = "template class SomeTemplateClass {\n" - " typedef void (SomeTemplateClass::*MessageDispatcherFunc)(SerialInputMessage&);\n" - "};\n"; - // The expected result.. - const std::string expected("template < typename DISPATCHER > class SomeTemplateClass { } ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef60() { // ticket #2035 - const char code[] = "typedef enum {qfalse, qtrue} qboolean;\n" - "typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent);\n" - "void f()\n" - "{\n" - " qboolean b;\n" - " localEntitiyAddFunc_t f;\n" - "}\n"; - // The expected result.. - const std::string expected("void f ( ) { int b ; int * f ; }"); - ASSERT_EQUALS(expected, tok(code, false)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef61() { // ticket #2074 and 2075 - const char code1[] = "typedef unsigned char (*Mf_GetIndexByte_Func) (void);\n" - "typedef const unsigned char * (*Mf_GetPointerToCurrentPos_Func)(void);\n"; - - // Check for output.. - checkSimplifyTypedef(code1); - ASSERT_EQUALS("", errout.str()); - - const char code2[] = "typedef unsigned long uint32_t;\n" - "typedef uint32_t (*write_type_t) (uint32_t);\n"; - - // Check for output.. - checkSimplifyTypedef(code2); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef62() { // ticket #2082 - const char code1[] = "typedef char TString[256];\n" - "void f()\n" - "{\n" - " TString a, b;\n" - "}"; - - // The expected tokens.. - const std::string expected1("void f ( ) { char a [ 256 ] ; char b [ 256 ] ; }"); - ASSERT_EQUALS(expected1, tok(code1, false)); - - // Check for output.. - checkSimplifyTypedef(code1); - ASSERT_EQUALS("", errout.str()); - - const char code2[] = "typedef char TString[256];\n" - "void f()\n" - "{\n" - " TString a = { 0 }, b = { 0 };\n" - "}"; - - // The expected tokens.. - const std::string expected2("void f ( ) { char a [ 256 ] ; a = { 0 } ; char b [ 256 ] ; b = { 0 } ; }"); - ASSERT_EQUALS(expected2, tok(code2, false)); - - // Check for output.. - checkSimplifyTypedef(code2); - ASSERT_EQUALS("", errout.str()); - - const char code3[] = "typedef char TString[256];\n" - "void f()\n" - "{\n" - " TString a = \"\", b = \"\";\n" - "}"; - - // The expected tokens.. - const std::string expected3("void f ( ) { char a [ 256 ] ; a = \"\" ; char b [ 256 ] ; b = \"\" ; }"); - ASSERT_EQUALS(expected3, tok(code3, false)); - - // Check for output.. - checkSimplifyTypedef(code3); - ASSERT_EQUALS("", errout.str()); - - const char code4[] = "typedef char TString[256];\n" - "void f()\n" - "{\n" - " TString a = \"1234\", b = \"5678\";\n" - "}"; - - // The expected tokens.. - const std::string expected4("void f ( ) { char a [ 256 ] ; a = \"1234\" ; char b [ 256 ] ; b = \"5678\" ; }"); - ASSERT_EQUALS(expected4, tok(code4, false)); - - // Check for output.. - checkSimplifyTypedef(code4); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef63() { // ticket #2175 'typedef float x[3];' - const char code[] = "typedef float x[3];\n" - "x a,b,c;\n"; - const std::string actual(tok(code)); - ASSERT_EQUALS("float a [ 3 ] ; float b [ 3 ] ; float c [ 3 ] ;", actual); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef64() { - const char code[] = "typedef __typeof__(__type1() + __type2()) __type;" - "__type t;\n"; - const std::string actual(tok(code)); - ASSERT_EQUALS("__typeof__ ( __type1 ( ) + __type2 ( ) ) t ;", actual); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef65() { // ticket #2314 - const char code[] = "typedef BAR Foo;\n" - "int main() {\n" - " Foo b(0);\n" - " return b > Foo(10);\n" - "}"; - const std::string actual(tok(code)); - ASSERT_EQUALS("int main ( ) { BAR < int > b ( 0 ) ; return b > BAR < int > ( 10 ) ; }", actual); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef66() { // ticket #2341 - const char code[] = "typedef long* GEN;\n" - "extern GEN (*foo)(long);"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef67() { // ticket #2354 - const char code[] = "typedef int ( * Function ) ( ) ;\n" - "void f ( ) {\n" - " ((Function * (*) (char *, char *, int, int)) global[6]) ( \"assoc\", \"eggdrop\", 106, 0);\n" - "}\n"; - const std::string expected = "void f ( ) { " - "( ( int ( * * ( * ) ( char * , char * , int , int ) ) ( ) ) global [ 6 ] ) ( \"assoc\" , \"eggdrop\" , 106 , 0 ) ; " - "}"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef68() { // ticket #2355 - const char code[] = "typedef FMAC1 void (* a) ();\n" - "void *(*b) ();\n"; - const std::string actual(tok(code)); - ASSERT_EQUALS("void * * b ;", actual); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef69() { // ticket #2348 - const char code[] = "typedef int (*CompilerHook)();\n" - "typedef struct VirtualMachine\n" - "{\n" - " CompilerHook *(*compilerHookVector)(void);\n" - "}VirtualMachine;\n"; - const std::string expected = "struct VirtualMachine " - "{ " - "int ( * * ( * compilerHookVector ) ( void ) ) ( ) ; " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef70() { // ticket #2348 - const char code[] = "typedef int pread_f ( int ) ;\n" - "pread_f *(*test_func)(char *filename);\n"; - const std::string expected = "int ( * ( * test_func ) ( char * filename ) ) ( int ) ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef71() { // ticket #2348 - { - const char code[] = "typedef int RexxFunctionHandler();\n" - "RexxFunctionHandler *(efuncs[1]);\n"; - const std::string expected = "int ( * ( efuncs [ 1 ] ) ) ( ) ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - { - const char code[] = "typedef int RexxFunctionHandler();\n" - "RexxFunctionHandler *(efuncs[]) = { NULL, NULL };\n"; - const std::string expected = "int ( * ( efuncs [ ] ) ) ( ) = { 0 , 0 } ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedef72() { // ticket #2374 - // inline operator - { - const char code[] = "class Fred {\n" - " typedef int* (Fred::*F);\n" - " operator F() const { }\n" - "};\n"; - const std::string expected = "class Fred { " - "" - "operatorint** ( ) const { } " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - // inline local variable - { - const char code[] = "class Fred {\n" - " typedef int INT;\n" - " void f1() const { INT i; }\n" - "};\n"; - const std::string expected = "class Fred { " - "" - "void f1 ( ) const { int i ; } " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - // out of line member variable - { - const char code[] = "class Fred {\n" - " typedef int INT;\n" - " void f1() const;\n" - "};\n" - "void Fred::f1() const { INT i; f(i); }\n"; - const std::string expected = "class Fred { " - "" - "void f1 ( ) const ; " - "} ; " - "void Fred :: f1 ( ) const { int i ; f ( i ) ; }"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - // out of line operator - { - const char code[] = "class Fred {\n" - " typedef int* (Fred::*F);\n" - " operator F() const;\n" - "};\n" - "Fred::operator F() const { }\n"; - const std::string expected = "class Fred { " - "" - "operatorint** ( ) const ; " - "} ; " - "Fred :: operatorint** ( ) const { }"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedef73() { // ticket #2412 - const char code[] = "struct B {};\n" - "typedef struct A : public B {\n" - " void f();\n" - "} a, *aPtr;\n"; - const std::string expected = "struct B { } ; " - "struct A : public B { " - "void f ( ) ; " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef74() { // ticket #2414 - const char code[] = "typedef long (*state_func_t)(void);\n" - "typedef state_func_t (*state_t)(void);\n" - "state_t current_state = death;\n" - "static char get_runlevel(const state_t);\n"; - const std::string expected = "long ( * ( * current_state ) ( void ) ) ( void ) ; current_state = death ; " - "static char get_runlevel ( const long ( * ( * ) ( void ) ) ( void ) ) ;"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef75() { // ticket #2426 - const char code[] = "typedef _Packed struct S { long l; };\n"; - ASSERT_EQUALS("", tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef76() { // ticket #2453 segmentation fault - const char code[] = "void f1(typedef int x) {}\n"; - const std::string expected = "void f1 ( typedef int x ) { }"; - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef77() { // ticket #2554 - const char code[] = "typedef char Str[10]; int x = sizeof(Str);\n"; - const std::string expected = "int x ; x = 10 ;"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef78() { // ticket #2568 - const char code[] = "typedef struct A A_t;\n" - "A_t a;\n" - "typedef struct A { } A_t;\n" - "A_t a1;\n"; - const std::string expected = "struct A a ; struct A { } ; struct A a1 ;"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef79() { // ticket #2348 - const char code[] = "typedef int (Tcl_ObjCmdProc) (int x);\n" - "typedef struct LangVtab\n" - "{\n" - " Tcl_ObjCmdProc * (*V_LangOptionCommand);\n" - "} LangVtab;\n"; - const std::string expected = "struct LangVtab " - "{ " - "int ( * ( * V_LangOptionCommand ) ) ( int x ) ; " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef80() { // ticket #2587 - const char code[] = "typedef struct s { };\n" - "void f() {\n" - " sizeof(struct s);\n" - "};\n"; - const std::string expected = "struct s { } ; " - "void f ( ) { " - "sizeof ( struct s ) ; " - "} ;"; - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef81() { // ticket #2603 segmentation fault - ASSERT_THROW(checkSimplifyTypedef("typedef\n"), InternalError); - - checkSimplifyTypedef("typedef constexpr\n"); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef82() { // ticket #2403 - checkSimplifyTypedef("class A {\n" - "public:\n" - " typedef int F(int idx);\n" - "};\n" - "class B {\n" - "public:\n" - " A::F ** f;\n" - "};\n" - "int main()\n" - "{\n" - " B * b = new B;\n" - " b->f = new A::F * [ 10 ];\n" - "}"); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef83() { // ticket #2620 - const char code[] = "typedef char Str[10];\n" - "void f(Str &cl) { }\n"; - - // The expected result.. - const std::string expected("void f ( char ( & cl ) [ 10 ] ) { }"); - - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef84() { // ticket #2630 (segmentation fault) - const char code1[] = "typedef y x () x\n"; - ASSERT_THROW(checkSimplifyTypedef(code1), InternalError); - - const char code2[] = "typedef struct template <>\n"; - ASSERT_THROW(checkSimplifyTypedef(code2), InternalError); - - const char code3[] = "typedef ::<>\n"; - ASSERT_THROW(checkSimplifyTypedef(code3), InternalError); - } - - void simplifyTypedef85() { // ticket #2651 - const char code[] = "typedef FOO ((BAR)(void, int, const int, int*));\n"; - const char expected[] = ";"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef86() { // ticket #2581 - const char code[] = "class relational {\n" - " typedef void (safe_bool_helper::*safe_bool)();\n" - "public:\n" - " operator safe_bool() const;\n" - " safe_bool operator!() const;\n" - "};\n"; - const char expected[] = "class relational { " - "" - "public: " - "operatorsafe_bool ( ) const ; " - "safe_bool operator! ( ) const ; " - "} ;"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef87() { // ticket #2651 - const char code[] = "typedef FOO (*(*BAR)(void, int, const int, int*));\n"; - const char expected[] = ";"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef88() { // ticket #2675 - const char code[] = "typedef short int (*x)(...);\n"; - const char expected[] = ";"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef89() { // ticket #2717 - const char code[] = "class Fred {\n" - " typedef void f(int) const;\n" - " f func;\n" - "};\n"; - const char expected[] = "class Fred { void func ( int ) const ; } ;"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef90() { // ticket #2718 - const char code[] = "typedef int IA[2];\n" - "void f(const IA&) {};\n"; - const char expected[] = "void f ( const int ( & ) [ 2 ] ) { } ;"; - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef91() { // ticket #2716 - const char code1[] = "namespace NS {\n" - " typedef int (*T)();\n" - " class A {\n" - " T f();\n" - " };\n" - "}\n" - "namespace NS {\n" - " T A::f() {}\n" - "}\n"; - const char expected1[] = "namespace NS { " - "" - "class A { " - "int * f ( ) ; " - "} ; " - "} " - "namespace NS { " - "int * A :: f ( ) { } " - "}"; - checkSimplifyTypedef(code1); - ASSERT_EQUALS(expected1, tok(code1)); - ASSERT_EQUALS("", errout.str()); - - const char code2[] = "namespace NS {\n" - " typedef int (*T)();\n" - " class A {\n" - " T f();\n" - " };\n" - "}\n" - "NS::T NS::A::f() {}\n"; - const char expected2[] = "namespace NS { " - "" - "class A { " - "int * f ( ) ; " - "} ; " - "} " - "int * NS :: A :: f ( ) { }"; - checkSimplifyTypedef(code2); - ASSERT_EQUALS(expected2, tok(code2)); - ASSERT_EQUALS("", errout.str()); - - const char code3[] = "namespace NS1 {\n" - " namespace NS2 {\n" - " typedef int (*T)();\n" - " class A {\n" - " T f();\n" - " };\n" - " }\n" - "}\n" - "namespace NS1 {\n" - " namespace NS2 {\n" - " T A::f() {}\n" - " }\n" - "}\n"; - const char expected3[] = "namespace NS1 { " - "namespace NS2 { " - "" - "class A { " - "int * f ( ) ; " - "} ; " - "} " - "} " - "namespace NS1 { " - "namespace NS2 { " - "int * A :: f ( ) { } " - "} " - "}"; - checkSimplifyTypedef(code3); - ASSERT_EQUALS(expected3, tok(code3)); - ASSERT_EQUALS("", errout.str()); - - const char code4[] = "namespace NS1 {\n" - " namespace NS2 {\n" - " typedef int (*T)();\n" - " class A {\n" - " T f();\n" - " };\n" - " }\n" - "}\n" - "namespace NS1 {\n" - " NS2::T NS2::A::f() {}\n" - "}\n"; - const char expected4[] = "namespace NS1 { " - "namespace NS2 { " - "" - "class A { " - "int * f ( ) ; " - "} ; " - "} " - "} " - "namespace NS1 { " - "int * NS2 :: A :: f ( ) { } " - "}"; - checkSimplifyTypedef(code4); - ASSERT_EQUALS(expected4, tok(code4)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef92() { // ticket #2736 (segmentation fault) - const char code[] = "typedef long Long;\n" - "namespace NS {\n" - "}\n"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef93() { // ticket #2738 (syntax error) - const char code[] = "struct s { double x; };\n" - "typedef struct s (*binop) (struct s, struct s);\n"; - const char expected[] = "struct s { double x ; } ;"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef94() { // ticket #1982 - const char code1[] = "class A {\n" - "public:\n" - " typedef struct {\n" - " int a[4];\n" - " } data;\n" - "};\n" - "A::data d;\n"; - const char expected1[] = "class A { " - "public: " - "struct data { " - "int a [ 4 ] ; " - "} ; " - "} ; " - "struct A :: data d ;"; - - checkSimplifyTypedef(code1); - ASSERT_EQUALS(expected1, tok(code1)); - TODO_ASSERT_EQUALS("[test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0.\n", "", errout.str()); - - const char code2[] = "class A {\n" - "public:\n" - " typedef struct {\n" - " int a[4];\n" - " } data;\n" - "};\n" - "::A::data d;\n"; - const char expected2[] = "class A { " - "public: " - "struct data { " - "int a [ 4 ] ; " - "} ; " - "} ; " - "struct :: A :: data d ;"; - - checkSimplifyTypedef(code2); - ASSERT_EQUALS(expected2, tok(code2)); - TODO_ASSERT_EQUALS("[test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0.\n", "", errout.str()); - - const char code3[] = "class A {\n" - "public:\n" - " typedef struct {\n" - " int a[4];\n" - " } data;\n" - "};\n" - "class B : public ::A::data { };\n"; - const char expected3[] = "class A { " - "public: " - "struct data { " - "int a [ 4 ] ; " - "} ; " - "} ; " - "class B : public :: A :: data { } ;"; - - checkSimplifyTypedef(code3); - ASSERT_EQUALS(expected3, tok(code3)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef95() { // ticket #2844 - const char code[] = "class symbol_table {\n" - "public:\n" - " typedef expression_error::error_code (*valid_func)(void *cbparam, const char *name, expression_space space);\n" - " valid_func f;\n" - "};\n"; - const char expected[] = "class symbol_table { " - "public: " - "expression_error :: error_code * f ; " - "} ;"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef96() { // ticket #2886 (segmentation fault) - const char code[] = "typedef struct x { }\n"; - ASSERT_THROW(tok(code), InternalError); - } - - void simplifyTypedef97() { // ticket #2983 (segmentation fault) - const char code[] = "typedef x y\n" - "(A); y\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef98() { // ticket #2963 - const char code[] = "typedef int type ## __LINE__;\n" - "typedef int type ## __LINE__;\n" - "type1 x;\n" - "type2 y;"; - ASSERT_EQUALS("int x ; int y ;", tok(code)); - } - - void simplifyTypedef99() { // ticket #2999 - const char code[] = "typedef struct Fred Fred;\n" - "struct Fred { };\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - - const char code1[] = "struct Fred { };\n" - "typedef struct Fred Fred;\n"; - tok(code1); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef100() { // ticket #3000 - const char code[] = "typedef struct Fred { } Fred;\n" - "Fred * foo() {\n" - " Fred *fred;\n" - " fred = se_alloc(sizeof(struct Fred));\n" - " return fred;\n" - "}\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef101() { // ticket #3003 (segmentation fault) - const char code[] = "typedef a x[];\n" - "y = x\n"; - ASSERT_THROW(tok(code), InternalError); - } - - void simplifyTypedef102() { // ticket #3004 - const char code[] = "typedef struct { } Fred;\n" - "void foo()\n" - "{\n" - " Fred * Fred;\n" - "}\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef103() { // ticket #3007 - const char code[] = "typedef struct { } Fred;\n" - "void foo()\n" - "{\n" - " Fred Fred;\n" - "}\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef104() { // ticket #3070 - const char code[] = "typedef int (*in_func) (void FAR *, unsigned char FAR * FAR *);\n"; - ASSERT_EQUALS(";", tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef105() { // ticket #3616 (segmentation fault) - const char code[] = "( int typedef char x; ){}\n"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef106() { // ticket #3619 (segmentation fault) - const char code[] = "typedef void f ();\ntypedef { f }"; - tok(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedef107() { // ticket #3963 (bad code => segmentation fault) - const char code[] = "typedef int x[]; int main() { return x }"; - ASSERT_THROW(tok(code), InternalError); - } - - void simplifyTypedef108() { // ticket #4777 - const char code[] = "typedef long* GEN;\n" - "void sort_factor(GEN *y, long n) {\n" - " GEN a, b;\n" - " foo(a, b);\n" - "}\n"; - const char expected[] = "void sort_factor ( long * * y , long n ) { " - "long * a ; long * b ; " - "foo ( a , b ) ; " - "}"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedef109() { - const char code[] = "typedef int&& rref;\n" - "rref var;"; - const char expected[] = "int & & var ;"; - - checkSimplifyTypedef(code); - ASSERT_EQUALS(expected, tok(code)); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedefFunction1() { - { - const char code[] = "typedef void (*my_func)();\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (*my_func)(void);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (*my_func)(int);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (*my_func)(int*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - // ticket # 1615 - const char code[] = "typedef void (*my_func)(arg_class*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - - { - const char code[] = "typedef void (my_func)();\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func)(void);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func)(int);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func)(int*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func)(arg_class*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - - { - const char code[] = "typedef void my_func();\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void my_func(void);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void my_func(int);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void my_func(int*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void my_func(arg_class*);\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - - { - const char code[] = "typedef void (my_func());\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func(void));\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func(int));\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func(int*));\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef void (my_func(arg_class*));\n" - "std::queue func_queue;"; - - // The expected result.. - const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); - ASSERT_EQUALS(expected, tok(code)); - - // Check for output.. - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedefFunction2() { // ticket #1685 - const char code[] = "typedef void voidfn (int);\n" - "voidfn xxx;"; - - // The expected result.. - const std::string expected("void xxx ( int ) ;"); - ASSERT_EQUALS(expected, tok(code)); - } - - void simplifyTypedefFunction3() { - { - const char code[] = "typedef C func1();\n" - "typedef C (* func2)();\n" - "typedef C (& func3)();\n" - "typedef C (C::* func4)();\n" - "typedef C (C::* func5)() const;\n" - "typedef C (C::* func6)() volatile;\n" - "typedef C (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - const std::string expected("C f1 ( ) ; " - "C * f2 ; " // this gets simplified to a regular pointer - "C ( & f3 ) ( ) ; " - "C * f4 ; " - "C ( C :: * f5 ) ( ) const ; " - "C * f6 ; " // volatile is removed - "C ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef C const func1();\n" - "typedef C const (* func2)();\n" - "typedef C const (& func3)();\n" - "typedef C const (C::* func4)();\n" - "typedef C const (C::* func5)() const;\n" - "typedef C const (C::* func6)() volatile;\n" - "typedef C const (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - // C const -> const C - const std::string expected("const C f1 ( ) ; " - "const C * f2 ; " // this gets simplified to a regular pointer - "const C ( & f3 ) ( ) ; " - "const C * f4 ; " - "const C ( C :: * f5 ) ( ) const ; " - "const C * f6 ; " // volatile is removed - "const C ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef const C func1();\n" - "typedef const C (* func2)();\n" - "typedef const C (& func3)();\n" - "typedef const C (C::* func4)();\n" - "typedef const C (C::* func5)() const;\n" - "typedef const C (C::* func6)() volatile;\n" - "typedef const C (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - const std::string expected("const C f1 ( ) ; " - "const C * f2 ; " // this gets simplified to a regular pointer - "const C ( & f3 ) ( ) ; " - "const C * f4 ; " - "const C ( C :: * f5 ) ( ) const ; " - "const C * f6 ; " // volatile is removed - "const C ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef C * func1();\n" - "typedef C * (* func2)();\n" - "typedef C * (& func3)();\n" - "typedef C * (C::* func4)();\n" - "typedef C * (C::* func5)() const;\n" - "typedef C * (C::* func6)() volatile;\n" - "typedef C * (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - const std::string expected("C * f1 ( ) ; " - "C * * f2 ; " // this gets simplified to a regular pointer - "C * ( & f3 ) ( ) ; " - "C * * f4 ; " - "C * ( C :: * f5 ) ( ) const ; " - "C * * f6 ; " // volatile is removed - "C * ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef const C * func1();\n" - "typedef const C * (* func2)();\n" - "typedef const C * (& func3)();\n" - "typedef const C * (C::* func4)();\n" - "typedef const C * (C::* func5)() const;\n" - "typedef const C * (C::* func6)() volatile;\n" - "typedef const C * (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - const std::string expected("const C * f1 ( ) ; " - "const C * * f2 ; " // this gets simplified to a regular pointer - "const C * ( & f3 ) ( ) ; " - "const C * * f4 ; " - "const C * ( C :: * f5 ) ( ) const ; " - "const C * * f6 ; " // volatile is removed - "const C * ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - { - const char code[] = "typedef C const * func1();\n" - "typedef C const * (* func2)();\n" - "typedef C const * (& func3)();\n" - "typedef C const * (C::* func4)();\n" - "typedef C const * (C::* func5)() const;\n" - "typedef C const * (C::* func6)() volatile;\n" - "typedef C const * (C::* func7)() const volatile;\n" - "func1 f1;\n" - "func2 f2;\n" - "func3 f3;\n" - "func4 f4;\n" - "func5 f5;\n" - "func6 f6;\n" - "func7 f7;"; - - // The expected result.. - // C const -> const C - const std::string expected("const C * f1 ( ) ; " - "const C * * f2 ; " // this gets simplified to a regular pointer - "const C * ( & f3 ) ( ) ; " - "const C * * f4 ; " - "const C * ( C :: * f5 ) ( ) const ; " - "const C * * f6 ; " // volatile is removed - "const C * ( C :: * f7 ) ( ) const ;"); // volatile is removed - ASSERT_EQUALS(expected, tok(code)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - } - - void simplifyTypedefFunction4() { - const char code[] = "typedef int ( * ( * type1 ) ( bool ) ) ( int , int ) ;\n" - "typedef int ( * ( type2 ) ( bool ) ) ( int , int ) ;\n" - "typedef int ( * type3 ( bool ) ) ( int , int ) ;\n" - "type1 t1;\n" - "type2 t2;\n" - "type3 t3;"; - - // The expected result.. - const std::string expected("int ( * ( * t1 ) ( bool ) ) ( int , int ) ; " - "int * t2 ( bool ) ; " - "int * t3 ( bool ) ;"); - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedefFunction5() { - const char code[] = "typedef int ( * type1 ) ( float ) ;\n" - "typedef int ( * const type2 ) ( float ) ;\n" - "typedef int ( * volatile type3 ) ( float ) ;\n" - "typedef int ( * const volatile type4 ) ( float ) ;\n" - "typedef int ( C :: * type5 ) ( float ) ;\n" - "typedef int ( C :: * const type6 ) ( float ) ;\n" - "typedef int ( C :: * volatile type7 ) ( float ) ;\n" - "typedef int ( C :: * const volatile type8 ) ( float ) ;\n" - "typedef int ( :: C :: * type9 ) ( float ) ;\n" - "typedef int ( :: C :: * const type10 ) ( float ) ;\n" - "typedef int ( :: C :: * volatile type11 ) ( float ) ;\n" - "typedef int ( :: C :: * const volatile type12 ) ( float ) ;\n" - "type1 t1;\n" - "type2 t2;\n" - "type3 t3;\n" - "type4 t4;\n" - "type5 t5;\n" - "type6 t6;\n" - "type7 t7;\n" - "type8 t8;\n" - "type9 t9;\n" - "type10 t10;\n" - "type11 t11;\n" - "type12 t12;"; - - // The expected result.. - const std::string expected("int * t1 ; " // simplified to regular pointer - "int * const t2 ; " - "int * t3 ; " // volatile removed, gets simplified to regular pointer - "int * const t4 ; " // volatile removed - "int * t5 ; " - "int * const t6 ; " - "int * t7 ; " // volatile removed - "int * const t8 ; " // volatile removed - "int ( :: C :: * t9 ) ( float ) ; " - "int ( :: C :: * const t10 ) ( float ) ; " - "int ( :: C :: * t11 ) ( float ) ; " // volatile removed - "int ( :: C :: * const t12 ) ( float ) ;"); // volatile removed - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedefFunction6() { - const char code[] = "typedef void (*testfp)();\n" - "struct Fred\n" - "{\n" - " testfp get1() { return 0; }\n" - " void ( * get2 ( ) ) ( ) { return 0 ; }\n" - " testfp get3();\n" - " void ( * get4 ( ) ) ( );\n" - "};\n" - "testfp Fred::get3() { return 0; }\n" - "void ( * Fred::get4 ( ) ) ( ) { return 0 ; }\n"; - - // The expected result.. - const std::string expected("struct Fred " - "{ " - "void * get1 ( ) { return 0 ; } " - "void * get2 ( ) { return 0 ; } " - "void * get3 ( ) ; " - "void * get4 ( ) ; " - "} ; " - "void * Fred :: get3 ( ) { return 0 ; } " - "void * Fred :: get4 ( ) { return 0 ; }"); - - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedefFunction7() { - const char code[] = "typedef void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * _func_Tp_SGIAssignableConcept ) () ;" - "_func_Tp_SGIAssignableConcept X;\n"; - - // The expected result.. - const std::string expected("void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * X ) ( ) ;"); - - ASSERT_EQUALS(expected, tok(code, false)); - - checkSimplifyTypedef(code); - ASSERT_EQUALS("", errout.str()); - } - - void simplifyTypedefFunction8() { - // #2376 - internal error - const char code[] = "typedef int f_expand(const nrv_byte *);\n" - "void f(f_expand *(*get_fexp(int))){}\n"; - checkSimplifyTypedef(code); - TODO_ASSERT_EQUALS("", "[test.cpp:2]: (debug) Function::addArguments found argument 'int' with varid 0.\n", errout.str()); // make sure that there is no internal error - } - - void simplifyTypedefShadow() { // shadow variable (#4445) - const char code[] = "typedef struct { int x; } xyz;;\n" - "void f(){\n" - " int abc, xyz;\n" // <- shadow variable - "}\n"; - ASSERT_EQUALS("struct xyz { int x ; } ; void f ( ) { int abc ; int xyz ; }", - tok(code,false)); - } - void simplifyOperator1() { // #3237 - error merging namespaces with operators const char code[] = "class c {\n" diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp new file mode 100644 index 000000000..4a50ddce3 --- /dev/null +++ b/test/testsimplifytypedef.cpp @@ -0,0 +1,2983 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "testsuite.h" +#include "tokenize.h" +#include "token.h" +#include "settings.h" + +#include + +extern std::ostringstream errout; + + +class TestSimplifyTypedef : public TestFixture { +public: + TestSimplifyTypedef() : TestFixture("TestSimplifyTypedef") { + } + + +private: + + void run() { + TEST_CASE(simplifyTypedef1) + TEST_CASE(simplifyTypedef2) + TEST_CASE(simplifyTypedef3) + TEST_CASE(simplifyTypedef4) + TEST_CASE(simplifyTypedef5) + TEST_CASE(simplifyTypedef6) + TEST_CASE(simplifyTypedef7); + TEST_CASE(simplifyTypedef8); + TEST_CASE(simplifyTypedef9); + TEST_CASE(simplifyTypedef10); + TEST_CASE(simplifyTypedef11); + TEST_CASE(simplifyTypedef12); + TEST_CASE(simplifyTypedef13); + TEST_CASE(simplifyTypedef14); + TEST_CASE(simplifyTypedef15); + TEST_CASE(simplifyTypedef16); + TEST_CASE(simplifyTypedef17); + TEST_CASE(simplifyTypedef18); // typedef vector a; + TEST_CASE(simplifyTypedef19); + TEST_CASE(simplifyTypedef20); + TEST_CASE(simplifyTypedef21); + TEST_CASE(simplifyTypedef22); + TEST_CASE(simplifyTypedef23); + TEST_CASE(simplifyTypedef24); + TEST_CASE(simplifyTypedef25); + TEST_CASE(simplifyTypedef26); + TEST_CASE(simplifyTypedef27); + TEST_CASE(simplifyTypedef28); + TEST_CASE(simplifyTypedef29); + TEST_CASE(simplifyTypedef30); + TEST_CASE(simplifyTypedef31); + TEST_CASE(simplifyTypedef32); + TEST_CASE(simplifyTypedef33); + TEST_CASE(simplifyTypedef34); // ticket #1411 + TEST_CASE(simplifyTypedef35); + TEST_CASE(simplifyTypedef36); // ticket #1434 + TEST_CASE(simplifyTypedef37); // ticket #1449 + TEST_CASE(simplifyTypedef38); + TEST_CASE(simplifyTypedef39); + TEST_CASE(simplifyTypedef40); + TEST_CASE(simplifyTypedef41); // ticket #1488 + TEST_CASE(simplifyTypedef42); // ticket #1506 + TEST_CASE(simplifyTypedef43); // ticket #1588 + TEST_CASE(simplifyTypedef44); + TEST_CASE(simplifyTypedef45); // ticket #1613 + TEST_CASE(simplifyTypedef46); + TEST_CASE(simplifyTypedef47); + TEST_CASE(simplifyTypedef48); // ticket #1673 + TEST_CASE(simplifyTypedef49); // ticket #1691 + TEST_CASE(simplifyTypedef50); + TEST_CASE(simplifyTypedef51); + TEST_CASE(simplifyTypedef52); // ticket #1782 + TEST_CASE(simplifyTypedef53); // ticket #1801 + TEST_CASE(simplifyTypedef54); // ticket #1814 + TEST_CASE(simplifyTypedef55); + TEST_CASE(simplifyTypedef56); // ticket #1829 + TEST_CASE(simplifyTypedef57); // ticket #1846 + TEST_CASE(simplifyTypedef58); // ticket #1963 + TEST_CASE(simplifyTypedef59); // ticket #2011 + TEST_CASE(simplifyTypedef60); // ticket #2035 + TEST_CASE(simplifyTypedef61); // ticket #2074 and 2075 + TEST_CASE(simplifyTypedef62); // ticket #2082 + TEST_CASE(simplifyTypedef63); // ticket #2175 'typedef float x[3];' + TEST_CASE(simplifyTypedef64); + TEST_CASE(simplifyTypedef65); // ticket #2314 + TEST_CASE(simplifyTypedef66); // ticket #2341 + TEST_CASE(simplifyTypedef67); // ticket #2354 + TEST_CASE(simplifyTypedef68); // ticket #2355 + TEST_CASE(simplifyTypedef69); // ticket #2348 + TEST_CASE(simplifyTypedef70); // ticket #2348 + TEST_CASE(simplifyTypedef71); // ticket #2348 + TEST_CASE(simplifyTypedef72); // ticket #2375 + TEST_CASE(simplifyTypedef73); // ticket #2412 + TEST_CASE(simplifyTypedef74); // ticket #2414 + TEST_CASE(simplifyTypedef75); // ticket #2426 + TEST_CASE(simplifyTypedef76); // ticket #2453 + TEST_CASE(simplifyTypedef77); // ticket #2554 + TEST_CASE(simplifyTypedef78); // ticket #2568 + TEST_CASE(simplifyTypedef79); // ticket #2348 + TEST_CASE(simplifyTypedef80); // ticket #2587 + TEST_CASE(simplifyTypedef81); // ticket #2603 + TEST_CASE(simplifyTypedef82); // ticket #2403 + TEST_CASE(simplifyTypedef83); // ticket #2620 + TEST_CASE(simplifyTypedef84); // ticket #2630 + TEST_CASE(simplifyTypedef85); // ticket #2651 + TEST_CASE(simplifyTypedef86); // ticket #2581 + TEST_CASE(simplifyTypedef87); // ticket #2651 + TEST_CASE(simplifyTypedef88); // ticket #2675 + TEST_CASE(simplifyTypedef89); // ticket #2717 + TEST_CASE(simplifyTypedef90); // ticket #2718 + TEST_CASE(simplifyTypedef91); // ticket #2716 + TEST_CASE(simplifyTypedef92); // ticket #2736 + TEST_CASE(simplifyTypedef93); // ticket #2738 + TEST_CASE(simplifyTypedef94); // ticket #1982 + TEST_CASE(simplifyTypedef95); // ticket #2844 + TEST_CASE(simplifyTypedef96); // ticket #2886 + TEST_CASE(simplifyTypedef97); // ticket #2983 (segmentation fault) + TEST_CASE(simplifyTypedef98); // ticket #2963 + TEST_CASE(simplifyTypedef99); // ticket #2999 + TEST_CASE(simplifyTypedef100); // ticket #3000 + TEST_CASE(simplifyTypedef101); // ticket #3003 (segmentation fault) + TEST_CASE(simplifyTypedef102); // ticket #3004 + TEST_CASE(simplifyTypedef103); // ticket #3007 + TEST_CASE(simplifyTypedef104); // ticket #3070 + TEST_CASE(simplifyTypedef105); // ticket #3616 + TEST_CASE(simplifyTypedef106); // ticket #3619 + TEST_CASE(simplifyTypedef107); // ticket #3963 - bad code => segmentation fault + TEST_CASE(simplifyTypedef108); // ticket #4777 + TEST_CASE(simplifyTypedef109); // ticket #1823 - rvalue reference + + TEST_CASE(simplifyTypedefFunction1); + TEST_CASE(simplifyTypedefFunction2); // ticket #1685 + TEST_CASE(simplifyTypedefFunction3); + TEST_CASE(simplifyTypedefFunction4); + TEST_CASE(simplifyTypedefFunction5); + TEST_CASE(simplifyTypedefFunction6); + TEST_CASE(simplifyTypedefFunction7); + TEST_CASE(simplifyTypedefFunction8); + + TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable + } + + std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Unspecified) { + errout.str(""); + + Settings settings; + settings.addEnabled("portability"); + settings.platform(type); + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + if (simplify) + tokenizer.simplifyTokenList2(); + + return tokenizer.tokens()->stringifyList(0, !simplify); + } + + std::string simplifyTypedef(const char code[]) { + errout.str(""); + + Settings settings; + Tokenizer tokenizer(&settings, this); + + std::istringstream istr(code); + tokenizer.list.createTokens(istr); + tokenizer.createLinks(); + tokenizer.simplifyTypedef(); + + return tokenizer.tokens()->stringifyList(0, false); + } + + + + void simplifyTypedef1() { + const char code[] = "class A\n" + "{\n" + "public:\n" + " typedef wchar_t duplicate;\n" + " void foo() {}\n" + "};\n" + "typedef A duplicate;\n" + "int main()\n" + "{\n" + " duplicate a;\n" + " a.foo();\n" + " A::duplicate c = 0;\n" + "}\n"; + + const std::string expected = + "class A " + "{ " + "public: " + "" + "void foo ( ) { } " + "} ; " + "int main ( ) " + "{ " + "A a ; " + "a . foo ( ) ; " + "wchar_t c ; c = 0 ; " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef2() { + const char code[] = "class A;\n" + "typedef A duplicate;\n" + "class A\n" + "{\n" + "public:\n" + "typedef wchar_t duplicate;\n" + "duplicate foo() { wchar_t b; return b; }\n" + "};\n"; + + const std::string expected = + "class A ; " + "class A " + "{ " + "public: " + "" + "wchar_t foo ( ) { wchar_t b ; return b ; } " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef3() { + const char code[] = "class A {};\n" + "typedef A duplicate;\n" + "wchar_t foo()\n" + "{\n" + "typedef wchar_t duplicate;\n" + "duplicate b;\n" + "return b;\n" + "}\n" + "int main()\n" + "{\n" + "duplicate b;\n" + "}\n"; + + const std::string expected = + "class A { } ; " + "wchar_t foo ( ) " + "{ " + "" + "wchar_t b ; " + "return b ; " + "} " + "int main ( ) " + "{ " + "A b ; " + "}"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef4() { + const char code[] = "typedef int s32;\n" + "typedef unsigned int u32;\n" + "void f()\n" + "{\n" + " s32 ivar = -2;\n" + " u32 uvar = 2;\n" + " return uvar / ivar;\n" + "}\n"; + + const std::string expected = + "void f ( ) " + "{ " + "int ivar ; ivar = -2 ; " + "unsigned int uvar ; uvar = 2 ; " + "return uvar / ivar ; " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef5() { + // ticket #780 + const char code[] = + "typedef struct yy_buffer_state *YY_BUFFER_STATE;\n" + "void f()\n" + "{\n" + " YY_BUFFER_STATE state;\n" + "}\n"; + + const char expected[] = + "void f ( ) " + "{ " + "struct yy_buffer_state * state ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef6() { + // ticket #983 + const char code[] = + "namespace VL {\n" + " typedef float float_t ;\n" + " inline VL::float_t fast_atan2(VL::float_t y, VL::float_t x){}\n" + "}\n"; + + const char expected[] = + "namespace VL { " + "" + "float fast_atan2 ( float y , float x ) { } " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef7() { + const char code[] = "typedef int abc ; " + "Fred :: abc f ;"; + const char expected[] = "Fred :: abc f ;"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef8() { + const char code[] = "typedef int INT;\n" + "typedef unsigned int UINT;\n" + "typedef int * PINT;\n" + "typedef unsigned int * PUINT;\n" + "typedef int & RINT;\n" + "typedef unsigned int & RUINT;\n" + "typedef const int & RCINT;\n" + "typedef const unsigned int & RCUINT;\n" + "INT ti;\n" + "UINT tui;\n" + "PINT tpi;\n" + "PUINT tpui;\n" + "RINT tri;\n" + "RUINT trui;\n" + "RCINT trci;\n" + "RCUINT trcui;"; + + const char expected[] = + "int ti ; " + "unsigned int tui ; " + "int * tpi ; " + "unsigned int * tpui ; " + "int & tri ; " + "unsigned int & trui ; " + "const int & trci ; " + "const unsigned int & trcui ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef9() { + const char code[] = "typedef struct s S, * PS;\n" + "typedef struct t { int a; } T, *TP;\n" + "typedef struct { int a; } U;\n" + "typedef struct { int a; } * V;\n" + "S s;\n" + "PS ps;\n" + "T t;\n" + "TP tp;\n" + "U u;\n" + "V v;"; + + const char expected[] = + "struct t { int a ; } ; " + "struct U { int a ; } ; " + "struct Unnamed0 { int a ; } ; " + "struct s s ; " + "struct s * ps ; " + "struct t t ; " + "struct t * tp ; " + "struct U u ; " + "struct Unnamed0 * v ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef10() { + const char code[] = "typedef union s S, * PS;\n" + "typedef union t { int a; float b ; } T, *TP;\n" + "typedef union { int a; float b; } U;\n" + "typedef union { int a; float b; } * V;\n" + "S s;\n" + "PS ps;\n" + "T t;\n" + "TP tp;\n" + "U u;\n" + "V v;"; + + const char expected[] = + "union t { int a ; float b ; } ; " + "union U { int a ; float b ; } ; " + "union Unnamed1 { int a ; float b ; } ; " + "union s s ; " + "union s * ps ; " + "union t t ; " + "union t * tp ; " + "union U u ; " + "union Unnamed1 * v ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef11() { + const char code[] = "typedef enum { a = 0 , b = 1 , c = 2 } abc;\n" + "typedef enum xyz { x = 0 , y = 1 , z = 2 } XYZ;\n" + "abc e1;\n" + "XYZ e2;"; + + const char expected[] = "int e1 ; " + "int e2 ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef12() { + const char code[] = "typedef vector V1;\n" + "typedef std::vector V2;\n" + "typedef std::vector > V3;\n" + "typedef std::list::iterator IntListIterator;\n" + "V1 v1;\n" + "V2 v2;\n" + "V3 v3;\n" + "IntListIterator iter;"; + + const char expected[] = + "vector < int > v1 ; " + "std :: vector < int > v2 ; " + "std :: vector < std :: vector < int > > v3 ; " + "std :: list < int > :: iterator iter ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef13() { + // ticket # 1167 + const char code[] = "typedef std::pair Func;" + "typedef std::vector CallQueue;" + "int main() {}"; + + // Tokenize and check output.. + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef14() { + // ticket # 1232 + const char code[] = "template struct E" + "{" + " typedef E0)?(N-1):0> v;" + " typedef typename add::val val;" + " FP_M(val);" + "};" + "template struct E " + "{" + " typedef typename D<1>::val val;" + " FP_M(val);" + "};"; + + // Tokenize and check output.. + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef15() { + { + const char code[] = "typedef char frame[10];\n" + "frame f;"; + + const char expected[] = "char f [ 10 ] ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "typedef unsigned char frame[10];\n" + "frame f;"; + + const char expected[] = "unsigned char f [ 10 ] ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef16() { + // ticket # 1252 + const char code[] = "typedef char MOT8;\n" + "typedef MOT8 CHFOO[4096];\n" + "typedef struct {\n" + " CHFOO freem;\n" + "} STRFOO;"; + + // Tokenize and check output.. + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef17() { + const char code[] = "typedef char * PCHAR, CHAR;\n" + "PCHAR pc;\n" + "CHAR c;"; + + const char expected[] = + "char * pc ; " + "char c ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef18() { + const char code[] = "typedef vector a;\n" + "a b;\n"; + + // Clear the error buffer.. + errout.str(""); + + Settings settings; + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList2(); + + tokenizer.validate(); + } + + void simplifyTypedef19() { + { + // ticket #1275 + const char code[] = "typedef struct {} A, *B, **C;\n" + "A a;\n" + "B b;\n" + "C c;"; + + const char expected[] = + "struct A { } ; " + "struct A a ; " + "struct A * b ; " + "struct A * * c ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "typedef struct {} A, *********B;\n" + "A a;\n" + "B b;"; + + const char expected[] = + "struct A { } ; " + "struct A a ; " + "struct A * * * * * * * * * b ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "typedef struct {} **********A, *B, C;\n" + "A a;\n" + "B b;\n" + "C c;"; + + const char expected[] = + "struct Unnamed2 { } ; " + "struct Unnamed2 * * * * * * * * * * a ; " + "struct Unnamed2 * b ; " + "struct Unnamed2 c ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef20() { + // ticket #1284 + const char code[] = "typedef jobject invoke_t (jobject, Proxy *, Method *, JArray< jobject > *);"; + + // Clear the error buffer.. + errout.str(""); + + Settings settings; + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + + tokenizer.simplifyTokenList2(); + + tokenizer.validate(); + } + + void simplifyTypedef21() { + const char code[] = "typedef void (* PF)();\n" + "typedef void * (* PFV)(void *);\n" + "PF pf;\n" + "PFV pfv;"; + + const char expected[] = + "" + "" + "void ( * pf ) ( ) ; " + "void * ( * pfv ) ( void * ) ;"; + + ASSERT_EQUALS(expected, simplifyTypedef(code)); + } + + void simplifyTypedef22() { + { + const char code[] = "class Fred {\n" + " typedef void (*testfp)();\n" + " testfp get() { return test; }\n" + " static void test() { }\n" + "};"; + + const char expected[] = + "class Fred { " + "" + "void * get ( ) { return test ; } " + "static void test ( ) { } " + "} ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "class Fred {\n" + " typedef void * (*testfp)(void *);\n" + " testfp get() { return test; }\n" + " static void * test(void * p) { return p; }\n" + "};\n"; + + const char expected[] = + "class Fred { " + "" + "void * * get ( ) { return test ; } " + "static void * test ( void * p ) { return p ; } " + "} ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "class Fred {\n" + " typedef unsigned int * (*testfp)(unsigned int *);\n" + " testfp get() { return test; }\n" + " static unsigned int * test(unsigned int * p) { return p; }\n" + "};\n"; + + const char expected[] = + "class Fred { " + "" + "unsigned int * * get ( ) { return test ; } " + "static unsigned int * test ( unsigned int * p ) { return p ; } " + "} ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "class Fred {\n" + " typedef const unsigned int * (*testfp)(const unsigned int *);\n" + " testfp get() { return test; }\n" + " static const unsigned int * test(const unsigned int * p) { return p; }\n" + "};\n"; + + // static const gets changed to const static + const char expected[] = + "class Fred { " + "" + "const unsigned int * * get ( ) { return test ; } " + "const static unsigned int * test ( const unsigned int * p ) { return p ; } " + "} ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "class Fred {\n" + " typedef void * (*testfp)(void *);\n" + " testfp get(int i) { return test; }\n" + " static void * test(void * p) { return p; }\n" + "};\n"; + + const char expected[] = + "class Fred { " + "" + "void * * get ( int i ) { return test ; } " + "static void * test ( void * p ) { return p ; } " + "} ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef23() { + const char code[] = "typedef bool (*Callback) (int i);\n" + "void addCallback(Callback callback) { }\n" + "void addCallback1(Callback callback, int j) { }"; + + const char expected[] = + "void addCallback ( bool * callback ) { } " + "void addCallback1 ( bool * callback , int j ) { }"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef24() { + { + const char code[] = "typedef int (*fp)();\n" + "void g( fp f )\n" + "{\n" + " fp f2 = (fp)f;\n" + "}"; + + const char expected[] = + "void g ( int * f ) " + "{ " + "int * f2 ; f2 = ( int * ) f ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "typedef int (*fp)();\n" + "void g( fp f )\n" + "{\n" + " fp f2 = static_cast(f);\n" + "}"; + + const char expected[] = + "void g ( int * f ) " + "{ " + "int * f2 ; f2 = static_cast < int * > ( f ) ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef25() { + { + // ticket #1298 + const char code[] = "typedef void (*fill_names_f) (const char *);\n" + "struct vfs_class {\n" + " void (*fill_names) (struct vfs_class *me, fill_names_f);\n" + "}"; + + const char expected[] = + "struct vfs_class { " + "void ( * fill_names ) ( struct vfs_class * me , void ( * ) ( const char * ) ) ; " + "}"; + + ASSERT_EQUALS(expected, simplifyTypedef(code)); + } + + { + const char code[] = "typedef void (*fill_names_f) (const char *);\n" + "struct vfs_class {\n" + " void (*fill_names) (fill_names_f, struct vfs_class *me);\n" + "}"; + + const char expected[] = + "struct vfs_class { " + "void ( * fill_names ) ( void ( * ) ( const char * ) , struct vfs_class * me ) ; " + "}"; + + ASSERT_EQUALS(expected, simplifyTypedef(code)); + } + } + + void simplifyTypedef26() { + { + const char code[] = "typedef void (*Callback) ();\n" + "void addCallback(Callback (*callback)());"; + + const char expected[] = "void addCallback ( void ( * ( * callback ) ( ) ) ( ) ) ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + // ticket # 1307 + const char code[] = "typedef void (*pc_video_update_proc)(bitmap_t *bitmap,\n" + "struct mscrtc6845 *crtc);\n" + "\n" + "struct mscrtc6845 *pc_video_start(pc_video_update_proc (*choosevideomode)(running_machine *machine, int *width, int *height, struct mscrtc6845 *crtc));"; + + const char expected[] = "struct mscrtc6845 * pc_video_start ( void ( * ( * choosevideomode ) ( running_machine * machine , int * width , int * height , struct mscrtc6845 * crtc ) ) ( bitmap_t * bitmap , struct mscrtc6845 * crtc ) ) ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef27() { + // ticket #1316 + const char code[] = "int main()\n" + "{\n" + " typedef int (*func_ptr)(float, double);\n" + " VERIFY((is_same::type, int>::value));\n" + "}"; + + const char expected[] = + "int main ( ) " + "{ " + "" + "VERIFY ( is_same < result_of < int ( * ( char , float ) ) ( float , double ) > :: type , int > :: value ) ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef28() { + const char code[] = "typedef std::pair (*F)(double);\n" + "F f;"; + + const char expected[] = "std :: pair < double , double > ( * f ) ( double ) ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef29() { + const char code[] = "typedef int array [ice_or::value, is_int::value>::value ? 1 : -1];\n" + "typedef int array1 [N];\n" + "typedef int array2 [N][M];\n" + "typedef int int_t, int_array[N];\n" + "array a;\n" + "array1 a1;\n" + "array2 a2;\n" + "int_t t;\n" + "int_array ia;"; + + const char expected[] = + "int a [ ice_or < is_int < int > :: value , is_int < UDT > :: value > :: value ? 1 : -1 ] ; " + "int a1 [ N ] ; " + "int a2 [ N ] [ M ] ; " + "int t ; " + "int ia [ N ] ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef30() { + const char code[] = "typedef ::std::list int_list;\n" + "typedef ::std::list::iterator int_list_iterator;\n" + "typedef ::std::list int_list_array[10];\n" + "int_list il;\n" + "int_list_iterator ili;\n" + "int_list_array ila;"; + + const char expected[] = + ":: std :: list < int > il ; " + ":: std :: list < int > :: iterator ili ; " + ":: std :: list < int > ila [ 10 ] ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef31() { + { + const char code[] = "class A {\n" + "public:\n" + " typedef int INT;\n" + " INT get() const;\n" + " void put(INT x) { a = x; }\n" + " INT a;\n" + "};\n" + "A::INT A::get() const { return a; }\n" + "A::INT i = A::a;"; + + const char expected[] = "class A { " + "public: " + "" + "int get ( ) const ; " + "void put ( int x ) { a = x ; } " + "int a ; " + "} ; " + "int A :: get ( ) const { return a ; } " + "int i ; i = A :: a ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "struct A {\n" + " typedef int INT;\n" + " INT get() const;\n" + " void put(INT x) { a = x; }\n" + " INT a;\n" + "};\n" + "A::INT A::get() const { return a; }\n" + "A::INT i = A::a;"; + + const char expected[] = "struct A { " + "" + "int get ( ) const ; " + "void put ( int x ) { a = x ; } " + "int a ; " + "} ; " + "int A :: get ( ) const { return a ; } " + "int i ; i = A :: a ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef32() { + const char code[] = "typedef char CHAR;\n" + "typedef CHAR * LPSTR;\n" + "typedef const CHAR * LPCSTR;\n" + "CHAR c;\n" + "LPSTR cp;\n" + "LPCSTR ccp;"; + + const char expected[] = + "char c ; " + "char * cp ; " + "const char * ccp ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef33() { + const char code[] = "class A {\n" + "public:\n" + " typedef char CHAR_A;\n" + " CHAR_A funA();\n" + " class B {\n" + " public:\n" + " typedef short SHRT_B;\n" + " SHRT_B funB();\n" + " class C {\n" + " public:\n" + " typedef int INT_C;\n" + " INT_C funC();\n" + " struct D {\n" + " typedef long LONG_D;\n" + " LONG_D funD();\n" + " LONG_D d;\n" + " };\n" + " INT_C c;\n" + " };\n" + " SHRT_B b;\n" + " };\n" + " CHAR_A a;\n" + "};\n" + "A::CHAR_A A::funA() { return a; }\n" + "A::B::SHRT_B A::B::funB() { return b; }\n" + "A::B::C::INT_C A::B::C::funC() { return c; }" + "A::B::C::D::LONG_D A::B::C::D::funD() { return d; }"; + + const char expected[] = + "class A { " + "public: " + "" + "char funA ( ) ; " + "class B { " + "public: " + "" + "short funB ( ) ; " + "class C { " + "public: " + "" + "int funC ( ) ; " + "struct D { " + "" + "long funD ( ) ; " + "long d ; " + "} ; " + "int c ; " + "} ; " + "short b ; " + "} ; " + "char a ; " + "} ; " + "char A :: funA ( ) { return a ; } " + "short A :: B :: funB ( ) { return b ; } " + "int A :: B :: C :: funC ( ) { return c ; } " + "long A :: B :: C :: D :: funD ( ) { return d ; }"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef34() { + // ticket #1411 + const char code[] = "class X { };\n" + "typedef X (*foofunc)(const X&);\n" + "int main()\n" + "{\n" + " foofunc *Foo = new foofunc[2];\n" + "}"; + const char expected[] = + "class X { } ; " + "int main ( ) " + "{ " + "X ( * * Foo ) ( const X & ) ; Foo = new X ( * ) ( const X & ) [ 2 ] ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + + // Check simplifyTypedef + void checkSimplifyTypedef(const char code[]) { + errout.str(""); + // Tokenize.. + Settings settings; + settings.inconclusive = true; + settings.addEnabled("style"); + settings.debugwarnings = true; // show warnings about unhandled typedef + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, "test.cpp"); + } + + void simplifyTypedef35() { + const char code[] = "typedef int A;\n" + "class S\n" + "{\n" + "public:\n" + " typedef float A;\n" + " A a;\n" + " virtual void fun(A x);\n" + "};\n" + "void S::fun(S::A) { };\n" + "class S1 : public S\n" + "{\n" + "public:\n" + " void fun(S::A) { }\n" + "};\n" + "struct T\n" + "{\n" + " typedef A B;\n" + " B b;\n" + "};\n" + "float fun1(float A) { return A; }\n" + "float fun2(float a) { float A = a++; return A; }\n" + "float fun3(int a)\n" + "{\n" + " typedef struct { int a; } A;\n" + " A s; s.a = a;\n" + " return s.a;\n" + "}\n" + "int main()\n" + "{\n" + " A a = 0;\n" + " S::A s = fun1(a) + fun2(a) - fun3(a);\n" + " return a + s;\n" + "}"; + + const char expected[] = "class S " + "{ " + "public: " + "" + "float a ; " + "virtual void fun ( float x ) ; " + "} ; " + "void S :: fun ( float ) { } ; " + "class S1 : public S " + "{ " + "public: " + "void fun ( float ) { } " + "} ; " + "struct T " + "{ " + "" + "int b ; " + "} ; " + "float fun1 ( float A ) { return A ; } " + "float fun2 ( float a ) { float A ; A = a ++ ; return A ; } " + "float fun3 ( int a ) " + "{ " + "struct A { int a ; } ; " + "struct A s ; s . a = a ; " + "return s . a ; " + "} " + "int main ( ) " + "{ " + "int a ; a = 0 ; " + "float s ; s = fun1 ( a ) + fun2 ( a ) - fun3 ( a ) ; " + "return a + s ; " + "}"; + + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:5] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" + "[test.cpp:20] -> [test.cpp:1]: (style, inconclusive) The function parameter 'A' hides a typedef with the same name.\n" + "[test.cpp:21] -> [test.cpp:1]: (style, inconclusive) The variable 'A' hides a typedef with the same name.\n" + "[test.cpp:24] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" + "[test.cpp:24]: (debug) ValueFlow bailout: parameter a, at '{'\n", errout.str()); + } + + void simplifyTypedef36() { + // ticket #1434 + const char code[] = "typedef void (*TIFFFaxFillFunc)();\n" + "void f(va_list ap)\n" + "{\n" + " *va_arg(ap, TIFFFaxFillFunc*) = 0;\n" + "}"; + const char expected[] = "void f ( va_list ap ) " + "{ " + "* va_arg ( ap , void ( * * ) ( ) ) = 0 ; " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + void simplifyTypedef37() { + { + // ticket #1449 + const char code[] = "template class V {};\n" + "typedef V A;\n" + "typedef int B;\n" + "typedef V A;\n" + "typedef int B;"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:2]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n" + "[test.cpp:5] -> [test.cpp:3]: (style, inconclusive) The typedef 'B' hides a typedef with the same name.\n", errout.str()); + } + + { + const char code[] = "typedef int INT;\n" + "void f()\n" + "{\n" + " INT i; { }\n" + "}"; + const char expected[] = "void f ( ) " + "{ " + "int i ; { } " + "}"; + ASSERT_EQUALS(expected, tok(code, false)); + } + } + + void simplifyTypedef38() { + const char code[] = "typedef C A;\n" + "struct AB : public A, public B { };"; + const char expected[] = "struct AB : public C , public B { } ;"; + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef39() { + const char code[] = "typedef int A;\n" + "template ::value;"; + const char expected[] = "template < const int , int > :: value ;"; + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef40() { + const char code[] = "typedef int A;\n" + "typedef int B;\n" + "template class C { };"; + const char expected[] = "template < class A , class B > class C { } ;"; + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:1]: (style, inconclusive) The template parameter 'A' hides a typedef with the same name.\n" + "[test.cpp:3] -> [test.cpp:2]: (style, inconclusive) The template parameter 'B' hides a typedef with the same name.\n", errout.str()); + + checkSimplifyTypedef("typedef tuple t2;\n" + "void ordering_test()\n" + "{\n" + " tuple t2(5, 3.3f);\n" + " BOOST_CHECK(t3 > t2);\n" + "}"); + ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:1]: (style, inconclusive) The template instantiation 't2' hides a typedef with the same name.\n", errout.str()); + + checkSimplifyTypedef("class MyOverflowingUnsigned\n" + "{\n" + "public:\n" + " typedef unsigned self_type::* bool_type;\n" + " operator bool_type() const { return this->v_ ? &self_type::v_ : 0; }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkSimplifyTypedef("typedef int (*fptr_type)(int, int);\n" + "struct which_one {\n" + " typedef fptr_type (*result_type)(bool x);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkSimplifyTypedef("class my_configuration\n" + "{\n" + "public:\n" + " template < typename T >\n" + " class hook\n" + " {\n" + " public:\n" + " typedef ::boost::rational rational_type;\n" + " public:\n" + " rational_type ( &r_ )[ 9 ];\n" + " };\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + checkSimplifyTypedef("class A\n" + "{\n" + " typedef B b;\n" + " friend b;\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef41() { + // ticket #1488 + checkSimplifyTypedef("class Y;\n" + "class X\n" + "{\n" + " typedef Y type;\n" + " friend class type;\n" + "};"); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef42() { + // ticket #1506 + checkSimplifyTypedef("typedef struct A { } A;\n" + "struct A;"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The struct 'A' forward declaration is unnecessary. Type struct is already declared earlier.\n", errout.str()); + + checkSimplifyTypedef("typedef union A { int i; float f; } A;\n" + "union A;"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The union 'A' forward declaration is unnecessary. Type union is already declared earlier.\n", errout.str()); + + const char code [] = "typedef std::map A;\n" + "class A;"; + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style) The class 'A' forward declaration is unnecessary. Type class is already declared earlier.\n", errout.str()); + TODO_ASSERT_EQUALS("class A ;", "class std :: map < std :: string , int > ;", tok(code)); + } + + void simplifyTypedef43() { + // ticket #1588 + { + const char code[] = "typedef struct foo A;\n" + "struct A\n" + "{\n" + " int alloclen;\n" + "};\n"; + + // The expected result.. + const std::string expected("struct A " + "{ " + "int alloclen ; " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The struct 'A' hides a typedef with the same name.\n", errout.str()); + } + + { + const char code[] = "typedef union foo A;\n" + "union A\n" + "{\n" + " int alloclen;\n" + "};\n"; + + // The expected result.. + const std::string expected("union A " + "{ " + "int alloclen ; " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The union 'A' hides a typedef with the same name.\n", errout.str()); + } + + { + const char code[] = "typedef class foo A;\n" + "class A\n" + "{\n" + " int alloclen;\n" + "};\n"; + + // The expected result.. + const std::string expected("class A " + "{ " + "int alloclen ; " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The class 'A' hides a typedef with the same name.\n", errout.str()); + } + } + + void simplifyTypedef44() { + { + const char code[] = "typedef std::map Map;\n" + "class MyMap : public Map\n" + "{\n" + "};\n"; + + // The expected result.. + const std::string expected("class MyMap : public std :: map < std :: string , int > " + "{ " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef std::map Map;\n" + "class MyMap : protected Map\n" + "{\n" + "};\n"; + + // The expected result.. + const std::string expected("class MyMap : protected std :: map < std :: string , int > " + "{ " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef std::map Map;\n" + "class MyMap : private Map\n" + "{\n" + "};\n"; + + // The expected result.. + const std::string expected("class MyMap : private std :: map < std :: string , int > " + "{ " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef struct foo { } A;\n" + "struct MyA : public A\n" + "{\n" + "};\n"; + + // The expected result.. + const std::string expected("struct foo { } ; " + "struct MyA : public foo " + "{ " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef class foo { } A;\n" + "class MyA : public A\n" + "{\n" + "};\n"; + + // The expected result.. + const std::string expected("class foo { } ; " + "class MyA : public foo " + "{ " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedef45() { + // ticket # 1613 + const char code[] = "void fn() {\n" + " typedef foo<> bar;\n" + " while (0 > bar(1)) {}\n" + "}"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef46() { + const char code[] = "typedef const struct A { int a; } * AP;\n" + "AP ap;\n"; + + // The expected result.. + const std::string expected("struct A { int a ; } ; " + "const struct A * ap ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef47() { + { + const char code[] = "typedef std::pair const I;\n" + "I i;"; + + // The expected result.. + const std::string expected("std :: pair < int , int > const i ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + { + const char code[] = "typedef void (X:: *F)();\n" + "F f;"; + + // The expected result.. + const std::string expected("void * f ;"); + ASSERT_EQUALS(expected, tok(code)); + } + } + + void simplifyTypedef48() { // ticket #1673 + const char code[] = "typedef struct string { } string;\n" + "void foo (LIST *module_name)\n" + "{\n" + " bar(module_name ? module_name->string : 0);\n" + "}\n"; + + // The expected result.. + const std::string expected("struct string { } ; " + "void foo ( LIST * module_name ) " + "{ " + "bar ( module_name ? module_name . string : 0 ) ; " + "}"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef49() { // ticket #1691 + const char code[] = "class Class2 {\n" + "typedef const Class & Const_Reference;\n" + "void some_method (Const_Reference x) const {}\n" + "void another_method (Const_Reference x) const {}\n" + "}"; + + // The expected result.. + const std::string expected("class Class2 { " + "" + "void some_method ( const Class & x ) const { } " + "void another_method ( const Class & x ) const { } " + "}"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef50() { + const char code[] = "typedef char (* type1)[10];\n" + "typedef char (& type2)[10];\n" + "typedef char (& type3)[x];\n" + "typedef char (& type4)[x + 2];\n" + "type1 t1;\n" + "type1 (*tp1)[2];\n" + "type2 t2;\n" + "type3 t3;\n" + "type4 t4;"; + + // The expected result.. + const std::string expected("char * t1 [ 10 ] ; " + "char ( * ( * tp1 ) [ 2 ] ) [ 10 ] ; " + "char ( & t2 ) [ 10 ] ; " + "char ( & t3 ) [ x ] ; " + "char ( & t4 ) [ x + 2 ] ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef51() { + const char code[] = "class A { public: int i; };\n" + "typedef const char (A :: * type1);\n" + "type1 t1 = &A::i;"; + + // The expected result.. + const std::string expected("class A { public: int i ; } ; " + "const char ( A :: * t1 ) ; t1 = & A :: i ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef52() { // ticket #1782 + { + const char code[] = "typedef char (* type1)[10];\n" + "type1 foo() { }"; + + // The expected result.. + const std::string expected("char ( * foo ( ) ) [ 10 ] { }"); + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef char (* type1)[10];\n" + "LOCAL(type1) foo() { }"; + + // this is invalid C so just make sure it doesn't generate an internal error + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedef53() { // ticket #1801 + { + const char code[] = "typedef int ( * int ( * ) ( ) ) ( ) ;"; + + // this is invalid C so just make sure it doesn't crash + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:1]: (debug) Failed to parse 'typedef int ( * int ( * ) ( ) ) ( ) ;'. The checking continues anyway.\n", errout.str()); + } + + { + const char code[] = "typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);\n" + "typedef int (*PPDMarkOption)(ppd_file_t *ppd, const char *keyword, const char *option);\n"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'PPDMarkOption' hides a typedef with the same name.\n", errout.str()); + } + + { + const char code[] = "typedef int * A;\n" + "typedef int * A;\n"; + checkSimplifyTypedef(code); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:1]: (style, inconclusive) The typedef 'A' hides a typedef with the same name.\n", errout.str()); + } + } + + void simplifyTypedef54() { // ticket #1814 + const char code[] = "void foo()\n" + "{\n" + " typedef std::basic_string string_type;\n" + " try\n" + " {\n" + " throw string_type(\"leak\");\n" + " }\n" + " catch (const string_type&)\n" + " {\n" + " pthread_exit (0);\n" + " }\n" + "}"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef55() { + const char code[] = "typedef volatile unsigned long * const hwreg_t ;\n" + "typedef void *const t1[2];\n" + "typedef int*const *_Iterator;\n" + "hwreg_t v1;\n" + "t1 v2;\n" + "_Iterator v3;\n"; + + // The expected result.. + const std::string expected("long * v1 ; " + "void * v2 [ 2 ] ; " + "int * * v3 ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef56() { // ticket #1829 + const char code[] = "struct C {\n" + " typedef void (*fptr)();\n" + " const fptr pr;\n" + " operator const fptr& () { return pr; }\n" + "};\n"; + + // The expected result.. + const std::string expected("struct C { " + "" + "const void * pr ; " // this gets simplified to a regular pointer + "operatorconstvoid(*)()& ( ) { return pr ; } " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef57() { // ticket #1846 + const char code[] = "void foo {\n" + " typedef int A;\n" + " A a = A(1) * A(2);\n" + "};\n"; + + // The expected result.. + const std::string expected("void foo { " + "" + "int a ; a = int ( 1 ) * int ( 2 ) ; " + "} ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef58() { // ticket #1963 + { + const char code[] = "typedef int vec2_t[2];\n" + "vec2_t coords[4] = {1,2,3,4,5,6,7,8};\n"; + + // The expected result.. + const std::string expected("int coords [ 4 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef int vec2_t[2];\n" + "vec2_t coords[4][5][6+1] = {1,2,3,4,5,6,7,8};\n"; + + // The expected result.. + const std::string expected("int coords [ 4 ] [ 5 ] [ 7 ] [ 2 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedef59() { // ticket #2011 + const char code[] = "template class SomeTemplateClass {\n" + " typedef void (SomeTemplateClass::*MessageDispatcherFunc)(SerialInputMessage&);\n" + "};\n"; + // The expected result.. + const std::string expected("template < typename DISPATCHER > class SomeTemplateClass { } ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef60() { // ticket #2035 + const char code[] = "typedef enum {qfalse, qtrue} qboolean;\n" + "typedef qboolean (*localEntitiyAddFunc_t) (struct le_s * le, entity_t * ent);\n" + "void f()\n" + "{\n" + " qboolean b;\n" + " localEntitiyAddFunc_t f;\n" + "}\n"; + // The expected result.. + const std::string expected("void f ( ) { int b ; int * f ; }"); + ASSERT_EQUALS(expected, tok(code, false)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef61() { // ticket #2074 and 2075 + const char code1[] = "typedef unsigned char (*Mf_GetIndexByte_Func) (void);\n" + "typedef const unsigned char * (*Mf_GetPointerToCurrentPos_Func)(void);\n"; + + // Check for output.. + checkSimplifyTypedef(code1); + ASSERT_EQUALS("", errout.str()); + + const char code2[] = "typedef unsigned long uint32_t;\n" + "typedef uint32_t (*write_type_t) (uint32_t);\n"; + + // Check for output.. + checkSimplifyTypedef(code2); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef62() { // ticket #2082 + const char code1[] = "typedef char TString[256];\n" + "void f()\n" + "{\n" + " TString a, b;\n" + "}"; + + // The expected tokens.. + const std::string expected1("void f ( ) { char a [ 256 ] ; char b [ 256 ] ; }"); + ASSERT_EQUALS(expected1, tok(code1, false)); + + // Check for output.. + checkSimplifyTypedef(code1); + ASSERT_EQUALS("", errout.str()); + + const char code2[] = "typedef char TString[256];\n" + "void f()\n" + "{\n" + " TString a = { 0 }, b = { 0 };\n" + "}"; + + // The expected tokens.. + const std::string expected2("void f ( ) { char a [ 256 ] ; a = { 0 } ; char b [ 256 ] ; b = { 0 } ; }"); + ASSERT_EQUALS(expected2, tok(code2, false)); + + // Check for output.. + checkSimplifyTypedef(code2); + ASSERT_EQUALS("", errout.str()); + + const char code3[] = "typedef char TString[256];\n" + "void f()\n" + "{\n" + " TString a = \"\", b = \"\";\n" + "}"; + + // The expected tokens.. + const std::string expected3("void f ( ) { char a [ 256 ] ; a = \"\" ; char b [ 256 ] ; b = \"\" ; }"); + ASSERT_EQUALS(expected3, tok(code3, false)); + + // Check for output.. + checkSimplifyTypedef(code3); + ASSERT_EQUALS("", errout.str()); + + const char code4[] = "typedef char TString[256];\n" + "void f()\n" + "{\n" + " TString a = \"1234\", b = \"5678\";\n" + "}"; + + // The expected tokens.. + const std::string expected4("void f ( ) { char a [ 256 ] ; a = \"1234\" ; char b [ 256 ] ; b = \"5678\" ; }"); + ASSERT_EQUALS(expected4, tok(code4, false)); + + // Check for output.. + checkSimplifyTypedef(code4); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef63() { // ticket #2175 'typedef float x[3];' + const char code[] = "typedef float x[3];\n" + "x a,b,c;\n"; + const std::string actual(tok(code)); + ASSERT_EQUALS("float a [ 3 ] ; float b [ 3 ] ; float c [ 3 ] ;", actual); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef64() { + const char code[] = "typedef __typeof__(__type1() + __type2()) __type;" + "__type t;\n"; + const std::string actual(tok(code)); + ASSERT_EQUALS("__typeof__ ( __type1 ( ) + __type2 ( ) ) t ;", actual); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef65() { // ticket #2314 + const char code[] = "typedef BAR Foo;\n" + "int main() {\n" + " Foo b(0);\n" + " return b > Foo(10);\n" + "}"; + const std::string actual(tok(code)); + ASSERT_EQUALS("int main ( ) { BAR < int > b ( 0 ) ; return b > BAR < int > ( 10 ) ; }", actual); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef66() { // ticket #2341 + const char code[] = "typedef long* GEN;\n" + "extern GEN (*foo)(long);"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef67() { // ticket #2354 + const char code[] = "typedef int ( * Function ) ( ) ;\n" + "void f ( ) {\n" + " ((Function * (*) (char *, char *, int, int)) global[6]) ( \"assoc\", \"eggdrop\", 106, 0);\n" + "}\n"; + const std::string expected = "void f ( ) { " + "( ( int ( * * ( * ) ( char * , char * , int , int ) ) ( ) ) global [ 6 ] ) ( \"assoc\" , \"eggdrop\" , 106 , 0 ) ; " + "}"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef68() { // ticket #2355 + const char code[] = "typedef FMAC1 void (* a) ();\n" + "void *(*b) ();\n"; + const std::string actual(tok(code)); + ASSERT_EQUALS("void * * b ;", actual); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef69() { // ticket #2348 + const char code[] = "typedef int (*CompilerHook)();\n" + "typedef struct VirtualMachine\n" + "{\n" + " CompilerHook *(*compilerHookVector)(void);\n" + "}VirtualMachine;\n"; + const std::string expected = "struct VirtualMachine " + "{ " + "int ( * * ( * compilerHookVector ) ( void ) ) ( ) ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef70() { // ticket #2348 + const char code[] = "typedef int pread_f ( int ) ;\n" + "pread_f *(*test_func)(char *filename);\n"; + const std::string expected = "int ( * ( * test_func ) ( char * filename ) ) ( int ) ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef71() { // ticket #2348 + { + const char code[] = "typedef int RexxFunctionHandler();\n" + "RexxFunctionHandler *(efuncs[1]);\n"; + const std::string expected = "int ( * ( efuncs [ 1 ] ) ) ( ) ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + { + const char code[] = "typedef int RexxFunctionHandler();\n" + "RexxFunctionHandler *(efuncs[]) = { NULL, NULL };\n"; + const std::string expected = "int ( * ( efuncs [ ] ) ) ( ) = { 0 , 0 } ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedef72() { // ticket #2374 + // inline operator + { + const char code[] = "class Fred {\n" + " typedef int* (Fred::*F);\n" + " operator F() const { }\n" + "};\n"; + const std::string expected = "class Fred { " + "" + "operatorint** ( ) const { } " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + // inline local variable + { + const char code[] = "class Fred {\n" + " typedef int INT;\n" + " void f1() const { INT i; }\n" + "};\n"; + const std::string expected = "class Fred { " + "" + "void f1 ( ) const { int i ; } " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + // out of line member variable + { + const char code[] = "class Fred {\n" + " typedef int INT;\n" + " void f1() const;\n" + "};\n" + "void Fred::f1() const { INT i; f(i); }\n"; + const std::string expected = "class Fred { " + "" + "void f1 ( ) const ; " + "} ; " + "void Fred :: f1 ( ) const { int i ; f ( i ) ; }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + // out of line operator + { + const char code[] = "class Fred {\n" + " typedef int* (Fred::*F);\n" + " operator F() const;\n" + "};\n" + "Fred::operator F() const { }\n"; + const std::string expected = "class Fred { " + "" + "operatorint** ( ) const ; " + "} ; " + "Fred :: operatorint** ( ) const { }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedef73() { // ticket #2412 + const char code[] = "struct B {};\n" + "typedef struct A : public B {\n" + " void f();\n" + "} a, *aPtr;\n"; + const std::string expected = "struct B { } ; " + "struct A : public B { " + "void f ( ) ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef74() { // ticket #2414 + const char code[] = "typedef long (*state_func_t)(void);\n" + "typedef state_func_t (*state_t)(void);\n" + "state_t current_state = death;\n" + "static char get_runlevel(const state_t);\n"; + const std::string expected = "long ( * ( * current_state ) ( void ) ) ( void ) ; current_state = death ; " + "static char get_runlevel ( const long ( * ( * ) ( void ) ) ( void ) ) ;"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef75() { // ticket #2426 + const char code[] = "typedef _Packed struct S { long l; };\n"; + ASSERT_EQUALS("", tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef76() { // ticket #2453 segmentation fault + const char code[] = "void f1(typedef int x) {}\n"; + const std::string expected = "void f1 ( typedef int x ) { }"; + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef77() { // ticket #2554 + const char code[] = "typedef char Str[10]; int x = sizeof(Str);\n"; + const std::string expected = "int x ; x = 10 ;"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef78() { // ticket #2568 + const char code[] = "typedef struct A A_t;\n" + "A_t a;\n" + "typedef struct A { } A_t;\n" + "A_t a1;\n"; + const std::string expected = "struct A a ; struct A { } ; struct A a1 ;"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef79() { // ticket #2348 + const char code[] = "typedef int (Tcl_ObjCmdProc) (int x);\n" + "typedef struct LangVtab\n" + "{\n" + " Tcl_ObjCmdProc * (*V_LangOptionCommand);\n" + "} LangVtab;\n"; + const std::string expected = "struct LangVtab " + "{ " + "int ( * ( * V_LangOptionCommand ) ) ( int x ) ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef80() { // ticket #2587 + const char code[] = "typedef struct s { };\n" + "void f() {\n" + " sizeof(struct s);\n" + "};\n"; + const std::string expected = "struct s { } ; " + "void f ( ) { " + "sizeof ( struct s ) ; " + "} ;"; + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef81() { // ticket #2603 segmentation fault + ASSERT_THROW(checkSimplifyTypedef("typedef\n"), InternalError); + + checkSimplifyTypedef("typedef constexpr\n"); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef82() { // ticket #2403 + checkSimplifyTypedef("class A {\n" + "public:\n" + " typedef int F(int idx);\n" + "};\n" + "class B {\n" + "public:\n" + " A::F ** f;\n" + "};\n" + "int main()\n" + "{\n" + " B * b = new B;\n" + " b->f = new A::F * [ 10 ];\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef83() { // ticket #2620 + const char code[] = "typedef char Str[10];\n" + "void f(Str &cl) { }\n"; + + // The expected result.. + const std::string expected("void f ( char ( & cl ) [ 10 ] ) { }"); + + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef84() { // ticket #2630 (segmentation fault) + const char code1[] = "typedef y x () x\n"; + ASSERT_THROW(checkSimplifyTypedef(code1), InternalError); + + const char code2[] = "typedef struct template <>\n"; + ASSERT_THROW(checkSimplifyTypedef(code2), InternalError); + + const char code3[] = "typedef ::<>\n"; + ASSERT_THROW(checkSimplifyTypedef(code3), InternalError); + } + + void simplifyTypedef85() { // ticket #2651 + const char code[] = "typedef FOO ((BAR)(void, int, const int, int*));\n"; + const char expected[] = ";"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef86() { // ticket #2581 + const char code[] = "class relational {\n" + " typedef void (safe_bool_helper::*safe_bool)();\n" + "public:\n" + " operator safe_bool() const;\n" + " safe_bool operator!() const;\n" + "};\n"; + const char expected[] = "class relational { " + "" + "public: " + "operatorsafe_bool ( ) const ; " + "safe_bool operator! ( ) const ; " + "} ;"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef87() { // ticket #2651 + const char code[] = "typedef FOO (*(*BAR)(void, int, const int, int*));\n"; + const char expected[] = ";"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef88() { // ticket #2675 + const char code[] = "typedef short int (*x)(...);\n"; + const char expected[] = ";"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef89() { // ticket #2717 + const char code[] = "class Fred {\n" + " typedef void f(int) const;\n" + " f func;\n" + "};\n"; + const char expected[] = "class Fred { void func ( int ) const ; } ;"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef90() { // ticket #2718 + const char code[] = "typedef int IA[2];\n" + "void f(const IA&) {};\n"; + const char expected[] = "void f ( const int ( & ) [ 2 ] ) { } ;"; + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef91() { // ticket #2716 + const char code1[] = "namespace NS {\n" + " typedef int (*T)();\n" + " class A {\n" + " T f();\n" + " };\n" + "}\n" + "namespace NS {\n" + " T A::f() {}\n" + "}\n"; + const char expected1[] = "namespace NS { " + "" + "class A { " + "int * f ( ) ; " + "} ; " + "} " + "namespace NS { " + "int * A :: f ( ) { } " + "}"; + checkSimplifyTypedef(code1); + ASSERT_EQUALS(expected1, tok(code1)); + ASSERT_EQUALS("", errout.str()); + + const char code2[] = "namespace NS {\n" + " typedef int (*T)();\n" + " class A {\n" + " T f();\n" + " };\n" + "}\n" + "NS::T NS::A::f() {}\n"; + const char expected2[] = "namespace NS { " + "" + "class A { " + "int * f ( ) ; " + "} ; " + "} " + "int * NS :: A :: f ( ) { }"; + checkSimplifyTypedef(code2); + ASSERT_EQUALS(expected2, tok(code2)); + ASSERT_EQUALS("", errout.str()); + + const char code3[] = "namespace NS1 {\n" + " namespace NS2 {\n" + " typedef int (*T)();\n" + " class A {\n" + " T f();\n" + " };\n" + " }\n" + "}\n" + "namespace NS1 {\n" + " namespace NS2 {\n" + " T A::f() {}\n" + " }\n" + "}\n"; + const char expected3[] = "namespace NS1 { " + "namespace NS2 { " + "" + "class A { " + "int * f ( ) ; " + "} ; " + "} " + "} " + "namespace NS1 { " + "namespace NS2 { " + "int * A :: f ( ) { } " + "} " + "}"; + checkSimplifyTypedef(code3); + ASSERT_EQUALS(expected3, tok(code3)); + ASSERT_EQUALS("", errout.str()); + + const char code4[] = "namespace NS1 {\n" + " namespace NS2 {\n" + " typedef int (*T)();\n" + " class A {\n" + " T f();\n" + " };\n" + " }\n" + "}\n" + "namespace NS1 {\n" + " NS2::T NS2::A::f() {}\n" + "}\n"; + const char expected4[] = "namespace NS1 { " + "namespace NS2 { " + "" + "class A { " + "int * f ( ) ; " + "} ; " + "} " + "} " + "namespace NS1 { " + "int * NS2 :: A :: f ( ) { } " + "}"; + checkSimplifyTypedef(code4); + ASSERT_EQUALS(expected4, tok(code4)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef92() { // ticket #2736 (segmentation fault) + const char code[] = "typedef long Long;\n" + "namespace NS {\n" + "}\n"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef93() { // ticket #2738 (syntax error) + const char code[] = "struct s { double x; };\n" + "typedef struct s (*binop) (struct s, struct s);\n"; + const char expected[] = "struct s { double x ; } ;"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef94() { // ticket #1982 + const char code1[] = "class A {\n" + "public:\n" + " typedef struct {\n" + " int a[4];\n" + " } data;\n" + "};\n" + "A::data d;\n"; + const char expected1[] = "class A { " + "public: " + "struct data { " + "int a [ 4 ] ; " + "} ; " + "} ; " + "struct A :: data d ;"; + + checkSimplifyTypedef(code1); + ASSERT_EQUALS(expected1, tok(code1)); + TODO_ASSERT_EQUALS("[test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0.\n", "", errout.str()); + + const char code2[] = "class A {\n" + "public:\n" + " typedef struct {\n" + " int a[4];\n" + " } data;\n" + "};\n" + "::A::data d;\n"; + const char expected2[] = "class A { " + "public: " + "struct data { " + "int a [ 4 ] ; " + "} ; " + "} ; " + "struct :: A :: data d ;"; + + checkSimplifyTypedef(code2); + ASSERT_EQUALS(expected2, tok(code2)); + TODO_ASSERT_EQUALS("[test.cpp:7]: (debug) Scope::checkVariable found variable 'd' with varid 0.\n", "", errout.str()); + + const char code3[] = "class A {\n" + "public:\n" + " typedef struct {\n" + " int a[4];\n" + " } data;\n" + "};\n" + "class B : public ::A::data { };\n"; + const char expected3[] = "class A { " + "public: " + "struct data { " + "int a [ 4 ] ; " + "} ; " + "} ; " + "class B : public :: A :: data { } ;"; + + checkSimplifyTypedef(code3); + ASSERT_EQUALS(expected3, tok(code3)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef95() { // ticket #2844 + const char code[] = "class symbol_table {\n" + "public:\n" + " typedef expression_error::error_code (*valid_func)(void *cbparam, const char *name, expression_space space);\n" + " valid_func f;\n" + "};\n"; + const char expected[] = "class symbol_table { " + "public: " + "expression_error :: error_code * f ; " + "} ;"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef96() { // ticket #2886 (segmentation fault) + const char code[] = "typedef struct x { }\n"; + ASSERT_THROW(tok(code), InternalError); + } + + void simplifyTypedef97() { // ticket #2983 (segmentation fault) + const char code[] = "typedef x y\n" + "(A); y\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef98() { // ticket #2963 + const char code[] = "typedef int type ## __LINE__;\n" + "typedef int type ## __LINE__;\n" + "type1 x;\n" + "type2 y;"; + ASSERT_EQUALS("int x ; int y ;", tok(code)); + } + + void simplifyTypedef99() { // ticket #2999 + const char code[] = "typedef struct Fred Fred;\n" + "struct Fred { };\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + + const char code1[] = "struct Fred { };\n" + "typedef struct Fred Fred;\n"; + tok(code1); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef100() { // ticket #3000 + const char code[] = "typedef struct Fred { } Fred;\n" + "Fred * foo() {\n" + " Fred *fred;\n" + " fred = se_alloc(sizeof(struct Fred));\n" + " return fred;\n" + "}\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef101() { // ticket #3003 (segmentation fault) + const char code[] = "typedef a x[];\n" + "y = x\n"; + ASSERT_THROW(tok(code), InternalError); + } + + void simplifyTypedef102() { // ticket #3004 + const char code[] = "typedef struct { } Fred;\n" + "void foo()\n" + "{\n" + " Fred * Fred;\n" + "}\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef103() { // ticket #3007 + const char code[] = "typedef struct { } Fred;\n" + "void foo()\n" + "{\n" + " Fred Fred;\n" + "}\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef104() { // ticket #3070 + const char code[] = "typedef int (*in_func) (void FAR *, unsigned char FAR * FAR *);\n"; + ASSERT_EQUALS(";", tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef105() { // ticket #3616 (segmentation fault) + const char code[] = "( int typedef char x; ){}\n"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef106() { // ticket #3619 (segmentation fault) + const char code[] = "typedef void f ();\ntypedef { f }"; + tok(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedef107() { // ticket #3963 (bad code => segmentation fault) + const char code[] = "typedef int x[]; int main() { return x }"; + ASSERT_THROW(tok(code), InternalError); + } + + void simplifyTypedef108() { // ticket #4777 + const char code[] = "typedef long* GEN;\n" + "void sort_factor(GEN *y, long n) {\n" + " GEN a, b;\n" + " foo(a, b);\n" + "}\n"; + const char expected[] = "void sort_factor ( long * * y , long n ) { " + "long * a ; long * b ; " + "foo ( a , b ) ; " + "}"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedef109() { + const char code[] = "typedef int&& rref;\n" + "rref var;"; + const char expected[] = "int & & var ;"; + + checkSimplifyTypedef(code); + ASSERT_EQUALS(expected, tok(code)); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedefFunction1() { + { + const char code[] = "typedef void (*my_func)();\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (*my_func)(void);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (*my_func)(int);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (*my_func)(int*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + // ticket # 1615 + const char code[] = "typedef void (*my_func)(arg_class*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + + { + const char code[] = "typedef void (my_func)();\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func)(void);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func)(int);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func)(int*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func)(arg_class*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + + { + const char code[] = "typedef void my_func();\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void my_func(void);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void my_func(int);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void my_func(int*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void my_func(arg_class*);\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + + { + const char code[] = "typedef void (my_func());\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func(void));\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( void ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func(int));\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func(int*));\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( int * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef void (my_func(arg_class*));\n" + "std::queue func_queue;"; + + // The expected result.. + const std::string expected("std :: queue < void ( * ) ( arg_class * ) > func_queue ;"); + ASSERT_EQUALS(expected, tok(code)); + + // Check for output.. + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedefFunction2() { // ticket #1685 + const char code[] = "typedef void voidfn (int);\n" + "voidfn xxx;"; + + // The expected result.. + const std::string expected("void xxx ( int ) ;"); + ASSERT_EQUALS(expected, tok(code)); + } + + void simplifyTypedefFunction3() { + { + const char code[] = "typedef C func1();\n" + "typedef C (* func2)();\n" + "typedef C (& func3)();\n" + "typedef C (C::* func4)();\n" + "typedef C (C::* func5)() const;\n" + "typedef C (C::* func6)() volatile;\n" + "typedef C (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + const std::string expected("C f1 ( ) ; " + "C * f2 ; " // this gets simplified to a regular pointer + "C ( & f3 ) ( ) ; " + "C * f4 ; " + "C ( C :: * f5 ) ( ) const ; " + "C * f6 ; " // volatile is removed + "C ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef C const func1();\n" + "typedef C const (* func2)();\n" + "typedef C const (& func3)();\n" + "typedef C const (C::* func4)();\n" + "typedef C const (C::* func5)() const;\n" + "typedef C const (C::* func6)() volatile;\n" + "typedef C const (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + // C const -> const C + const std::string expected("const C f1 ( ) ; " + "const C * f2 ; " // this gets simplified to a regular pointer + "const C ( & f3 ) ( ) ; " + "const C * f4 ; " + "const C ( C :: * f5 ) ( ) const ; " + "const C * f6 ; " // volatile is removed + "const C ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef const C func1();\n" + "typedef const C (* func2)();\n" + "typedef const C (& func3)();\n" + "typedef const C (C::* func4)();\n" + "typedef const C (C::* func5)() const;\n" + "typedef const C (C::* func6)() volatile;\n" + "typedef const C (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + const std::string expected("const C f1 ( ) ; " + "const C * f2 ; " // this gets simplified to a regular pointer + "const C ( & f3 ) ( ) ; " + "const C * f4 ; " + "const C ( C :: * f5 ) ( ) const ; " + "const C * f6 ; " // volatile is removed + "const C ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef C * func1();\n" + "typedef C * (* func2)();\n" + "typedef C * (& func3)();\n" + "typedef C * (C::* func4)();\n" + "typedef C * (C::* func5)() const;\n" + "typedef C * (C::* func6)() volatile;\n" + "typedef C * (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + const std::string expected("C * f1 ( ) ; " + "C * * f2 ; " // this gets simplified to a regular pointer + "C * ( & f3 ) ( ) ; " + "C * * f4 ; " + "C * ( C :: * f5 ) ( ) const ; " + "C * * f6 ; " // volatile is removed + "C * ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef const C * func1();\n" + "typedef const C * (* func2)();\n" + "typedef const C * (& func3)();\n" + "typedef const C * (C::* func4)();\n" + "typedef const C * (C::* func5)() const;\n" + "typedef const C * (C::* func6)() volatile;\n" + "typedef const C * (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + const std::string expected("const C * f1 ( ) ; " + "const C * * f2 ; " // this gets simplified to a regular pointer + "const C * ( & f3 ) ( ) ; " + "const C * * f4 ; " + "const C * ( C :: * f5 ) ( ) const ; " + "const C * * f6 ; " // volatile is removed + "const C * ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + { + const char code[] = "typedef C const * func1();\n" + "typedef C const * (* func2)();\n" + "typedef C const * (& func3)();\n" + "typedef C const * (C::* func4)();\n" + "typedef C const * (C::* func5)() const;\n" + "typedef C const * (C::* func6)() volatile;\n" + "typedef C const * (C::* func7)() const volatile;\n" + "func1 f1;\n" + "func2 f2;\n" + "func3 f3;\n" + "func4 f4;\n" + "func5 f5;\n" + "func6 f6;\n" + "func7 f7;"; + + // The expected result.. + // C const -> const C + const std::string expected("const C * f1 ( ) ; " + "const C * * f2 ; " // this gets simplified to a regular pointer + "const C * ( & f3 ) ( ) ; " + "const C * * f4 ; " + "const C * ( C :: * f5 ) ( ) const ; " + "const C * * f6 ; " // volatile is removed + "const C * ( C :: * f7 ) ( ) const ;"); // volatile is removed + ASSERT_EQUALS(expected, tok(code)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + } + + void simplifyTypedefFunction4() { + const char code[] = "typedef int ( * ( * type1 ) ( bool ) ) ( int , int ) ;\n" + "typedef int ( * ( type2 ) ( bool ) ) ( int , int ) ;\n" + "typedef int ( * type3 ( bool ) ) ( int , int ) ;\n" + "type1 t1;\n" + "type2 t2;\n" + "type3 t3;"; + + // The expected result.. + const std::string expected("int ( * ( * t1 ) ( bool ) ) ( int , int ) ; " + "int * t2 ( bool ) ; " + "int * t3 ( bool ) ;"); + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedefFunction5() { + const char code[] = "typedef int ( * type1 ) ( float ) ;\n" + "typedef int ( * const type2 ) ( float ) ;\n" + "typedef int ( * volatile type3 ) ( float ) ;\n" + "typedef int ( * const volatile type4 ) ( float ) ;\n" + "typedef int ( C :: * type5 ) ( float ) ;\n" + "typedef int ( C :: * const type6 ) ( float ) ;\n" + "typedef int ( C :: * volatile type7 ) ( float ) ;\n" + "typedef int ( C :: * const volatile type8 ) ( float ) ;\n" + "typedef int ( :: C :: * type9 ) ( float ) ;\n" + "typedef int ( :: C :: * const type10 ) ( float ) ;\n" + "typedef int ( :: C :: * volatile type11 ) ( float ) ;\n" + "typedef int ( :: C :: * const volatile type12 ) ( float ) ;\n" + "type1 t1;\n" + "type2 t2;\n" + "type3 t3;\n" + "type4 t4;\n" + "type5 t5;\n" + "type6 t6;\n" + "type7 t7;\n" + "type8 t8;\n" + "type9 t9;\n" + "type10 t10;\n" + "type11 t11;\n" + "type12 t12;"; + + // The expected result.. + const std::string expected("int * t1 ; " // simplified to regular pointer + "int * const t2 ; " + "int * t3 ; " // volatile removed, gets simplified to regular pointer + "int * const t4 ; " // volatile removed + "int * t5 ; " + "int * const t6 ; " + "int * t7 ; " // volatile removed + "int * const t8 ; " // volatile removed + "int ( :: C :: * t9 ) ( float ) ; " + "int ( :: C :: * const t10 ) ( float ) ; " + "int ( :: C :: * t11 ) ( float ) ; " // volatile removed + "int ( :: C :: * const t12 ) ( float ) ;"); // volatile removed + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedefFunction6() { + const char code[] = "typedef void (*testfp)();\n" + "struct Fred\n" + "{\n" + " testfp get1() { return 0; }\n" + " void ( * get2 ( ) ) ( ) { return 0 ; }\n" + " testfp get3();\n" + " void ( * get4 ( ) ) ( );\n" + "};\n" + "testfp Fred::get3() { return 0; }\n" + "void ( * Fred::get4 ( ) ) ( ) { return 0 ; }\n"; + + // The expected result.. + const std::string expected("struct Fred " + "{ " + "void * get1 ( ) { return 0 ; } " + "void * get2 ( ) { return 0 ; } " + "void * get3 ( ) ; " + "void * get4 ( ) ; " + "} ; " + "void * Fred :: get3 ( ) { return 0 ; } " + "void * Fred :: get4 ( ) { return 0 ; }"); + + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedefFunction7() { + const char code[] = "typedef void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * _func_Tp_SGIAssignableConcept ) () ;" + "_func_Tp_SGIAssignableConcept X;\n"; + + // The expected result.. + const std::string expected("void ( __gnu_cxx :: _SGIAssignableConcept < _Tp > :: * X ) ( ) ;"); + + ASSERT_EQUALS(expected, tok(code, false)); + + checkSimplifyTypedef(code); + ASSERT_EQUALS("", errout.str()); + } + + void simplifyTypedefFunction8() { + // #2376 - internal error + const char code[] = "typedef int f_expand(const nrv_byte *);\n" + "void f(f_expand *(*get_fexp(int))){}\n"; + checkSimplifyTypedef(code); + TODO_ASSERT_EQUALS("", "[test.cpp:2]: (debug) Function::addArguments found argument 'int' with varid 0.\n", errout.str()); // make sure that there is no internal error + } + + void simplifyTypedefShadow() { // shadow variable (#4445) + const char code[] = "typedef struct { int x; } xyz;;\n" + "void f(){\n" + " int abc, xyz;\n" // <- shadow variable + "}\n"; + ASSERT_EQUALS("struct xyz { int x ; } ; void f ( ) { int abc ; int xyz ; }", + tok(code,false)); + } +}; + +REGISTER_TEST(TestSimplifyTypedef) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 48a808845..a62d8dbc8 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -16,7 +16,6 @@ * along with this program. If not, see . */ - #include "testsuite.h" #include "tokenize.h" #include "token.h" @@ -24,10 +23,10 @@ #include "path.h" #include "preprocessor.h" // usually tests here should not use preprocessor... #include -#include -#include extern std::ostringstream errout; + + class TestTokenizer : public TestFixture { public: TestTokenizer() : TestFixture("TestTokenizer") { @@ -220,129 +219,6 @@ private: TEST_CASE(simplifyExternC); TEST_CASE(simplifyKeyword); // #5842 - remove C99 static keyword between [] - TEST_CASE(varid1); - TEST_CASE(varid2); - TEST_CASE(varid3); - TEST_CASE(varid4); - TEST_CASE(varid5); - TEST_CASE(varid6); - TEST_CASE(varid7); - TEST_CASE(varidReturn1); - TEST_CASE(varidReturn2); - TEST_CASE(varid8); - TEST_CASE(varid9); - TEST_CASE(varid10); - TEST_CASE(varid11); - TEST_CASE(varid12); - TEST_CASE(varid13); - TEST_CASE(varid14); - TEST_CASE(varid15); - TEST_CASE(varid16); - TEST_CASE(varid17); // ticket #1810 - TEST_CASE(varid18); - TEST_CASE(varid19); - TEST_CASE(varid20); - TEST_CASE(varid24); - TEST_CASE(varid25); - TEST_CASE(varid26); // ticket #1967 (list of function pointers) - TEST_CASE(varid27); // Ticket #2280 (same name for namespace and variable) - TEST_CASE(varid28); // ticket #2630 - TEST_CASE(varid29); // ticket #1974 - TEST_CASE(varid30); // ticket #2614 - TEST_CASE(varid31); // ticket #2831 (segmentation fault) - TEST_CASE(varid32); // ticket #2835 (segmentation fault) - TEST_CASE(varid33); // ticket #2875 (segmentation fault) - TEST_CASE(varid34); // ticket #2825 - TEST_CASE(varid35); // ticket #2937 - TEST_CASE(varid36); // ticket #2980 (segmentation fault) - TEST_CASE(varid37); // ticket #3092 (varid for 'Bar bar(*this);') - TEST_CASE(varid38); // ticket #3272 (varid for 'FOO class C;') - TEST_CASE(varid39); // ticket #3279 (varid for 'FOO::BAR const') - TEST_CASE(varid40); // ticket #3279 - TEST_CASE(varid41); // ticket #3340 (varid for union type) - TEST_CASE(varid42); // ticket #3316 (varid for array) - TEST_CASE(varid43); - TEST_CASE(varid44); - TEST_CASE(varid45); // #3466 - TEST_CASE(varid46); // struct varname - TEST_CASE(varid47); // function parameters - TEST_CASE(varid48); // #3785 - return (a*b) - TEST_CASE(varid49); // #3799 - void f(std::vector) - TEST_CASE(varid50); // #3760 - explicit - TEST_CASE(varid51); // don't set varid for template function - TEST_CASE(varid52); // Set varid for nested templates - TEST_CASE(varid53); // #4172 - Template instantiation: T<&functionName> list[4]; - TEST_CASE(varid54); // hang - TEST_CASE(varid55); // #5868: Function::addArgument with varid 0 for argument named the same as a typedef - TEST_CASE(varid_cpp_keywords_in_c_code); - TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete" - TEST_CASE(varidFunctionCall1); - TEST_CASE(varidFunctionCall2); - TEST_CASE(varidFunctionCall3); - TEST_CASE(varidFunctionCall4); // ticket #3280 - TEST_CASE(varidStl); - TEST_CASE(varid_delete); - TEST_CASE(varid_functions); - TEST_CASE(varid_sizeof); - TEST_CASE(varid_reference_to_containers); - TEST_CASE(varid_in_class1); - TEST_CASE(varid_in_class2); - TEST_CASE(varid_in_class3); // #3092 - shadow variable in member function - TEST_CASE(varid_in_class4); // #3271 - public: class C; - TEST_CASE(varid_in_class5); // #3584 - std::vector<::FOO::B> b; - TEST_CASE(varid_in_class6); // #3755 - TEST_CASE(varid_in_class7); // set variable id for struct members - TEST_CASE(varid_in_class8); // unknown macro in class - TEST_CASE(varid_in_class9); // #4291 - id for variables accessed through 'this' - TEST_CASE(varid_in_class10); - TEST_CASE(varid_in_class11); // #4277 - anonymous union - TEST_CASE(varid_in_class12); // #4637 - method - TEST_CASE(varid_in_class13); // #4637 - method - TEST_CASE(varid_in_class14); - TEST_CASE(varid_in_class15); // #5533 - functions - TEST_CASE(varid_in_class16); - TEST_CASE(varid_in_class17); // #6056 - no varid for member functions - TEST_CASE(varid_initList); - TEST_CASE(varid_operator); - TEST_CASE(varid_throw); - TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type - TEST_CASE(varid_using); // ticket #3648 - TEST_CASE(varid_catch); - TEST_CASE(varid_functionPrototypeTemplate); - TEST_CASE(varid_templatePtr); // #4319 - TEST_CASE(varid_templateNamespaceFuncPtr); // #4172 - TEST_CASE(varid_templateArray); - TEST_CASE(varid_variadicFunc); - TEST_CASE(varid_typename); // #4644 - TEST_CASE(varid_rvalueref); - TEST_CASE(varid_arrayFuncPar); // #5294 - TEST_CASE(varid_sizeofPassed); // #5295 - TEST_CASE(varid_classInFunction); // #5293 - TEST_CASE(varid_pointerToArray); // #2645 - TEST_CASE(varid_cpp11initialization); // #4344 - TEST_CASE(varid_inheritedMembers); // #4101 - - TEST_CASE(varidclass1); - TEST_CASE(varidclass2); - TEST_CASE(varidclass3); - TEST_CASE(varidclass4); - TEST_CASE(varidclass5); - TEST_CASE(varidclass6); - TEST_CASE(varidclass7); - TEST_CASE(varidclass8); - TEST_CASE(varidclass9); - TEST_CASE(varidclass10); // variable declaration below usage - TEST_CASE(varidclass11); // variable declaration below usage - TEST_CASE(varidclass12); - TEST_CASE(varidclass13); - TEST_CASE(varidclass14); - TEST_CASE(varidclass15); // initializer list - TEST_CASE(varidclass16); // #4577 - TEST_CASE(varidclass17); // #6073 - TEST_CASE(varid_classnameshaddowsvariablename); // #3990 - - TEST_CASE(varidnamespace1); - TEST_CASE(file1); TEST_CASE(file2); TEST_CASE(file3); @@ -3301,2209 +3177,6 @@ private: return tokenizer.tokens()->stringifyList(true); } - void varid1() { - { - const std::string actual = tokenizeDebugListing( - "static int i = 1;\n" - "void f()\n" - "{\n" - " int i = 2;\n" - " for (int i = 0; i < 10; ++i)\n" - " i = 3;\n" - " i = 4;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: static int i@1 = 1 ;\n" - "2: void f ( )\n" - "3: {\n" - "4: int i@2 ; i@2 = 2 ;\n" - "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 ) {\n" - "6: i@3 = 3 ; }\n" - "7: i@2 = 4 ;\n" - "8: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing( - "static int i = 1;\n" - "void f()\n" - "{\n" - " int i = 2;\n" - " for (int i = 0; i < 10; ++i)\n" - " {\n" - " i = 3;\n" - " }\n" - " i = 4;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: static int i@1 = 1 ;\n" - "2: void f ( )\n" - "3: {\n" - "4: int i@2 ; i@2 = 2 ;\n" - "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n" - "6: {\n" - "7: i@3 = 3 ;\n" - "8: }\n" - "9: i@2 = 4 ;\n" - "10: }\n"); - - ASSERT_EQUALS(expected, actual); - } - } - - void varid2() { - const std::string actual = tokenizeDebugListing( - "void f()\n" - "{\n" - " struct ABC abc;\n" - " abc.a = 3;\n" - " i = abc.a;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( )\n" - "2: {\n" - "3: struct ABC abc@1 ;\n" - "4: abc@1 . a@2 = 3 ;\n" - "5: i = abc@1 . a@2 ;\n" - "6: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid3() { - const std::string actual = tokenizeDebugListing( - "static char str[4];\n" - "void f()\n" - "{\n" - " char str[10];\n" - " str[0] = 0;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: static char str@1 [ 4 ] ;\n" - "2: void f ( )\n" - "3: {\n" - "4: char str@2 [ 10 ] ;\n" - "5: str@2 [ 0 ] = 0 ;\n" - "6: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid4() { - const std::string actual = tokenizeDebugListing( - "void f(const unsigned int a[])\n" - "{\n" - " int i = *(a+10);\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( const int a@1 [ ] )\n" - "2: {\n" - "3: int i@2 ; i@2 = * ( a@1 + 10 ) ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid5() { - const std::string actual = tokenizeDebugListing( - "void f()\n" - "{\n" - " int a,b;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( )\n" - "2: {\n" - "3: int a@1 ; int b@2 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - - void varid6() { - const std::string actual = tokenizeDebugListing( - "int f(int a, int b)\n" - "{\n" - " return a+b;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: int f ( int a@1 , int b@2 )\n" - "2: {\n" - "3: return a@1 + b@2 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - - void varid7() { - const std::string actual = tokenizeDebugListing( - "void func() {\n" - " char a[256] = \"test\";\n" - " {\n" - " char b[256] = \"test\";\n" - " }\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void func ( ) {\n" - "2: char a@1 [ 256 ] = \"test\" ;\n" - "3: {\n" - "4: char b@2 [ 256 ] = \"test\" ;\n" - "5: }\n" - "6: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varidReturn1() { - const std::string actual = tokenizeDebugListing( - "int f()\n" - "{\n" - " int a;\n" - " return a;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: int f ( )\n" - "2: {\n" - "3: int a@1 ;\n" - "4: return a@1 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varidReturn2() { - const std::string actual = tokenizeDebugListing( - "void foo()\n" - "{\n" - " unsigned long mask = (1UL << size_) - 1;\n" - " return (abits_val_ & mask);\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: long mask@1 ; mask@1 = ( 1UL << size_ ) - 1 ;\n" - "4: return ( abits_val_ & mask@1 ) ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid8() { - const std::string actual = tokenizeDebugListing( - "void func()\n" - "{\n" - " std::string str(\"test\");\n" - " str.clear();\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: void func ( )\n" - "2: {\n" - "3: std :: string str@1 ( \"test\" ) ;\n" - "4: str@1 . clear ( ) ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid9() { - const std::string actual = tokenizeDebugListing( - "typedef int INT32;\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid10() { - const std::string actual = tokenizeDebugListing( - "void foo()\n" - "{\n" - " int abc;\n" - " struct abc abc1;\n" - "}", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: int abc@1 ;\n" - "4: struct abc abc1@2 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid11() { - const std::string actual = tokenizeDebugListing( - "class Foo;\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Foo ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid12() { - const std::string actual = tokenizeDebugListing( - "static void a()\n" - "{\n" - " class Foo *foo;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: static void a ( )\n" - "2: {\n" - "3: class Foo * foo@1 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid13() { - const std::string actual = tokenizeDebugListing( - "void f()\n" - "{\n" - " int a; int b;\n" - " a = a;\n" - "}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( )\n" - "2: {\n" - "3: int a@1 ; int b@2 ;\n" - "4: a@1 = a@1 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid14() { - // Overloaded operator* - const std::string actual = tokenizeDebugListing( - "void foo()\n" - "{\n" - "A a;\n" - "B b;\n" - "b * a;\n" - "}", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: A a@1 ;\n" - "4: B b@2 ;\n" - "5: b@2 * a@1 ;\n" - "6: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid15() { - { - const std::string actual = tokenizeDebugListing( - "struct S {\n" - " struct T {\n" - " } t;\n" - "} s;", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: struct S {\n" - "2: struct T {\n" - "3: } ; struct T t@1 ;\n" - "4: } ; struct S s@2 ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing( - "struct S {\n" - " struct T {\n" - " } t;\n" - "};", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: struct S {\n" - "2: struct T {\n" - "3: } ; struct T t@1 ;\n" - "4: } ;\n"); - - ASSERT_EQUALS(expected, actual); - } - } - - void varid16() { - const char code[] ="void foo()\n" - "{\n" - " int x = 1;\n" - " y = (z * x);\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: int x@1 ; x@1 = 1 ;\n" - "4: y = z * x@1 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c")); - } - - void varid17() { // ticket #1810 - const char code[] ="char foo()\n" - "{\n" - " char c('c');\n" - " return c;\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: char foo ( )\n" - "2: {\n" - "3: char c@1 ( 'c' ) ;\n" - "4: return c@1 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c")); - } - - void varid18() { - const char code[] ="char foo(char c)\n" - "{\n" - " bar::c = c;\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: char foo ( char c@1 )\n" - "2: {\n" - "3: bar :: c = c@1 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid19() { - const char code[] ="void foo()\n" - "{\n" - " std::pair, int> x;\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: std :: pair < std :: vector < double > , int > x@1 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid20() { - const char code[] ="void foo()\n" - "{\n" - " pair, vector > x;\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: void foo ( )\n" - "2: {\n" - "3: pair < vector < int > , vector < double > > x@1 ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid24() { - const char code[] ="class foo()\n" - "{\n" - "public:\n" - " ;\n" - "private:\n" - " static int i;\n" - "};\n"; - - const std::string expected("\n\n##file 0\n" - "1: class foo ( )\n" - "2: {\n" - "3: public:\n" - "4: ;\n" - "5: private:\n" - "6: static int i@1 ;\n" - "7: } ;\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid25() { - const char code[] ="class foo()\n" - "{\n" - "public:\n" - " ;\n" - "private:\n" - " mutable int i;\n" - "};\n"; - - const std::string expected("\n\n##file 0\n" - "1: class foo ( )\n" - "2: {\n" - "3: public:\n" - "4: ;\n" - "5: private:\n" - "6: mutable int i@1 ;\n" - "7: } ;\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid26() { - const char code[] ="list functions;\n"; - const std::string expected("\n\n##file 0\n" - "1: list < int ( * ) ( ) > functions@1 ;\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid27() { - const char code[] ="int fooled_ya;\n" - "fooled_ya::iterator iter;\n"; - const std::string expected("\n\n##file 0\n" - "1: int fooled_ya@1 ;\n" - "2: fooled_ya :: iterator iter@2 ;\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid28() { // ticket #2630 (segmentation fault) - tokenizeDebugListing("template \n"); - ASSERT_EQUALS("", errout.str()); - } - - void varid29() { - const char code[] ="class A {\n" - " B,1> b;\n" - "};\n"; - const std::string expected("\n\n##file 0\n" - "1: class A {\n" - "2: B < C < 1 > , 1 > b@1 ;\n" - "3: } ;\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid30() { // ticket #2614 - const char code1[] = "void f(EventPtr *eventP, ActionPtr **actionsP)\n" - "{\n" - " EventPtr event = *eventP;\n" - " *actionsP = &event->actions;\n" - "}\n"; - const std::string expected1("\n\n##file 0\n" - "1: void f ( EventPtr * eventP@1 , ActionPtr * * actionsP@2 )\n" - "2: {\n" - "3: EventPtr event@3 ; event@3 = * eventP@1 ;\n" - "4: * actionsP@2 = & event@3 . actions@4 ;\n" - "5: }\n"); - ASSERT_EQUALS(expected1, tokenizeDebugListing(code1, false, "test.c")); - - const char code2[] = "void f(int b, int c) {\n" - " x(a*b*c,10);\n" - "}\n"; - const std::string expected2("\n\n##file 0\n" - "1: void f ( int b@1 , int c@2 ) {\n" - "2: x ( a * b@1 * c@2 , 10 ) ;\n" - "3: }\n"); - ASSERT_EQUALS(expected2, tokenizeDebugListing(code2, false, "test.c")); - - const char code3[] = "class Nullpointer : public ExecutionPath\n" - " {\n" - " Nullpointer(Check *c, const unsigned int id, const std::string &name)\n" - " : ExecutionPath(c, id)\n" - " {\n" - " }\n" - "}\n"; - const std::string expected3("\n\n##file 0\n" - "1: class Nullpointer : public ExecutionPath\n" - "2: {\n" - "3: Nullpointer ( Check * c@1 , const int id@2 , const std :: string & name@3 )\n" - "4: : ExecutionPath ( c@1 , id@2 )\n" - "5: {\n" - "6: }\n" - "7: }\n"); - ASSERT_EQUALS(expected3, tokenizeDebugListing(code3)); - } - - void varid31() { // ticket #2831 (segmentation fault) - const char code[] ="z"; - tokenizeDebugListing(code); - ASSERT_EQUALS("", errout.str()); - } - - void varid32() { // ticket #2835 (segmentation fault) - const char code[] ="><,f) - const char code[] ="void f(std::vector)"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: void f ( std :: vector < int > )\n", - tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid50() { // #3760 - explicit - const char code[] ="class A { explicit A(const A&); };"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A { explicit A ( const A & ) ; } ;\n", - tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid51() { // don't set varid on template function - const char code[] ="T t; t.x<0>();"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: T t@1 ; t@1 . x < 0 > ( ) ;\n", - tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid52() { - const char code[] ="A::D> e;\n" - "B< C<> > b[10];\n" - "B> c[10];"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: A < B < C > :: D > e@1 ;\n" - "2: B < C < > > b@2 [ 10 ] ;\n" - "3: B < C < > > c@3 [ 10 ] ;\n", - tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid53() { // #4172 - Template instantiation: T<&functionName> list[4]; - ASSERT_EQUALS("\n\n##file 0\n" - "1: A < & f > list@1 [ 4 ] ;\n", - tokenizeDebugListing("A<&f> list[4];", false, "test.cpp")); - } - - void varid54() { // hang - // Original source code: libgc - tokenizeDebugListing("STATIC ptr_t GC_approx_sp(void) { word sp; sp = (word)&sp; return((ptr_t)sp); }",true); - } - - void varid55() { // Ticket #5868 - const char code[] = "typedef struct foo {} foo; " - "void bar1(struct foo foo) {} " - "void baz1(foo foo) {} " - "void bar2(struct foo& foo) {} " - "void baz2(foo& foo) {} " - "void bar3(struct foo* foo) {} " - "void baz3(foo* foo) {}"; - const char expected[] = "\n\n##file 0\n1: struct foo { } ; " - "void bar1 ( struct foo foo@1 ) { } " - "void baz1 ( struct foo foo@2 ) { } " - "void bar2 ( struct foo & foo@3 ) { } " - "void baz2 ( struct foo & foo@4 ) { } " - "void bar3 ( struct foo * foo@5 ) { } " - "void baz3 ( struct foo * foo@6 ) { }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid_cpp_keywords_in_c_code() { - const char code[] = "void f() {\n" - " delete d;\n" - " throw t;\n" - "}"; - - const char expected[] = "\n\n##file 0\n" - "1: void f ( ) {\n" - "2: delete d@1 ;\n" - "3: throw t@2 ;\n" - "4: }\n"; - - ASSERT_EQUALS(expected, tokenizeDebugListing(code,false,"test.c")); - } - - void varid_cpp_keywords_in_c_code2() { // #5373 - const char code[] = "int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, " - "unsigned long bits, int wake, int delete, struct extent_state **cached_state, " - "gfp_t mask) {\n" - " struct extent_state *state;\n" - "}" - "int clear_extent_dirty() {\n" - " return clear_extent_bit(tree, start, end, EXTENT_DIRTY | EXTENT_DELALLOC | " - " EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);\n" - "}"; - tokenizeDebugListing(code, false, "test.c"); - } - - void varidFunctionCall1() { - const char code[] ="void f() {\n" - " int x;\n" - " x = a(y*x,10);\n" - "}"; - const std::string expected("\n\n##file 0\n" - "1: void f ( ) {\n" - "2: int x@1 ;\n" - "3: x@1 = a ( y * x@1 , 10 ) ;\n" - "4: }\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c")); - } - - void varidFunctionCall2() { - // #2491 - const char code[] ="void f(int b) {\n" - " x(a*b,10);\n" - "}"; - const std::string expected1("\n\n##file 0\n" - "1: void f ( int b@1 ) {\n" - "2: x ( a * b"); - const std::string expected2(" , 10 ) ;\n" - "3: }\n"); - ASSERT_EQUALS(expected1+"@1"+expected2, tokenizeDebugListing(code,false,"test.c")); - } - - void varidFunctionCall3() { - // Ticket #2339 - const char code[] ="void f() {\n" - " int a = 0;\n" - " int b = c - (foo::bar * a);\n" - "}"; - - const std::string expected("\n\n##file 0\n" - "1: void f ( ) {\n" - "2: int a@1 ; a@1 = 0 ;\n" - "3: int b@2 ; b@2 = c - ( foo :: bar * a@1 ) ;\n" - "4: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidFunctionCall4() { - // Ticket #3280 - const char code1[] = "void f() { int x; fun(a,b*x); }"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: void f ( ) { int x@1 ; fun ( a , b * x@1 ) ; }\n", - tokenizeDebugListing(code1, false, "test.c")); - const char code2[] = "void f(int a) { int x; fun(a,b*x); }"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: void f ( int a@1 ) { int x@2 ; fun ( a@1 , b * x@2 ) ; }\n", - tokenizeDebugListing(code2, false, "test.c")); - } - - - void varidStl() { - const std::string actual = tokenizeDebugListing( - "list ints;\n" - "list::iterator it;\n" - "std::vector dirs;\n" - "std::map coords;\n" - "std::tr1::unordered_map xy;\n" - "std::list tokens;\n" - "static std::vector ex1;\n" - "extern std::vector ex2;\n" - "std::map m;\n" - ); - - const std::string expected("\n\n##file 0\n" - "1: list < int > ints@1 ;\n" - "2: list < int > :: iterator it@2 ;\n" - "3: std :: vector < std :: string > dirs@3 ;\n" - "4: std :: map < int , int > coords@4 ;\n" - "5: std :: tr1 :: unordered_map < int , int > xy@5 ;\n" - "6: std :: list < boost :: wave :: token_id > tokens@6 ;\n" - "7: static std :: vector < CvsProcess * > ex1@7 ;\n" - "8: extern std :: vector < CvsProcess * > ex2@8 ;\n" - "9: std :: map < int , 1 > m@9 ;\n" - ); - - ASSERT_EQUALS(expected, actual); - } - - void varid_delete() { - const std::string actual = tokenizeDebugListing( - "void f()\n" - "{\n" - " int *a;\n" - " delete a;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( )\n" - "2: {\n" - "3: int * a@1 ;\n" - "4: delete a@1 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid_functions() { - { - const std::string actual = tokenizeDebugListing( - "void f();\n" - "void f(){}\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( ) ;\n" - "2: void f ( ) { }\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing( - "A f(3);\n" - "A f2(true);\n" - "A g();\n" - "A e(int c);\n", false, "test.c"); - - const std::string expected("\n\n##file 0\n" - "1: A f@1 ( 3 ) ;\n" - "2: A f2@2 ( true ) ;\n" - "3: A g ( ) ;\n" - "4: A e ( int c@3 ) ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing( - "void f1(int &p)\n" - "{\n" - " p = 0;\n" - "}\n" - "void f2(std::string &str)\n" - "{\n" - " str.clear();\n" - "}\n" - "void f3(const std::string &s)\n" - "{\n" - " s.size();\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: void f1 ( int & p@1 )\n" - "2: {\n" - "3: p@1 = 0 ;\n" - "4: }\n" - "5: void f2 ( std :: string & str@2 )\n" - "6: {\n" - "7: str@2 . clear ( ) ;\n" - "8: }\n" - "9: void f3 ( const std :: string & s@3 )\n" - "10: {\n" - "11: s@3 . size ( ) ;\n" - "12: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing("void f(struct foobar);", false, "test.c"); - const std::string expected("\n\n##file 0\n" - "1: void f ( struct foobar ) ;\n"); - ASSERT_EQUALS(expected, actual); - } - } - - void varid_sizeof() { - const char code[] = "x = sizeof(a*b);"; - const char expected[] = "\n\n##file 0\n" - "1: x = sizeof ( a * b ) ;\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code,false,"test.c")); - } - - void varid_reference_to_containers() { - const std::string actual = tokenizeDebugListing( - "void f()\n" - "{\n" - " std::vector b;\n" - " std::vector &a = b;\n" - " std::vector *c = &b;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: void f ( )\n" - "2: {\n" - "3: std :: vector < int > b@1 ;\n" - "4: std :: vector < int > & a@2 = b@1 ;\n" - "5: std :: vector < int > * c@3 ; c@3 = & b@1 ;\n" - "6: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid_in_class1() { - { - const std::string actual = tokenizeDebugListing( - "class Foo\n" - "{\n" - "public:\n" - " std::string name1;\n" - " std::string name2;\n" - "};\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Foo\n" - "2: {\n" - "3: public:\n" - "4: std :: string name1@1 ;\n" - "5: std :: string name2@2 ;\n" - "6: } ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - { - const std::string actual = tokenizeDebugListing( - "class foo\n" - "{\n" - "public:\n" - " void do_something(const int x, const int y);\n" - " void bar();\n" - "};\n" - "\n" - "void foo::bar()\n" - "{\n" - " POINT pOutput = { 0 , 0 };\n" - " int x = pOutput.x;\n" - " int y = pOutput.y;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: class foo\n" - "2: {\n" - "3: public:\n" - "4: void do_something ( const int x@1 , const int y@2 ) ;\n" - "5: void bar ( ) ;\n" - "6: } ;\n" - "7:\n" - "8: void foo :: bar ( )\n" - "9: {\n" - "10: POINT pOutput@3 ; pOutput@3 = { 0 , 0 } ;\n" - "11: int x@4 ; x@4 = pOutput@3 . x@5 ;\n" - "12: int y@6 ; y@6 = pOutput@3 . y@7 ;\n" - "13: }\n"); - - ASSERT_EQUALS(expected, actual); - } - } - - void varid_in_class2() { - const std::string actual = tokenizeDebugListing( - "struct Foo {\n" - " int x;\n" - "};\n" - "\n" - "struct Bar {\n" - " Foo foo;\n" - " int x;\n" - " void f();\n" - "};\n" - "\n" - "void Bar::f()\n" - "{\n" - " foo.x = x;\n" - "}\n"); - const std::string expected("\n\n##file 0\n" - "1: struct Foo {\n" - "2: int x@1 ;\n" - "3: } ;\n" - "4:\n" - "5: struct Bar {\n" - "6: Foo foo@2 ;\n" - "7: int x@3 ;\n" - "8: void f ( ) ;\n" - "9: } ;\n" - "10:\n" - "11: void Bar :: f ( )\n" - "12: {\n" - "13: foo@2 . x@4 = x@3 ;\n" - "14: }\n"); - ASSERT_EQUALS(expected, actual); - } - - void varid_in_class3() { - const char code[] = "class Foo {\n" - " void blah() {\n" - " Bar x(*this);\n" // <- .. - " }\n" - " int x;\n" // <- .. don't assign same varid - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo {\n" - "2: void blah ( ) {\n" - "3: Bar x@1 ( * this ) ;\n" - "4: }\n" - "5: int x@2 ;\n" - "6: } ;\n", tokenizeDebugListing(code)); - } - - void varid_in_class4() { - const char code[] = "class Foo {\n" - "public: class C;\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo {\n" - "2: public: class C ;\n" - "3: } ;\n", - tokenizeDebugListing(code)); - } - - void varid_in_class5() { - const char code[] = "struct Foo {\n" - " std::vector<::X> v;\n" - "}"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct Foo {\n" - "2: std :: vector < :: X > v@1 ;\n" - "3: }\n", - tokenizeDebugListing(code)); - } - - void varid_in_class6() { - const char code[] = "class A {\n" - " void f(const char *str) const {\n" - " std::stringstream sst;\n" - " sst.str();\n" - " }\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: void f ( const char * str@1 ) const {\n" - "3: std :: stringstream sst@2 ;\n" - "4: sst@2 . str ( ) ;\n" - "5: }\n" - "6: } ;\n", - tokenizeDebugListing(code)); - } - - void varid_in_class7() { - const char code[] = "class A {\n" - " void f() {\n" - " abc.a = 0;\n" - " }\n" - " struct ABC abc;\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: void f ( ) {\n" - "3: abc@1 . a@2 = 0 ;\n" - "4: }\n" - "5: struct ABC abc@1 ;\n" - "6: } ;\n", - tokenizeDebugListing(code)); - } - - void varid_in_class8() { // #3776 - unknown macro - const char code[] = "class A {\n" - " UNKNOWN_MACRO(A)\n" - "private:\n" - " int x;\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: UNKNOWN_MACRO ( A )\n" - "3: private:\n" - "4: int x@1 ;\n" - "5: } ;\n", - tokenizeDebugListing(code)); - } - - void varid_in_class9() { // #4291 - id for variables accessed through 'this' - const char code1[] = "class A {\n" - " int var;\n" - "public:\n" - " void setVar();\n" - "};\n" - "void A::setVar() {\n" - " this->var = var;\n" - "}"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: int var@1 ;\n" - "3: public:\n" - "4: void setVar ( ) ;\n" - "5: } ;\n" - "6: void A :: setVar ( ) {\n" - "7: this . var@1 = var@1 ;\n" - "8: }\n", - tokenizeDebugListing(code1)); - - const char code2[] = "class Foo : public FooBase {\n" - " void Clone(FooBase& g);\n" - " short m_bar;\n" - "};\n" - "void Foo::Clone(FooBase& g) {\n" - " g->m_bar = m_bar;\n" - "}"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo : public FooBase {\n" - "2: void Clone ( FooBase & g@1 ) ;\n" - "3: short m_bar@2 ;\n" - "4: } ;\n" - "5: void Foo :: Clone ( FooBase & g@3 ) {\n" - "6: g@3 . m_bar@4 = m_bar@2 ;\n" - "7: }\n", - tokenizeDebugListing(code2)); // #4311 - } - - void varid_in_class10() { - const char code[] = "class Foo : public FooBase {\n" - " void Clone(FooBase& g);\n" - " short m_bar;\n" - "};\n" - "void Foo::Clone(FooBase& g) {\n" - " ((FooBase)g)->m_bar = m_bar;\n" - "}"; - TODO_ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo : public FooBase {\n" - "2: void Clone ( FooBase & g@1 ) ;\n" - "3: short m_bar@2 ;\n" - "4: } ;\n" - "5: void Foo :: Clone ( FooBase & g@3 ) {\n" - "6: ( ( FooBase ) g@3 ) . m_bar@4 = m_bar@2 ;\n" - "7: }\n", - "\n\n##file 0\n" - "1: class Foo : public FooBase {\n" - "2: void Clone ( FooBase & g@1 ) ;\n" - "3: short m_bar@2 ;\n" - "4: } ;\n" - "5: void Foo :: Clone ( FooBase & g@3 ) {\n" - "6: ( ( FooBase ) g@3 ) . m_bar = m_bar@2 ;\n" - "7: }\n", - tokenizeDebugListing(code)); - } - - void varid_in_class11() { // #4277 - anonymous union - const char code1[] = "class Foo {\n" - " union { float a; int b; };\n" - " void f() { a=0; }\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo {\n" - "2: union { float a@1 ; int b@2 ; } ;\n" - "3: void f ( ) { a@1 = 0 ; }\n" - "4: } ;\n", - tokenizeDebugListing(code1)); - - const char code2[] = "class Foo {\n" - " void f() { a=0; }\n" - " union { float a; int b; };\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo {\n" - "2: void f ( ) { a@1 = 0 ; }\n" - "3: union { float a@1 ; int b@2 ; } ;\n" - "4: } ;\n", - tokenizeDebugListing(code2)); - } - - void varid_in_class12() { // #4637 - method - const char code[] = "class Foo {\n" - "private:\n" - " void f(void);\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Foo {\n" - "2: private:\n" - "3: void f ( ) ;\n" - "4: } ;\n", - tokenizeDebugListing(code)); - } - - void varid_in_class13() { - const char code1[] = "struct a { char typename; };"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct a { char typename@1 ; } ;\n", - tokenizeDebugListing(code1, false, "test.c")); - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct a { char typename ; } ;\n", // not valid C++ code - tokenizeDebugListing(code1, false, "test.cpp")); - - const char code2[] = "struct a { char typename[2]; };"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct a { char typename@1 [ 2 ] ; } ;\n", - tokenizeDebugListing(code2, false, "test.c")); - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct a { char typename [ 2 ] ; } ;\n", // not valid C++ code - tokenizeDebugListing(code2, false, "test.cpp")); - } - - void varid_in_class14() { - const char code[] = "class Tokenizer { TokenList list; };\n" - "\n" - "void Tokenizer::f() {\n" - " std::list x;\n" // <- not member variable - " list.do_something();\n" // <- member variable - " Tokenizer::list.do_something();\n" // <- redundant scope info - "}\n"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Tokenizer { TokenList list@1 ; } ;\n" - "2:\n" - "3: void Tokenizer :: f ( ) {\n" - "4: std :: list < int > x@2 ;\n" - "5: list@1 . do_something ( ) ;\n" - "6: Tokenizer :: list@1 . do_something ( ) ;\n" - "7: }\n", tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid_in_class15() { // #5533 - functions - const char code[] = "class Fred {\n" - " void x(int a) const;\n" - " void y() { a=0; }\n" // <- unknown variable - "}\n"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Fred {\n" - "2: void x ( int a@1 ) const ;\n" - "3: void y ( ) { a = 0 ; }\n" - "4: }\n", tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid_in_class16() { // Set varId for inline member functions - const char code[] = "class Fred {\n" - " int x;\n" - " void foo(int x) { this->x = x; }\n" - "};\n"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Fred {\n" - "2: int x@1 ;\n" - "3: void foo ( int x@2 ) { this . x@1 = x@2 ; }\n" - "4: } ;\n", tokenizeDebugListing(code, false, "test.cpp")); - } - - void varid_in_class17() { // #6056 - Set no varid for member functions - const char code1[] = "class Fred {\n" - " int method_with_internal(X&);\n" - " int method_with_internal(X*);\n" - " int method_with_internal(int&);\n" - " int method_with_internal(A* b, X&);\n" - " int method_with_internal(X&, A* b);\n" - " int method_with_internal(const B &, int);\n" - " void Set(BAR);\n" - " FOO Set(BAR);\n" - " int method_with_class(B b);\n" - " bool function(std::map & m);\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class Fred {\n" - "2: int method_with_internal ( X & ) ;\n" - "3: int method_with_internal ( X * ) ;\n" - "4: int method_with_internal ( int & ) ;\n" - "5: int method_with_internal ( A * b@1 , X & ) ;\n" - "6: int method_with_internal ( X & , A * b@2 ) ;\n" - "7: int method_with_internal ( const B & , int ) ;\n" - "8: void Set ( BAR ) ;\n" - "9: FOO Set ( BAR ) ;\n" - "10: int method_with_class ( B < B > b@3 ) ;\n" - "11: bool function ( std :: map < int , int , MYless > & m@4 ) ;\n" - "12: } ;\n", tokenizeDebugListing(code1, false, "test.cpp")); - - const char code2[] = "int i;\n" - "SomeType someVar1(i, i);\n" - "SomeType someVar2(j, j);\n" - "SomeType someVar3(j, 1);\n" - "SomeType someVar4(new bar);"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: int i@1 ;\n" - "2: SomeType someVar1@2 ( i@1 , i@1 ) ;\n" - "3: SomeType someVar2 ( j , j ) ;\n" // This one could be a function - "4: SomeType someVar3@3 ( j , 1 ) ;\n" - "5: SomeType someVar4@4 ( new bar ) ;\n", tokenizeDebugListing(code2, false, "test.cpp")); - } - - void varid_initList() { - const char code1[] = "class A {\n" - " A() : x(0) {}\n" - " int x;\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: A ( ) : x@1 ( 0 ) { }\n" - "3: int x@1 ;\n" - "4: } ;\n", - tokenizeDebugListing(code1)); - - const char code2[] = "class A {\n" - " A(int x) : x(x) {}\n" - " int x;\n" - "};"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: A ( int x@1 ) : x@2 ( x@1 ) { }\n" - "3: int x@2 ;\n" - "4: } ;\n", - tokenizeDebugListing(code2)); - - const char code3[] = "class A {\n" - " A(int x);\n" - " int x;\n" - "};\n" - "A::A(int x) : x(x) {}"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: A ( int x@1 ) ;\n" - "3: int x@2 ;\n" - "4: } ;\n" - "5: A :: A ( int x@3 ) : x@2 ( x@3 ) { }\n", - tokenizeDebugListing(code3)); - - const char code4[] = "struct A {\n" - " int x;\n" - " A(int x) : x(x) {}\n" - "};\n"; - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct A {\n" - "2: int x@1 ;\n" - "3: A ( int x@2 ) : x@1 ( x@2 ) { }\n" - "4: } ;\n", - tokenizeDebugListing(code4)); - } - - void varid_operator() { - { - const std::string actual = tokenizeDebugListing( - "class Foo\n" - "{\n" - "public:\n" - " void operator=(const Foo &);\n" - "};\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Foo\n" - "2: {\n" - "3: public:\n" - "4: void operator= ( const Foo & ) ;\n" - "5: } ;\n"); - - ASSERT_EQUALS(expected, actual); - } - { - const std::string actual = tokenizeDebugListing( - "struct Foo {\n" - " void * operator new [](int);\n" - "};\n"); - const std::string expected("\n\n##file 0\n" - "1: struct Foo {\n" - "2: void * operatornew[] ( int ) ;\n" - "3: } ;\n"); - - ASSERT_EQUALS(expected, actual); - } - } - - void varid_throw() { // ticket #1723 - const std::string actual = tokenizeDebugListing( - "UserDefinedException* pe = new UserDefinedException();\n" - "throw pe;\n"); - - const std::string expected("\n\n##file 0\n" - "1: UserDefinedException * pe@1 ; pe@1 = new UserDefinedException ( ) ;\n" - "2: throw pe@1 ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varid_unknown_macro() { - // #2638 - unknown macro - const char code[] = "void f() {\n" - " int a[10];\n" - " AAA\n" - " a[0] = 0;\n" - "}"; - const char expected[] = "\n\n##file 0\n" - "1: void f ( ) {\n" - "2: int a@1 [ 10 ] ;\n" - "3: AAA\n" - "4: a@1 [ 0 ] = 0 ;\n" - "5: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code, false, "test.c")); - } - - void varid_using() { - // #3648 - const char code[] = "using std::size_t;"; - const char expected[] = "\n\n##file 0\n" - "1: using long ;\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid_catch() { - const char code[] = "void f() {\n" - " try { dostuff(); }\n" - " catch (exception &e) { }\n" - "}"; - const char expected[] = "\n\n##file 0\n" - "1: void f ( ) {\n" - "2: try { dostuff ( ) ; }\n" - "3: catch ( exception & e@1 ) { }\n" - "4: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid_functionPrototypeTemplate() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: function < void ( ) > fptr@1 ;\n", tokenizeDebugListing("function fptr;")); - } - - void varid_templatePtr() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: std :: map < int , FooTemplate < int > * > dummy_member@1 [ 1 ] ;\n", tokenizeDebugListing("std::map*> dummy_member[1];")); - } - - void varid_templateNamespaceFuncPtr() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: KeyListT < float , & NIFFile :: getFloat > mKeyList@1 [ 4 ] ;\n", tokenizeDebugListing("KeyListT mKeyList[4];")); - } - - void varid_templateArray() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: VertexArrayIterator < float [ 2 ] > attrPos@1 ; attrPos@1 = m_AttributePos . GetIterator < float [ 2 ] > ( ) ;\n", - tokenizeDebugListing("VertexArrayIterator attrPos = m_AttributePos.GetIterator();")); - } - - void varid_variadicFunc() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: int foo ( . . . ) ;\n", tokenizeDebugListing("int foo(...);")); - } - - void varid_typename() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: template < int d , class A , class B >\n", tokenizeDebugListing("template")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: template < int d , typename A , typename B >\n", tokenizeDebugListing("template")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: typename A a@1 ;\n", tokenizeDebugListing("typename A a;")); - } - - void varid_rvalueref() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: int & & a@1 ;\n", tokenizeDebugListing("int&& a;")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: void foo ( int & & a@1 ) { }\n", tokenizeDebugListing("void foo(int&& a) {}")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: class C {\n" - "2: C ( int & & a@1 ) ;\n" - "3: } ;\n", - tokenizeDebugListing("class C {\n" - " C(int&& a);\n" - "};")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: void foo ( int & & ) ;\n", tokenizeDebugListing("void foo(int&&);")); - } - - void varid_arrayFuncPar() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: void check ( const char fname@1 [ ] = 0 ) { }\n", tokenizeDebugListing("void check( const char fname[] = 0) { }")); - } - - void varid_sizeofPassed() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: void which_test ( ) {\n" - "2: const char * argv@1 [ 2 ] = { \"./test_runner\" , \"TestClass\" } ;\n" - "3: options args@2 ( sizeof argv@1 / sizeof ( argv@1 [ 0 ] ) , argv@1 ) ;\n" - "4: args@2 . which_test ( ) ;\n" - "5: }\n", - tokenizeDebugListing("void which_test() {\n" - " const char* argv[] = { \"./test_runner\", \"TestClass\" };\n" - " options args(sizeof argv / sizeof argv[0], argv);\n" - " args.which_test();\n" - "}")); - } - - void varid_classInFunction() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: void AddSuppression ( ) {\n" - "2: class QErrorLogger {\n" - "3: void reportErr ( ErrorLogger :: ErrorMessage & msg@1 ) {\n" - "4: }\n" - "5: } ;\n" - "6: }\n", - tokenizeDebugListing("void AddSuppression() {\n" - " class QErrorLogger {\n" - " void reportErr(ErrorLogger::ErrorMessage &msg) {\n" - " }\n" - " }; \n" - "}")); - } - - void varid_pointerToArray() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: int * a1@1 [ 10 ] ;\n" - "2: void f1 ( ) {\n" - "3: int * a2@2 [ 10 ] ;\n" - "4: int ( & a3@3 ) [ 10 ] ;\n" - "5: }\n" - "6: struct A {\n" - "7: int ( & a4@4 ) [ 10 ] ;\n" - "8: int f2 ( int i@5 ) { return a4@4 [ i@5 ] ; }\n" - "9: int f3 ( int ( & a5@6 ) [ 10 ] , int i@7 ) { return a5@6 [ i@7 ] ; }\n" - "10: } ;\n" - "11: int f4 ( int ( & a6@8 ) [ 10 ] , int i@9 ) { return a6@8 [ i@9 ] ; }\n", - tokenizeDebugListing("int (*a1)[10];\n" // pointer to array of 10 ints - "void f1() {\n" - " int(*a2)[10];\n" - " int(&a3)[10];\n" - "}\n" - "struct A {\n" - " int(&a4)[10];\n" - " int f2(int i) { return a4[i]; }\n" - " int f3(int(&a5)[10], int i) { return a5[i]; }\n" - "};\n" - "int f4(int(&a6)[10], int i) { return a6[i]; }")); - } - - void varid_cpp11initialization() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: int i@1 { 1 } ;\n" - "2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n" - "3: namespace n { int z@3 ; } ;\n" - "4: int & j@4 { i@1 } ;\n", - tokenizeDebugListing("int i{1};\n" - "std::vector vec{1, 2, 3};\n" - "namespace n { int z; };\n" - "int& j{i};\n")); - - // #6030 - ASSERT_EQUALS("\n\n##file 0\n" - "1: struct S3 : public S1 , public S2 { } ;\n", - tokenizeDebugListing("struct S3 : public S1, public S2 { };")); - - // #6058 - ASSERT_EQUALS("\n\n##file 0\n" - "1: class CPPCHECKLIB Scope { } ;\n", - tokenizeDebugListing("class CPPCHECKLIB Scope { };")); - - // #6073 - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A : public B , public C :: D {\n" - "2: int i@1 ;\n" - "3: A ( int i@2 ) : B { i@2 } , C :: D { i@2 } , i@1 { i@2 } {\n" - "4: int j@3 { i@2 } ;\n" - "5: }\n" - "6: } ;\n", - tokenizeDebugListing("class A: public B, public C::D {\n" - " int i;\n" - " A(int i): B{i}, C::D{i}, i{i} {\n" - " int j{i};\n" - " }\n" - "};")); - } - - void varid_inheritedMembers() { - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: int a@1 ;\n" - "3: } ;\n" - "4: class B : public A {\n" - "5: void f ( ) ;\n" - "6: } ;\n" - "7: void B :: f ( ) {\n" - "8: a@1 = 0 ;\n" - "9: }\n", - tokenizeDebugListing("class A {\n" - " int a;\n" - "};\n" - "class B : public A {\n" - " void f();\n" - "};\n" - "void B::f() {\n" - " a = 0;\n" - "}")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: int a@1 ;\n" - "3: } ;\n" - "4: class B : A {\n" - "5: void f ( ) ;\n" - "6: } ;\n" - "7: void B :: f ( ) {\n" - "8: a@1 = 0 ;\n" - "9: }\n", - tokenizeDebugListing("class A {\n" - " int a;\n" - "};\n" - "class B : A {\n" - " void f();\n" - "};\n" - "void B::f() {\n" - " a = 0;\n" - "}")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: int a@1 ;\n" - "3: } ;\n" - "4: class B : protected B , public A {\n" - "5: void f ( ) ;\n" - "6: } ;\n" - "7: void B :: f ( ) {\n" - "8: a@1 = 0 ;\n" - "9: }\n", - tokenizeDebugListing("class A {\n" - " int a;\n" - "};\n" - "class B : protected B, public A {\n" - " void f();\n" - "};\n" - "void B::f() {\n" - " a = 0;\n" - "}")); - - ASSERT_EQUALS("\n\n##file 0\n" - "1: class A {\n" - "2: int a@1 ;\n" - "3: } ;\n" - "4: class B : public A {\n" - "5: void f ( ) {\n" - "6: a@1 = 0 ;\n" - "7: }\n" - "8: } ;\n", - tokenizeDebugListing("class A {\n" - " int a;\n" - "};\n" - "class B : public A {\n" - " void f() {\n" - " a = 0;\n" - " }\n" - "};")); - } - - void varidclass1() { - const std::string actual = tokenizeDebugListing( - "class Fred\n" - "{\n" - "private:\n" - " int i;\n" - "\n" - " void foo1();\n" - " void foo2()\n" - " {\n" - " ++i;\n" - " }\n" - "}\n" - "\n" - "Fred::foo1()\n" - "{\n" - " i = 0;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Fred\n" - "2: {\n" - "3: private:\n" - "4: int i@1 ;\n" - "5:\n" - "6: void foo1 ( ) ;\n" - "7: void foo2 ( )\n" - "8: {\n" - "9: ++ i@1 ;\n" - "10: }\n" - "11: }\n" - "12:\n" - "13: Fred :: foo1 ( )\n" - "14: {\n" - "15: i@1 = 0 ;\n" - "16: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - - void varidclass2() { - const std::string actual = tokenizeDebugListing( - "class Fred\n" - "{ void f(); };\n" - "\n" - "void A::foo1()\n" - "{\n" - " int i = 0;\n" - "}\n" - "\n" - "void Fred::f()\n" - "{\n" - " i = 0;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Fred\n" - "2: { void f ( ) ; } ;\n" - "3:\n" - "4: void A :: foo1 ( )\n" - "5: {\n" - "6: int i@1 ; i@1 = 0 ;\n" - "7: }\n" - "8:\n" - "9: void Fred :: f ( )\n" - "10: {\n" - "11: i = 0 ;\n" - "12: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - - void varidclass3() { - const std::string actual = tokenizeDebugListing( - "class Fred\n" - "{ int i; void f(); };\n" - "\n" - "void Fred::f()\n" - "{\n" - " i = 0;\n" - "}\n" - "\n" - "void A::f()\n" - "{\n" - " i = 0;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Fred\n" - "2: { int i@1 ; void f ( ) ; } ;\n" - "3:\n" - "4: void Fred :: f ( )\n" - "5: {\n" - "6: i@1 = 0 ;\n" - "7: }\n" - "8:\n" - "9: void A :: f ( )\n" - "10: {\n" - "11: i = 0 ;\n" - "12: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - - void varidclass4() { - const std::string actual = tokenizeDebugListing( - "class Fred\n" - "{ int i; void f(); };\n" - "\n" - "void Fred::f()\n" - "{\n" - " if (i) { }\n" - " i = 0;\n" - "}\n"); - - const std::string expected("\n\n##file 0\n" - "1: class Fred\n" - "2: { int i@1 ; void f ( ) ; } ;\n" - "3:\n" - "4: void Fred :: f ( )\n" - "5: {\n" - "6: if ( i@1 ) { }\n" - "7: i@1 = 0 ;\n" - "8: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varidclass5() { - const std::string actual = tokenizeDebugListing( - "class A { };\n" - "class B\n" - "{\n" - " A *a;\n" - " B() : a(new A)\n" - " { }\n" - "};\n"); - - const std::string expected("\n\n##file 0\n" - "1: class A { } ;\n" - "2: class B\n" - "3: {\n" - "4: A * a@1 ;\n" - "5: B ( ) : a@1 ( new A )\n" - "6: { }\n" - "7: } ;\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varidclass6() { - const std::string actual = tokenizeDebugListing( - "class A\n" - "{\n" - " public:\n" - " static char buf[20];\n" - "};\n" - "char A::buf[20];\n" - "int main()\n" - "{\n" - " char buf[2];\n" - " A::buf[10] = 0;\n" - "}"); - - const std::string wanted("\n\n##file 0\n" - "1: class A\n" - "2: {\n" - "3: public:\n" - "4: static char buf@1 [ 20 ] ;\n" - "5: } ;\n" - "6: char A :: buf@1 [ 20 ] ;\n" - "7: int main ( )\n" - "8: {\n" - "9: char buf@2 [ 2 ] ;\n" - "10: A :: buf@1 [ 10 ] = 0 ;\n" - "11: }\n"); - - ASSERT_EQUALS(wanted, actual); - } - - void varidclass7() { - const std::string actual = tokenizeDebugListing( - "int main()\n" - "{\n" - " char buf[2];\n" - " A::buf[10] = 0;\n" - "}"); - - const std::string expected("\n\n##file 0\n" - "1: int main ( )\n" - "2: {\n" - "3: char buf@1 [ 2 ] ;\n" - "4: A :: buf [ 10 ] = 0 ;\n" - "5: }\n"); - - ASSERT_EQUALS(expected, actual); - } - - void varidclass8() { - const char code[] ="class Fred {\n" - "public:\n" - " void foo(int d) {\n" - " int i = bar(x * d);\n" - " }\n" - " int x;\n" - "}\n"; - - const std::string expected("\n\n##file 0\n" - "1: class Fred {\n" - "2: public:\n" - "3: void foo ( int d@1 ) {\n" - "4: int i@2 ; i@2 = bar ( x@3 * d@1 ) ;\n" - "5: }\n" - "6: int x@3 ;\n" - "7: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass9() { - const char code[] ="typedef char Str[10];" - "class A {\n" - "public:\n" - " void f(Str &cl);\n" - " void g(Str cl);\n" - "}\n" - "void Fred::f(Str &cl) {\n" - " sizeof(cl);\n" - "}"; - - const std::string expected("\n\n" - "##file 0\n" - "1: class A {\n" - "2: public:\n" - "3: void f ( char ( & cl@1 ) [ 10 ] ) ;\n" - "4: void g ( char cl@2 [ 10 ] ) ;\n" - "5: }\n" - "6: void Fred :: f ( char ( & cl@3 ) [ 10 ] ) {\n" - "7: sizeof ( cl@3 ) ;\n" - "8: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass10() { - const char code[] ="class A {\n" - " void f() {\n" - " a = 3;\n" - " }\n" - " int a;\n" - "};\n"; - - const std::string expected("\n\n##file 0\n" - "1: class A {\n" - "2: void f ( ) {\n" - "3: a@1 = 3 ;\n" - "4: }\n" - "5: int a@1 ;\n" - "6: } ;\n"); - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass11() { - const char code[] ="class Fred {\n" - " int a;\n" - " void f();\n" - "};\n" - "class Wilma {\n" - " int a;\n" - " void f();\n" - "};\n" - "void Fred::f() { a = 0; }\n" - "void Wilma::f() { a = 0; }\n"; - - const std::string expected("\n\n##file 0\n" - "1: class Fred {\n" - "2: int a@1 ;\n" - "3: void f ( ) ;\n" - "4: } ;\n" - "5: class Wilma {\n" - "6: int a@2 ;\n" - "7: void f ( ) ;\n" - "8: } ;\n" - "9: void Fred :: f ( ) { a@1 = 0 ; }\n" - "10: void Wilma :: f ( ) { a@2 = 0 ; }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass12() { - const char code[] ="class Fred {\n" - " int a;\n" - " void f() { Fred::a = 0; }\n" - "};\n"; - - const std::string expected("\n\n##file 0\n" - "1: class Fred {\n" - "2: int a@1 ;\n" - "3: void f ( ) { Fred :: a@1 = 0 ; }\n" - "4: } ;\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass13() { - const char code[] ="class Fred {\n" - " int a;\n" - " void f() { Foo::Fred::a = 0; }\n" - "};\n"; - - const std::string expected("\n\n##file 0\n" - "1: class Fred {\n" - "2: int a@1 ;\n" - "3: void f ( ) { Foo :: Fred :: a = 0 ; }\n" - "4: } ;\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass14() { - // don't give friend classes varid - { - const char code[] ="class A {\n" - "friend class B;\n" - "}"; - - const std::string expected("\n\n##file 0\n" - "1: class A {\n" - "2: friend class B ;\n" - "3: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - { - const char code[] ="class A {\n" - "private: friend class B;\n" - "}"; - - const std::string expected("\n\n##file 0\n" - "1: class A {\n" - "2: private: friend class B ;\n" - "3: }\n"); - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - } - - void varidclass15() { - const char code[] = "class A {\n" - " int a;\n" - " int b;\n" - " A();\n" - "};\n" - "A::A() : a(0) { b = 1; }"; - const char expected[] = "\n\n##file 0\n" - "1: class A {\n" - "2: int a@1 ;\n" - "3: int b@2 ;\n" - "4: A ( ) ;\n" - "5: } ;\n" - "6: A :: A ( ) : a@1 ( 0 ) { b@2 = 1 ; }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass16() { - const char code[] = "struct A;\n" - "typedef bool (A::* FuncPtr)();\n" - "struct A {\n" - " FuncPtr pFun;\n" - " void setPFun(int mode);\n" - " bool funcNorm();\n" - "};\n" - "void A::setPFun(int mode) {\n" - " pFun = &A::funcNorm;\n" - "}"; - const char expected[] = "\n\n##file 0\n" - "1: struct A ;\n" - "2:\n" - "3: struct A {\n" - "4: bool * pFun@1 ;\n" - "5: void setPFun ( int mode@2 ) ;\n" - "6: bool funcNorm ( ) ;\n" - "7: } ;\n" - "8: void A :: setPFun ( int mode@3 ) {\n" - "9: pFun@1 = & A :: funcNorm ;\n" - "10: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varidclass17() { - const char code[] = "class A: public B, public C::D {\n" - " int i;\n" - " A(int i): B(i), C::D(i), i(i) {\n" - " int j(i);\n" - " }\n" - "};"; - const char expected[] = "\n\n##file 0\n" - "1: class A : public B , public C :: D {\n" - "2: int i@1 ;\n" - "3: A ( int i@2 ) : B ( i@2 ) , C :: D ( i@2 ) , i@1 ( i@2 ) {\n" - "4: int j@3 ; j@3 = i@2 ;\n" - "5: }\n" - "6: } ;\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - - void varid_classnameshaddowsvariablename() { - const char code[] = "class Data;\n" - "void strange_declarated(const Data& Data);\n" - "void handleData(const Data& data) {\n" - " strange_declarated(data);\n" - "}\n"; - const char expected[] = "\n\n##file 0\n" - "1: class Data ;\n" - "2: void strange_declarated ( const Data & Data@1 ) ;\n" - "3: void handleData ( const Data & data@2 ) {\n" - "4: strange_declarated ( data@2 ) ;\n" - "5: }\n"; - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - - } - - void varidnamespace1() { - const char code[] = "namespace A {\n" - " char buf[20];\n" - "}\n" - "int main() {\n" - " return foo(A::buf);\n" - "}"; - - const char expected[] = "\n\n##file 0\n" - "1: namespace A {\n" - "2: char buf@1 [ 20 ] ;\n" - "3: }\n" - "4: int main ( ) {\n" - "5: return foo ( A :: buf@1 ) ;\n" - "6: }\n"; - - ASSERT_EQUALS(expected, tokenizeDebugListing(code)); - } - void file1() { const char code[] = "a1\n" "#file \"b\"\n" diff --git a/test/testvarid.cpp b/test/testvarid.cpp new file mode 100644 index 000000000..a5ebf51a7 --- /dev/null +++ b/test/testvarid.cpp @@ -0,0 +1,2383 @@ +/* + * Cppcheck - A tool for static C/C++ code analysis + * Copyright (C) 2007-2014 Daniel Marjamäki and Cppcheck team. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "testsuite.h" +#include "tokenize.h" +#include "token.h" +#include "settings.h" + +#include + +extern std::ostringstream errout; + + +class TestVarID : public TestFixture { +public: + TestVarID() : TestFixture("TestVarID") { + } + +private: + + void run() { + TEST_CASE(varid1); + TEST_CASE(varid2); + TEST_CASE(varid3); + TEST_CASE(varid4); + TEST_CASE(varid5); + TEST_CASE(varid6); + TEST_CASE(varid7); + TEST_CASE(varidReturn1); + TEST_CASE(varidReturn2); + TEST_CASE(varid8); + TEST_CASE(varid9); + TEST_CASE(varid10); + TEST_CASE(varid11); + TEST_CASE(varid12); + TEST_CASE(varid13); + TEST_CASE(varid14); + TEST_CASE(varid15); + TEST_CASE(varid16); + TEST_CASE(varid17); // ticket #1810 + TEST_CASE(varid18); + TEST_CASE(varid19); + TEST_CASE(varid20); + TEST_CASE(varid24); + TEST_CASE(varid25); + TEST_CASE(varid26); // ticket #1967 (list of function pointers) + TEST_CASE(varid27); // Ticket #2280 (same name for namespace and variable) + TEST_CASE(varid28); // ticket #2630 + TEST_CASE(varid29); // ticket #1974 + TEST_CASE(varid30); // ticket #2614 + TEST_CASE(varid31); // ticket #2831 (segmentation fault) + TEST_CASE(varid32); // ticket #2835 (segmentation fault) + TEST_CASE(varid33); // ticket #2875 (segmentation fault) + TEST_CASE(varid34); // ticket #2825 + TEST_CASE(varid35); // ticket #2937 + TEST_CASE(varid36); // ticket #2980 (segmentation fault) + TEST_CASE(varid37); // ticket #3092 (varid for 'Bar bar(*this);') + TEST_CASE(varid38); // ticket #3272 (varid for 'FOO class C;') + TEST_CASE(varid39); // ticket #3279 (varid for 'FOO::BAR const') + TEST_CASE(varid40); // ticket #3279 + TEST_CASE(varid41); // ticket #3340 (varid for union type) + TEST_CASE(varid42); // ticket #3316 (varid for array) + TEST_CASE(varid43); + TEST_CASE(varid44); + TEST_CASE(varid45); // #3466 + TEST_CASE(varid46); // struct varname + TEST_CASE(varid47); // function parameters + TEST_CASE(varid48); // #3785 - return (a*b) + TEST_CASE(varid49); // #3799 - void f(std::vector) + TEST_CASE(varid50); // #3760 - explicit + TEST_CASE(varid51); // don't set varid for template function + TEST_CASE(varid52); // Set varid for nested templates + TEST_CASE(varid53); // #4172 - Template instantiation: T<&functionName> list[4]; + TEST_CASE(varid54); // hang + TEST_CASE(varid55); // #5868: Function::addArgument with varid 0 for argument named the same as a typedef + TEST_CASE(varid_cpp_keywords_in_c_code); + TEST_CASE(varid_cpp_keywords_in_c_code2); // #5373: varid=0 for argument called "delete" + TEST_CASE(varidFunctionCall1); + TEST_CASE(varidFunctionCall2); + TEST_CASE(varidFunctionCall3); + TEST_CASE(varidFunctionCall4); // ticket #3280 + TEST_CASE(varidStl); + TEST_CASE(varid_delete); + TEST_CASE(varid_functions); + TEST_CASE(varid_sizeof); + TEST_CASE(varid_reference_to_containers); + TEST_CASE(varid_in_class1); + TEST_CASE(varid_in_class2); + TEST_CASE(varid_in_class3); // #3092 - shadow variable in member function + TEST_CASE(varid_in_class4); // #3271 - public: class C; + TEST_CASE(varid_in_class5); // #3584 - std::vector<::FOO::B> b; + TEST_CASE(varid_in_class6); // #3755 + TEST_CASE(varid_in_class7); // set variable id for struct members + TEST_CASE(varid_in_class8); // unknown macro in class + TEST_CASE(varid_in_class9); // #4291 - id for variables accessed through 'this' + TEST_CASE(varid_in_class10); + TEST_CASE(varid_in_class11); // #4277 - anonymous union + TEST_CASE(varid_in_class12); // #4637 - method + TEST_CASE(varid_in_class13); // #4637 - method + TEST_CASE(varid_in_class14); + TEST_CASE(varid_in_class15); // #5533 - functions + TEST_CASE(varid_in_class16); + TEST_CASE(varid_in_class17); // #6056 - no varid for member functions + TEST_CASE(varid_initList); + TEST_CASE(varid_operator); + TEST_CASE(varid_throw); + TEST_CASE(varid_unknown_macro); // #2638 - unknown macro is not type + TEST_CASE(varid_using); // ticket #3648 + TEST_CASE(varid_catch); + TEST_CASE(varid_functionPrototypeTemplate); + TEST_CASE(varid_templatePtr); // #4319 + TEST_CASE(varid_templateNamespaceFuncPtr); // #4172 + TEST_CASE(varid_templateArray); + TEST_CASE(varid_variadicFunc); + TEST_CASE(varid_typename); // #4644 + TEST_CASE(varid_rvalueref); + TEST_CASE(varid_arrayFuncPar); // #5294 + TEST_CASE(varid_sizeofPassed); // #5295 + TEST_CASE(varid_classInFunction); // #5293 + TEST_CASE(varid_pointerToArray); // #2645 + TEST_CASE(varid_cpp11initialization); // #4344 + TEST_CASE(varid_inheritedMembers); // #4101 + + TEST_CASE(varidclass1); + TEST_CASE(varidclass2); + TEST_CASE(varidclass3); + TEST_CASE(varidclass4); + TEST_CASE(varidclass5); + TEST_CASE(varidclass6); + TEST_CASE(varidclass7); + TEST_CASE(varidclass8); + TEST_CASE(varidclass9); + TEST_CASE(varidclass10); // variable declaration below usage + TEST_CASE(varidclass11); // variable declaration below usage + TEST_CASE(varidclass12); + TEST_CASE(varidclass13); + TEST_CASE(varidclass14); + TEST_CASE(varidclass15); // initializer list + TEST_CASE(varidclass16); // #4577 + TEST_CASE(varidclass17); // #6073 + TEST_CASE(varid_classnameshaddowsvariablename); // #3990 + + TEST_CASE(varidnamespace1); + } + + std::string tokenize(const char code[], bool simplify = false, const char filename[] = "test.cpp") { + errout.str(""); + + Settings settings; + settings.standards.c = Standards::C89; + settings.standards.cpp = Standards::CPP03; + + Tokenizer tokenizer(&settings, this); + std::istringstream istr(code); + tokenizer.tokenize(istr, filename); + + if (simplify) + tokenizer.simplifyTokenList2(); + + // result.. + return tokenizer.tokens()->stringifyList(true); + } + + void varid1() { + { + const std::string actual = tokenize( + "static int i = 1;\n" + "void f()\n" + "{\n" + " int i = 2;\n" + " for (int i = 0; i < 10; ++i)\n" + " i = 3;\n" + " i = 4;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: static int i@1 = 1 ;\n" + "2: void f ( )\n" + "3: {\n" + "4: int i@2 ; i@2 = 2 ;\n" + "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 ) {\n" + "6: i@3 = 3 ; }\n" + "7: i@2 = 4 ;\n" + "8: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize( + "static int i = 1;\n" + "void f()\n" + "{\n" + " int i = 2;\n" + " for (int i = 0; i < 10; ++i)\n" + " {\n" + " i = 3;\n" + " }\n" + " i = 4;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: static int i@1 = 1 ;\n" + "2: void f ( )\n" + "3: {\n" + "4: int i@2 ; i@2 = 2 ;\n" + "5: for ( int i@3 = 0 ; i@3 < 10 ; ++ i@3 )\n" + "6: {\n" + "7: i@3 = 3 ;\n" + "8: }\n" + "9: i@2 = 4 ;\n" + "10: }\n"); + + ASSERT_EQUALS(expected, actual); + } + } + + void varid2() { + const std::string actual = tokenize( + "void f()\n" + "{\n" + " struct ABC abc;\n" + " abc.a = 3;\n" + " i = abc.a;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( )\n" + "2: {\n" + "3: struct ABC abc@1 ;\n" + "4: abc@1 . a@2 = 3 ;\n" + "5: i = abc@1 . a@2 ;\n" + "6: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid3() { + const std::string actual = tokenize( + "static char str[4];\n" + "void f()\n" + "{\n" + " char str[10];\n" + " str[0] = 0;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: static char str@1 [ 4 ] ;\n" + "2: void f ( )\n" + "3: {\n" + "4: char str@2 [ 10 ] ;\n" + "5: str@2 [ 0 ] = 0 ;\n" + "6: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid4() { + const std::string actual = tokenize( + "void f(const unsigned int a[])\n" + "{\n" + " int i = *(a+10);\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( const int a@1 [ ] )\n" + "2: {\n" + "3: int i@2 ; i@2 = * ( a@1 + 10 ) ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid5() { + const std::string actual = tokenize( + "void f()\n" + "{\n" + " int a,b;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( )\n" + "2: {\n" + "3: int a@1 ; int b@2 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varid6() { + const std::string actual = tokenize( + "int f(int a, int b)\n" + "{\n" + " return a+b;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: int f ( int a@1 , int b@2 )\n" + "2: {\n" + "3: return a@1 + b@2 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varid7() { + const std::string actual = tokenize( + "void func() {\n" + " char a[256] = \"test\";\n" + " {\n" + " char b[256] = \"test\";\n" + " }\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void func ( ) {\n" + "2: char a@1 [ 256 ] = \"test\" ;\n" + "3: {\n" + "4: char b@2 [ 256 ] = \"test\" ;\n" + "5: }\n" + "6: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidReturn1() { + const std::string actual = tokenize( + "int f()\n" + "{\n" + " int a;\n" + " return a;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: int f ( )\n" + "2: {\n" + "3: int a@1 ;\n" + "4: return a@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidReturn2() { + const std::string actual = tokenize( + "void foo()\n" + "{\n" + " unsigned long mask = (1UL << size_) - 1;\n" + " return (abits_val_ & mask);\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: long mask@1 ; mask@1 = ( 1UL << size_ ) - 1 ;\n" + "4: return ( abits_val_ & mask@1 ) ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid8() { + const std::string actual = tokenize( + "void func()\n" + "{\n" + " std::string str(\"test\");\n" + " str.clear();\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: void func ( )\n" + "2: {\n" + "3: std :: string str@1 ( \"test\" ) ;\n" + "4: str@1 . clear ( ) ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid9() { + const std::string actual = tokenize( + "typedef int INT32;\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid10() { + const std::string actual = tokenize( + "void foo()\n" + "{\n" + " int abc;\n" + " struct abc abc1;\n" + "}", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: int abc@1 ;\n" + "4: struct abc abc1@2 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid11() { + const std::string actual = tokenize( + "class Foo;\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Foo ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid12() { + const std::string actual = tokenize( + "static void a()\n" + "{\n" + " class Foo *foo;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: static void a ( )\n" + "2: {\n" + "3: class Foo * foo@1 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid13() { + const std::string actual = tokenize( + "void f()\n" + "{\n" + " int a; int b;\n" + " a = a;\n" + "}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( )\n" + "2: {\n" + "3: int a@1 ; int b@2 ;\n" + "4: a@1 = a@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid14() { + // Overloaded operator* + const std::string actual = tokenize( + "void foo()\n" + "{\n" + "A a;\n" + "B b;\n" + "b * a;\n" + "}", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: A a@1 ;\n" + "4: B b@2 ;\n" + "5: b@2 * a@1 ;\n" + "6: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid15() { + { + const std::string actual = tokenize( + "struct S {\n" + " struct T {\n" + " } t;\n" + "} s;", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: struct S {\n" + "2: struct T {\n" + "3: } ; struct T t@1 ;\n" + "4: } ; struct S s@2 ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize( + "struct S {\n" + " struct T {\n" + " } t;\n" + "};", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: struct S {\n" + "2: struct T {\n" + "3: } ; struct T t@1 ;\n" + "4: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } + } + + void varid16() { + const char code[] ="void foo()\n" + "{\n" + " int x = 1;\n" + " y = (z * x);\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: int x@1 ; x@1 = 1 ;\n" + "4: y = z * x@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, tokenize(code, false, "test.c")); + } + + void varid17() { // ticket #1810 + const char code[] ="char foo()\n" + "{\n" + " char c('c');\n" + " return c;\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: char foo ( )\n" + "2: {\n" + "3: char c@1 ( 'c' ) ;\n" + "4: return c@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, tokenize(code, false, "test.c")); + } + + void varid18() { + const char code[] ="char foo(char c)\n" + "{\n" + " bar::c = c;\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: char foo ( char c@1 )\n" + "2: {\n" + "3: bar :: c = c@1 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid19() { + const char code[] ="void foo()\n" + "{\n" + " std::pair, int> x;\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: std :: pair < std :: vector < double > , int > x@1 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid20() { + const char code[] ="void foo()\n" + "{\n" + " pair, vector > x;\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: void foo ( )\n" + "2: {\n" + "3: pair < vector < int > , vector < double > > x@1 ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid24() { + const char code[] ="class foo()\n" + "{\n" + "public:\n" + " ;\n" + "private:\n" + " static int i;\n" + "};\n"; + + const std::string expected("\n\n##file 0\n" + "1: class foo ( )\n" + "2: {\n" + "3: public:\n" + "4: ;\n" + "5: private:\n" + "6: static int i@1 ;\n" + "7: } ;\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid25() { + const char code[] ="class foo()\n" + "{\n" + "public:\n" + " ;\n" + "private:\n" + " mutable int i;\n" + "};\n"; + + const std::string expected("\n\n##file 0\n" + "1: class foo ( )\n" + "2: {\n" + "3: public:\n" + "4: ;\n" + "5: private:\n" + "6: mutable int i@1 ;\n" + "7: } ;\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid26() { + const char code[] ="list functions;\n"; + const std::string expected("\n\n##file 0\n" + "1: list < int ( * ) ( ) > functions@1 ;\n"); + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid27() { + const char code[] ="int fooled_ya;\n" + "fooled_ya::iterator iter;\n"; + const std::string expected("\n\n##file 0\n" + "1: int fooled_ya@1 ;\n" + "2: fooled_ya :: iterator iter@2 ;\n"); + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid28() { // ticket #2630 (segmentation fault) + tokenize("template \n"); + ASSERT_EQUALS("", errout.str()); + } + + void varid29() { + const char code[] ="class A {\n" + " B,1> b;\n" + "};\n"; + const std::string expected("\n\n##file 0\n" + "1: class A {\n" + "2: B < C < 1 > , 1 > b@1 ;\n" + "3: } ;\n"); + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid30() { // ticket #2614 + const char code1[] = "void f(EventPtr *eventP, ActionPtr **actionsP)\n" + "{\n" + " EventPtr event = *eventP;\n" + " *actionsP = &event->actions;\n" + "}\n"; + const std::string expected1("\n\n##file 0\n" + "1: void f ( EventPtr * eventP@1 , ActionPtr * * actionsP@2 )\n" + "2: {\n" + "3: EventPtr event@3 ; event@3 = * eventP@1 ;\n" + "4: * actionsP@2 = & event@3 . actions@4 ;\n" + "5: }\n"); + ASSERT_EQUALS(expected1, tokenize(code1, false, "test.c")); + + const char code2[] = "void f(int b, int c) {\n" + " x(a*b*c,10);\n" + "}\n"; + const std::string expected2("\n\n##file 0\n" + "1: void f ( int b@1 , int c@2 ) {\n" + "2: x ( a * b@1 * c@2 , 10 ) ;\n" + "3: }\n"); + ASSERT_EQUALS(expected2, tokenize(code2, false, "test.c")); + + const char code3[] = "class Nullpointer : public ExecutionPath\n" + " {\n" + " Nullpointer(Check *c, const unsigned int id, const std::string &name)\n" + " : ExecutionPath(c, id)\n" + " {\n" + " }\n" + "}\n"; + const std::string expected3("\n\n##file 0\n" + "1: class Nullpointer : public ExecutionPath\n" + "2: {\n" + "3: Nullpointer ( Check * c@1 , const int id@2 , const std :: string & name@3 )\n" + "4: : ExecutionPath ( c@1 , id@2 )\n" + "5: {\n" + "6: }\n" + "7: }\n"); + ASSERT_EQUALS(expected3, tokenize(code3)); + } + + void varid31() { // ticket #2831 (segmentation fault) + const char code[] ="z"; + tokenize(code); + ASSERT_EQUALS("", errout.str()); + } + + void varid32() { // ticket #2835 (segmentation fault) + const char code[] ="><,f) + const char code[] ="void f(std::vector)"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: void f ( std :: vector < int > )\n", + tokenize(code, false, "test.cpp")); + } + + void varid50() { // #3760 - explicit + const char code[] ="class A { explicit A(const A&); };"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A { explicit A ( const A & ) ; } ;\n", + tokenize(code, false, "test.cpp")); + } + + void varid51() { // don't set varid on template function + const char code[] ="T t; t.x<0>();"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: T t@1 ; t@1 . x < 0 > ( ) ;\n", + tokenize(code, false, "test.cpp")); + } + + void varid52() { + const char code[] ="A::D> e;\n" + "B< C<> > b[10];\n" + "B> c[10];"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: A < B < C > :: D > e@1 ;\n" + "2: B < C < > > b@2 [ 10 ] ;\n" + "3: B < C < > > c@3 [ 10 ] ;\n", + tokenize(code, false, "test.cpp")); + } + + void varid53() { // #4172 - Template instantiation: T<&functionName> list[4]; + ASSERT_EQUALS("\n\n##file 0\n" + "1: A < & f > list@1 [ 4 ] ;\n", + tokenize("A<&f> list[4];", false, "test.cpp")); + } + + void varid54() { // hang + // Original source code: libgc + tokenize("STATIC ptr_t GC_approx_sp(void) { word sp; sp = (word)&sp; return((ptr_t)sp); }",true); + } + + void varid55() { // Ticket #5868 + const char code[] = "typedef struct foo {} foo; " + "void bar1(struct foo foo) {} " + "void baz1(foo foo) {} " + "void bar2(struct foo& foo) {} " + "void baz2(foo& foo) {} " + "void bar3(struct foo* foo) {} " + "void baz3(foo* foo) {}"; + const char expected[] = "\n\n##file 0\n1: struct foo { } ; " + "void bar1 ( struct foo foo@1 ) { } " + "void baz1 ( struct foo foo@2 ) { } " + "void bar2 ( struct foo & foo@3 ) { } " + "void baz2 ( struct foo & foo@4 ) { } " + "void bar3 ( struct foo * foo@5 ) { } " + "void baz3 ( struct foo * foo@6 ) { }\n"; + ASSERT_EQUALS(expected, tokenize(code, false, "test.cpp")); + } + + void varid_cpp_keywords_in_c_code() { + const char code[] = "void f() {\n" + " delete d;\n" + " throw t;\n" + "}"; + + const char expected[] = "\n\n##file 0\n" + "1: void f ( ) {\n" + "2: delete d@1 ;\n" + "3: throw t@2 ;\n" + "4: }\n"; + + ASSERT_EQUALS(expected, tokenize(code,false,"test.c")); + } + + void varid_cpp_keywords_in_c_code2() { // #5373 + const char code[] = "int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, " + "unsigned long bits, int wake, int delete, struct extent_state **cached_state, " + "gfp_t mask) {\n" + " struct extent_state *state;\n" + "}" + "int clear_extent_dirty() {\n" + " return clear_extent_bit(tree, start, end, EXTENT_DIRTY | EXTENT_DELALLOC | " + " EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);\n" + "}"; + tokenize(code, false, "test.c"); + } + + void varidFunctionCall1() { + const char code[] ="void f() {\n" + " int x;\n" + " x = a(y*x,10);\n" + "}"; + const std::string expected("\n\n##file 0\n" + "1: void f ( ) {\n" + "2: int x@1 ;\n" + "3: x@1 = a ( y * x@1 , 10 ) ;\n" + "4: }\n"); + ASSERT_EQUALS(expected, tokenize(code, false, "test.c")); + } + + void varidFunctionCall2() { + // #2491 + const char code[] ="void f(int b) {\n" + " x(a*b,10);\n" + "}"; + const std::string expected1("\n\n##file 0\n" + "1: void f ( int b@1 ) {\n" + "2: x ( a * b"); + const std::string expected2(" , 10 ) ;\n" + "3: }\n"); + ASSERT_EQUALS(expected1+"@1"+expected2, tokenize(code,false,"test.c")); + } + + void varidFunctionCall3() { + // Ticket #2339 + const char code[] ="void f() {\n" + " int a = 0;\n" + " int b = c - (foo::bar * a);\n" + "}"; + + const std::string expected("\n\n##file 0\n" + "1: void f ( ) {\n" + "2: int a@1 ; a@1 = 0 ;\n" + "3: int b@2 ; b@2 = c - ( foo :: bar * a@1 ) ;\n" + "4: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidFunctionCall4() { + // Ticket #3280 + const char code1[] = "void f() { int x; fun(a,b*x); }"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: void f ( ) { int x@1 ; fun ( a , b * x@1 ) ; }\n", + tokenize(code1, false, "test.c")); + const char code2[] = "void f(int a) { int x; fun(a,b*x); }"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: void f ( int a@1 ) { int x@2 ; fun ( a@1 , b * x@2 ) ; }\n", + tokenize(code2, false, "test.c")); + } + + + void varidStl() { + const std::string actual = tokenize( + "list ints;\n" + "list::iterator it;\n" + "std::vector dirs;\n" + "std::map coords;\n" + "std::tr1::unordered_map xy;\n" + "std::list tokens;\n" + "static std::vector ex1;\n" + "extern std::vector ex2;\n" + "std::map m;\n" + ); + + const std::string expected("\n\n##file 0\n" + "1: list < int > ints@1 ;\n" + "2: list < int > :: iterator it@2 ;\n" + "3: std :: vector < std :: string > dirs@3 ;\n" + "4: std :: map < int , int > coords@4 ;\n" + "5: std :: tr1 :: unordered_map < int , int > xy@5 ;\n" + "6: std :: list < boost :: wave :: token_id > tokens@6 ;\n" + "7: static std :: vector < CvsProcess * > ex1@7 ;\n" + "8: extern std :: vector < CvsProcess * > ex2@8 ;\n" + "9: std :: map < int , 1 > m@9 ;\n" + ); + + ASSERT_EQUALS(expected, actual); + } + + void varid_delete() { + const std::string actual = tokenize( + "void f()\n" + "{\n" + " int *a;\n" + " delete a;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( )\n" + "2: {\n" + "3: int * a@1 ;\n" + "4: delete a@1 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid_functions() { + { + const std::string actual = tokenize( + "void f();\n" + "void f(){}\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( ) ;\n" + "2: void f ( ) { }\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize( + "A f(3);\n" + "A f2(true);\n" + "A g();\n" + "A e(int c);\n", false, "test.c"); + + const std::string expected("\n\n##file 0\n" + "1: A f@1 ( 3 ) ;\n" + "2: A f2@2 ( true ) ;\n" + "3: A g ( ) ;\n" + "4: A e ( int c@3 ) ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize( + "void f1(int &p)\n" + "{\n" + " p = 0;\n" + "}\n" + "void f2(std::string &str)\n" + "{\n" + " str.clear();\n" + "}\n" + "void f3(const std::string &s)\n" + "{\n" + " s.size();\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: void f1 ( int & p@1 )\n" + "2: {\n" + "3: p@1 = 0 ;\n" + "4: }\n" + "5: void f2 ( std :: string & str@2 )\n" + "6: {\n" + "7: str@2 . clear ( ) ;\n" + "8: }\n" + "9: void f3 ( const std :: string & s@3 )\n" + "10: {\n" + "11: s@3 . size ( ) ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize("void f(struct foobar);", false, "test.c"); + const std::string expected("\n\n##file 0\n" + "1: void f ( struct foobar ) ;\n"); + ASSERT_EQUALS(expected, actual); + } + } + + void varid_sizeof() { + const char code[] = "x = sizeof(a*b);"; + const char expected[] = "\n\n##file 0\n" + "1: x = sizeof ( a * b ) ;\n"; + ASSERT_EQUALS(expected, tokenize(code,false,"test.c")); + } + + void varid_reference_to_containers() { + const std::string actual = tokenize( + "void f()\n" + "{\n" + " std::vector b;\n" + " std::vector &a = b;\n" + " std::vector *c = &b;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: void f ( )\n" + "2: {\n" + "3: std :: vector < int > b@1 ;\n" + "4: std :: vector < int > & a@2 = b@1 ;\n" + "5: std :: vector < int > * c@3 ; c@3 = & b@1 ;\n" + "6: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid_in_class1() { + { + const std::string actual = tokenize( + "class Foo\n" + "{\n" + "public:\n" + " std::string name1;\n" + " std::string name2;\n" + "};\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Foo\n" + "2: {\n" + "3: public:\n" + "4: std :: string name1@1 ;\n" + "5: std :: string name2@2 ;\n" + "6: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenize( + "class foo\n" + "{\n" + "public:\n" + " void do_something(const int x, const int y);\n" + " void bar();\n" + "};\n" + "\n" + "void foo::bar()\n" + "{\n" + " POINT pOutput = { 0 , 0 };\n" + " int x = pOutput.x;\n" + " int y = pOutput.y;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: class foo\n" + "2: {\n" + "3: public:\n" + "4: void do_something ( const int x@1 , const int y@2 ) ;\n" + "5: void bar ( ) ;\n" + "6: } ;\n" + "7:\n" + "8: void foo :: bar ( )\n" + "9: {\n" + "10: POINT pOutput@3 ; pOutput@3 = { 0 , 0 } ;\n" + "11: int x@4 ; x@4 = pOutput@3 . x@5 ;\n" + "12: int y@6 ; y@6 = pOutput@3 . y@7 ;\n" + "13: }\n"); + + ASSERT_EQUALS(expected, actual); + } + } + + void varid_in_class2() { + const std::string actual = tokenize( + "struct Foo {\n" + " int x;\n" + "};\n" + "\n" + "struct Bar {\n" + " Foo foo;\n" + " int x;\n" + " void f();\n" + "};\n" + "\n" + "void Bar::f()\n" + "{\n" + " foo.x = x;\n" + "}\n"); + const std::string expected("\n\n##file 0\n" + "1: struct Foo {\n" + "2: int x@1 ;\n" + "3: } ;\n" + "4:\n" + "5: struct Bar {\n" + "6: Foo foo@2 ;\n" + "7: int x@3 ;\n" + "8: void f ( ) ;\n" + "9: } ;\n" + "10:\n" + "11: void Bar :: f ( )\n" + "12: {\n" + "13: foo@2 . x@4 = x@3 ;\n" + "14: }\n"); + ASSERT_EQUALS(expected, actual); + } + + void varid_in_class3() { + const char code[] = "class Foo {\n" + " void blah() {\n" + " Bar x(*this);\n" // <- .. + " }\n" + " int x;\n" // <- .. don't assign same varid + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo {\n" + "2: void blah ( ) {\n" + "3: Bar x@1 ( * this ) ;\n" + "4: }\n" + "5: int x@2 ;\n" + "6: } ;\n", tokenize(code)); + } + + void varid_in_class4() { + const char code[] = "class Foo {\n" + "public: class C;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo {\n" + "2: public: class C ;\n" + "3: } ;\n", + tokenize(code)); + } + + void varid_in_class5() { + const char code[] = "struct Foo {\n" + " std::vector<::X> v;\n" + "}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct Foo {\n" + "2: std :: vector < :: X > v@1 ;\n" + "3: }\n", + tokenize(code)); + } + + void varid_in_class6() { + const char code[] = "class A {\n" + " void f(const char *str) const {\n" + " std::stringstream sst;\n" + " sst.str();\n" + " }\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: void f ( const char * str@1 ) const {\n" + "3: std :: stringstream sst@2 ;\n" + "4: sst@2 . str ( ) ;\n" + "5: }\n" + "6: } ;\n", + tokenize(code)); + } + + void varid_in_class7() { + const char code[] = "class A {\n" + " void f() {\n" + " abc.a = 0;\n" + " }\n" + " struct ABC abc;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: void f ( ) {\n" + "3: abc@1 . a@2 = 0 ;\n" + "4: }\n" + "5: struct ABC abc@1 ;\n" + "6: } ;\n", + tokenize(code)); + } + + void varid_in_class8() { // #3776 - unknown macro + const char code[] = "class A {\n" + " UNKNOWN_MACRO(A)\n" + "private:\n" + " int x;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: UNKNOWN_MACRO ( A )\n" + "3: private:\n" + "4: int x@1 ;\n" + "5: } ;\n", + tokenize(code)); + } + + void varid_in_class9() { // #4291 - id for variables accessed through 'this' + const char code1[] = "class A {\n" + " int var;\n" + "public:\n" + " void setVar();\n" + "};\n" + "void A::setVar() {\n" + " this->var = var;\n" + "}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int var@1 ;\n" + "3: public:\n" + "4: void setVar ( ) ;\n" + "5: } ;\n" + "6: void A :: setVar ( ) {\n" + "7: this . var@1 = var@1 ;\n" + "8: }\n", + tokenize(code1)); + + const char code2[] = "class Foo : public FooBase {\n" + " void Clone(FooBase& g);\n" + " short m_bar;\n" + "};\n" + "void Foo::Clone(FooBase& g) {\n" + " g->m_bar = m_bar;\n" + "}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo : public FooBase {\n" + "2: void Clone ( FooBase & g@1 ) ;\n" + "3: short m_bar@2 ;\n" + "4: } ;\n" + "5: void Foo :: Clone ( FooBase & g@3 ) {\n" + "6: g@3 . m_bar@4 = m_bar@2 ;\n" + "7: }\n", + tokenize(code2)); // #4311 + } + + void varid_in_class10() { + const char code[] = "class Foo : public FooBase {\n" + " void Clone(FooBase& g);\n" + " short m_bar;\n" + "};\n" + "void Foo::Clone(FooBase& g) {\n" + " ((FooBase)g)->m_bar = m_bar;\n" + "}"; + TODO_ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo : public FooBase {\n" + "2: void Clone ( FooBase & g@1 ) ;\n" + "3: short m_bar@2 ;\n" + "4: } ;\n" + "5: void Foo :: Clone ( FooBase & g@3 ) {\n" + "6: ( ( FooBase ) g@3 ) . m_bar@4 = m_bar@2 ;\n" + "7: }\n", + "\n\n##file 0\n" + "1: class Foo : public FooBase {\n" + "2: void Clone ( FooBase & g@1 ) ;\n" + "3: short m_bar@2 ;\n" + "4: } ;\n" + "5: void Foo :: Clone ( FooBase & g@3 ) {\n" + "6: ( ( FooBase ) g@3 ) . m_bar = m_bar@2 ;\n" + "7: }\n", + tokenize(code)); + } + + void varid_in_class11() { // #4277 - anonymous union + const char code1[] = "class Foo {\n" + " union { float a; int b; };\n" + " void f() { a=0; }\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo {\n" + "2: union { float a@1 ; int b@2 ; } ;\n" + "3: void f ( ) { a@1 = 0 ; }\n" + "4: } ;\n", + tokenize(code1)); + + const char code2[] = "class Foo {\n" + " void f() { a=0; }\n" + " union { float a; int b; };\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo {\n" + "2: void f ( ) { a@1 = 0 ; }\n" + "3: union { float a@1 ; int b@2 ; } ;\n" + "4: } ;\n", + tokenize(code2)); + } + + void varid_in_class12() { // #4637 - method + const char code[] = "class Foo {\n" + "private:\n" + " void f(void);\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Foo {\n" + "2: private:\n" + "3: void f ( ) ;\n" + "4: } ;\n", + tokenize(code)); + } + + void varid_in_class13() { + const char code1[] = "struct a { char typename; };"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct a { char typename@1 ; } ;\n", + tokenize(code1, false, "test.c")); + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct a { char typename ; } ;\n", // not valid C++ code + tokenize(code1, false, "test.cpp")); + + const char code2[] = "struct a { char typename[2]; };"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct a { char typename@1 [ 2 ] ; } ;\n", + tokenize(code2, false, "test.c")); + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct a { char typename [ 2 ] ; } ;\n", // not valid C++ code + tokenize(code2, false, "test.cpp")); + } + + void varid_in_class14() { + const char code[] = "class Tokenizer { TokenList list; };\n" + "\n" + "void Tokenizer::f() {\n" + " std::list x;\n" // <- not member variable + " list.do_something();\n" // <- member variable + " Tokenizer::list.do_something();\n" // <- redundant scope info + "}\n"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Tokenizer { TokenList list@1 ; } ;\n" + "2:\n" + "3: void Tokenizer :: f ( ) {\n" + "4: std :: list < int > x@2 ;\n" + "5: list@1 . do_something ( ) ;\n" + "6: Tokenizer :: list@1 . do_something ( ) ;\n" + "7: }\n", tokenize(code, false, "test.cpp")); + } + + void varid_in_class15() { // #5533 - functions + const char code[] = "class Fred {\n" + " void x(int a) const;\n" + " void y() { a=0; }\n" // <- unknown variable + "}\n"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Fred {\n" + "2: void x ( int a@1 ) const ;\n" + "3: void y ( ) { a = 0 ; }\n" + "4: }\n", tokenize(code, false, "test.cpp")); + } + + void varid_in_class16() { // Set varId for inline member functions + const char code[] = "class Fred {\n" + " int x;\n" + " void foo(int x) { this->x = x; }\n" + "};\n"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Fred {\n" + "2: int x@1 ;\n" + "3: void foo ( int x@2 ) { this . x@1 = x@2 ; }\n" + "4: } ;\n", tokenize(code, false, "test.cpp")); + } + + void varid_in_class17() { // #6056 - Set no varid for member functions + const char code1[] = "class Fred {\n" + " int method_with_internal(X&);\n" + " int method_with_internal(X*);\n" + " int method_with_internal(int&);\n" + " int method_with_internal(A* b, X&);\n" + " int method_with_internal(X&, A* b);\n" + " int method_with_internal(const B &, int);\n" + " void Set(BAR);\n" + " FOO Set(BAR);\n" + " int method_with_class(B b);\n" + " bool function(std::map & m);\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class Fred {\n" + "2: int method_with_internal ( X & ) ;\n" + "3: int method_with_internal ( X * ) ;\n" + "4: int method_with_internal ( int & ) ;\n" + "5: int method_with_internal ( A * b@1 , X & ) ;\n" + "6: int method_with_internal ( X & , A * b@2 ) ;\n" + "7: int method_with_internal ( const B & , int ) ;\n" + "8: void Set ( BAR ) ;\n" + "9: FOO Set ( BAR ) ;\n" + "10: int method_with_class ( B < B > b@3 ) ;\n" + "11: bool function ( std :: map < int , int , MYless > & m@4 ) ;\n" + "12: } ;\n", tokenize(code1, false, "test.cpp")); + + const char code2[] = "int i;\n" + "SomeType someVar1(i, i);\n" + "SomeType someVar2(j, j);\n" + "SomeType someVar3(j, 1);\n" + "SomeType someVar4(new bar);"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: int i@1 ;\n" + "2: SomeType someVar1@2 ( i@1 , i@1 ) ;\n" + "3: SomeType someVar2 ( j , j ) ;\n" // This one could be a function + "4: SomeType someVar3@3 ( j , 1 ) ;\n" + "5: SomeType someVar4@4 ( new bar ) ;\n", tokenize(code2, false, "test.cpp")); + } + + void varid_initList() { + const char code1[] = "class A {\n" + " A() : x(0) {}\n" + " int x;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: A ( ) : x@1 ( 0 ) { }\n" + "3: int x@1 ;\n" + "4: } ;\n", + tokenize(code1)); + + const char code2[] = "class A {\n" + " A(int x) : x(x) {}\n" + " int x;\n" + "};"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: A ( int x@1 ) : x@2 ( x@1 ) { }\n" + "3: int x@2 ;\n" + "4: } ;\n", + tokenize(code2)); + + const char code3[] = "class A {\n" + " A(int x);\n" + " int x;\n" + "};\n" + "A::A(int x) : x(x) {}"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: A ( int x@1 ) ;\n" + "3: int x@2 ;\n" + "4: } ;\n" + "5: A :: A ( int x@3 ) : x@2 ( x@3 ) { }\n", + tokenize(code3)); + + const char code4[] = "struct A {\n" + " int x;\n" + " A(int x) : x(x) {}\n" + "};\n"; + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct A {\n" + "2: int x@1 ;\n" + "3: A ( int x@2 ) : x@1 ( x@2 ) { }\n" + "4: } ;\n", + tokenize(code4)); + } + + void varid_operator() { + { + const std::string actual = tokenize( + "class Foo\n" + "{\n" + "public:\n" + " void operator=(const Foo &);\n" + "};\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Foo\n" + "2: {\n" + "3: public:\n" + "4: void operator= ( const Foo & ) ;\n" + "5: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } + { + const std::string actual = tokenize( + "struct Foo {\n" + " void * operator new [](int);\n" + "};\n"); + const std::string expected("\n\n##file 0\n" + "1: struct Foo {\n" + "2: void * operatornew[] ( int ) ;\n" + "3: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } + } + + void varid_throw() { // ticket #1723 + const std::string actual = tokenize( + "UserDefinedException* pe = new UserDefinedException();\n" + "throw pe;\n"); + + const std::string expected("\n\n##file 0\n" + "1: UserDefinedException * pe@1 ; pe@1 = new UserDefinedException ( ) ;\n" + "2: throw pe@1 ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varid_unknown_macro() { + // #2638 - unknown macro + const char code[] = "void f() {\n" + " int a[10];\n" + " AAA\n" + " a[0] = 0;\n" + "}"; + const char expected[] = "\n\n##file 0\n" + "1: void f ( ) {\n" + "2: int a@1 [ 10 ] ;\n" + "3: AAA\n" + "4: a@1 [ 0 ] = 0 ;\n" + "5: }\n"; + ASSERT_EQUALS(expected, tokenize(code, false, "test.c")); + } + + void varid_using() { + // #3648 + const char code[] = "using std::size_t;"; + const char expected[] = "\n\n##file 0\n" + "1: using long ;\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid_catch() { + const char code[] = "void f() {\n" + " try { dostuff(); }\n" + " catch (exception &e) { }\n" + "}"; + const char expected[] = "\n\n##file 0\n" + "1: void f ( ) {\n" + "2: try { dostuff ( ) ; }\n" + "3: catch ( exception & e@1 ) { }\n" + "4: }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid_functionPrototypeTemplate() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: function < void ( ) > fptr@1 ;\n", tokenize("function fptr;")); + } + + void varid_templatePtr() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: std :: map < int , FooTemplate < int > * > dummy_member@1 [ 1 ] ;\n", tokenize("std::map*> dummy_member[1];")); + } + + void varid_templateNamespaceFuncPtr() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: KeyListT < float , & NIFFile :: getFloat > mKeyList@1 [ 4 ] ;\n", tokenize("KeyListT mKeyList[4];")); + } + + void varid_templateArray() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: VertexArrayIterator < float [ 2 ] > attrPos@1 ; attrPos@1 = m_AttributePos . GetIterator < float [ 2 ] > ( ) ;\n", + tokenize("VertexArrayIterator attrPos = m_AttributePos.GetIterator();")); + } + + void varid_variadicFunc() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: int foo ( . . . ) ;\n", tokenize("int foo(...);")); + } + + void varid_typename() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: template < int d , class A , class B >\n", tokenize("template")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: template < int d , typename A , typename B >\n", tokenize("template")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: typename A a@1 ;\n", tokenize("typename A a;")); + } + + void varid_rvalueref() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: int & & a@1 ;\n", tokenize("int&& a;")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: void foo ( int & & a@1 ) { }\n", tokenize("void foo(int&& a) {}")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: class C {\n" + "2: C ( int & & a@1 ) ;\n" + "3: } ;\n", + tokenize("class C {\n" + " C(int&& a);\n" + "};")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: void foo ( int & & ) ;\n", tokenize("void foo(int&&);")); + } + + void varid_arrayFuncPar() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: void check ( const char fname@1 [ ] = 0 ) { }\n", tokenize("void check( const char fname[] = 0) { }")); + } + + void varid_sizeofPassed() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: void which_test ( ) {\n" + "2: const char * argv@1 [ 2 ] = { \"./test_runner\" , \"TestClass\" } ;\n" + "3: options args@2 ( sizeof argv@1 / sizeof ( argv@1 [ 0 ] ) , argv@1 ) ;\n" + "4: args@2 . which_test ( ) ;\n" + "5: }\n", + tokenize("void which_test() {\n" + " const char* argv[] = { \"./test_runner\", \"TestClass\" };\n" + " options args(sizeof argv / sizeof argv[0], argv);\n" + " args.which_test();\n" + "}")); + } + + void varid_classInFunction() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: void AddSuppression ( ) {\n" + "2: class QErrorLogger {\n" + "3: void reportErr ( ErrorLogger :: ErrorMessage & msg@1 ) {\n" + "4: }\n" + "5: } ;\n" + "6: }\n", + tokenize("void AddSuppression() {\n" + " class QErrorLogger {\n" + " void reportErr(ErrorLogger::ErrorMessage &msg) {\n" + " }\n" + " }; \n" + "}")); + } + + void varid_pointerToArray() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: int * a1@1 [ 10 ] ;\n" + "2: void f1 ( ) {\n" + "3: int * a2@2 [ 10 ] ;\n" + "4: int ( & a3@3 ) [ 10 ] ;\n" + "5: }\n" + "6: struct A {\n" + "7: int ( & a4@4 ) [ 10 ] ;\n" + "8: int f2 ( int i@5 ) { return a4@4 [ i@5 ] ; }\n" + "9: int f3 ( int ( & a5@6 ) [ 10 ] , int i@7 ) { return a5@6 [ i@7 ] ; }\n" + "10: } ;\n" + "11: int f4 ( int ( & a6@8 ) [ 10 ] , int i@9 ) { return a6@8 [ i@9 ] ; }\n", + tokenize("int (*a1)[10];\n" // pointer to array of 10 ints + "void f1() {\n" + " int(*a2)[10];\n" + " int(&a3)[10];\n" + "}\n" + "struct A {\n" + " int(&a4)[10];\n" + " int f2(int i) { return a4[i]; }\n" + " int f3(int(&a5)[10], int i) { return a5[i]; }\n" + "};\n" + "int f4(int(&a6)[10], int i) { return a6[i]; }")); + } + + void varid_cpp11initialization() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: int i@1 { 1 } ;\n" + "2: std :: vector < int > vec@2 { 1 , 2 , 3 } ;\n" + "3: namespace n { int z@3 ; } ;\n" + "4: int & j@4 { i@1 } ;\n", + tokenize("int i{1};\n" + "std::vector vec{1, 2, 3};\n" + "namespace n { int z; };\n" + "int& j{i};\n")); + + // #6030 + ASSERT_EQUALS("\n\n##file 0\n" + "1: struct S3 : public S1 , public S2 { } ;\n", + tokenize("struct S3 : public S1, public S2 { };")); + + // #6058 + ASSERT_EQUALS("\n\n##file 0\n" + "1: class CPPCHECKLIB Scope { } ;\n", + tokenize("class CPPCHECKLIB Scope { };")); + + // #6073 + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A : public B , public C :: D {\n" + "2: int i@1 ;\n" + "3: A ( int i@2 ) : B { i@2 } , C :: D { i@2 } , i@1 { i@2 } {\n" + "4: int j@3 { i@2 } ;\n" + "5: }\n" + "6: } ;\n", + tokenize("class A: public B, public C::D {\n" + " int i;\n" + " A(int i): B{i}, C::D{i}, i{i} {\n" + " int j{i};\n" + " }\n" + "};")); + } + + void varid_inheritedMembers() { + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: } ;\n" + "4: class B : public A {\n" + "5: void f ( ) ;\n" + "6: } ;\n" + "7: void B :: f ( ) {\n" + "8: a@1 = 0 ;\n" + "9: }\n", + tokenize("class A {\n" + " int a;\n" + "};\n" + "class B : public A {\n" + " void f();\n" + "};\n" + "void B::f() {\n" + " a = 0;\n" + "}")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: } ;\n" + "4: class B : A {\n" + "5: void f ( ) ;\n" + "6: } ;\n" + "7: void B :: f ( ) {\n" + "8: a@1 = 0 ;\n" + "9: }\n", + tokenize("class A {\n" + " int a;\n" + "};\n" + "class B : A {\n" + " void f();\n" + "};\n" + "void B::f() {\n" + " a = 0;\n" + "}")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: } ;\n" + "4: class B : protected B , public A {\n" + "5: void f ( ) ;\n" + "6: } ;\n" + "7: void B :: f ( ) {\n" + "8: a@1 = 0 ;\n" + "9: }\n", + tokenize("class A {\n" + " int a;\n" + "};\n" + "class B : protected B, public A {\n" + " void f();\n" + "};\n" + "void B::f() {\n" + " a = 0;\n" + "}")); + + ASSERT_EQUALS("\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: } ;\n" + "4: class B : public A {\n" + "5: void f ( ) {\n" + "6: a@1 = 0 ;\n" + "7: }\n" + "8: } ;\n", + tokenize("class A {\n" + " int a;\n" + "};\n" + "class B : public A {\n" + " void f() {\n" + " a = 0;\n" + " }\n" + "};")); + } + + void varidclass1() { + const std::string actual = tokenize( + "class Fred\n" + "{\n" + "private:\n" + " int i;\n" + "\n" + " void foo1();\n" + " void foo2()\n" + " {\n" + " ++i;\n" + " }\n" + "}\n" + "\n" + "Fred::foo1()\n" + "{\n" + " i = 0;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred\n" + "2: {\n" + "3: private:\n" + "4: int i@1 ;\n" + "5:\n" + "6: void foo1 ( ) ;\n" + "7: void foo2 ( )\n" + "8: {\n" + "9: ++ i@1 ;\n" + "10: }\n" + "11: }\n" + "12:\n" + "13: Fred :: foo1 ( )\n" + "14: {\n" + "15: i@1 = 0 ;\n" + "16: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varidclass2() { + const std::string actual = tokenize( + "class Fred\n" + "{ void f(); };\n" + "\n" + "void A::foo1()\n" + "{\n" + " int i = 0;\n" + "}\n" + "\n" + "void Fred::f()\n" + "{\n" + " i = 0;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred\n" + "2: { void f ( ) ; } ;\n" + "3:\n" + "4: void A :: foo1 ( )\n" + "5: {\n" + "6: int i@1 ; i@1 = 0 ;\n" + "7: }\n" + "8:\n" + "9: void Fred :: f ( )\n" + "10: {\n" + "11: i = 0 ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varidclass3() { + const std::string actual = tokenize( + "class Fred\n" + "{ int i; void f(); };\n" + "\n" + "void Fred::f()\n" + "{\n" + " i = 0;\n" + "}\n" + "\n" + "void A::f()\n" + "{\n" + " i = 0;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred\n" + "2: { int i@1 ; void f ( ) ; } ;\n" + "3:\n" + "4: void Fred :: f ( )\n" + "5: {\n" + "6: i@1 = 0 ;\n" + "7: }\n" + "8:\n" + "9: void A :: f ( )\n" + "10: {\n" + "11: i = 0 ;\n" + "12: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + + void varidclass4() { + const std::string actual = tokenize( + "class Fred\n" + "{ int i; void f(); };\n" + "\n" + "void Fred::f()\n" + "{\n" + " if (i) { }\n" + " i = 0;\n" + "}\n"); + + const std::string expected("\n\n##file 0\n" + "1: class Fred\n" + "2: { int i@1 ; void f ( ) ; } ;\n" + "3:\n" + "4: void Fred :: f ( )\n" + "5: {\n" + "6: if ( i@1 ) { }\n" + "7: i@1 = 0 ;\n" + "8: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidclass5() { + const std::string actual = tokenize( + "class A { };\n" + "class B\n" + "{\n" + " A *a;\n" + " B() : a(new A)\n" + " { }\n" + "};\n"); + + const std::string expected("\n\n##file 0\n" + "1: class A { } ;\n" + "2: class B\n" + "3: {\n" + "4: A * a@1 ;\n" + "5: B ( ) : a@1 ( new A )\n" + "6: { }\n" + "7: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidclass6() { + const std::string actual = tokenize( + "class A\n" + "{\n" + " public:\n" + " static char buf[20];\n" + "};\n" + "char A::buf[20];\n" + "int main()\n" + "{\n" + " char buf[2];\n" + " A::buf[10] = 0;\n" + "}"); + + const std::string wanted("\n\n##file 0\n" + "1: class A\n" + "2: {\n" + "3: public:\n" + "4: static char buf@1 [ 20 ] ;\n" + "5: } ;\n" + "6: char A :: buf@1 [ 20 ] ;\n" + "7: int main ( )\n" + "8: {\n" + "9: char buf@2 [ 2 ] ;\n" + "10: A :: buf@1 [ 10 ] = 0 ;\n" + "11: }\n"); + + ASSERT_EQUALS(wanted, actual); + } + + void varidclass7() { + const std::string actual = tokenize( + "int main()\n" + "{\n" + " char buf[2];\n" + " A::buf[10] = 0;\n" + "}"); + + const std::string expected("\n\n##file 0\n" + "1: int main ( )\n" + "2: {\n" + "3: char buf@1 [ 2 ] ;\n" + "4: A :: buf [ 10 ] = 0 ;\n" + "5: }\n"); + + ASSERT_EQUALS(expected, actual); + } + + void varidclass8() { + const char code[] ="class Fred {\n" + "public:\n" + " void foo(int d) {\n" + " int i = bar(x * d);\n" + " }\n" + " int x;\n" + "}\n"; + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: public:\n" + "3: void foo ( int d@1 ) {\n" + "4: int i@2 ; i@2 = bar ( x@3 * d@1 ) ;\n" + "5: }\n" + "6: int x@3 ;\n" + "7: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass9() { + const char code[] ="typedef char Str[10];" + "class A {\n" + "public:\n" + " void f(Str &cl);\n" + " void g(Str cl);\n" + "}\n" + "void Fred::f(Str &cl) {\n" + " sizeof(cl);\n" + "}"; + + const std::string expected("\n\n" + "##file 0\n" + "1: class A {\n" + "2: public:\n" + "3: void f ( char ( & cl@1 ) [ 10 ] ) ;\n" + "4: void g ( char cl@2 [ 10 ] ) ;\n" + "5: }\n" + "6: void Fred :: f ( char ( & cl@3 ) [ 10 ] ) {\n" + "7: sizeof ( cl@3 ) ;\n" + "8: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass10() { + const char code[] ="class A {\n" + " void f() {\n" + " a = 3;\n" + " }\n" + " int a;\n" + "};\n"; + + const std::string expected("\n\n##file 0\n" + "1: class A {\n" + "2: void f ( ) {\n" + "3: a@1 = 3 ;\n" + "4: }\n" + "5: int a@1 ;\n" + "6: } ;\n"); + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass11() { + const char code[] ="class Fred {\n" + " int a;\n" + " void f();\n" + "};\n" + "class Wilma {\n" + " int a;\n" + " void f();\n" + "};\n" + "void Fred::f() { a = 0; }\n" + "void Wilma::f() { a = 0; }\n"; + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: int a@1 ;\n" + "3: void f ( ) ;\n" + "4: } ;\n" + "5: class Wilma {\n" + "6: int a@2 ;\n" + "7: void f ( ) ;\n" + "8: } ;\n" + "9: void Fred :: f ( ) { a@1 = 0 ; }\n" + "10: void Wilma :: f ( ) { a@2 = 0 ; }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass12() { + const char code[] ="class Fred {\n" + " int a;\n" + " void f() { Fred::a = 0; }\n" + "};\n"; + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: int a@1 ;\n" + "3: void f ( ) { Fred :: a@1 = 0 ; }\n" + "4: } ;\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass13() { + const char code[] ="class Fred {\n" + " int a;\n" + " void f() { Foo::Fred::a = 0; }\n" + "};\n"; + + const std::string expected("\n\n##file 0\n" + "1: class Fred {\n" + "2: int a@1 ;\n" + "3: void f ( ) { Foo :: Fred :: a = 0 ; }\n" + "4: } ;\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass14() { + // don't give friend classes varid + { + const char code[] ="class A {\n" + "friend class B;\n" + "}"; + + const std::string expected("\n\n##file 0\n" + "1: class A {\n" + "2: friend class B ;\n" + "3: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + + { + const char code[] ="class A {\n" + "private: friend class B;\n" + "}"; + + const std::string expected("\n\n##file 0\n" + "1: class A {\n" + "2: private: friend class B ;\n" + "3: }\n"); + + ASSERT_EQUALS(expected, tokenize(code)); + } + } + + void varidclass15() { + const char code[] = "class A {\n" + " int a;\n" + " int b;\n" + " A();\n" + "};\n" + "A::A() : a(0) { b = 1; }"; + const char expected[] = "\n\n##file 0\n" + "1: class A {\n" + "2: int a@1 ;\n" + "3: int b@2 ;\n" + "4: A ( ) ;\n" + "5: } ;\n" + "6: A :: A ( ) : a@1 ( 0 ) { b@2 = 1 ; }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass16() { + const char code[] = "struct A;\n" + "typedef bool (A::* FuncPtr)();\n" + "struct A {\n" + " FuncPtr pFun;\n" + " void setPFun(int mode);\n" + " bool funcNorm();\n" + "};\n" + "void A::setPFun(int mode) {\n" + " pFun = &A::funcNorm;\n" + "}"; + const char expected[] = "\n\n##file 0\n" + "1: struct A ;\n" + "2:\n" + "3: struct A {\n" + "4: bool * pFun@1 ;\n" + "5: void setPFun ( int mode@2 ) ;\n" + "6: bool funcNorm ( ) ;\n" + "7: } ;\n" + "8: void A :: setPFun ( int mode@3 ) {\n" + "9: pFun@1 = & A :: funcNorm ;\n" + "10: }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varidclass17() { + const char code[] = "class A: public B, public C::D {\n" + " int i;\n" + " A(int i): B(i), C::D(i), i(i) {\n" + " int j(i);\n" + " }\n" + "};"; + const char expected[] = "\n\n##file 0\n" + "1: class A : public B , public C :: D {\n" + "2: int i@1 ;\n" + "3: A ( int i@2 ) : B ( i@2 ) , C :: D ( i@2 ) , i@1 ( i@2 ) {\n" + "4: int j@3 ; j@3 = i@2 ;\n" + "5: }\n" + "6: } ;\n"; + ASSERT_EQUALS(expected, tokenize(code)); + } + + void varid_classnameshaddowsvariablename() { + const char code[] = "class Data;\n" + "void strange_declarated(const Data& Data);\n" + "void handleData(const Data& data) {\n" + " strange_declarated(data);\n" + "}\n"; + const char expected[] = "\n\n##file 0\n" + "1: class Data ;\n" + "2: void strange_declarated ( const Data & Data@1 ) ;\n" + "3: void handleData ( const Data & data@2 ) {\n" + "4: strange_declarated ( data@2 ) ;\n" + "5: }\n"; + ASSERT_EQUALS(expected, tokenize(code)); + + } + + void varidnamespace1() { + const char code[] = "namespace A {\n" + " char buf[20];\n" + "}\n" + "int main() {\n" + " return foo(A::buf);\n" + "}"; + + const char expected[] = "\n\n##file 0\n" + "1: namespace A {\n" + "2: char buf@1 [ 20 ] ;\n" + "3: }\n" + "4: int main ( ) {\n" + "5: return foo ( A :: buf@1 ) ;\n" + "6: }\n"; + + ASSERT_EQUALS(expected, tokenize(code)); + } +}; + +REGISTER_TEST(TestVarID)