Added support for noreturn functions from library to CheckOther::checkUnreachableCode()

This commit is contained in:
PKEuS 2013-08-06 02:11:59 -07:00
parent bf6c6d0263
commit 5e3ccda408
3 changed files with 31 additions and 13 deletions

View File

@ -1567,6 +1567,8 @@ void CheckOther::checkUnreachableCode()
} else if (Token::Match(tok, "goto %any% ;")) { } else if (Token::Match(tok, "goto %any% ;")) {
secondBreak = tok->tokAt(3); secondBreak = tok->tokAt(3);
labelName = tok->next(); labelName = tok->next();
} else if (Token::Match(tok, "%var% (") && _settings->library.isnoreturn(tok->str())) {
secondBreak = tok->linkAt(1)->tokAt(2);
} }
// Statements follow directly, no line between them. (#3383) // Statements follow directly, no line between them. (#3383)

View File

@ -57,6 +57,11 @@ public:
_dealloc[functionname] = id; _dealloc[functionname] = id;
} }
/** add noreturn function setting */
void setnoreturn(const std::string& funcname, bool noreturn) {
_noreturn[funcname] = noreturn;
}
/** is allocation type memory? */ /** is allocation type memory? */
static bool ismemory(int id) { static bool ismemory(int id) {
return ((id > 0) && ((id & 1) == 0)); return ((id > 0) && ((id & 1) == 0));

View File

@ -178,27 +178,30 @@ private:
TEST_CASE(checkCommaSeparatedReturn); TEST_CASE(checkCommaSeparatedReturn);
} }
void check(const char code[], const char *filename = NULL, bool experimental = false, bool inconclusive = true, bool posix = false, bool runSimpleChecks=true) { void check(const char code[], const char *filename = NULL, bool experimental = false, bool inconclusive = true, bool posix = false, bool runSimpleChecks=true, Settings* settings = 0) {
// Clear the error buffer.. // Clear the error buffer..
errout.str(""); errout.str("");
Settings settings; if (!settings) {
settings.addEnabled("style"); static Settings _settings;
settings.addEnabled("warning"); settings = &_settings;
settings.addEnabled("portability"); }
settings.addEnabled("performance"); settings->addEnabled("style");
settings.inconclusive = inconclusive; settings->addEnabled("warning");
settings.experimental = experimental; settings->addEnabled("portability");
settings.standards.posix = posix; settings->addEnabled("performance");
settings->inconclusive = inconclusive;
settings->experimental = experimental;
settings->standards.posix = posix;
// Tokenize.. // Tokenize..
Tokenizer tokenizer(&settings, this); Tokenizer tokenizer(settings, this);
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, filename ? filename : "test.cpp"); tokenizer.tokenize(istr, filename ? filename : "test.cpp");
// Check.. // Check..
CheckOther checkOther(&tokenizer, &settings, this); CheckOther checkOther(&tokenizer, settings, this);
checkOther.runChecks(&tokenizer, &settings, this); checkOther.runChecks(&tokenizer, settings, this);
if (runSimpleChecks) { if (runSimpleChecks) {
const std::string str1(tokenizer.tokens()->stringifyList(0,true)); const std::string str1(tokenizer.tokens()->stringifyList(0,true));
@ -206,7 +209,7 @@ private:
const std::string str2(tokenizer.tokens()->stringifyList(0,true)); const std::string str2(tokenizer.tokens()->stringifyList(0,true));
if (str1 != str2) if (str1 != str2)
warn(("Unsimplified code in test case\nstr1="+str1+"\nstr2="+str2).c_str()); warn(("Unsimplified code in test case\nstr1="+str1+"\nstr2="+str2).c_str());
checkOther.runSimplifiedChecks(&tokenizer, &settings, this); checkOther.runSimplifiedChecks(&tokenizer, settings, this);
} }
} }
@ -2658,6 +2661,14 @@ private:
"}", 0, false, false, false, false); "}", 0, false, false, false, false);
ASSERT_EQUALS("[test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary.\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary.\n", errout.str());
Settings settings;
settings.library.setnoreturn("exit", true);
check("void foo() {\n"
" exit(0);\n"
" break;\n"
"}", 0, false, false, false, false, &settings);
ASSERT_EQUALS("[test.cpp:3]: (style) Consecutive return, break, continue, goto or throw statements are unnecessary.\n", errout.str());
check("void foo(int a)\n" check("void foo(int a)\n"
"{\n" "{\n"
" switch(a) {\n" " switch(a) {\n"