#This script is designed to run with Understand - CodeCheck use base ("Understand::Codecheck"); use strict; use constant ERR1 => '#include statement preceded by code other than preprocesser directives or comments'; sub register_tr_text() { my $check = shift; $check->add_tr_text(ERR1); } sub name { return "19.1 #include statements in a file should only be preceded by other preprocessor directives or comments";} sub description { return "19.1 (Advisory) #include statements in a file should only be preceded by other preprocessor directives or comments";} sub detailed_description { return <<"END_DESC"

All the #include statements in a particular code file should be grouped together near the head of the file. The rule states that the only items which may precede a #include in a file are other preprocessor directives or comments.

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 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 $flag = 0; my $lexeme = $lexer->first(); while ($lexeme){ if(!$flag && $lexeme->token eq "Preprocessor"){ while($lexeme && $lexeme->token ne "Newline"){ $lexeme=$lexeme->next; } } if($lexeme->token !~ /Newline|Comment|Whitespace/ && !$lexeme->inactive()){ $flag=1; } if($flag && $lexeme->token eq "Preprocessor" && $lexeme->next->text =~ /include/i){ $check->violation($file,$file,$lexeme->line_begin,$lexeme->column_begin,ERR1); while($lexeme && $lexeme->token ne "Newline"){ $lexeme = $lexeme->next; } } $lexeme = $lexeme->next; } }