Skip to content

Instantly share code, notes, and snippets.

@davidzchen
Last active November 24, 2023 18:19
Show Gist options
  • Save davidzchen/9187973 to your computer and use it in GitHub Desktop.
Save davidzchen/9187973 to your computer and use it in GitHub Desktop.
Sample C code using the LLVM coding style
/* Sample file using the LLVM coding standard
http://llvm.org/docs/CodingStandards.html
General rules:
- Indents are two spaces. No tabs should be used anywhere.
- Each line must be at most 80 characters long.
- Use C-style comments when writing C code
- File names should be PascalCase.c
Note: The LLVM coding standard is a C++ coding standard. There are
aspects specific to C that the coding standard does not specify, and these
exceptions are noted below.
*/
#include <stdlib.h>
#include <stdbool.h>
// For macros, use ALL_CAPS separated by underscore:
#define FLAG_FOO 0x0
// If a macro's replacement is not just a literal, enclose it in parentheses:
#define FLAG_BAZ (0x1 << 3)
// Constants are PascalCase.
const int StateFoo = 0;
// Type names should be PascalCase.
typedef struct LinkedList LinkedList;
// Enum values should be PascalCase. Unless an enum is just a bunch of
// convenience constants, they should each have a prefix corresponding to the
// enum declaration name.
typedef enum {
State_Foo,
State_Bar,
State_Baz,
State_Qux
} State;
// Names of members of structs are PascalCase.
typedef struct Sample {
int FirstField;
bool SecondField;
Mode Mode;
State State;
struct Sample *Next;
} Sample;
// Function names are camelCase. Opening braces come at the end of the last
// line for the function declaration rather than on the next line.
bool sampleEqual(Sample *Self, Sample *Other) {
// Local variables are PascalCase.
if (Self == NULL && Other == NULL) {
return true;
}
if (Self == NULL || Other == NULL) {
return false;
}
// For statements that span multiple lines, break after the logical operator
// and align each line with the start of the first line.
if (Self->FirstField == Other->FirstField &&
Self->SecondField == Other->SecondField &&
Self->State == Other->State &&
Self->Mode == Other->Mode &&
Self->Next == Other->Next) {
return true;
}
// If the previous block ends with areturn (or break or continue), do not
// follow it with an else.
return false;
}
// For function declarations that span multiple lines, then align subsequent
// lines with the first parameter.
Sample *sampleNew(int FirstField,
bool SecondField,
Mode Mode,
State State,
Sample *Next) {
Sample *NewSample = (Sample *) malloc(sizeof(*NewSample));
if (NewSample == NULL) {
return NULL;
}
memset(NewSample, 0, sizeof(NewSample));
NewSample->FirstField = FirstField;
NewSample->SecondField = SecondField;
NewSample->Mode = Mode;
NewSample->State = State;
NewSample->Next = Next;
return Sample;
}
Sample *sampleClone(Sample *Self) {
if (Self == NULL) {
return NULL;
}
// For function calls that span multiple lines, align each subsequent line.
return sampleNew(Self->FirstField,
Self->SecondField,
Self->Mode,
Self->State,
Self->Next);
}
// For function declarations (and function calls) where you cannot fit
// the parameters with the first after the opening parentheses, then align
// the parameters indented four spaces on the next line:
static void sampleDoSomethingWithALongName(
Sample *Self,
int ParameterWithALongName,
bool AnotherParameter,
int AnotherParameter) {
if (Self == NULL) {
return;
}
// else and else if comes after the previous closing brace and not on the
// next line.
bool LocalVariable;
if (ParamterWithALongName == StateFoo) {
LocalVariable = true;
} else {
LocalVariable = false;
}
Self->FirstParameter += AnotherParameter;
Self->SecondParameter |= LocalVariable;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment