深度C++17:if(init;condition)

本博客将定期更新关于C++17新特性的详细解读,并强调指出开发者不应仅仅为了追求新特性而使用它们。

本博客将持续更新对C++17新特性的解读。

在解读这个新特性之前,请注意,不要为了标新写法而使用新特性。



使用以下类似格式@cppcheck.checker def check_directive_comments(cfg, data): """ 检查 #else 和 #endif 指令是否有引用其匹配的开放指令条件的尾随注释。 """ # 用于跟踪活动预处理条件的栈 condition_stack = [] # 按行号跟踪指令 directive_lines = {} # 处理原始标记以识别预处理指令 line_num = 0 line_tokens = [] # 第一遍 - 按行收集标记并识别指令 for token in data.rawTokens: # 将标记添加到当前行 line_tokens.append(token) # 未到达行尾,继续遍历 if (token.next and (token.next.linenr == token.linenr)): continue line_num = token.linenr # 非预处理指令行,跳过 if line_tokens and line_tokens[0].str != '#': # 重置为新行 line_tokens = [] continue if len(line_tokens) <= 1: # 重置为新行 line_tokens = [] continue # 第 1 个字符为 '#',第 2 个字符为指令类型 directive_type = line_tokens[1].str # 处理 #if, #ifdef, #ifndef if directive_type in ['if', 'ifdef', 'ifndef']: if len(line_tokens) > 2: # 收集构成条件的所有标记 condition_tokens = line_tokens[2:] condition = ' '.join(t.str for t in condition_tokens if not (t.str.startswith('//') or t.str.startswith('/*'))) condition = condition.strip(' ') # 处理 #if defined(...) 情况,支持多个defined组合 if directive_type == 'if' and 'defined' in condition: condition = condition.replace(' ', '') condition = condition.replace('(', '') condition = condition.replace(')', '') condition = condition.replace('defined', '') # 处理 #ifdef 和 #ifndef elif directive_type in ['ifdef', 'ifndef'] and len(line_tokens) > 2: condition = line_tokens[2].str directive_lines[line_num] = {"type": directive_type, "condition": condition} condition_stack.append(condition) else: condition_stack.append("") # 处理 #else 和 #endif elif directive_type in ['else', 'endif']: if not condition_stack: # 重置为新行 line_tokens = [] break current_condition = condition_stack[-1] directive_lines[line_num] = {"type": directive_type, "condition": current_condition} if directive_type == 'endif': condition_stack.pop() # 检查行标记中是否有注释 has_comment = False for each in line_tokens: if each.str.startswith('//') or each.str.startswith('/*'): has_comment = True break if has_comment: # 重置为新行 line_tokens = [] continue # 找到要报告的指令标记 for idx, each in enumerate(line_tokens): if idx > 0 and line_tokens[idx - 1].str == '#' and each.str in ['else', 'endif']: directive_info = directive_lines[line_num] error_message = f"#{directive_info['type']} directive should have a comment, preferably referencing condition '{directive_info['condition']}'" error_key = (each.linenr, error_message) if error_key not in reported_errors: cppcheck.reportError( each, 'severity:warning', f"#{directive_info['type']} directive should have a comment, preferably referencing condition '{directive_info['condition']}'" ) reported_errors.add(error_key) break # 重置为新行 line_tokens = []
10-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值