cpp-include-sorter
C/C++ Include Sorter
Automatically sorts #include statements in C/C++ source files with intelligent handling of conditional compilation blocks.
Quick Start
Sort all .cpp files in a directory:
python scripts/sort_includes.py <directory-path>
Preview changes without modifying files:
python scripts/sort_includes.py <directory-path> --dry-run
Sorting Rules
Includes are organized into 3 categories with blank lines between each group:
- Corresponding header - For
file.cpp,file.his placed first (prefers longest path if duplicates exist) - System headers - Headers with angle brackets
<>(sorted alphabetically) - Local headers - Headers with double quotes
""(sorted alphabetically)
Conditional Compilation Blocks
#ifdef/#ifndef/#if defined blocks are treated as units:
- Blocks participate in global sorting based on their first include
- Headers inside each block are also sorted (system headers first, then local headers)
- Block structure (
#ifdef...#endif) is preserved #elifand#elseblocks are supported
Example
Before:
#include "common/rs_log.h"
#include "rs_trace.h"
#include <memory>
#ifdef RS_ENABLE_GPU
#include "feature/uifirst/rs_sub_thread_manager.h"
#include "feature/capture/rs_ui_capture_task_parallel.h"
#endif
#include "platform/common/rs_system_properties.h"
After:
#include "rs_trace.h" // 1. Corresponding header (longest path preferred)
#include <memory> // 2. System headers (alphabetical)
#include "common/rs_log.h" // 3. Local headers (alphabetical)
#ifdef RS_ENABLE_GPU
#include "feature/capture/rs_ui_capture_task_parallel.h"
#include "feature/uifirst/rs_sub_thread_manager.h"
#endif
#include "platform/common/rs_system_properties.h"
Features
- Duplicate include handling: When multiple includes with same filename but different paths exist (e.g.,
"file.h"and"path/to/file.h"), the longest path is used as the corresponding header and placed first - Comment preservation: Inline comments (
// comment) and preceding comments are preserved - Nested conditional blocks: Handles
#elifand#elsewithin#ifdefblocks - Validation: Verifies no headers are lost during sorting
Script Reference
See scripts/sort_includes.py for implementation details.
Key functions:
extract_includes_with_ifdef()- Parses includes and conditional blockssort_includes_with_ifdef()- Sorts with 3-category ruleformat_ifdef_block()- Formats conditional blocks with sorted includes
Verification
After sorting, verify:
- All
#ifdefblocks contain same number of headers as before - Corresponding header has complete path (not shortened)
- Total include count unchanged
- Comments preserved
Use git diff to compare before/after:
git diff <file.cpp> | grep "^[-+]" | grep "include"
More from openharmonyinsight/openharmony-skills
openharmony-cpp
Expert coding guide for OpenHarmony C++ development. Use this skill when writing, refactoring, or reviewing C++ code for OpenHarmony projects. It enforces strict project-specific conventions (naming, formatting, headers) and critical security requirements (input validation, memory safety).
76oh-ut-generator
|
65cpp-core-guidelines-review
Parallel C++ Core Guidelines code review using multiple specialized sub-agents. Use when reviewing C++ code, modules, or files against C++ Core Guidelines to identify violations. Each sub-agent reviews against a specific guideline section (Functions, Classes, Resource Management, etc.) and outputs findings to separate markdown files in the review/ directory, followed by a consolidated summary.
59openharmony-build
This skill should be used when the user asks to "编译 OpenHarmony", "build OpenHarmony", "编译完整代码", "执行编译", "编译 OpenHarmony 代码", "快速编译", "跳过gn编译", "fast-build", "编译测试", "编译测试用例", "build ace_engine_test", "编译 sdk", "编译 SDK", "build sdk", "build SDK", "编译 ohos-sdk", "编译测试列表", "build test list", "按列表编译测试", "编译指定测试", or mentions building the full OpenHarmony system, fast rebuild, test compilation, SDK compilation, or building tests from a target list. Handles complete build process including build execution, success verification, and failure log analysis with primary focus on out/{product}/build.log.
55ohos-chromium-security-review
|
55ai-generated-business-code-review
Use when reviewing or scoring AI-generated business/application code quality in any language, especially when a numeric score, risk level, or must-fix checklist is requested, or when C++ code must comply with OpenHarmony C++ and security standards
53