Refactorizations in test suite:

- Removed redundant tokenization helper functions in testtokenize.cpp and testsimplifytokens.cpp
- Pass arguments as const char[] or const std::string& to avoid instanciation or at least copying of strings
- Removed redundant calls to Tokenizer::validate() - This function is already called implicitly by tokenize() and simplifyTokenList()
This commit is contained in:
PKEuS 2012-04-17 09:51:08 +02:00
parent 09d41b2f84
commit 88aa08e71b
3 changed files with 251 additions and 374 deletions

View File

@ -46,7 +46,7 @@ public:
return Preprocessor::replaceIfDefined(str); return Preprocessor::replaceIfDefined(str);
} }
static std::string expandMacros(std::string code, ErrorLogger *errorLogger = 0) { static std::string expandMacros(const std::string& code, ErrorLogger *errorLogger = 0) {
return Preprocessor::expandMacros(code, "file.cpp", "", errorLogger); return Preprocessor::expandMacros(code, "file.cpp", "", errorLogger);
} }

File diff suppressed because it is too large Load Diff

View File

@ -5554,189 +5554,148 @@ private:
ASSERT_EQUALS("switch(){ case}", labels_("switch(){case}")); ASSERT_EQUALS("switch(){ case}", labels_("switch(){case}"));
} }
// Check simplifyInitVar
void checkSimplifyInitVar(const char code[], bool simplify = false) {
// Tokenize..
Settings settings;
settings.inconclusive = true;
settings.addEnabled("style");
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
errout.str("");
tokenizer.tokenize(istr, "test.cpp");
if (simplify)
tokenizer.simplifyTokenList();
tokenizer.validate();
}
void simplifyInitVar() { void simplifyInitVar() {
{ {
const char code[] = "int i ; int p(0);"; const char code[] = "int i ; int p(0);";
ASSERT_EQUALS("int i ; int p ; p = 0 ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; int p ; p = 0 ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int i; int *p(0);"; const char code[] = "int i; int *p(0);";
ASSERT_EQUALS("int i ; int * p ; p = 0 ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; int * p ; p = 0 ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int p(0);"; const char code[] = "int p(0);";
ASSERT_EQUALS("int p ; p = 0 ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int p ; p = 0 ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int *p(0);"; const char code[] = "int *p(0);";
ASSERT_EQUALS("int * p ; p = 0 ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int * p ; p = 0 ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int i ; int p(i);"; const char code[] = "int i ; int p(i);";
ASSERT_EQUALS("int i ; int p ; p = i ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; int p ; p = i ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int i; int *p(&i);"; const char code[] = "int i; int *p(&i);";
ASSERT_EQUALS("int i ; int * p ; p = & i ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; int * p ; p = & i ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int i; void *p(&i);"; const char code[] = "int i; void *p(&i);";
ASSERT_EQUALS("int i ; void * p ; p = & i ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; void * p ; p = & i ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; struct S s; struct S *p(&s);"; const char code[] = "struct S { }; struct S s; struct S *p(&s);";
ASSERT_EQUALS("struct S { } ; struct S s ; struct S * p ; p = & s ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; struct S s ; struct S * p ; p = & s ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; S s; S *p(&s);"; const char code[] = "struct S { }; S s; S *p(&s);";
ASSERT_EQUALS("struct S { } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "union S { int i; float f; }; union S s; union S *p(&s);"; const char code[] = "union S { int i; float f; }; union S s; union S *p(&s);";
ASSERT_EQUALS("union S { int i ; float f ; } ; union S s ; union S * p ; p = & s ;", tokenizeAndStringify(code)); ASSERT_EQUALS("union S { int i ; float f ; } ; union S s ; union S * p ; p = & s ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "union S { int i; float f; }; S s; S *p(&s);"; const char code[] = "union S { int i; float f; }; S s; S *p(&s);";
ASSERT_EQUALS("union S { int i ; float f ; } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code)); ASSERT_EQUALS("union S { int i ; float f ; } ; S s ; S * p ; p = & s ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class C { }; class C c; class C *p(&c);"; const char code[] = "class C { }; class C c; class C *p(&c);";
ASSERT_EQUALS("class C { } ; class C c ; class C * p ; p = & c ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class C { } ; class C c ; class C * p ; p = & c ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class C { }; C c; C *p(&c);"; const char code[] = "class C { }; C c; C *p(&c);";
ASSERT_EQUALS("class C { } ; C c ; C * p ; p = & c ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class C { } ; C c ; C * p ; p = & c ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; struct S s; struct S s1(s);"; const char code[] = "struct S { }; struct S s; struct S s1(s);";
ASSERT_EQUALS("struct S { } ; struct S s ; struct S s1 ( s ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; struct S s ; struct S s1 ( s ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; S s; S s1(s);"; const char code[] = "struct S { }; S s; S s1(s);";
ASSERT_EQUALS("struct S { } ; S s ; S s1 ( s ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; S s ; S s1 ( s ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; struct S s; struct S s1(&s);"; const char code[] = "struct S { }; struct S s; struct S s1(&s);";
ASSERT_EQUALS("struct S { } ; struct S s ; struct S s1 ( & s ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; struct S s ; struct S s1 ( & s ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "struct S { }; S s; S s1(&s);"; const char code[] = "struct S { }; S s; S s1(&s);";
ASSERT_EQUALS("struct S { } ; S s ; S s1 ( & s ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("struct S { } ; S s ; S s1 ( & s ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class S { int function(); };"; const char code[] = "class S { int function(); };";
ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class S { int function(void); };"; const char code[] = "class S { int function(void); };";
ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class S { int function ( ) ; } ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class S { int function(int); };"; const char code[] = "class S { int function(int); };";
ASSERT_EQUALS("class S { int function ( int ) ; } ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class S { int function ( int ) ; } ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int function(void);"; const char code[] = "int function(void);";
ASSERT_EQUALS("int function ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function ( ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int function(int);"; const char code[] = "int function(int);";
ASSERT_EQUALS("int function ( int ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function ( int ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "extern int function(void);"; const char code[] = "extern int function(void);";
ASSERT_EQUALS("extern int function ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("extern int function ( ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int function1(void); int function2(void);"; const char code[] = "int function1(void); int function2(void);";
ASSERT_EQUALS("int function1 ( ) ; int function2 ( ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function1 ( ) ; int function2 ( ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
@ -5745,35 +5704,30 @@ private:
// We can't tell if this a function prototype or a variable without knowing // We can't tell if this a function prototype or a variable without knowing
// what A is. Since A is undefined, just leave it alone. // what A is. Since A is undefined, just leave it alone.
ASSERT_EQUALS("int function ( A ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int function ( A ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int i; int function(A);"; const char code[] = "int i; int function(A);";
ASSERT_EQUALS("int i ; int function ( A ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("int i ; int function ( A ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class A { } ; int foo(A);"; const char code[] = "class A { } ; int foo(A);";
ASSERT_EQUALS("class A { } ; int foo ( A ) ;", tokenizeAndStringify(code)); ASSERT_EQUALS("class A { } ; int foo ( A ) ;", tokenizeAndStringify(code));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "class A { } ; A a; int foo(a);"; const char code[] = "class A { } ; A a; int foo(a);";
ASSERT_EQUALS("class A { } ; A a ; int foo ; foo = a ;", tokenizeAndStringify(code, false)); ASSERT_EQUALS("class A { } ; A a ; int foo ; foo = a ;", tokenizeAndStringify(code, false));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
{ {
const char code[] = "int x(f());"; const char code[] = "int x(f());";
ASSERT_EQUALS("int x ; x = f ( ) ;", tokenizeAndStringify(code, false)); ASSERT_EQUALS("int x ; x = f ( ) ;", tokenizeAndStringify(code, false));
checkSimplifyInitVar(code);
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
} }