メインコンテンツ

MISRA C:2012 Rule 17.3

A function shall not be declared implicitly

説明

ルール定義

A function shall not be declared implicitly 1 .

根拠

暗黙的な宣言は、関数を宣言または定義する前に呼び出す場合に発生します。関数を呼び出す前に明示的に宣言すると、コンパイラにより引数と戻り値の型を宣言内のパラメーター タイプと一致させることができます。暗黙的な宣言が発生する場合、コンパイラにより引数と戻り値の型が仮定されます。たとえば、戻り値の型として int を仮定します。仮定は想定していた型と一致せず予期しない型変換を起こす可能性があります。

レポート内の追加のメッセージ

Function 'XX' has no complete visible prototype at call.

トラブルシューティング

ルール違反を想定していてもその違反が表示されない場合、コーディング規約違反が想定どおりに表示されない理由の診断を参照します。

すべて展開する

#include <math.h>

extern double power3 (double val, int exponent);
int getChoice(void);

double func() {
    double res;
    int ch = getChoice();
    if(ch == 0) {
        res = power(2.0, 10);    /* Non-compliant */
    }
    else if( ch==1) {
        res = power2(2.0, 10);   /* Non-compliant */
    }
    else {
        res = power3(2.0, 10);   /* Compliant */
        return res;
    }
}

double power2 (double val, int exponent) {
    return (pow(val, exponent));
}

この例では、宣言されていない関数がコード内で呼び出される場合、ルールに違反します。関数定義がコード内の後の方に存在する場合でも、ルール違反は発生します。

関数がコード内で呼び出される前に宣言される場合、ルールに違反しません。関数定義が別のファイルにありリンク段階でのみ使用できる場合、以下のいずれかの方法で関数を宣言できます。

  • 現在のファイル内で関数を extern キーワードで宣言する。

  • 関数をヘッダー ファイル内で宣言し、ヘッダー ファイルを現在のファイルにインクルードする。

チェック情報

グループ: 関数
カテゴリ: Mandatory
AGC カテゴリ: Mandatory

バージョン履歴

R2014b で導入


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace® Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.