当前位置:网站首页>1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability

1285_ Expand macros defined by AUTOSAR functions and variables with scripts to improve readability

2022-06-30 07:03:00 grey_ csdn

All learning summary : https://github.com/GreyZhang/hack_autosar

AUTOSAR Code to read , Get used to non AUTOSAR People with a code style may feel uncomfortable . One of the big reasons may be that the compiler requires some abstract processing .

such as , The above is a typical representative of some definitions .

Actually , I think if the semantic analysis of the code reading tool or code writing tool we use is very strong or weak , None of this is a problem . If it is strong enough, it can automatically process all for us , In fact, our problem today has been solved . But semantic analysis is weak , Even grammar hints are not enough , You may get more information to help you understand the code .

I Believe , Many people don't even have to Source Insight You can also write code with Source Insight Look at the code . But unfortunately , Most of the time, such an analysis will put Source Insight Baffled . and , Sometimes in the face of such definition information, sometimes our brain has to constantly translate , After all, it's a little different from straightforward code expression .

When I do code debugging, I sometimes get confused because of this part , A simple process I have done is to simplify this part directly to ensure that its semantics remain unchanged . and , After the change, you can compare it through compilation hex Is there a change . natural , We hope that our modified presentation will not change the semantics of the code , That is to say hex unchanged .

Similar deployment is actually easy , We only need to do a regular expression matching process to achieve semantic expression .

#!/usr/bin/perl -w

use File::Find;

find(\&process_c_code_file, '.');

sub process_c_code_file

{

if(/\.c$|\.h$/)

{

if($_ ne "Compiler.h")

{

open(CODE, "<$_");

my @code = ;

close CODE;

my $code = join '',@code;

$code =~ s/CONSTP2FUNC\((\w+)\s*,\s*\w+\)/$1 (* const )/g;

$code =~ s/P2FUNC\((\w+)\s*,\s*\w+\)/$1 (*fctname)/g;

$code =~ s/FUNC\((\w+)\s*,\s*\w+\)/$1/g;

$code =~ s/CONSTP2VAR\((\w+)\s*,\s*\w+,\s*\w+\)/$1 * const/g;

$code =~ s/CONSTP2CONST\((\w+)\s*,\s*\w+,\s*\w+\)/const $1 * const/g;

$code =~ s/P2CONST\((\w+)\s*,\s*\w+,\s*\w+\)/const $1 */g;

$code =~ s/P2VAR\((\w+)\s*,\s*\w+,\s*\w+\)/$1 */g;

open(CODE, ">$_");

print CODE "$code";

close CODE;

}

}

}

It's for me to use perl Implementation of a simple small script , The specific script source file can be found in github Link . For the above definition , This script can do some processing .

however , You need to pay attention to different versions of AUTOSAR The abstract representation of this compiler may differ . therefore , Regular expression processing may require some attention , Make some corresponding modifications .

When you use it , Put this script in the code root directory ,cmd In the implementation of perl autosar2common.pl that will do .

The above is the effect of modification , There should be some improvement in readability .

Easy to write , It may not be considerate , If there is bug Welcome to release the fixed version !

原网站

版权声明
本文为[grey_ csdn]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/181/202206300651269541.html