Skip to content

Commit

Permalink
testdata: add .clang-format
Browse files Browse the repository at this point in the history
Enforce a common formatting across C code.
  • Loading branch information
lmb committed Nov 5, 2020
1 parent 0b54ddf commit 7dc8214
Show file tree
Hide file tree
Showing 23 changed files with 51 additions and 29 deletions.
17 changes: 17 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
Language: Cpp
BasedOnStyle: LLVM
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: true
AlignEscapedNewlines: DontAlign
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortFunctionsOnASingleLine: false
BreakBeforeBraces: Attach
IndentWidth: 4
KeepEmptyLinesAtTheStartOfBlocks: false
TabWidth: 4
UseTab: ForContinuationAndIndentation
ColumnLimit: 1000
...
2 changes: 1 addition & 1 deletion cmd/bpf2go/example_bpfeb.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cmd/bpf2go/example_bpfel.go

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion cmd/bpf2go/testdata/minimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

char __license[] __attribute__((section("license"), used)) = "MIT";

__attribute__((section("socket"), used)) int filter() { return 0; }
__attribute__((section("socket"), used)) int filter() {
return 0;
}
10 changes: 6 additions & 4 deletions testdata/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ typedef unsigned int uint32_t;
typedef unsigned long uint64_t;

#define __section(NAME) __attribute__((section(NAME), used))
#define __uint(name, val) int (*name)[val]
#define __uint(name, val) int(*name)[val]
#define __type(name, val) typeof(val) *name

#define BPF_MAP_TYPE_ARRAY (1)
Expand All @@ -24,6 +24,8 @@ struct bpf_map_def {
unsigned int map_flags;
};

static void* (*map_lookup_elem)(const void *map, const void *key) = (void*)1;
static int (*perf_event_output)(const void *ctx, const void *map, uint64_t index, const void *data, uint64_t size) = (void*)25;
static uint32_t (*get_smp_processor_id)(void) = (void*)8;
static void *(*map_lookup_elem)(const void *map, const void *key) = (void *)1;

static int (*perf_event_output)(const void *ctx, const void *map, uint64_t index, const void *data, uint64_t size) = (void *)25;

static uint32_t (*get_smp_processor_id)(void) = (void *)8;
Binary file modified testdata/invalid_map-eb.elf
Binary file not shown.
Binary file modified testdata/invalid_map-el.elf
Binary file not shown.
15 changes: 8 additions & 7 deletions testdata/invalid_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ struct {
struct bpf_map_def def;
uint32_t dummy;
} invalid_map __section("maps") = {
.def = {
.type = BPF_MAP_TYPE_ARRAY,
.key_size = 4,
.value_size = 2,
.max_entries = 1,
},
.dummy = 1,
.def =
{
.type = BPF_MAP_TYPE_ARRAY,
.key_size = 4,
.value_size = 2,
.max_entries = 1,
},
.dummy = 1,
};
Binary file modified testdata/loader-clang-6.0-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-6.0-el.elf
Binary file not shown.
Binary file modified testdata/loader-clang-7-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-7-el.elf
Binary file not shown.
Binary file modified testdata/loader-clang-8-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-8-el.elf
Binary file not shown.
Binary file modified testdata/loader-clang-9-eb.elf
Binary file not shown.
Binary file modified testdata/loader-clang-9-el.elf
Binary file not shown.
26 changes: 13 additions & 13 deletions testdata/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct bpf_map_def hash_map __section("maps") = {
.key_size = 4,
.value_size = 2,
.max_entries = 1,
.map_flags = 0,
.map_flags = 0,
};

struct bpf_map_def hash_map2 __section("maps") = {
Expand All @@ -22,14 +22,14 @@ struct bpf_map_def hash_map2 __section("maps") = {
};

struct bpf_map_def array_of_hash_map __section("maps") = {
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
.key_size = sizeof(uint32_t),
.type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
.key_size = sizeof(uint32_t),
.max_entries = 2,
};

struct bpf_map_def hash_of_hash_map __section("maps") = {
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.key_size = sizeof(uint32_t),
.type = BPF_MAP_TYPE_HASH_OF_MAPS,
.key_size = sizeof(uint32_t),
.max_entries = 2,
};

Expand Down Expand Up @@ -60,30 +60,30 @@ int __attribute__((noinline)) global_fn2(uint32_t arg) {
}

int __attribute__((noinline)) __section("other") global_fn3(uint32_t arg) {
return arg+1;
return arg + 1;
}

int __attribute__((noinline)) global_fn(uint32_t arg) {
return static_fn(arg) + global_fn2(arg) + global_fn3(arg);
}

#if __clang_major__ >= 9
static volatile unsigned int key1 = 0; // .bss
static volatile unsigned int key2 = 1; // .data
static volatile unsigned int key1 = 0; // .bss
static volatile unsigned int key2 = 1; // .data
static volatile const unsigned int key3 = 2; // .rodata
static volatile const uint32_t arg; // .rodata, rewritten by loader
static volatile const uint32_t arg; // .rodata, rewritten by loader
#endif

__section("xdp") int xdp_prog() {
#if __clang_major__ < 9
unsigned int key1 = 0;
unsigned int key2 = 1;
unsigned int key3 = 2;
uint32_t arg = 1;
uint32_t arg = 1;
#endif
map_lookup_elem(&hash_map, (void*)&key1);
map_lookup_elem(&hash_map2, (void*)&key2);
map_lookup_elem(&hash_map2, (void*)&key3);
map_lookup_elem(&hash_map, (void *)&key1);
map_lookup_elem(&hash_map2, (void *)&key2);
map_lookup_elem(&hash_map2, (void *)&key3);
return static_fn(arg) + global_fn(arg);
}

Expand Down
Binary file modified testdata/raw_tracepoint-eb.elf
Binary file not shown.
Binary file modified testdata/raw_tracepoint-el.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion testdata/raw_tracepoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ struct bpf_args {
};

__section("raw_tracepoint/sched_process_exec") int sched_process_exec(struct bpf_args *ctx) {
return 0;
return 0;
}
Binary file modified testdata/rewrite-eb.elf
Binary file not shown.
Binary file modified testdata/rewrite-el.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion testdata/rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ __section("socket") int rewrite() {
}

__section("socket/map") int rewrite_map() {
unsigned int key = 0;
unsigned int key = 0;
unsigned int *value = map_lookup_elem(&map_val, &key);
if (!value) {
return 0;
Expand Down

0 comments on commit 7dc8214

Please sign in to comment.