2008-11-20 20:23:05 +01:00
|
|
|
/*
|
2008-11-03 08:53:30 +01:00
|
|
|
* c++check - c/c++ syntax checking
|
2008-11-06 20:40:49 +01:00
|
|
|
* Copyright (C) 2007 Daniel Marjamäki
|
2008-11-03 08:53:30 +01:00
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// The preprocessor that c++check uses is a bit special. Instead of generating
|
|
|
|
// the code for a known configuration, it generates the code for each configuration.
|
|
|
|
|
|
|
|
|
2008-11-22 23:54:56 +01:00
|
|
|
#include <cstring>
|
2008-11-03 08:53:30 +01:00
|
|
|
#include "testsuite.h"
|
2008-11-20 20:18:55 +01:00
|
|
|
#define UNIT_TESTING // Get access to "private" data in Tokenizer
|
2008-11-22 23:54:56 +01:00
|
|
|
#include "tokenize.h"
|
2008-11-22 21:00:36 +01:00
|
|
|
|
2008-11-20 23:19:26 +01:00
|
|
|
extern std::ostringstream errout;
|
2008-11-03 08:53:30 +01:00
|
|
|
class TestTokenizer : public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestTokenizer() : TestFixture("TestTokenizer")
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE( multiline );
|
2008-11-04 20:09:31 +01:00
|
|
|
TEST_CASE( longtok );
|
2008-11-05 08:28:40 +01:00
|
|
|
|
|
|
|
TEST_CASE( inlineasm );
|
2008-11-20 20:18:55 +01:00
|
|
|
|
2008-11-22 21:00:36 +01:00
|
|
|
TEST_CASE( dupfuncname );
|
2008-11-03 08:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool cmptok(const char *expected[], const TOKEN *actual)
|
|
|
|
{
|
|
|
|
unsigned int i = 0;
|
|
|
|
for (; expected[i] && actual; ++i, actual = actual->next)
|
|
|
|
{
|
|
|
|
if ( strcmp( expected[i], actual->str ) != 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (expected[i] == NULL && actual == NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void multiline()
|
|
|
|
{
|
|
|
|
const char filedata[] = "#define str \"abc\" \\\n"
|
|
|
|
" \"def\"\n";
|
|
|
|
|
|
|
|
// tokenize..
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer tokenizer;
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer._files.push_back( "test.cpp" );
|
2008-11-20 20:18:55 +01:00
|
|
|
std::istringstream istr(filedata);
|
2008-11-09 08:19:53 +01:00
|
|
|
tokenizer.TokenizeCode(istr, 0);
|
2008-11-03 08:53:30 +01:00
|
|
|
|
|
|
|
// Expected result..
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *expected[] =
|
2008-11-03 08:53:30 +01:00
|
|
|
{
|
|
|
|
"def",
|
|
|
|
"str",
|
|
|
|
";",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compare..
|
2008-11-16 16:58:52 +01:00
|
|
|
ASSERT_EQUALS( true, cmptok(expected, tokenizer.tokens()) );
|
2008-11-03 08:53:30 +01:00
|
|
|
}
|
|
|
|
|
2008-11-04 20:09:31 +01:00
|
|
|
|
|
|
|
void longtok()
|
|
|
|
{
|
|
|
|
std::string filedata(10000,'a');
|
|
|
|
|
|
|
|
// tokenize..
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer tokenizer;
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer._files.push_back( "test.cpp" );
|
2008-11-20 20:18:55 +01:00
|
|
|
std::istringstream istr(filedata);
|
2008-11-09 08:19:53 +01:00
|
|
|
tokenizer.TokenizeCode(istr, 0);
|
2008-11-04 20:09:31 +01:00
|
|
|
|
|
|
|
// Expected result..
|
2008-11-16 16:58:52 +01:00
|
|
|
ASSERT_EQUALS( std::string(10000,'a'), std::string(tokenizer.tokens()->str) );
|
2008-11-04 20:09:31 +01:00
|
|
|
}
|
|
|
|
|
2008-11-05 08:28:40 +01:00
|
|
|
|
|
|
|
void inlineasm()
|
|
|
|
{
|
|
|
|
const char filedata[] = "void foo()\n"
|
|
|
|
"{\n"
|
|
|
|
" __asm\n"
|
|
|
|
" {\n"
|
|
|
|
" jmp $jump1\n"
|
|
|
|
" $jump1:\n"
|
|
|
|
" }\n"
|
|
|
|
"}\n";
|
2008-11-09 08:19:53 +01:00
|
|
|
|
2008-11-05 08:28:40 +01:00
|
|
|
// tokenize..
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer tokenizer;
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer._files.push_back( "test.cpp" );
|
2008-11-20 20:18:55 +01:00
|
|
|
std::istringstream istr(filedata);
|
2008-11-09 08:19:53 +01:00
|
|
|
tokenizer.TokenizeCode(istr, 0);
|
2008-11-05 08:28:40 +01:00
|
|
|
|
|
|
|
// Expected result..
|
2008-11-09 08:19:53 +01:00
|
|
|
const char *expected[] =
|
2008-11-05 08:28:40 +01:00
|
|
|
{
|
|
|
|
"void",
|
|
|
|
"foo",
|
|
|
|
"(",
|
|
|
|
")",
|
|
|
|
"{",
|
|
|
|
"}",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compare..
|
2008-11-16 16:58:52 +01:00
|
|
|
ASSERT_EQUALS( true, cmptok(expected, tokenizer.tokens()) );
|
2008-11-05 08:28:40 +01:00
|
|
|
}
|
2008-11-20 20:18:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
void dupfuncname()
|
|
|
|
{
|
|
|
|
const char code[] = "void a()\n"
|
|
|
|
"{ }\n"
|
|
|
|
"void a(int i)\n"
|
|
|
|
"{ }\n"
|
|
|
|
"void b()\n"
|
|
|
|
"{ }\n";
|
|
|
|
// tokenize..
|
2008-11-22 20:39:12 +01:00
|
|
|
Tokenizer tokenizer;
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer._files.push_back( "test.cpp" );
|
2008-11-20 20:18:55 +01:00
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.TokenizeCode(istr, 0);
|
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
tokenizer.fillFunctionList();
|
2008-11-20 20:18:55 +01:00
|
|
|
|
2008-11-23 12:08:07 +01:00
|
|
|
ASSERT_EQUALS( 1, tokenizer._functionList.size() );
|
|
|
|
ASSERT_EQUALS( std::string("b"), tokenizer._functionList[0]->str );
|
2008-11-22 21:00:36 +01:00
|
|
|
}
|
2008-11-03 08:53:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST( TestTokenizer )
|