1. 程式人生 > >編譯器檢查函數聲明和函數實際調用參數之間的格式化字符串是否匹配

編譯器檢查函數聲明和函數實際調用參數之間的格式化字符串是否匹配

ftime truct 需要 .cpp 成員函數 rst 之間 def stdarg.h

背景

在輸出日誌時 被 RUNLOG_DEBUG("%s,%d", para_int, para_str.c_str()) 坑過, 才知道 能校驗 格式化字符串和 字段本身是否匹配 的重要性;

參考文獻:https://www.cnblogs.com/marvin-notes/p/4482805.html 感謝原作者

方法

attribute format 該屬性 可以給被聲明的 函數 加上 類似 printf /scanf 的特征, 能用於 編譯器檢查 函數聲明和時間調用參數直接的 格式化字符串是否 匹配; GNU CC需要使用 –Wall 才能使用

語法: format (archetype, string-index, first-to-check)

archetype :  printf, scanf, strftime或strfmon,  表示按照那種風格進行檢查
string-index: 傳入參數的第幾個參數是格式化字符串, 就是 "%s,%d" 的下標(從1 開始)
first-to-check: 指定從 函數的 第幾個參數開始 校驗

實例:

__attribute__((format(printf,m,n)))

m:第幾個參數為格式化字符串(format string)
n:參數集合中的第一個,即參數“…”裏的第一個參數在函數參數總數排在第幾.這裏需要註意,有時函數參數裏還有“隱身”的,如C++的類成員函數的第一個參數實際上是"隱身"的"this"指針;

代碼實例

#include <stdarg.h>
#include <string>
#include <stdio.h>

using namespace std;

#define mm_p(fmt, args...) myprint(fmt, args)
void myprint(const char *fmt,...)  __attribute__((format(printf,1,2)));
void myprint(const char *fmt,...)
{
    va_list ap; 
    va_start(ap, fmt);
    vprintf(fmt,ap);
    va_end(ap);
}

struct ss
{
public:
    void pri(const char* fmt, ...) __attribute__((format(printf,2,3)))
    {   
    }   
};
int main()
{

    mm_p("dfjkaj[%d][%s]\n",123,456);
    myprint("dfjkaj[%d][%d]\n",123,456);

    ss s;
    s.pri("%s[%s", "dd",456);
}
g++ -Wall test_var.cpp -o test_var
test_var.cpp: In function ‘int main()‘:
test_var.cpp:27: warning: format ‘%s‘ expects type ‘char*‘, but argument 3 has type ‘int‘
test_var.cpp:31: warning: format ‘%s‘ expects type ‘char*‘, but argument 4 has type ‘int‘

編譯器檢查函數聲明和函數實際調用參數之間的格式化字符串是否匹配