2010-12-30 09:46:44 +01:00
|
|
|
/*
|
|
|
|
* Cppcheck - A tool for static C/C++ code analysis
|
2011-01-09 20:33:36 +01:00
|
|
|
* Copyright (C) 2007-2011 Daniel Marjamäki and Cppcheck team.
|
2010-12-30 09:46:44 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "testsuite.h"
|
|
|
|
#include "testutils.h"
|
|
|
|
#include "symboldatabase.h"
|
|
|
|
|
2011-01-28 08:33:02 +01:00
|
|
|
#define GET_SYMBOL_DB(code) \
|
|
|
|
errout.str(""); \
|
|
|
|
Settings settings; \
|
|
|
|
Tokenizer tokenizer(&settings, this); \
|
|
|
|
std::istringstream istr(code); \
|
|
|
|
tokenizer.tokenize(istr, "test.cpp"); \
|
|
|
|
const SymbolDatabase *db = tokenizer.getSymbolDatabase();
|
|
|
|
|
2010-12-30 09:46:44 +01:00
|
|
|
class TestSymbolDatabase: public TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestSymbolDatabase()
|
|
|
|
:TestFixture("TestSymbolDatabase")
|
|
|
|
,si(NULL, NULL, NULL)
|
|
|
|
,vartok(NULL)
|
2010-12-30 22:57:43 +01:00
|
|
|
,typetok(NULL)
|
2011-01-21 23:48:42 +01:00
|
|
|
,t(NULL)
|
|
|
|
,found(false)
|
2011-02-27 16:21:14 +01:00
|
|
|
,isArray(false)
|
2010-12-30 09:46:44 +01:00
|
|
|
{}
|
|
|
|
|
|
|
|
private:
|
2011-01-17 18:29:19 +01:00
|
|
|
const Scope si;
|
2010-12-30 09:46:44 +01:00
|
|
|
const Token* vartok;
|
2010-12-30 22:57:43 +01:00
|
|
|
const Token* typetok;
|
2011-01-02 00:36:22 +01:00
|
|
|
const Token* t;
|
|
|
|
bool found;
|
2011-02-27 16:21:14 +01:00
|
|
|
bool isArray;
|
2010-12-30 22:57:43 +01:00
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
vartok = NULL;
|
|
|
|
typetok = NULL;
|
2011-01-02 00:36:22 +01:00
|
|
|
t = NULL;
|
|
|
|
found = false;
|
2011-02-27 16:21:14 +01:00
|
|
|
isArray = false;
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
2010-12-30 09:46:44 +01:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
TEST_CASE(test_isVariableDeclarationCanHandleNull);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesSimpleDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesStdDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedStdDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesManyScopes);
|
2010-12-30 22:57:43 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesPointers);
|
2010-12-30 09:46:44 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationDoesNotIdentifyConstness);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesFirstOfManyVariables);
|
2010-12-30 22:57:43 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesScopedPointerDeclaration);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesDeclarationWithIndirection);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesDeclarationWithMultipleIndirection);
|
2011-01-01 01:19:32 +01:00
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesArray);
|
|
|
|
TEST_CASE(test_isVariableDeclarationIdentifiesOfArrayPointers);
|
2011-01-02 00:36:22 +01:00
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesTemplatedPointerVariable);
|
2011-01-18 08:29:42 +01:00
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesTemplatedPointerToPointerVariable);
|
2011-01-18 10:14:12 +01:00
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesTemplatedArrayVariable);
|
2011-01-02 00:36:22 +01:00
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesTemplatedVariable);
|
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesTemplatedVariableIterator);
|
|
|
|
TEST_CASE(isVariableDeclarationIdentifiesNestedTemplateVariable);
|
|
|
|
TEST_CASE(isVariableDeclarationDoesNotIdentifyTemplateClass);
|
|
|
|
TEST_CASE(canFindMatchingBracketsNeedsOpen);
|
|
|
|
TEST_CASE(canFindMatchingBracketsInnerPair);
|
|
|
|
TEST_CASE(canFindMatchingBracketsOuterPair);
|
|
|
|
TEST_CASE(canFindMatchingBracketsWithTooManyClosing);
|
|
|
|
TEST_CASE(canFindMatchingBracketsWithTooManyOpening);
|
2011-01-28 08:33:02 +01:00
|
|
|
|
|
|
|
TEST_CASE(hasRegularFunction);
|
|
|
|
TEST_CASE(hasInlineClassFunction);
|
|
|
|
TEST_CASE(hasMissingInlineClassFunction);
|
|
|
|
TEST_CASE(hasClassFunction);
|
|
|
|
|
|
|
|
TEST_CASE(hasRegularFunctionReturningFunctionPointer);
|
|
|
|
TEST_CASE(hasInlineClassFunctionReturningFunctionPointer);
|
|
|
|
TEST_CASE(hasMissingInlineClassFunctionReturningFunctionPointer);
|
|
|
|
TEST_CASE(hasClassFunctionReturningFunctionPointer);
|
2011-02-25 13:17:55 +01:00
|
|
|
|
|
|
|
TEST_CASE(hasGlobalVariables1);
|
|
|
|
TEST_CASE(hasGlobalVariables2);
|
|
|
|
TEST_CASE(hasGlobalVariables3);
|
2011-04-29 02:53:31 +02:00
|
|
|
|
|
|
|
TEST_CASE(functionArgs1);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationCanHandleNull()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(NULL, vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(false, result);
|
|
|
|
ASSERT(NULL == vartok);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT(NULL == typetok);
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesSimpleDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize simpleDeclaration("int x;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(simpleDeclaration.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize ScopedDeclaration("::int x;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(ScopedDeclaration.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesStdDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize StdDeclaration("std::string x;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(StdDeclaration.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("string", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedStdDeclaration()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize StdDeclaration("::std::string x;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(StdDeclaration.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("string", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesManyScopes()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize manyScopes("AA::BB::CC::DD::EE x;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(manyScopes.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("x", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("EE", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
2010-12-30 22:57:43 +01:00
|
|
|
void test_isVariableDeclarationIdentifiesPointers()
|
2010-12-30 09:46:44 +01:00
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize pointer("int* p;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(pointer.tokens(), vartok, typetok, isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationDoesNotIdentifyConstness()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize constness("const int* cp;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(constness.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(false, result);
|
|
|
|
ASSERT(NULL == vartok);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT(NULL == typetok);
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesFirstOfManyVariables()
|
|
|
|
{
|
2010-12-30 22:57:43 +01:00
|
|
|
reset();
|
2010-12-30 09:46:44 +01:00
|
|
|
givenACodeSampleToTokenize multipleDeclaration("int first, second;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(multipleDeclaration.tokens(), vartok, typetok, isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("first", vartok->str());
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesScopedPointerDeclaration()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize manyScopes("AA::BB::CC::DD::EE* p;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(manyScopes.tokens(), vartok, typetok, isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("EE", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesDeclarationWithIndirection()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize pointerToPointer("int** pp;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(pointerToPointer.tokens(), vartok, typetok, isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("pp", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesDeclarationWithMultipleIndirection()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize pointerToPointer("int***** p;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(pointerToPointer.tokens(), vartok, typetok, isArray);
|
2010-12-30 22:57:43 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("p", vartok->str());
|
|
|
|
ASSERT_EQUALS("int", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2010-12-30 09:46:44 +01:00
|
|
|
}
|
2011-01-01 01:19:32 +01:00
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesArray()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize array("::std::string v[3];");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(array.tokens(), vartok, typetok, isArray);
|
2011-01-01 01:19:32 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("v", vartok->str());
|
|
|
|
ASSERT_EQUALS("string", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(true == isArray);
|
2011-01-01 01:19:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_isVariableDeclarationIdentifiesOfArrayPointers()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize array("A *a[5];");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(array.tokens(), vartok, typetok, isArray);
|
2011-01-01 01:19:32 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("a", vartok->str());
|
|
|
|
ASSERT_EQUALS("A", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(true == isArray);
|
2011-01-01 01:19:32 +01:00
|
|
|
}
|
2011-01-02 00:36:22 +01:00
|
|
|
|
|
|
|
void isVariableDeclarationIdentifiesTemplatedPointerVariable()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::set<char>* chars;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("chars", vartok->str());
|
|
|
|
ASSERT_EQUALS("set", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
|
2011-01-18 08:29:42 +01:00
|
|
|
void isVariableDeclarationIdentifiesTemplatedPointerToPointerVariable()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<int>*** ints;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-18 10:14:12 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("ints", vartok->str());
|
|
|
|
ASSERT_EQUALS("deque", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-18 10:14:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isVariableDeclarationIdentifiesTemplatedArrayVariable()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<int> ints[3];");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-18 10:14:12 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("ints", vartok->str());
|
|
|
|
ASSERT_EQUALS("deque", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(true == isArray);
|
2011-01-18 08:29:42 +01:00
|
|
|
}
|
|
|
|
|
2011-01-02 00:36:22 +01:00
|
|
|
void isVariableDeclarationIdentifiesTemplatedVariable()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::vector<int> ints;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("ints", vartok->str());
|
|
|
|
ASSERT_EQUALS("vector", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isVariableDeclarationIdentifiesTemplatedVariableIterator()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::list<int>::const_iterator floats;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("floats", vartok->str());
|
|
|
|
ASSERT_EQUALS("const_iterator", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isVariableDeclarationIdentifiesNestedTemplateVariable()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
ASSERT_EQUALS(true, result);
|
|
|
|
ASSERT_EQUALS("intsets", vartok->str());
|
|
|
|
ASSERT_EQUALS("deque", typetok->str());
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void isVariableDeclarationDoesNotIdentifyTemplateClass()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("template <class T> class SomeClass{};");
|
2011-02-27 16:21:14 +01:00
|
|
|
bool result = si.isVariableDeclaration(var.tokens(), vartok, typetok, isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
ASSERT_EQUALS(false, result);
|
2011-02-27 16:21:14 +01:00
|
|
|
ASSERT(false == isArray);
|
2011-01-02 00:36:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsNeedsOpen()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
found = si.findClosingBracket(var.tokens(), t);
|
|
|
|
ASSERT(! found);
|
|
|
|
ASSERT(! t);
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsInnerPair()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
found = si.findClosingBracket(var.tokens()->tokAt(7), t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT_EQUALS(var.tokens()->strAt(9), t->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsOuterPair()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("std::deque<std::set<int> > intsets;");
|
|
|
|
|
|
|
|
found = si.findClosingBracket(var.tokens()->tokAt(3), t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT_EQUALS(var.tokens()->strAt(10), t->str());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsWithTooManyClosing()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("X< 1>2 > x1;\n");
|
|
|
|
|
|
|
|
found = si.findClosingBracket(var.tokens()->tokAt(1), t);
|
|
|
|
ASSERT(found);
|
|
|
|
ASSERT_EQUALS(">", t->str());
|
|
|
|
ASSERT_EQUALS(var.tokens()->strAt(3), t->str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void canFindMatchingBracketsWithTooManyOpening()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
givenACodeSampleToTokenize var("X < (2 < 1) > x1;\n");
|
|
|
|
|
|
|
|
found = si.findClosingBracket(var.tokens()->tokAt(1), t);
|
|
|
|
ASSERT(!found);
|
|
|
|
}
|
2011-01-28 08:33:02 +01:00
|
|
|
|
|
|
|
void hasRegularFunction()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("void func() { }\n")
|
|
|
|
|
|
|
|
// 2 scopes: Global and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 2 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(1));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(1));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(1));
|
|
|
|
ASSERT(function && function->hasBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasInlineClassFunction()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void func() { } };\n")
|
|
|
|
|
|
|
|
// 3 scopes: Global, Class, and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 3 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(4));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(4));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(4));
|
|
|
|
ASSERT(function && function->hasBody && function->isInline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasMissingInlineClassFunction()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void func(); };\n")
|
|
|
|
|
|
|
|
// 2 scopes: Global and Class (no Function scope because there is no function implementation)
|
|
|
|
ASSERT(db && db->scopeList.size() == 2 && !tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(4));
|
|
|
|
|
|
|
|
ASSERT(scope == NULL);
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(4));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(4));
|
|
|
|
ASSERT(function && !function->hasBody);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasClassFunction()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void func(); }; Fred::func() { }\n")
|
|
|
|
|
|
|
|
// 3 scopes: Global, Class, and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 3 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(12));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(12));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(12));
|
|
|
|
ASSERT(function && function->hasBody && !function->isInline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasRegularFunctionReturningFunctionPointer()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("void (*func(int f))(char) { }\n")
|
|
|
|
|
|
|
|
// 2 scopes: Global and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 2 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(3));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(3));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(3));
|
|
|
|
ASSERT(function && function->hasBody && function->retFuncPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasInlineClassFunctionReturningFunctionPointer()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void (*func(int f))(char) { } };\n")
|
|
|
|
|
|
|
|
// 3 scopes: Global, Class, and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 3 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(6));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(6));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(6));
|
|
|
|
ASSERT(function && function->hasBody && function->isInline && function->retFuncPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasMissingInlineClassFunctionReturningFunctionPointer()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); };\n")
|
|
|
|
|
|
|
|
// 2 scopes: Global and Class (no Function scope because there is no function implementation)
|
|
|
|
ASSERT(db && db->scopeList.size() == 2 && !tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(6));
|
|
|
|
|
|
|
|
ASSERT(scope == NULL);
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(6));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(6));
|
|
|
|
ASSERT(function && !function->hasBody && function->retFuncPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasClassFunctionReturningFunctionPointer()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("class Fred { void (*func(int f))(char); }; void (*Fred::func(int f))(char) { }\n")
|
|
|
|
|
|
|
|
// 3 scopes: Global, Class, and Function
|
|
|
|
ASSERT(db && db->scopeList.size() == 3 && tokenizer.getFunctionTokenByName("func"));
|
|
|
|
|
|
|
|
if (db)
|
|
|
|
{
|
|
|
|
const Scope *scope = db->findFunctionScopeByToken(tokenizer.tokens()->tokAt(23));
|
|
|
|
|
|
|
|
ASSERT(scope && scope->className == "func");
|
|
|
|
|
|
|
|
const Function *function = db->findFunctionByToken(tokenizer.tokens()->tokAt(23));
|
|
|
|
|
|
|
|
ASSERT(function && function->token->str() == "func");
|
|
|
|
ASSERT(function && function->token == tokenizer.tokens()->tokAt(23));
|
|
|
|
ASSERT(function && function->hasBody && !function->isInline && function->retFuncPtr);
|
|
|
|
}
|
|
|
|
}
|
2011-02-25 13:17:55 +01:00
|
|
|
|
|
|
|
void hasGlobalVariables1()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("int i;\n")
|
|
|
|
|
|
|
|
ASSERT(db && db->scopeList.size() == 1);
|
|
|
|
if (db && db->scopeList.size() == 1)
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator it = db->scopeList.begin();
|
|
|
|
ASSERT(it->varlist.size() == 1);
|
|
|
|
if (it->varlist.size() == 1)
|
2011-02-25 13:17:55 +01:00
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Variable>::const_iterator var = it->varlist.begin();
|
2011-02-25 13:17:55 +01:00
|
|
|
ASSERT(var->name() == "i");
|
|
|
|
ASSERT(var->typeStartToken()->str() == "int");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasGlobalVariables2()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("int array[2][2];\n")
|
|
|
|
|
|
|
|
ASSERT(db && db->scopeList.size() == 1);
|
|
|
|
if (db && db->scopeList.size() == 1)
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator it = db->scopeList.begin();
|
|
|
|
ASSERT(it->varlist.size() == 1);
|
|
|
|
if (it->varlist.size() == 1)
|
2011-02-25 13:17:55 +01:00
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Variable>::const_iterator var = it->varlist.begin();
|
2011-02-25 13:17:55 +01:00
|
|
|
ASSERT(var->name() == "array");
|
|
|
|
ASSERT(var->typeStartToken()->str() == "int");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void hasGlobalVariables3()
|
|
|
|
{
|
|
|
|
GET_SYMBOL_DB("int array[2][2] = { { 0, 0 }, { 0, 0 } };\n")
|
|
|
|
|
|
|
|
ASSERT(db && db->scopeList.size() == 1);
|
|
|
|
if (db && db->scopeList.size() == 1)
|
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Scope>::const_iterator it = db->scopeList.begin();
|
|
|
|
ASSERT(it->varlist.size() == 1);
|
|
|
|
if (it->varlist.size() == 1)
|
2011-02-25 13:17:55 +01:00
|
|
|
{
|
2011-03-11 01:43:29 +01:00
|
|
|
std::list<Variable>::const_iterator var = it->varlist.begin();
|
2011-02-25 13:17:55 +01:00
|
|
|
ASSERT(var->name() == "array");
|
|
|
|
ASSERT(var->typeStartToken()->str() == "int");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-29 02:53:31 +02:00
|
|
|
|
|
|
|
void check(const char code[])
|
|
|
|
{
|
|
|
|
// Clear the error log
|
|
|
|
errout.str("");
|
|
|
|
|
|
|
|
// Check..
|
|
|
|
Settings settings;
|
|
|
|
settings.debugwarnings = true;
|
|
|
|
|
|
|
|
// Tokenize..
|
|
|
|
Tokenizer tokenizer(&settings, this);
|
|
|
|
std::istringstream istr(code);
|
|
|
|
tokenizer.tokenize(istr, "test.cpp");
|
|
|
|
tokenizer.simplifyTokenList();
|
|
|
|
|
|
|
|
// force symbol database creation
|
|
|
|
tokenizer.getSymbolDatabase();
|
|
|
|
}
|
|
|
|
|
|
|
|
void functionArgs1()
|
|
|
|
{
|
|
|
|
check("void f(std::vector<std::string s>, const std::vector<int> & v) { }\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
|
|
|
|
check("void f(std::map<std::string, std::vector<int> > m) { }\n");
|
|
|
|
|
|
|
|
ASSERT_EQUALS("", errout.str());
|
|
|
|
}
|
|
|
|
|
2010-12-30 09:46:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_TEST(TestSymbolDatabase)
|
|
|
|
|