Skip to main content
Glama

check_include_guard

Verify C++ header file include guards are correctly implemented to prevent multiple inclusion errors. Analyzes code structure and provides compliance feedback with improvement suggestions.

Instructions

检查 C++ 头文件的包含保护是否正确 参数: code: 头文件的完整代码 file_path: 可选的文件路径,用于生成建议的保护宏名 返回: 检查结果,包含是否符合规范、详细说明和建议

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYes
file_pathNo

Implementation Reference

  • MCP tool handler and registration for 'check_include_guard'. This function is decorated with @mcp.tool() to register it as an MCP tool. It receives input parameters, delegates to the IncludeGuardChecker instance, formats the results, and returns the analysis string.
    @mcp.tool() def check_include_guard(code: str, file_path: str = "") -> str: """ 检查 C++ 头文件的包含保护是否正确 参数: code: 头文件的完整代码 file_path: 可选的文件路径,用于生成建议的保护宏名 返回: 检查结果,包含是否符合规范、详细说明和建议 """ checker = get_include_guard_checker() file_path_param = file_path if file_path else None is_valid, details, suggestions = checker.check_include_guard(code, file_path_param) result = details if suggestions and not is_valid: result += f"\n建议的保护宏名:\n" for sug in suggestions: result += f" • {sug}\n" return result
  • Core helper function implementing the include guard checking logic within the IncludeGuardChecker class. Performs detailed analysis for #pragma once, traditional #ifndef/#define/#endif guards, validates macro names, generates suggestions, and provides explanatory details.
    def check_include_guard( self, code: str, file_path: Optional[str] = None ) -> Tuple[bool, str, List[str]]: """ 检查头文件的包含保护 Args: code: 头文件代码 file_path: 可选的文件路径,用于生成建议的保护宏名 Returns: (是否符合规范, 详细说明, 建议列表) """ lines = code.strip().split('\n') if len(lines) < 3: return False, "文件太短,无法包含有效的包含保护", [] # 检查 #pragma once has_pragma_once = self._check_pragma_once(code) # 检查传统的 #ifndef/#define/#endif 保护 has_traditional_guard, guard_name = self._check_traditional_guard(code) suggestions = [] details = "" if has_pragma_once: details = "✓ 使用了 #pragma once\n\n" details += "说明: #pragma once 是现代编译器广泛支持的简洁方式。\n" details += "优点: 简洁、避免宏名冲突\n" details += "注意: 大多数编译器支持,但不是 C++ 标准的一部分\n" return True, details, [] if has_traditional_guard: # 检查保护宏命名是否合理 is_valid_name, name_message = self._validate_guard_name(guard_name, file_path) details = f"✓ 使用了传统的包含保护\n" details += f"保护宏名: {guard_name}\n\n" if is_valid_name: details += f"命名检查: ✓ 符合规范\n" details += f"{name_message}\n" else: details += f"命名检查: ✗ 可以改进\n" details += f"{name_message}\n\n" # 生成建议的宏名 if file_path: suggested_names = self._generate_guard_names(file_path) suggestions = suggested_names details += "建议的保护宏名:\n" for name in suggested_names: details += f" • {name}\n" details += "\n包含保护规范:\n" details += " • 宏名应全大写,使用下划线分隔\n" details += " • 包含文件路径或项目名作为前缀,避免冲突\n" details += " • 以 _H、_HPP 或 _H_ 结尾\n" details += " • 避免以下划线开头(保留给编译器)\n" return True, details, suggestions # 没有任何保护 details = "✗ 缺少包含保护!\n\n" details += "头文件应使用以下方式之一防止重复包含:\n\n" details += "方式1: #pragma once (推荐)\n" details += "```cpp\n" details += "#pragma once\n\n" details += "// 头文件内容\n" details += "```\n\n" details += "方式2: 传统包含保护\n" details += "```cpp\n" if file_path: guard_name = self._generate_guard_names(file_path)[0] suggestions = self._generate_guard_names(file_path) else: guard_name = "MY_HEADER_H" details += f"#ifndef {guard_name}\n" details += f"#define {guard_name}\n\n" details += "// 头文件内容\n\n" details += f"#endif // {guard_name}\n" details += "```\n" return False, details, suggestions
  • Factory function to retrieve the singleton IncludeGuardChecker instance, used by the MCP handler.
    def get_checker() -> IncludeGuardChecker: """获取全局包含保护检查器实例""" global _checker if _checker is None: _checker = IncludeGuardChecker() return _checker

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/SongJiangzhou/cpp_guidelines'

If you have feedback or need assistance with the MCP directory API, please join our Discord server