跳到正文
Skillpedia
通用编程12k · #11500

fprime-cpp-design

它能做什么

它列出 F Prime 飞行代码必须遵守的 C 和 C++ 设计规则,例如启动之后避免动态内存。适用于编写 F Prime 代码的工程师。

适用场景

  • ✓当你在 F Prime 组件中编写或修改 C++ 代码时。
  • ✓当你想知道应使用哪些 F Prime 类型和编码惯用法时。
  • ✓当你审查 F Prime 代码,需要某条注释背后的规则时。

技能链接

https://github.com/nasa/fprime/blob/devel/.github/skills/fprime-cpp-design/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/nasa/fprime/blob/devel/.github/skills/fprime-cpp-design/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请检查这个 F Prime C++ 函数是否符合飞行软件的设计规则。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会将代码与编号的设计规则对照,说明每个问题违反了哪条规则,并提出符合该规则的修复建议。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Skill: F Prime C/C++ design rules

The single source of truth for the C/C++ design rules F Prime flight software is held to. Used in two directions:

  • Developer-side. §1 is positive guidance for writing or modifying F Prime C++ code.
  • Reviewer-side. §2 gives the finding-class name to use per rule; §3 gives severity hints. Combine with .github/skills/triage-classifier/SKILL.md.

External references in §4 are authoritative for their specific clauses; this skill summarizes the rules the agents enforce. Where this skill and an external reference disagree, this skill wins for review purposes.


1. The rules

Rules are grouped by theme. Numbers are stable and may be cited in PR comments and in other agent files.

A. Memory & lifetime

CPP-1 — No dynamic memory after initialization

Allocate all dynamic storage during component initialization or boot. After init, no new / delete / malloc / free, and no implicit allocation through STL containers, std::string, or exception machinery (see CPP-25).

Allowed: pre-sized arrays sized at compile or init time, Fw::Buffer references to memory owned elsewhere (CPP-2), object pools allocated once at boot.

// Avoid (allocation post-init)
char* scratch = new char[len];

// Prefer (sized at declaration)
static constexpr U32 SCRATCH_BYTES = 256;
U8 m_scratch[SCRATCH_BYTES];

CPP-2 — Fw::Buffer ownership transfers must close in every branch

Any function receiving an Fw::Buffer must, in every branch (success, failure, error, early-return), either transfer ownership out (pass to another component, store in a tracked slot) or return it to the sender. A dropped Fw::Buffer leaks; a double-transferred one is a use-after-free.

CPP-17 — Follow Rule of Three / Rule of Five

If a class manages a resource (raw pointer, file handle, externally- owned memory), implement (or = delete) all of: destructor, copy constructor, copy assignment, and — under Rule of Five — move constructor and move assignment. Most F Prime components are not intended to be copied; mark the copy ops = delete explicitly.

CPP-19 — Initialize all variables

Every variable — member, local, global — has an explicit initializer. Uninitialized reads are undefined behavior. Prefer member-initializer lists for class members; use brace-initialization (Type x{}; or Type x{value};) for locals.

CPP-20 — Destructors are virtual or protected non-virtual

A class meant to be inherited from declares either a virtual destructor (polymorphic deletion is safe) or a protected non-virtual destructor (deletion through the base pointer is prevented).

B. Asserts and untrusted inputs

CPP-4 — FW_ASSERT is for programming errors only

FW_ASSERT is for programmer-controllable invariants — guaranteed by construction. It is not input validation. Never FW_ASSERT on a value reachable from:

  • A ground command argument (see .github/skills/fprime-ground-input-tracing/SKILL.md).
  • A hardware input — packet, register read, port from a driver (see .github/skills/fprime-hardware-input-tracing/SKILL.md).
  • Any off-device data crossing a hub / bridge / driver boundary.

Validate untrusted inputs and return the error through the component's contract (typically a VALIDATION_ERROR command response, a sized return, or a soft-failure event). The security-review agent mirrors this as ground-reachable-assert / hardware-reachable-assert; CPP-4 is the C++-side framing of the same finding.

C. F Prime types and idioms

CPP-3 — Always use fixed-size numerical types

For numerical fields (integers, floats), always use a fixed-size type unless an external API forces a non-fixed-size type. Fixed-size types are either:

  • The explicit fixed-size types: U8, I8, U16, I16, U32, I32, U64, I64, F32, F64.
  • The configurable Fw* types whose width the project pins to a known fixed-size type at config time (e.g., FwSizeType, FwIndexType).

Bare C/C++ numerical types (int, unsigned, long, size_t, float, double) are forbidden in F Prime code except at external API boundaries that require them.

CPP-28 — Prefer configurable Fw* types when range may vary across projects

When the appropriate width for a value depends on the project (e.g., buffer sizes, container indices, time tick counts), use the configurable Fw* type rather than a bare fixed-size type. The configurable type lets the f

常见问题

启动之后代码还会分配内存吗?

规则避免初始化之后的动态内存,因此存储空间在启动时就已确定大小并分配。

规则是如何组织的?

它们按主题编号和分组,例如内存和生命周期。

它能替代风格指南吗?

不能。它概括了项目强制执行的规则,并把具体条款指向外部参考资料。

详情

作者的原始描述

Authoritative C/C++ design rules and idioms for F Prime flight software. Use this skill in two directions — as a *developer-side* reference when writing or modifying F Prime C++ code (what idioms to follow, what features to avoid, which F Prime types to prefer), and as a *reviewer-side* reference for finding-class names and severity hints when flagging C/C++ design violations. Trigger on any work that touches F Prime C/C++ source: component implementations, drivers, services, framework types, OSAL code, or unit-test infrastructure. Keywords: F Prime, C++14, FW_ASSERT, Fw::Buffer, Fw::String, J

repo: nasa/fprime

热度

GitHub 星标
12k
排名
#11500
许可证
Apache-2.0
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能