src/tango.
Most contributors do not use a code formatting tool. The Firedancer codebase uses a distinctive style that emphasizes readability through vertical alignment.
General Guidelines
Text Word Wrap
Aspire to word wrap text (comments, not code) at 72 columns for readability. After accounting for the indent, this is right around the level the publishing industry has used for hundreds of years for making print easily readable with minimal eye strain.Repository Organization
- See
doc/organization.txtfor detailed organization guidelines - Avoid cluttering the repository root
File Extensions
| Extension | File Type |
|---|---|
.c | Standalone C translation unit |
.h | Reusable C include file, no symbol defs (header) |
.c | Include-once C file, with symbol defs |
.s | Assembly files |
| (none) | Shell scripts |
Include Guards
Header files must use#ifndef include guards. Never use #pragma once.
Given file: src/path/to/file/fd_file_name.h:
Vertical Alignment
Popular code formatting tools would produce compact but less readable code. Firedancer uses vertical alignment for better readability.Spacing Rules
Function Calls
Zero arguments: No spaces inside bracketssizeof: Usually no spaces
Control Flow
Annotate uncommon error paths withFD_UNLIKELY.
Single-line statements: No braces required
Function Prototypes
- Modifiers and return types on separate lines
- One function argument per line
- Vertically align function argument types and names
Type System
Integers
Usefd_util_base.h types instead of stdint.h integer types.
| stdint | fd_util_base |
|---|---|
int8_t | schar |
uint8_t | uchar |
int16_t | short |
uint16_t | ushort |
int32_t | int |
uint32_t | uint |
int64_t | long |
ptrdiff_t | long |
uint64_t | ulong |
size_t | ulong |
For more information on why Firedancer uses custom integer types, see
src/util/fd_util_base.h and doc/rant/integer-types.md.Booleans
Do not usebool (stdbool). Instead use int.
1is “true”0is “false”
Function Documentation
- Documentation goes before the function prototype in a comment block
- Comments should mention the function name toward the beginning
- Public API functions must be documented
- Implementations must not repeat the comment
Macros
Enclose Arguments in Braces
Use do/while(0) Scopes
Evaluate Arguments Only Once
Portability
Build Capabilities
Firedancer aspires to compile under any LP64 environment. Components with additional assumptions should check for capabilities viaFD_HAS_{...} switches.
Example Makefile:
Language Features
Stick to ISO C17. GNU C extensions are permitted as long as they are well supported by Clang and CBMC.seccomp Sandbox
Firedancer uses a strict sandbox architecture on Linux platforms using seccomp.File I/O
Preferfd_io over stdio.h for streaming file I/O. Make sure to handle EINTR correctly.
Security Best Practices
Complex Function Exit
For functions with complex control flow that need cleanup, use ado/while scope:
cleanup attribute:
FD_SCRATCH_SCOPE_BEGIN in src/util/scratch/fd_scratch.h.