#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 "16-0-1 #include directives in a file shall only be preceded by other preprocessor directives or comments";} sub description { return "16-0-1 (Required) #include directives in a file shall only be preceded by other preprocessor directives or comments";} sub detailed_description { return <<"END_DESC"

Rationale
To aid code readability, all the #include directives in a particular code file should be grouped together near the head of the file. The only items which may precede a #include in a file are other preprocessor directives or comments.

Example
#include  // Compliant
int32_t i;
#include  // Non-compliant
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; } }