PAYATHON 2026

gcc 15.2 中 format 属性为何报可变参数列表错误?

排障赵师傅

Ts&&... LogArguments 是 C++ 函数参数包。GCC 的 format 属性在第三个参数非零时,要求它指向 C 风格的可变参数列表 ...,参数包不满足这一要求,因此调整编号也无法解决报错。

将隐式 this 计为第 1 个参数是正确的。这段代码的报错已有明确原因,不能据此判断 GCC 15.2 出现了旧 bug 的回归。

format(printf, 5, 6) 为什么不成立?

属性的写法是:

__attribute__((format(printf, string_index, first_to_check)))

string_index 指定格式字符串的位置,first_to_check 指定待检查的可变参数起始位置。非静态成员函数的参数编号包含隐式 this,所以显式参数从 2 开始。GCC 15.2 文档

原构造函数的参数编号如下:

编号 参数
1 隐式 this
2 instrumentsId
3 logMode
4 logControllerType
5 formatString

第二个属性参数 5 没有问题。第三个属性参数 6 指向的是:

Ts&&... LogArguments

这里的省略号表示参数包展开。模板实例化后,它会形成若干个类型确定的普通参数。参数包虽然能接受不同数量的实参,但与下面的 C 风格可变参数不同:

void log_printf(const char* formatString, ...);

GCC 不支持用这样的非零索引对模板参数包执行 format 属性检查。LLVM 关于该属性的变更记录也区分了这两种情况,并说明 Clang 对非 C 风格可变参数函数的支持属于扩展。LLVM 变更记录

如何保留 printf 风格的编译期检查?

可以让日志入口接受 C 风格的 ...,通过 va_list 和 vsnprintf 完成格式化。

C 风格的 ... 必须放在参数列表末尾,因此原先位于日志参数之后的 sourceLocation 需要移到前面。下面的代码保留构造函数调用形式,移除类模板和推导指引,并显式传入调用位置:

#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <source_location>
#include <vector>

using uint = unsigned int;
constexpr std::size_t MAX_LOG_LINE_LENGTH = 2'500;

enum LogControllerMode { Default = 0 };
enum class LogControllerType { Normal };

struct FAEWE_LOG
{
    __attribute__((format(printf, 6, 7)))
    FAEWE_LOG(
        const std::source_location& sourceLocation,
        uint instrumentsId,
        [[maybe_unused]] LogControllerMode logMode,
        [[maybe_unused]]
        const std::vector<LogControllerType>& logControllerType,
        const char* formatString,
        ...)
    {
        char resolvedParameters[MAX_LOG_LINE_LENGTH];

        va_list args;
        va_start(args, formatString);
        const int result = std::vsnprintf(
            resolvedParameters,
            sizeof(resolvedParameters),
            formatString,
            args);
        va_end(args);

        if (result < 0) {
            return;
        }

        std::printf(
            "%s:%u - [%u] %s\n",
            sourceLocation.file_name(),
            static_cast<unsigned int>(sourceLocation.line()),
            instrumentsId,
            resolvedParameters);
    }
};

int main()
{
    FAEWE_LOG(
        std::source_location::current(),
        5U,
        LogControllerMode::Default,
        {LogControllerType::Normal},
        "formatted: %s %u",
        "formatted",
        73U);
}

新声明中,this 的编号是 1,sourceLocation 是 2,formatString 是 6,C 风格的可变参数从 7 开始,因此属性应写为:

__attribute__((format(printf, 6, 7)))

显式调用 std::source_location::current(),记录的是该调用所在的位置。

如何检查原示例中的格式问题?

可以用以下命令检查语法和告警:

g++ -std=c++20 -Wall -Wextra -Wformat=2 \
    -Wformat-signedness -fsyntax-only example.cpp

原调用中的 73U 是 unsigned int,应使用 %u;如果使用 %d,则应传入 int。-Wformat-signedness 用于启用格式要求与实参之间的有符号性检查。GCC 告警选项

原日志前缀也有同样的问题:

printf("%s:%d - [%d] %s\n", ...);

其中,instrumentsId 是 unsigned int,应使用 %u。上面的修正示例还将行号转换为 unsigned int,与 %u 对应。

还需要注意什么?

  • 将属性改为 format(printf, 5, 0) 后,不会检查后续实参的类型,因此无法验证 LogArguments... 与格式说明符是否匹配。GCC 文档说明了第三个参数为零时的用途。
  • 如果保留模板接口并删除属性,当前的属性声明错误会消失,但函数体中的 snprintf 不会自动恢复调用点的格式检查。
  • 使用 C 风格 ... 时,传给 %s 的应是有效的字符指针。对于 std::string 等对象,需要先取得适当的表示,例如调用 .c_str()。
  • 固定缓冲区仍可能截断输出。当 vsnprintf 的返回值非负且不小于缓冲区大小时,表示输出被截断,业务代码可据此处理。
  • 给出的代码只有一个构造函数声明。其余涉及 7、4 的诊断,需要结合各自的声明核对,不能仅凭数字判断原因。

备注:内容仅供参考。