#This script is designed to run with Understand - CodeCheck use base ("Understand::Codecheck"); use strict; use constant ERR1 => '#include %1 contains one or more backslash characters'; sub register_tr_text() { my $check = shift; $check->add_tr_text(ERR1); } sub name { return "16-2-5 The backslash character should not occur in a header file name";} sub description { return "16-2-5 (Advisory) The '\\' character should not occur in a header file name";} sub detailed_description { return <<"END_DESC"
Rationale
It is undefined behaviour if the \\ character is used between < and > delimiters or between the "
delimiters in a header name preprocessing token.
Note that this rule is only advisory, since some environments use \\ as a file name delimiter.
Compilers for these environments often support the use of / in #include directives.
#include "fi\\le.h" // Non-compliantEND_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 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 $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($string =~ /^<.*\\+.*>$|^".*\\+.*"$/){ $check->violation($file,$file,$lexeme->line_begin,$lexeme->column_begin,ERR1,$string); } } }continue{ $lexeme = $lexeme->next; } }