Skip to content

Commit

Permalink
Merge pull request #508 from akheron/clang-format
Browse files Browse the repository at this point in the history
Format code with clang-format
  • Loading branch information
akheron committed Oct 21, 2019
2 parents 79075d5 + 78da356 commit d288cc1
Show file tree
Hide file tree
Showing 47 changed files with 2,441 additions and 2,832 deletions.
5 changes: 5 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
BasedOnStyle: LLVM
AlignConsecutiveMacros: true
ColumnLimit: 90
IndentCaseLabels: true
IndentWidth: 4
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ stamp-h1
*.exe
.idea
cmake-build-debug/
*.log
*.trs
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
env:
global:
- CLANG_FORMAT_VERSION=9
matrix:
- JANSSON_BUILD_METHOD=cmake JANSSON_CMAKE_OPTIONS="-DJANSSON_TEST_WITH_VALGRIND=ON" JANSSON_EXTRA_INSTALL="valgrind"
- JANSSON_BUILD_METHOD=autotools
- JANSSON_BUILD_METHOD=coverage JANSSON_CMAKE_OPTIONS="-DJANSSON_COVERAGE=ON -DJANSSON_COVERALLS=ON -DCMAKE_BUILD_TYPE=Debug" JANSSON_EXTRA_INSTALL="lcov curl"
- JANSSON_BUILD_METHOD=fuzzer
- JANSSON_BUILD_METHOD=lint CLANG_FORMAT=clang-format-9
dist: bionic
language: c
compiler:
- gcc
Expand All @@ -14,13 +18,17 @@ matrix:
env: JANSSON_BUILD_METHOD=coverage JANSSON_CMAKE_OPTIONS="-DJANSSON_COVERAGE=ON -DJANSSON_COVERALLS=ON -DCMAKE_BUILD_TYPE=Debug" JANSSON_EXTRA_INSTALL="lcov curl"
- compiler: clang
env: JANSSON_BUILD_METHOD=fuzzer
- compiler: gcc
env: JANSSON_BUILD_METHOD=lint CLANG_FORMAT=clang-format-9
allow_failures:
- env: JANSSON_BUILD_METHOD=coverage JANSSON_CMAKE_OPTIONS="-DJANSSON_COVERAGE=ON -DJANSSON_COVERALLS=ON -DCMAKE_BUILD_TYPE=Debug" JANSSON_EXTRA_INSTALL="lcov curl"
install:
- sudo apt-get update -qq
- sudo apt-get install -y -qq cmake $JANSSON_EXTRA_INSTALL
- if [ "$TRAVIS_COMPILER" = "clang" ]; then sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y && wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | sudo apt-key add - && sudo apt-add-repository "deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-9 main" -y && sudo apt-get install -y -qq clang-9 clang-format-9; fi
script:
- if [ "$JANSSON_BUILD_METHOD" = "autotools" ]; then autoreconf -f -i && CFLAGS=-Werror ./configure && make check; fi
- if [ "$JANSSON_BUILD_METHOD" = "cmake" ]; then mkdir build && cd build && cmake $JANSSON_CMAKE_OPTIONS .. && cmake --build . && ctest --output-on-failure; fi
- if [ "$JANSSON_BUILD_METHOD" = "coverage" ]; then mkdir build && cd build && cmake $JANSSON_CMAKE_OPTIONS .. && cmake --build . && cmake --build . --target coveralls; fi
- if [ "$JANSSON_BUILD_METHOD" = "fuzzer" ]; then ./test/ossfuzz/travisoss.sh; fi
- if [ "$JANSSON_BUILD_METHOD" = "lint" ]; then ./scripts/clang-format-check; fi
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ dvi:

pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = jansson.pc

TESTS = scripts/clang-format-check
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AC_PREREQ([2.60])
AC_INIT([jansson], [2.12], [petri@digip.org])
AC_INIT([jansson], [2.12], [https://github.com/akheron/jansson/issues])

AC_CONFIG_AUX_DIR([.])
AM_INIT_AUTOMAKE([1.10 foreign])
Expand Down
79 changes: 29 additions & 50 deletions doc/github_commits.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,33 @@
#include <stdlib.h>
#include <string.h>

#include <jansson.h>
#include <curl/curl.h>
#include <jansson.h>

#define BUFFER_SIZE (256 * 1024) /* 256 KB */
#define BUFFER_SIZE (256 * 1024) /* 256 KB */

#define URL_FORMAT "https://api.github.com/repos/%s/%s/commits"
#define URL_SIZE 256
#define URL_FORMAT "https://api.github.com/repos/%s/%s/commits"
#define URL_SIZE 256

/* Return the offset of the first newline in text or the length of
text if there's no newline */
static int newline_offset(const char *text)
{
static int newline_offset(const char *text) {
const char *newline = strchr(text, '\n');
if(!newline)
if (!newline)
return strlen(text);
else
return (int)(newline - text);
}

struct write_result
{
struct write_result {
char *data;
int pos;
};

static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
{
static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) {
struct write_result *result = (struct write_result *)stream;

if(result->pos + size * nmemb >= BUFFER_SIZE - 1)
{
if (result->pos + size * nmemb >= BUFFER_SIZE - 1) {
fprintf(stderr, "error: too small buffer\n");
return 0;
}
Expand All @@ -49,8 +45,7 @@ static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream)
return size * nmemb;
}

static char *request(const char *url)
{
static char *request(const char *url) {
CURL *curl = NULL;
CURLcode status;
struct curl_slist *headers = NULL;
Expand All @@ -59,17 +54,14 @@ static char *request(const char *url)

curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(!curl)
if (!curl)
goto error;

data = malloc(BUFFER_SIZE);
if(!data)
if (!data)
goto error;

struct write_result write_result = {
.data = data,
.pos = 0
};
struct write_result write_result = {.data = data, .pos = 0};

curl_easy_setopt(curl, CURLOPT_URL, url);

Expand All @@ -81,16 +73,14 @@ static char *request(const char *url)
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result);

status = curl_easy_perform(curl);
if(status != 0)
{
if (status != 0) {
fprintf(stderr, "error: unable to request data from %s:\n", url);
fprintf(stderr, "%s\n", curl_easy_strerror(status));
goto error;
}

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if(code != 200)
{
if (code != 200) {
fprintf(stderr, "error: server responded with code %ld\n", code);
goto error;
}
Expand All @@ -105,27 +95,25 @@ static char *request(const char *url)
return data;

error:
if(data)
if (data)
free(data);
if(curl)
if (curl)
curl_easy_cleanup(curl);
if(headers)
if (headers)
curl_slist_free_all(headers);
curl_global_cleanup();
return NULL;
}

int main(int argc, char *argv[])
{
int main(int argc, char *argv[]) {
size_t i;
char *text;
char url[URL_SIZE];

json_t *root;
json_error_t error;

if(argc != 3)
{
if (argc != 3) {
fprintf(stderr, "usage: %s USER REPOSITORY\n\n", argv[0]);
fprintf(stderr, "List commits at USER's REPOSITORY.\n\n");
return 2;
Expand All @@ -134,65 +122,56 @@ int main(int argc, char *argv[])
snprintf(url, URL_SIZE, URL_FORMAT, argv[1], argv[2]);

text = request(url);
if(!text)
if (!text)
return 1;

root = json_loads(text, 0, &error);
free(text);

if(!root)
{
if (!root) {
fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
return 1;
}

if(!json_is_array(root))
{
if (!json_is_array(root)) {
fprintf(stderr, "error: root is not an array\n");
json_decref(root);
return 1;
}

for(i = 0; i < json_array_size(root); i++)
{
for (i = 0; i < json_array_size(root); i++) {
json_t *data, *sha, *commit, *message;
const char *message_text;

data = json_array_get(root, i);
if(!json_is_object(data))
{
if (!json_is_object(data)) {
fprintf(stderr, "error: commit data %d is not an object\n", (int)(i + 1));
json_decref(root);
return 1;
}

sha = json_object_get(data, "sha");
if(!json_is_string(sha))
{
if (!json_is_string(sha)) {
fprintf(stderr, "error: commit %d: sha is not a string\n", (int)(i + 1));
return 1;
}

commit = json_object_get(data, "commit");
if(!json_is_object(commit))
{
if (!json_is_object(commit)) {
fprintf(stderr, "error: commit %d: commit is not an object\n", (int)(i + 1));
json_decref(root);
return 1;
}

message = json_object_get(commit, "message");
if(!json_is_string(message))
{
if (!json_is_string(message)) {
fprintf(stderr, "error: commit %d: message is not a string\n", (int)(i + 1));
json_decref(root);
return 1;
}

message_text = json_string_value(message);
printf("%.8s %.*s\n",
json_string_value(sha),
newline_offset(message_text),
printf("%.8s %.*s\n", json_string_value(sha), newline_offset(message_text),
message_text);
}

Expand Down
67 changes: 32 additions & 35 deletions examples/simple_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <jansson.h>
#include <stdio.h>
#include <stdlib.h>
#include <jansson.h>

/* forward refs */
void print_json(json_t *root);
Expand All @@ -40,49 +40,47 @@ void print_json_true(json_t *element, int indent);
void print_json_false(json_t *element, int indent);
void print_json_null(json_t *element, int indent);

void print_json(json_t *root) {
print_json_aux(root, 0);
}
void print_json(json_t *root) { print_json_aux(root, 0); }

void print_json_aux(json_t *element, int indent) {
switch (json_typeof(element)) {
case JSON_OBJECT:
print_json_object(element, indent);
break;
case JSON_ARRAY:
print_json_array(element, indent);
break;
case JSON_STRING:
print_json_string(element, indent);
break;
case JSON_INTEGER:
print_json_integer(element, indent);
break;
case JSON_REAL:
print_json_real(element, indent);
break;
case JSON_TRUE:
print_json_true(element, indent);
break;
case JSON_FALSE:
print_json_false(element, indent);
break;
case JSON_NULL:
print_json_null(element, indent);
break;
default:
fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
case JSON_OBJECT:
print_json_object(element, indent);
break;
case JSON_ARRAY:
print_json_array(element, indent);
break;
case JSON_STRING:
print_json_string(element, indent);
break;
case JSON_INTEGER:
print_json_integer(element, indent);
break;
case JSON_REAL:
print_json_real(element, indent);
break;
case JSON_TRUE:
print_json_true(element, indent);
break;
case JSON_FALSE:
print_json_false(element, indent);
break;
case JSON_NULL:
print_json_null(element, indent);
break;
default:
fprintf(stderr, "unrecognized JSON type %d\n", json_typeof(element));
}
}

void print_json_indent(int indent) {
int i;
for (i = 0; i < indent; i++) { putchar(' '); }
for (i = 0; i < indent; i++) {
putchar(' ');
}
}

const char *json_plural(int count) {
return count == 1 ? "" : "s";
}
const char *json_plural(int count) { return count == 1 ? "" : "s"; }

void print_json_object(json_t *element, int indent) {
size_t size;
Expand All @@ -98,7 +96,6 @@ void print_json_object(json_t *element, int indent) {
printf("JSON Key: \"%s\"\n", key);
print_json_aux(value, indent + 2);
}

}

void print_json_array(json_t *element, int indent) {
Expand Down
3 changes: 3 additions & 0 deletions scripts/clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

find . -type f -a '(' -name '*.c' -o -name '*.h' ')' | xargs clang-format -i
27 changes: 27 additions & 0 deletions scripts/clang-format-check
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

CLANG_FORMAT=${CLANG_FORMAT:-clang-format}
CLANG_FORMAT_VERSION=${CLANG_FORMAT_VERSION:-}

if ! type $CLANG_FORMAT >/dev/null || \
! $CLANG_FORMAT --version | grep -q "version ${CLANG_FORMAT_VERSION}"; then
# If running tests, mark this test as skipped.
exit 77
fi

errors=0
paths=$(git ls-files | grep '\.[ch]$')
for path in $paths; do
in=$(cat $path)
out=$($CLANG_FORMAT $path)

if [ "$in" != "$out" ]; then
diff -u -L $path -L "$path.formatted" $path - <<<$out
errors=1
fi
done

if [ $errors -ne 0 ]; then
echo "Formatting errors detected, run ./scripts/clang-format to fix!"
exit 1
fi

0 comments on commit d288cc1

Please sign in to comment.