Misra: Add rule 16.3

This commit is contained in:
Daniel Marjamäki 2017-04-14 10:46:35 +02:00
parent aa2d1fd683
commit 174bcc8d34
3 changed files with 58 additions and 22 deletions

View File

@ -122,3 +122,17 @@ void misra_16_2() {
break;
}
}
void misra_16_3() {
switch (x) {
case 1:
case 2:
a=1;
case 3: // 16.3
a=2;
// fallthrough
case 5:
break;
default: break;
}
}

View File

@ -419,6 +419,26 @@ def misra_16_2(data):
if token.str == 'case' and token.scope.type != 'Switch':
reportError(token, 16, 2)
# The goal is handle comments/annotations by other tools
def misra_16_3(rawTokens):
# state: 0=no, 1=break is seen but not its ';', 2=after 'break;', 'comment', '{'
state = 0
for token in rawTokens:
if token.str == 'break':
state = 1
elif token.str == ';':
if state == 1:
state = 2
else:
state = 0
elif token.str.startswith('/*') or token.str.startswith('//'):
if token.str.lower().find('fallthrough')>0:
state = 2
elif token.str == '{':
state = 2
elif token.str == 'case' and state != 2:
reportError(token, 16, 3)
for arg in sys.argv[1:]:
print('Checking ' + arg + '...')
data = cppcheckdata.parsedump(arg)
@ -455,4 +475,6 @@ for arg in sys.argv[1:]:
misra_15_6(data.rawTokens)
misra_15_7(cfg)
misra_16_2(cfg)
if cfgNumber == 1:
misra_16_3(data.rawTokens)

View File

@ -135,6 +135,28 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
simplecpp::TokenList tokens1(fileStream, files, filename, &outputList);
preprocessor.loadFiles(tokens1, files);
// write dump file xml prolog
std::ofstream fdump;
if (_settings.dump) {
const std::string dumpfile(filename + ".dump");
fdump.open(dumpfile.c_str());
if (fdump.is_open()) {
fdump << "<?xml version=\"1.0\"?>" << std::endl;
fdump << "<dumps>" << std::endl;
fdump << " <rawtokens>" << std::endl;
for (unsigned int i = 0; i < files.size(); ++i)
fdump << " <file index=\"" << i << "\" name=\"" << ErrorLogger::toxml(files[i]) << "\"/>" << std::endl;
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
fdump << " <tok "
<< "fileIndex=\"" << tok->location.fileIndex << "\" "
<< "linenr=\"" << tok->location.line << "\" "
<< "str=\"" << ErrorLogger::toxml(tok->str) << "\""
<< "/>" << std::endl;
}
fdump << " </rawtokens>" << std::endl;
}
}
// Parse comments and then remove them
preprocessor.inlineSuppressions(tokens1);
tokens1.removeComments();
@ -209,28 +231,6 @@ unsigned int CppCheck::processFile(const std::string& filename, const std::strin
}
}
// write dump file xml prolog
std::ofstream fdump;
if (_settings.dump) {
const std::string dumpfile(filename + ".dump");
fdump.open(dumpfile.c_str());
if (fdump.is_open()) {
fdump << "<?xml version=\"1.0\"?>" << std::endl;
fdump << "<dumps>" << std::endl;
fdump << " <rawtokens>" << std::endl;
for (unsigned int i = 0; i < files.size(); ++i)
fdump << " <file index=\"" << i << "\" name=\"" << ErrorLogger::toxml(files[i]) << "\"/>" << std::endl;
for (const simplecpp::Token *tok = tokens1.cfront(); tok; tok = tok->next) {
fdump << " <tok "
<< "fileIndex=\"" << tok->location.fileIndex << "\" "
<< "linenr=\"" << tok->location.line << "\" "
<< "str=\"" << ErrorLogger::toxml(tok->str) << "\""
<< "/>" << std::endl;
}
fdump << " </rawtokens>" << std::endl;
}
}
std::set<unsigned long long> checksums;
unsigned int checkCount = 0;
for (std::set<std::string>::const_iterator it = configurations.begin(); it != configurations.end(); ++it) {