#This script is designed to run with Understand - CodeCheck use base ("Understand::Codecheck"); use strict; use constant ERR1 => '#include %1 contains one or more non-standard characters'; use constant OPT1 => "Don't test for \\ character between < and > delimiters and between \" delimiters"; sub register_tr_text() { my $check = shift; $check->add_tr_text(ERR1); } sub name { return "19.2 Non-standard characters should not occur in header file names in #include directives";} sub description { return "19.2 (Advisory) Non-standard characters should not occur in header file names in #include directives";} sub detailed_description { return <<"END_DESC" If the ', \\, ", or /* characters are used between < and > delimiters or the ', \\, or /* characters are used between the " delimiters in a header name preprocessing token, then the behaviour is undefined. Use of the \\ character is permitted in filename paths without the need for a deviation if required by the host operating system of the development environment. END_DESC } sub test_language { my $language = shift; return $language =~ /C\+\+/; #Handles C and C++ } sub test_entity { return 1;} sub test_global { return 0;} sub define_options{ my $check = shift; $check->option->checkbox("DontTest",OPT1,0); } sub check { my $check = shift; my $file = shift; return unless $file->kind->check("file ~unknown ~unresolved"); return unless $file->filerefs("include","file"); my $lexer = $file->lexer(); return unless $lexer; my $dontTest = $check->option->lookup("DontTest"); my $lexeme = $lexer->first(); while ($lexeme){ if($lexeme->token eq "Preprocessor" && $lexeme->text eq "include"){ my $string = ""; do { $lexeme = $lexeme->next }while($lexeme && $lexeme->token eq "Whitespace"); $string = $lexeme->ent->type if($lexeme->ent && $lexeme->ent->kind->check("macro")); $string = $lexeme->text if !$string && $lexeme->token eq "String"; next if !$string; if($dontTest && $string =~ /^<.*('|"|\/\*).*>$|^".*('|\/\*).*"$/){ $check->violation($file,$file,$lexeme->line_begin,$lexeme->column_begin,ERR1,$string); } if(!$dontTest && $string =~ /^<.*('|\\|"|\/\*).*>$|^".*('|\\|\/\*).*"$/){ $check->violation($file,$file,$lexeme->line_begin,$lexeme->column_begin,ERR1,$string); } } }continue{ $lexeme = $lexeme->next; } }