Warning Level

本文详细介绍了如何在C#开发环境中设置警告级别,包括使用/warn、/wall等选项来控制编译器生成的警告信息。指导开发者如何在新项目中设置默认警告级别,并提供了在命令行和Visual Studio环境下设置警告级别的方法。

1. /warn (Specify Warning Level)

/warn:option

where:

option
The minimum warning level you want displayed for the build. Valid values are 0-4:
Warning levelMeaning
0Turns off emission of all warning messages.
1Displays severe warning messages.
2Displays level 1 warnings plus certain, less-severe warnings, such as warnings about hiding class members.
3Displays level 2 warnings plus certain, less-severe warnings, such as warnings about expressions that always evaluate to true or false.
4Displays all level 3 warnings plus informational warnings. This is the default warning level at the command line.
Remarks

The /warn option specifies the warning level for the compiler to display.

The Build Errors documentation describes the warnings, indicates each warning's level, and indicates potential problems (rather than actual coding errors) with statements that may not compile as you intend.

Use /warnaserror to treat all warnings as errors. Use /nowarn to disable certain warnings.

/w is the short form of /warn.

To set this compiler option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box. For details, see Setting Visual C# Project Properties.
  2. Click the Configuration Properties folder.
  3. Click the Build property page.
  4. Modify the Warning Level property.

To set this compiler option programmatically

See WarningLevel Property.

Example

Compile in.cs and have the compiler only display level 1 warnings:

csc /warn:1 in.cs




2. /w, /Wn, /WX, /Wall, /wln, /wdn, /wen, /won (Warning Level)


//z 2011-09-28 23:43:05@is2120.优快云 转载请注明出处

最近总用到相关内容,整理了一下

Specify how the compiler generates warnings for a given compilation.

/w
/Wn
/WX
/Wall
/wln
/wdn
/wen
/won
Remarks

The options and related arguments are described in the following table.

Option

Description

/w

Disables all compiler warnings.

/W n

Specifies the highest level of warning generated by the compiler. Valid warning levels for n range from 0 to 4:

  • Level 0 disables all warnings.

  • Level 1 displays severe warnings. Level 1 is the default warning level at the command line.

  • Level 2 displays all level 1 warnings and warnings less severe than level 1.

  • Level 3 displays all level 2 warnings and all other warnings recommended for production purposes.

  • Level 4 displays all level 3 warnings plus informational warnings, which in most cases can be safely ignored. This option should be used only to provide "lint" level warnings and is not recommended as your usual warning level setting.

For a new project, it may be best to use /W4 in all compilations. This will ensure the fewest possible hard-to-find code defects.

/Wall

Enables all warnings, including those disabled by default. See Compiler Warnings That Are Off By Default.

/WX

Treats all compiler warnings as errors. For a new project, it may be best to use /WX in all compilations; resolving all warnings will ensure the fewest possible hard-to-find code defects.

The linker also has a /WX option; see /WX (Treat Linker Warnings as Errors) for more information.

/w ln

Specifies the level for a particular warning. The first parameter sets the warning level (same as /Wn) and the second parameter is the actual warning number.

For example, /w14326 causes C4326 to be generated as a level 1 warning.

/wd n

Disables the specified compiler warning where nis the compiler warning number.

For example, /wd4326 disables compiler warning C4326.

/we n

Treats the specific compiler warning as an error where n is a compiler warning.

For example, /we4326 flags warning number C4326 as an error.

/wo n

Reports the error only once where n is a compiler warning.

For example, /wo4326 will cause warning C4326 to be reported only once.

If you create a precompiled header (/Yc (Create Precompiled Header File)) with one of the /w options, any use of the precompiled header (/Yu (Use Precompiled Header File)) will cause those same /w options to be in effect again. You can override the /w setting in the precompiled header with another /w option at the command line.

Pragma directives in source code are unaffected by the /w option.

You can also use warning to control the level of warning reported at compile time.

The C/C++ Build Errors describes the warnings, indicates each warning's level, and indicates potential problems (rather than actual coding errors) with statements that may not compile as you intend.

To set this compiler option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.

  2. Click the C/C++ folder.

  3. Click the General property page and modify the Warning Level or Treat Warnings as Errors properties.

  4. Click the Advanced property page and modify the Disable Specific Warnings property.

  5. For the remaining options, click the Command Line property page and type the compiler option in the Additional Options box.

To set this compiler option programmatically


3. Change new projects warning level in VS2008 (Express)

I don't know how to do it at the IDE but you cand always edit the new project templates at:

%PROGRAM_FILES%\Microsoft Visual Studio 9.0\Common7\IDE\ProjectTemplates\

If you're using the express version there could be a minor variation in the path:

%PROGRAM_FILES%\Microsoft Visual Studio 9.0\Common7\IDE\{Version}\ProjectTemplates\

Where {Version} is the express flavor you are using, VCSExpress, VBExpress, etc.

The templates are zip files, just edit the project changing:

<WarningLevel>3</WarningLevel>

to

<WarningLevel>4</WarningLevel>


U1 u1_s_GetNextWarning( u1_t_current_level) { U1 u1_t_next_warning; if (u1_t_current_level == NO_WARNING_DISPLAY) { u1_t_next_warning = u1_s_GetHighestExwarning(); } else // 添加 else { // 优先级: Warning1 > Warning2 > Warning3 if (u1_t_current_level == WARNING_LEVEL_1) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_2] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_2]) { u1_t_next_warning = WARNING_LEVEL_2; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_3] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_3]) { u1_t_next_warning = WARNING_LEVEL_3; } else { u1_t_next_warning = WARNING_LEVEL_1; } } else if (u1_t_current_level == WARNING_LEVEL_2) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_3] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_3]) { u1_t_next_warning = WARNING_LEVEL_3; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_1] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_1]) { u1_t_next_warning = WARNING_LEVEL_1; } else { u1_t_next_warning = WARNING_LEVEL_2; } } else if (u1_t_current_level == WARNING_LEVEL_3) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_1] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_1]) { u1_t_next_warning = WARNING_LEVEL_1; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_2] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_2]) { u1_t_next_warning = WARNING_LEVEL_2; } else { u1_t_next_warning = WARNING_LEVEL_3; } } else { u1_t_next_warning = u1_t_current_level; // 添加最终的 else 分支 } } return u1_t_next_warning; } 逐行解释该函数
最新发布
09-06
#include <stdio.h> #include <windows.h> #include <conio.h> #include <stdbool.h> // 状态宏定义 #define TRUE 1 #define FALSE 0 #define NO_WARNING_DISPLAY 0 #define WARNING_STATE_NEW 0 #define WARNING_STATE_EXISTING 1 #define PHASE_T_DIS_MIN 0 #define PHASE_T_DIS_CYCLE 1 // 警告级别宏定义 #define WARNING_LEVEL_1 1 #define WARNING_LEVEL_2 2 #define WARNING_LEVEL_3 3 #define MAX_WARNING_LEVEL 3 // 时间参数宏定义 #define TIME_UNITS_PER_SECOND 10 #define T_DIS_MIN_DURATION (3 * TIME_UNITS_PER_SECOND) // 3s = 30 * 100ms #define T_DIS_CYCLE_DURATION (3 * TIME_UNITS_PER_SECOND) // 警告生成时间点 #define WARNING3_GENERATE_TIME 0 #define WARNING2_GENERATE_TIME 10 #define WARNING1_GENERATE_TIME 20 // 打印间隔 #define PRINT_INTERVAL TIME_UNITS_PER_SECOND typedef unsigned char uint8; uint8 warnings[2][4] = { 0 }; uint8 current_warning = NO_WARNING_DISPLAY; uint8 phase = PHASE_T_DIS_MIN; uint8 timer = 0; void initialize_system(); void generate_warnings(uint8 current_time); uint8 get_highest_new_warning(); uint8 get_highest_existing_warning(); uint8 get_next_warning_by_priority(uint8 current); uint8 all_warnings_are_old(); void handle_no_warning_display(); void handle_t_dis_min_phase(); void handle_t_dis_cycle_phase(); void update_display_state(); void print_current_state(uint8 current_time); // 初始化系统 void initialize_system() { printf("警告显示系统启动\n"); printf("T_dis_min = %ds, T_dis_cycle = %ds\n",T_DIS_MIN_DURATION / TIME_UNITS_PER_SECOND,T_DIS_CYCLE_DURATION / TIME_UNITS_PER_SECOND); printf("按任意键停止程序\n"); printf("----------------------------------------\n"); } // 生成警告 void generate_warnings(uint8 current_time) { if (current_time == WARNING3_GENERATE_TIME) { warnings[WARNING_STATE_NEW][WARNING_LEVEL_3] = TRUE; printf("时间 0.0s: Warning%d 产生\n", WARNING_LEVEL_3); } else if (current_time == WARNING2_GENERATE_TIME) { warnings[WARNING_STATE_NEW][WARNING_LEVEL_2] = TRUE; printf("时间 %u.%us: Warning%d 产生\n", current_time / TIME_UNITS_PER_SECOND, (current_time % TIME_UNITS_PER_SECOND) * (100 / TIME_UNITS_PER_SECOND), WARNING_LEVEL_2); } else if (current_time == WARNING1_GENERATE_TIME) { warnings[WARNING_STATE_NEW][WARNING_LEVEL_1] = TRUE; printf("时间 %u.%us: Warning%d 产生\n", current_time / TIME_UNITS_PER_SECOND, (current_time % TIME_UNITS_PER_SECOND) * (100 / TIME_UNITS_PER_SECOND), WARNING_LEVEL_1); } } // 获取最高优先级的新警告 uint8 get_highest_new_warning() { for (uint8 level = WARNING_LEVEL_1; level <= MAX_WARNING_LEVEL; level++) { if (warnings[WARNING_STATE_NEW][level]) { return level; } } return NO_WARNING_DISPLAY; } // 获取最高优先级的已产生警告 uint8 get_highest_existing_warning() { for (uint8 level = WARNING_LEVEL_1; level <= MAX_WARNING_LEVEL; level++) { if (warnings[WARNING_STATE_NEW][level] || warnings[WARNING_STATE_EXISTING][level]) { return level; } } return NO_WARNING_DISPLAY; } // 按优先级获取下一个警告 uint8 get_next_warning_by_priority(uint8 current) { if (current == NO_WARNING_DISPLAY) { return get_highest_existing_warning(); } else // 添加 else { // 优先级: Warning1 > Warning2 > Warning3 if (current == WARNING_LEVEL_1) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_2] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_2]) { return WARNING_LEVEL_2; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_3] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_3]) { return WARNING_LEVEL_3; } else { return WARNING_LEVEL_1; } } else if (current == WARNING_LEVEL_2) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_3] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_3]) { return WARNING_LEVEL_3; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_1] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_1]) { return WARNING_LEVEL_1; } else { return WARNING_LEVEL_2; } } else if (current == WARNING_LEVEL_3) { if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_1] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_1]) { return WARNING_LEVEL_1; } else if (warnings[WARNING_STATE_EXISTING][WARNING_LEVEL_2] || warnings[WARNING_STATE_NEW][WARNING_LEVEL_2]) { return WARNING_LEVEL_2; } else { return WARNING_LEVEL_3; } } else { return current; // 添加最终的 else 分支 } } } // 检查是否所有警告都是旧警告 uint8 all_warnings_are_old() { for (uint8 level = WARNING_LEVEL_1; level <= MAX_WARNING_LEVEL; level++) { if (warnings[WARNING_STATE_NEW][level]) { return FALSE; } } return TRUE; } // 处理无警告显示 void handle_no_warning_display() { uint8 new_warning = get_highest_new_warning(); if (new_warning != NO_WARNING_DISPLAY) { current_warning = new_warning; phase = PHASE_T_DIS_MIN; timer = 0; warnings[WARNING_STATE_NEW][new_warning] = FALSE; warnings[WARNING_STATE_EXISTING][new_warning] = TRUE; } else { current_warning = get_highest_existing_warning(); if (current_warning != NO_WARNING_DISPLAY) { phase = PHASE_T_DIS_CYCLE; timer = 0; } } } // 处理T_dis_min阶段 void handle_t_dis_min_phase() { timer++; if (timer >= T_DIS_MIN_DURATION) { phase = PHASE_T_DIS_CYCLE; timer = 0; uint8 new_warning = get_highest_new_warning(); if (new_warning != NO_WARNING_DISPLAY) { current_warning = new_warning; phase = PHASE_T_DIS_MIN; timer = 0; warnings[WARNING_STATE_NEW][new_warning] = FALSE; warnings[WARNING_STATE_EXISTING][new_warning] = TRUE; } else if (all_warnings_are_old()) { current_warning = get_highest_existing_warning(); phase = PHASE_T_DIS_CYCLE; timer = 0; } } } // 处理T_dis_cycle阶段 void handle_t_dis_cycle_phase() { uint8 new_warning = get_highest_new_warning(); uint8 higher_new = NO_WARNING_DISPLAY; if (new_warning != NO_WARNING_DISPLAY && new_warning < current_warning) { higher_new = new_warning; } if (higher_new != NO_WARNING_DISPLAY) { current_warning = higher_new; phase = PHASE_T_DIS_MIN; timer = 0; warnings[WARNING_STATE_NEW][higher_new] = FALSE; warnings[WARNING_STATE_EXISTING][higher_new] = TRUE; } else { timer++; if (timer >= T_DIS_CYCLE_DURATION) { new_warning = get_highest_new_warning(); if (new_warning != NO_WARNING_DISPLAY) { current_warning = new_warning; phase = PHASE_T_DIS_MIN; timer = 0; warnings[WARNING_STATE_NEW][new_warning] = FALSE; warnings[WARNING_STATE_EXISTING][new_warning] = TRUE; } else { current_warning = get_next_warning_by_priority(current_warning); phase = PHASE_T_DIS_CYCLE; timer = 0; } } } } // 更新显示状态 void update_display_state() { if (current_warning == NO_WARNING_DISPLAY) { handle_no_warning_display(); } else { if (phase == PHASE_T_DIS_MIN) { handle_t_dis_min_phase(); } else { handle_t_dis_cycle_phase(); } } } // 打印当前状态 void print_current_state(uint8 current_time) { if (current_time % PRINT_INTERVAL == 0) { printf("时间 %u.%us: ", current_time / TIME_UNITS_PER_SECOND, (current_time % TIME_UNITS_PER_SECOND) * (100 / TIME_UNITS_PER_SECOND)); if (current_warning == NO_WARNING_DISPLAY) { printf("没有警告显示"); } else { printf("正在显示: Warning%d (%s)", current_warning, phase == PHASE_T_DIS_MIN ? "T_dis_min" : "T_dis_cycle"); } printf(" | 警告状态: "); for (uint8 level = WARNING_LEVEL_1; level <= MAX_WARNING_LEVEL; level++) { if (warnings[WARNING_STATE_NEW][level] || warnings[WARNING_STATE_EXISTING][level]) { printf("W%d(%s) ", level, warnings[WARNING_STATE_NEW][level] ? "新" : "旧"); } } printf("\n"); } } // 主函数 int main() { uint8 current_time = 0; initialize_system(); while (TRUE) { if (_kbhit()) { _getch(); break; } generate_warnings(current_time); update_display_state(); print_current_state(current_time); Sleep(100); // 延迟100ms current_time++; } printf("----------------------------------------\n"); printf("程序已停止\n"); return 0; } 将代码中的三元运算改为ifelse,必要时可添加新变量
09-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值