Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Last active May 16, 2025 04:22
Show Gist options
  • Save codebrainz/2b1ef2285301fbb8104e2cab2f8c99b3 to your computer and use it in GitHub Desktop.
Save codebrainz/2b1ef2285301fbb8104e2cab2f8c99b3 to your computer and use it in GitHub Desktop.
WIP Clang Format style file for c3lang
# Treat C3 like C++ for clang-format
Language: Cpp
BasedOnStyle: LLVM
# Indentation & Tabs
UseTab: ForIndentation
TabWidth: 4
IndentWidth: 4
ContinuationIndentWidth: 8
# Switch-Case Layout
IndentCaseLabels: true
#CaseIndentWidth: 1
# Bracing & Single-Line Rules
BreakBeforeBraces: Allman
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortFunctionsOnASingleLine: None
BreakAfterAttributes: Leave
#BreakBeforeAttributes: Leave
# Line Length & Wrapping
ColumnLimit: 120
BinPackParameters: false
BinPackArguments: false
AllowAllArgumentsOnNextLine: false
PenaltyBreakBeforeFirstCallParameter: 50
#PenaltyBreakBeforeFirstLambdaArrow: 50
# Import/Include Handling
SortIncludes: false
IncludeIsMainRegex: "(?i)^(.*[/\\\\])?([^/\\\\]+)\\.c3$"
# C3-Specific Tokens
AttributeMacros:
- align
- benchmark
- bigendian
- builtin
- callconv
- compact
- const
- deprecated
- dynamic
- export
- extern
- finalizer
- if
- init
- inline
- link
- littleendian
- local
- maydiscard
- naked
- noalias
- nodiscard
- noinit
- noinline
- nopadding
- norecurse
- noreturn
- nosanitize
- nostrip
- obfuscate
- operator
- optional
- overlap
- packed
- private
- pure
- reflect
- section
- tag
- test
- unused
- used
- wasm
- winmain
- weak
StatementMacros:
- defer
- panic
# Formatter On/Off Pragmas
CommentPragmas: '^(//\\s*(?:c3|clang)-format-(?:off|on))'
@codebrainz
Copy link
Author

Me

clang-format is splitting some @ attributes, inserting a space between the @ and the keyword (ex. inline). I suspect it's because inline is a real c/c++ keyword, whereas in c3, it's an attribute @inline which becomes invalid when clang-format inserts whitespace after the @
ChatGPT said:

ChatGPT o3

clang-format hard-codes its treatment of the Objective-C “@-keywords”.
If the token after the at-sign is one of the dozen keywords that Clang itself
recognises (@interface, @implementation, @end, @property, …) it keeps
them glued together; otherwise it prints the two tokens separately, adding a
space between @ and the identifier to avoid the creation of an accidental
single token in C/C++ lexing. There is no style option that changes this
rule:

The only knob that touches “@ …” spacing is ObjCSpaceAfterProperty and
it applies exclusively to @property
Stack Overflow
.

Lists such as AttributeMacros, StatementMacros, etc. are consulted after
the tokens have been produced, so they cannot influence how the @ token is
glued to (or separated from) inline, noreturn, and similar words.

Because C3’s attributes (@inline, @naked, …) are not recognised
keywords, the formatter will always rewrite

@inline fn foo() { … }

to

@ inline fn foo() { … }

and there is no YAML switch to stop it.
What you can do
Goal Work-around
Leave the attribute exactly as you wrote it Surround the region with the normal pragmas
// clang-format off / // clang-format on or any pattern you put in CommentPragmas.
Format the file but keep the attribute token intact Patch clang-format (add the C3 keywords to the Objective-C keyword table in TokenAnnotator.cpp and rebuild), or keep a fork that recognises C3.
Avoid touching the source at all Run clang-format only on C/C++ source and skip .c3 files (DisableFormat: true in a C3-specific section).
Until someone extends clang-format with first-class C3 support (or at least a
generic “leave @identifier alone” switch), disabling formatting around those
lines—or maintaining a small custom build—is the only reliable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment