メインコンテンツ

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

コンパイル前のキーワードの削除または置き換え

R2025b

Polyspace® コンパイラは ANSI® C99 規格 (ISO/IEC 9899:1999) に厳密に準拠しています。この規格からの逸脱がコンパイラで許容されている場合、Polyspace の既定のオプションを使用したコンパイルではコンパイラをエミュレートできません。たとえば、Polyspace の既定では認識されない特定の非 ANSI キーワードをコンパイラに許容させることができます。

コンパイラを厳密にエミュレートするには、ターゲットおよびコンパイラのオプションを指定します。それでも認識されないキーワードによってコンパイル エラーが発生する場合は、検証の目的のみでそれらのキーワードを削除したり、置き換えたりすることができます。オプション [プリプロセッサ定義] (-D) を使用すると、単純な置き換えができます。関数の属性といった、スペースで区切られたキーワードのグループを削除するなどの複雑な置き換えには、オプション [前処理済みファイルに適用するコマンド/スクリプト] (-post-preprocessing-command) を使用します。

認識されないキーワードの削除

解析の目的で、サポートされていないキーワードをコードから削除できます。たとえば、far および 0x キーワード (0x は絶対アドレスより優先されます) を削除するには、次の手順に従います。

  1. 次のテンプレートを C:\Polyspace\myTpl.pl という名前を付けて保存します。

    #!/usr/bin/perl
    
    ##############################################################
    # Post Processing template script
    #
    ##############################################################
    # Usage from GUI:
    #
    # 1) Linux: /usr/bin/perl PostProcessingTemplate.pl
    # 2) Windows: polyspaceroot\sys\perl\win32\bin\perl.exe <pathtoscript>\
    # PostProcessingTemplate.pl
    #
    ##############################################################
    
    $version = 0.1;
    
    $INFILE = STDIN;
    $OUTFILE = STDOUT;
    
    while (<$INFILE>)
    {
    
        # Remove far keyword
        s/far//;
    
        # Remove "@ 0xFE1" address constructs
        s/\@\s0x[A-F0-9]*//g;
    
        # Remove "@0xFE1" address constructs
        s/\@0x[A-F0-9]*//g;
    
        # Remove "@ ((unsigned)&LATD*8)+2" type constructs
        s/\@\s\(\(unsigned\)\&[A-Z0-9]+\*8\)\+\d//g;
    
        # DON'T DELETE LINE BELOW: Print the current processed line
        print $OUTFILE $_;
    }

    Perl regex の概要を参照してください。

    #########################################################
    # Metacharacter What it matches
    #########################################################
    # Single Characters
    # . Any character except newline
    # [a-z0-9] Any single character in the set
    # [^a-z0-9] Any character not in set
    # \d A digit same as
    # \D A non digit same as [^0-9]
    # \w An Alphanumeric (word) character
    # \W Non Alphanumeric (non-word) character
    #
    # Whitespace Characters
    # \s Whitespace character
    # \S Non-whitespace character
    # \n newline
    # \r return
    # \t tab
    # \f formfeed
    # \b backspace
    #
    # Anchored Characters
    # \B word boundary when no inside []
    # \B non-word boundary
    # ^ Matches to beginning of line
    # $ Matches to end of line
    #
    # Repeated Characters
    # x? 0 or 1 occurrence of x
    # x* 0 or more x's
    # x+ 1 or more x's
    # x{m,n} Matches at least m x's and no more than n x's
    # abc All of abc respectively
    # to|be|great One of "to", "be" or "great"
    #
    # Remembered Characters
    # (string) Used for back referencing see below
    # \1 or $1 First set of parentheses
    # \2 or $2 First second of parentheses
    # \3 or $3 First third of parentheses
    ##########################################################
    # Back referencing
    #
    # e.g. swap first two words around on a line
    # red cat -> cat red
    # s/(\w+) (\w+)/$2 $1/;
    #
    ##########################################################
  2. [構成] ペインで、 [環境設定] を選択します。

  3. [前処理済みファイルに適用するコマンド/スクリプト] の右側にある をクリックします。

  4. [ファイルを開く] ダイアログ ボックスを使用して、C:\Polyspace に移動します。

  5. [ファイル名] フィールドで、myTpl.pl と入力します。

  6. [開く] をクリックします。[前処理済みファイルに適用するコマンド/スクリプト] フィールドに C:\Polyspace\myTpl.pl が表示されます。

認識されない関数の属性の削除

解析の目的で、サポートされていない関数の属性をコードから削除できます。

汎用コンパイラを指定して以下のコードに対して検証を実行する場合、noreturn 属性によるコンパイル エラーを確認できます。コードは GNU® コンパイラを使用してコンパイルされます。

void fatal () __attribute__ ((noreturn));
          
void fatal (/* ... */)
{
    /* ... */ /* Print error message. */ /* ... */
    exit (1);
}

属性が認識されず、その属性がコード解析に影響しない場合、検証の目的でその属性をコードから削除できます。たとえば、次の Perl スクリプトを使用して、noreturn 属性を削除できます。

while ($line = <STDIN>) 
{

# __attribute__ ((noreturn))

  # Remove far keyword
  $line =~ s/__attribute__\ \(\(noreturn\)\)//g;
  
  # Print the current processed line to STDOUT
  print $line;
}

オプション [前処理済みファイルに適用するコマンド/スクリプト] (-post-preprocessing-command) を使用してスクリプトを指定します。

参考

Polyspace 解析オプション

トピック