diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index caa0a6e7f..3707b7e5e 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -1058,7 +1058,7 @@ std::list Preprocessor::getcfgs(const std::string &filedata, const if (!line.empty() && line.compare(0, 3, "#if") != 0) includeguard = false; - if (line[0] != '#') + if (line.empty() || line[0] != '#') continue; if (includeguard) diff --git a/lib/token.cpp b/lib/token.cpp index 5c4804ed6..6e89de54c 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -57,19 +57,22 @@ void Token::str(const std::string &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])) - _isNumber = true; - else if (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1])) - _isNumber = true; - else - _isNumber = false; + if (std::isdigit(_str[0])) + _isNumber = true; + else if (_str.length() > 1 && _str[0] == '-' && std::isdigit(_str[1])) + _isNumber = true; + else + _isNumber = false; - if (_str == "true" || _str == "false") - _isBoolean = true; - else - _isBoolean = false; + if (_str == "true" || _str == "false") + _isBoolean = true; + else + _isBoolean = false; + } _varId = 0; } diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index b47604643..828c08cfb 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3398,7 +3398,7 @@ void Tokenizer::setVarId() _varId = 0; 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; if (_errorLogger) @@ -3416,7 +3416,7 @@ void Tokenizer::setVarId() continue; } - if (Token::Match(tok, "[,;{}(] %type%")) + if (Token::Match(tok, "[,;{}(] %type%") || Token::Match(tok, "[;{}(,] ::")) { // not function declaration? // TODO: Better checking @@ -3456,6 +3456,10 @@ void Tokenizer::setVarId() while (Token::Match(tok, "const|static|extern|public:|private:|protected:|;|mutable")) tok = tok->next(); + // skip global namespace prefix + if (Token::Match(tok, "::")) + tok = tok->next(); + while (Token::Match(tok, "%var% ::")) tok = tok->tokAt(2); diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 91c8406cc..c7c8bd15f 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -88,6 +88,9 @@ private: // return c_str().. TEST_CASE(returncstr1); 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()); } + + void testglobalnamespace() + { + check("class SharedPtrHolder\n" + "{\n" + " ::std::tr1::shared_ptr pNum;\n" + "public:\n" + " void SetNum(const ::std::tr1::shared_ptr & apNum)\n" + " {\n" + " pNum = apNum;\n" + " }\n" + "}"); + + ASSERT_EQUALS("", errout.str()); + } + }; REGISTER_TEST(TestAutoVariables)