#This script is designed to run with Understand - CodeCheck use base qw(Understand::Codecheck); use strict; use constant ERR1 => 'Violation: invalid escape sequence \'\\%1\' in: %2'; sub register_tr_text { my $check = shift; $check->add_tr_text(ERR1); } sub name { '2-13-1 escape sequences are standardized' } sub description { '2-13-1 (Required) Only escape sequences defined in the ISO C standard shall be used.' } sub detailed_description { <<'END_DESC'
Rationale
The use of an undefined escape sequence leads to undefined behaviour.
The defined escape sequences (ISO/IEC 14882:2003 [1] 2.13.2) are:
\n, \t, \v, \b, \r, \f, \a, \\, \?, \', \", \<Octal Number>, \x<Hexadecimal Number>END_DESC } sub test_language { my $language = shift; return $language eq 'C++'; } sub test_entity { 1 } sub test_global { 0 } sub define_options { } sub check { my $check = shift; my $file = shift; return unless $file->kind->check('file'); my $lexer = $file->lexer(0); my @lexemes = $lexer->lexemes if $lexer; foreach my $lexeme (@lexemes){ next unless $lexeme->token eq 'String'; my $content = $lexeme->text; next unless $content =~ m! ^ ['"] !sx; while ($content =~ m! \G [^\\]* \\ (x[0-9a-fA-F]{1,2} | [0-7]{2,3} | .) !sxg) { my $escape = $1; # \n, \t, \v, \b, \r, \f, \a, \\, \?, \', \" next if $escape =~ m! ^ (?: n | t | v | b | r | f | a | \\ | \? | ' | " | 0 | [0-7]{2,3} | x[0-9a-fA-F]{1,2} ) $ !sx; $check->violation(0, $file, $lexeme->line_begin, $lexeme->column_begin, ERR1, $escape, $content); } } return; }