Fixed #2676 (setVarId: variables with global scope qualification :: don't get varids)

This commit is contained in:
Martin Exner 2011-04-18 06:56:39 +02:00 committed by Daniel Marjamäki
parent 4fe0eab735
commit 2b08744b97
4 changed files with 40 additions and 14 deletions

View File

@ -1058,7 +1058,7 @@ std::list<std::string> Preprocessor::getcfgs(const std::string &filedata, const
if (!line.empty() && line.compare(0, 3, "#if") != 0) if (!line.empty() && line.compare(0, 3, "#if") != 0)
includeguard = false; includeguard = false;
if (line[0] != '#') if (line.empty() || line[0] != '#')
continue; continue;
if (includeguard) if (includeguard)

View File

@ -57,19 +57,22 @@ void Token::str(const std::string &s)
{ {
_str = s; _str = s;
_isName = bool(_str[0] == '_' || std::isalpha(_str[0])); if (!_str.empty())
{
_isName = bool(_str[0] == '_' || std::isalpha(_str[0]));
if (std::isdigit(_str[0])) if (std::isdigit(_str[0]))
_isNumber = true; _isNumber = true;
else if (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1])) else if (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1]))
_isNumber = true; _isNumber = true;
else else
_isNumber = false; _isNumber = false;
if (_str == "true" || _str == "false") if (_str == "true" || _str == "false")
_isBoolean = true; _isBoolean = true;
else else
_isBoolean = false; _isBoolean = false;
}
_varId = 0; _varId = 0;
} }

View File

@ -3398,7 +3398,7 @@ void Tokenizer::setVarId()
_varId = 0; _varId = 0;
for (Token *tok = _tokens; tok; tok = tok->next()) for (Token *tok = _tokens; tok; tok = tok->next())
{ {
if (tok != _tokens && !Token::Match(tok, "[;{}(,] %type%")) if (tok != _tokens && !Token::Match(tok, "[;{}(,] %type%") && !Token::Match(tok, "[;{}(,] ::"))
continue; continue;
if (_errorLogger) if (_errorLogger)
@ -3416,7 +3416,7 @@ void Tokenizer::setVarId()
continue; continue;
} }
if (Token::Match(tok, "[,;{}(] %type%")) if (Token::Match(tok, "[,;{}(] %type%") || Token::Match(tok, "[;{}(,] ::"))
{ {
// not function declaration? // not function declaration?
// TODO: Better checking // TODO: Better checking
@ -3456,6 +3456,10 @@ void Tokenizer::setVarId()
while (Token::Match(tok, "const|static|extern|public:|private:|protected:|;|mutable")) while (Token::Match(tok, "const|static|extern|public:|private:|protected:|;|mutable"))
tok = tok->next(); tok = tok->next();
// skip global namespace prefix
if (Token::Match(tok, "::"))
tok = tok->next();
while (Token::Match(tok, "%var% ::")) while (Token::Match(tok, "%var% ::"))
tok = tok->tokAt(2); tok = tok->tokAt(2);

View File

@ -88,6 +88,9 @@ private:
// return c_str().. // return c_str()..
TEST_CASE(returncstr1); TEST_CASE(returncstr1);
TEST_CASE(returncstr2); TEST_CASE(returncstr2);
// global namespace
TEST_CASE(testglobalnamespace);
} }
@ -429,6 +432,22 @@ private:
ASSERT_EQUALS("[test.cpp:11]: (error) Returning pointer to temporary\n", errout.str()); ASSERT_EQUALS("[test.cpp:11]: (error) Returning pointer to temporary\n", errout.str());
} }
void testglobalnamespace()
{
check("class SharedPtrHolder\n"
"{\n"
" ::std::tr1::shared_ptr<int> pNum;\n"
"public:\n"
" void SetNum(const ::std::tr1::shared_ptr<int> & apNum)\n"
" {\n"
" pNum = apNum;\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
}; };
REGISTER_TEST(TestAutoVariables) REGISTER_TEST(TestAutoVariables)