cpp-import-mining
Mine #import preprocessor directives in C/C++ source (Sonar cpp:S3805); read-only metrics for review and remediation
# cpp-import-mining
Read-only mining codemod that surfaces `#import` preprocessor directives in
C/C++/Objective-C++ source. Sonar rule [cpp:S3805](https://rules.sonarsource.com/cpp/RSPEC-3805)
flags `#import` because it is an Objective-C variant of `#include` that requires
callers to know a header should only be included once. Compliant code uses
`#include` with include guards (or `#pragma once`) in the header itself.
No source edits are produced; the codemod emits one metric row per `#import`
site so you can quantify violations before applying a fix codemod or manual
remediation.
## Detected pattern
| Noncompliant | Compliant |
| --- | --- |
| `#import "foo.h"` | `#include "foo.h"` |
| `#import <stdio.h>` | `#include <stdio.h>` |
`#include` directives are ignored. Other preprocessor directives (`#define`,
`#if`, etc.) are not reported.
## Metric: `cpp_import_directive`
| field | description |
| --- | --- |
| `rule_id` | Always `cpp:S3805` |
| `file_path` | Relative path of the scanned file |
| `line` | 1-based line number of the `#import` |
| `header_path` | Imported header path without quotes or angle brackets |
| `import_style` | `quoted`, `angled`, or `unknown` |
| `severity` | Always `major` (Sonar classification) |
| `remediation` | Suggested fix summary |
## Run locally
```bash
cd codemods/cpp-import-mining
npm test
npx codemod@latest workflow run \
-w codemods/cpp-import-mining/workflow.yaml \
--target /path/to/cpp-repo
```
## Development
```bash
npm test
npm run check-types
npx codemod@latest workflow validate -w workflow.yaml
```
## License
MIT