分析仪无法确定位置

The analyzer is unable to determine the position

本文关键字:位置 无法确定 分析仪      更新时间:2023-10-16

我从 PVS Studio 收到以下错误 V669 "fetch_mask"参数是非常量引用。分析器无法确定修改此参数的位置。该函数可能包含错误。

这是一行:

XXH64_state_t* hash_state, uint32_t& fetch_mask

我正在计算错误是 &不是我修复它fetch_mask的一部分:

XXH64_state_t* hash_state, uint32_t &fetch_mask

错误消失了。但是,travis.cl 测试说我需要重新格式化并失败。

我认为发现了有问题的原始代码。

void TextureCache::HashTextureBindings(
XXH64_state_t* hash_state, uint32_t& fetch_mask,
const std::vector<Shader::TextureBinding>& bindings) {
for (auto& binding : bindings) {
uint32_t fetch_bit = 1 << binding.fetch_constant;
if (fetch_mask & fetch_bit) {
void HashTextureBindings(XXH64_state_t* hash_state, uint32_t& fetch_mask,
const std::vector<Shader::TextureBinding>& bindings);

我删除了 &,因为我认为它应该是uint32_t fetch_mask而不是 uint32_t&fetch_mask

据我了解,我们正在谈论这段代码。我将编写一个函数的整个主体:

void TextureCache::HashTextureBindings(
XXH64_state_t* hash_state, uint32_t& fetch_mask,
const std::vector<Shader::TextureBinding>& bindings) {
for (auto& binding : bindings) {
uint32_t fetch_bit = 1 << binding.fetch_constant;
if (fetch_mask & fetch_bit) {
// We've covered this binding.
continue;
}
auto& regs = *register_file_;
int r = XE_GPU_REG_SHADER_CONSTANT_FETCH_00_0 + binding.fetch_constant * 6;
auto group =
reinterpret_cast<const xenos::xe_gpu_fetch_group_t*>(&regs.values[r]);
auto& fetch = group->texture_fetch;
XXH64_update(hash_state, &fetch, sizeof(fetch));
}
}

如您所见,变量fetch_mask仅用于读取表达式if (fetch_mask & fetch_bit(。通过引用传递整数变量而不修改它是很奇怪的。这通常表明代码包含错误。是的,这并不总是一个错误,但应该仔细验证此代码。

对于PVS-Studio分析仪,无论是写入uint32_t&fetch_mask还是uint32_t&fetch_mask都无关紧要。无论如何,它会发出 警告 .

可能,这里没有错误。您可以通过删除链接来消除警告,就像您所做的那样。另一种选择是使用抑制误报的机制之一。

该错误指示您正在将对变量的非常量引用传递到函数中,但 PVS-Studio 无法查看该变量是否实际被修改。通过引用传递参数的目的是,函数可以修改原始值。如果函数不修改通过引用传递的值,则应const该引用(即uint32_t const& fetch_mask(。

按值传递变量是另一种表达方式,即函数不会修改原始值(尽管我也更喜欢通过 const 值传递,这样它就不会被意外分配(。对于整型数据类型,通常通过 (const( 值传递它们。

诊断本质上是质疑预期目的和代码是否匹配。代码的目的是,通过fetch_mask形式参数传递的变量可以修改,但实现不会尝试这样做(除非它以 PVS-Studio 看不到的方式这样做,例如通过别名(。


作为参考,以下是 V669 的官方文档:

分析器检测到参数正在通过引用传递到函数中,但未在函数体内修改。这可能表示由印刷错误等引起的错误。