check_naming
Validate C++ identifier naming against style guidelines for variables, functions, classes, and other code elements to ensure consistent conventions.
Instructions
检查 C++ 标识符命名是否符合规范
参数:
identifier: 要检查的标识符名称
category: 标识符类别,可选值:
- variable: 变量
- constant: 常量
- function: 函数
- class: 类
- namespace: 命名空间
- member_variable: 成员变量
- template_parameter: 模板参数
- file_naming: 文件命名
返回:
检查结果,包含是否符合规范、详细说明和建议
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| identifier | Yes | ||
| category | Yes |
Implementation Reference
- cpp_style_server.py:32-61 (handler)MCP tool handler for 'check_naming', decorated with @mcp.tool(). Receives identifier and category, instantiates NamingChecker via get_naming_checker(), calls its check_naming method, formats the result with details and suggestions, and returns the string response.@mcp.tool() def check_naming(identifier: str, category: str) -> str: """ 检查 C++ 标识符命名是否符合规范 参数: identifier: 要检查的标识符名称 category: 标识符类别,可选值: - variable: 变量 - constant: 常量 - function: 函数 - class: 类 - namespace: 命名空间 - member_variable: 成员变量 - template_parameter: 模板参数 - file_naming: 文件命名 返回: 检查结果,包含是否符合规范、详细说明和建议 """ checker = get_naming_checker() is_valid, details, suggestions = checker.check_naming(identifier, category) result = details if suggestions: result += f"\n推荐使用:\n" for sug in suggestions: result += f" • {sug}\n" return result
- Core naming validation logic in NamingChecker.check_naming(self, identifier: str, category: str). Loads conventions from JSON, validates style using regex patterns for various cases (snake_case, UPPER_SNAKE_CASE, PascalCase, etc.), generates suggestions via helper methods, builds comprehensive details including rules and examples.def check_naming(self, identifier: str, category: str) -> Tuple[bool, str, List[str]]: """ 检查标识符命名是否符合规范 Args: identifier: 要检查的标识符 category: 类别 (variable, constant, function, class, namespace, member_variable, template_parameter, file_naming) Returns: (是否符合规范, 详细说明, 建议列表) """ if category not in self.conventions: return False, f"未知的类别: {category}", [] conv = self.conventions[category] style = conv["style"] # 根据不同风格进行检查 is_valid, message = self._check_style(identifier, style, category) suggestions = [] if not is_valid: suggestions = self._generate_suggestions(identifier, style, category) # 构建详细说明 details = f"类别: {category}\n" details += f"推荐风格: {style}\n" details += f"标识符: {identifier}\n" details += f"检查结果: {'✓ 符合规范' if is_valid else '✗ 不符合规范'}\n" if message: details += f"说明: {message}\n" if suggestions: details += f"\n建议的命名:\n" for sug in suggestions: details += f" • {sug}\n" # 添加规则说明 details += f"\n规范要求:\n" for rule in conv.get("rules", []): details += f" • {rule}\n" # 添加示例 if "examples" in conv: details += f"\n正确示例: {', '.join(conv['examples']['good'])}\n" details += f"错误示例: {', '.join(conv['examples']['bad'])}\n" return is_valid, details, suggestions
- NamingChecker class containing all helper methods for naming checks, including _check_style, _generate_suggestions, and conversion utilities like _to_snake_case, _to_pascal_case.class NamingChecker: """C++ 命名规范检查器""" def __init__(self): """加载命名规范数据""" data_path = Path(__file__).parent.parent / "data" / "naming_conventions.json" with open(data_path, 'r', encoding='utf-8') as f: self.conventions = json.load(f) def check_naming(self, identifier: str, category: str) -> Tuple[bool, str, List[str]]: """ 检查标识符命名是否符合规范 Args: identifier: 要检查的标识符 category: 类别 (variable, constant, function, class, namespace, member_variable, template_parameter, file_naming) Returns: (是否符合规范, 详细说明, 建议列表) """ if category not in self.conventions: return False, f"未知的类别: {category}", [] conv = self.conventions[category] style = conv["style"] # 根据不同风格进行检查 is_valid, message = self._check_style(identifier, style, category) suggestions = [] if not is_valid: suggestions = self._generate_suggestions(identifier, style, category) # 构建详细说明 details = f"类别: {category}\n" details += f"推荐风格: {style}\n" details += f"标识符: {identifier}\n" details += f"检查结果: {'✓ 符合规范' if is_valid else '✗ 不符合规范'}\n" if message: details += f"说明: {message}\n" if suggestions: details += f"\n建议的命名:\n" for sug in suggestions: details += f" • {sug}\n" # 添加规则说明 details += f"\n规范要求:\n" for rule in conv.get("rules", []): details += f" • {rule}\n" # 添加示例 if "examples" in conv: details += f"\n正确示例: {', '.join(conv['examples']['good'])}\n" details += f"错误示例: {', '.join(conv['examples']['bad'])}\n" return is_valid, details, suggestions
- Singleton factory get_checker() that lazily initializes and returns the global NamingChecker instance.def get_checker() -> NamingChecker: """获取全局命名检查器实例""" global _checker if _checker is None: _checker = NamingChecker() return _checker
- cpp_style_server.py:32-61 (registration)The @mcp.tool() decorator registers this function as the 'check_naming' MCP tool.@mcp.tool() def check_naming(identifier: str, category: str) -> str: """ 检查 C++ 标识符命名是否符合规范 参数: identifier: 要检查的标识符名称 category: 标识符类别,可选值: - variable: 变量 - constant: 常量 - function: 函数 - class: 类 - namespace: 命名空间 - member_variable: 成员变量 - template_parameter: 模板参数 - file_naming: 文件命名 返回: 检查结果,包含是否符合规范、详细说明和建议 """ checker = get_naming_checker() is_valid, details, suggestions = checker.check_naming(identifier, category) result = details if suggestions: result += f"\n推荐使用:\n" for sug in suggestions: result += f" • {sug}\n" return result