C-禁止在块范围内使用相同的变量名 [英] C - Prevent use of same variable name in block scope

查看:381
本文介绍了C-禁止在块范围内使用相同的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些将要重构的代码,这些代码将在具有相同名称的不同范围内广泛使用多个变量,即:

I've inherited some code which I'm going to be refactoring that makes extensive use of multiple variables at different scopes having the same name, ie:

int test = 456;
int main(void) {
    int test = 0;
    //...

    for (i=0; i<MAX_VAL; i++) {
        int test = 123;
        //...
    }
    return 0;
}

我知道,如果仅在两个相关的作用域级别使用相同的变量名,则

I know that if the same variable name is only used at two relevant levels of scope, I can access the globally accessible one by declaring extern int test within the deeper/lower levels of scope. With more than two levels though, I'm not sure which test variable is being accessed: the global scope variable or the variable at one scope level higher.

我决定重新编写代码以使用不同的变量名,并且这样做,发现了许多过去很难跟踪的错误.使用这种行为时,是否有办法触发警告?我必须在Linux中通过GCC编译代码,在Windows中通过Visual Studio 2010编译代码.如果不可能采用可移植的方法,那么这些编译器中的每一个是否都有办法警告不同作用域中具有相同名称的多个变量?这样,我就可以构建代码并获得使用此行为的所有位置的列表.

I've decided to re-write the code to use different variable names, and in doing so, have uncovered a lot of bugs that have been hard to track in the past. Is there a way to trigger a warning when such behavior is used? I have to compile the code in both Linux via GCC and Windows via Visual Studio 2010. If a portable approach isn't possible, is there a way for each of these compilers to warn about multiple variables at different scopes with the same name? This would let me build the code and have a list of all locations where such behavior is used.

谢谢.

推荐答案

gcc和clang都支持-Wshadow标志,该标志会警告有关相互影射的变量:

Both gcc and clang support the -Wshadow flag which warns about variables that shadow one another:

-影子

每当局部变量或类型声明对另一个变量,参数,类型,类成员(在C ++中)或实例变量(在Objective-C中)或对内置函数进行阴影时,都发出警告.请注意,在C ++中,如果局部变量在显式的typedef阴影下,则编译器会发出警告,但在struct/class/enum的阴影下,则不会发出警告.

Warn whenever a local variable or type declaration shadows another variable, parameter, type, class member (in C++), or instance variable (in Objective-C) or whenever a built-in function is shadowed. Note that in C++, the compiler warns if a local variable shadows an explicit typedef, but not if it shadows a struct/class/enum.

从Visual Studio 2015开始默认情况下,您会收到警告:

From Visual Studio 2015 you get a warning by default:

阴影变量

如果封闭范围中已经包含相同名称的变量,则变量声明会遮盖"另一个变量.例如:

A variable declaration "shadows" another if the enclosing scope already contains a variable with the same name. For example:

void f(int x)
{
  int y;
  {
    char x; //C4457
    char y; //C4456
  }
}

x的内部声明遮盖了函数f的参数,因此编译器将发出:

The inner declaration of x shadows the parameter of function f, so the compiler will emit:

警告C4457:"x"的声明隐藏了函数参数

y的内部声明在函数范围内遮盖了y的声明,因此编译器将发出:

The inner declaration of y shadows the declaration of y in the function scope, so the compiler will emit:

警告C4456:"y"的声明隐藏了先前的本地声明

请注意,和以前一样,与函数参数同名但未包含在内部作用域中的变量声明将触发错误:

Note that, as before, a variable declaration with the same name as a function parameter but not enclosed in an inner scope triggers an error instead:

 void f(int x)
 {
   char x; //C2082
 }

编译器发出:

错误C2082:重新定义形式参数'x'

对于旧版本的Visual Studio,您可以启用代码分析 (请参阅如何在MSVC ++中启用6000系列警告(代码分析警告)?)并查看警告 C6244

For older versions of Visual Studio you could enable Code Analysis (see How do I enable the 6000 series warnings (code analysis warnings) in MSVC++?) and take a look at warnings C6244 and C6246.

这篇关于C-禁止在块范围内使用相同的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆