Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added formatting checks for C/C++ and python code #1348

Merged
merged 5 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bazelci/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,5 @@ tasks:
test_targets: *min_supported_targets

buildifier:
version: "6.1.0"
UebelAndre marked this conversation as resolved.
Show resolved Hide resolved
# keep this argument in sync with .pre-commit-config.yaml
warnings: "all"
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
BasedOnStyle: Google
IndentWidth: 4
...
34 changes: 34 additions & 0 deletions .github/workflows/formatting.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Formatting

on:
push:
branches:
- main
pull_request:
types:
- opened
- synchronize

jobs:
code-format-checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: DoozyX/[email protected]
with:
source: '.'
extensions: 'h,c,cc,cpp,proto,java'
clangFormatVersion: 14
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11
- name: Install dependencies
run: |
pip install 'black==24.10.0' 'isort==5.13.2'
jsharpe marked this conversation as resolved.
Show resolved Hide resolved
- name: Run black
run: |
python -m black --check --diff ./
- name: Run isort
run: |
python -m isort --profile=black --check-only ./
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
# CMake
cmake-build-*/

# Python
/*venv/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

Expand Down
11 changes: 5 additions & 6 deletions examples/cmake_android/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include <jni.h>

#include <string>

extern "C"
JNIEXPORT jstring
extern "C" JNIEXPORT jstring

JNICALL
Java_com_example_android_bazel_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
JNICALL
Java_com_example_android_bazel_MainActivity_stringFromJNI(
JNIEnv *env, jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
8 changes: 4 additions & 4 deletions examples/cmake_android/hello_lib-example.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <stdio.h>

#include "hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package examples.cmake_android.java.com.example.android.bazel;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

static {
System.loadLibrary("app");
}
Expand Down
34 changes: 16 additions & 18 deletions examples/cmake_android/zlib-example.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copied from https://gist.github.com/arq5x/5315739 for test purposes

#include <assert.h>
#include <stdio.h>
#include <string.h> // for strlen
#include <assert.h>

#include "zlib.h"

// adapted from: http://stackoverflow.com/questions/7540259/deflate-and-inflate-zlib-h-in-c
int main(int argc, char* argv[])
{
// adapted from:
// http://stackoverflow.com/questions/7540259/deflate-and-inflate-zlib-h-in-c
int main(int argc, char *argv[]) {
// original string len = 36
char a[50] = "Hello Hello Hello Hello Hello Hello!";

Expand All @@ -17,11 +18,9 @@ int main(int argc, char* argv[])
// placeholder for the UNcompressed (inflated) version of "b"
char c[50];


printf("Uncompressed size is: %lu\n", strlen(a));
printf("Uncompressed string is: %s\n", a);


printf("\n----------\n\n");

// STEP 1.
Expand All @@ -33,10 +32,11 @@ int main(int argc, char* argv[])
defstream.zfree = Z_NULL;
defstream.opaque = Z_NULL;
// setup "a" as the input and "b" as the compressed output
defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array
defstream.avail_in =
(uInt)strlen(a) + 1; // size of input, string + terminator
defstream.next_in = (Bytef *)a; // input char array
defstream.avail_out = (uInt)sizeof(b); // size of output
defstream.next_out = (Bytef *)b; // output char array

// the actual compression work.
deflateInit(&defstream, Z_BEST_COMPRESSION);
Expand All @@ -47,10 +47,8 @@ int main(int argc, char* argv[])
printf("Compressed size is: %lu\n", strlen(b));
printf("Compressed string is: %s\n", b);


printf("\n----------\n\n");


// STEP 2.
// inflate b into c
// zlib struct
Expand All @@ -59,10 +57,11 @@ int main(int argc, char* argv[])
infstream.zfree = Z_NULL;
infstream.opaque = Z_NULL;
// setup "b" as the input and "c" as the compressed output
infstream.avail_in = (uInt)((char*)defstream.next_out - b); // size of input
infstream.next_in = (Bytef *)b; // input char array
infstream.avail_out = (uInt)sizeof(c); // size of output
infstream.next_out = (Bytef *)c; // output char array
infstream.avail_in =
(uInt)((char *)defstream.next_out - b); // size of input
infstream.next_in = (Bytef *)b; // input char array
infstream.avail_out = (uInt)sizeof(c); // size of output
infstream.next_out = (Bytef *)c; // output char array

// the actual DE-compression work.
inflateInit(&infstream);
Expand All @@ -72,9 +71,8 @@ int main(int argc, char* argv[])
printf("Uncompressed size is: %lu\n", strlen(c));
printf("Uncompressed string is: %s\n", c);


// make sure uncompressed is exactly equal to original.
assert(strcmp(a,c)==0);
assert(strcmp(a, c) == 0);

return 0;
}
4 changes: 2 additions & 2 deletions examples/cmake_crosstool/hello.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
#include <iostream>

int main(int argc, char* argv[]) {
std::cout << "Hello! sqrt(time) = " << std::sqrt(time(NULL)) << std::endl;
return EXIT_SUCCESS;
std::cout << "Hello! sqrt(time) = " << std::sqrt(time(NULL)) << std::endl;
return EXIT_SUCCESS;
}
7 changes: 3 additions & 4 deletions examples/cmake_crosstool/static/hello_client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
7 changes: 3 additions & 4 deletions examples/cmake_crosstool/static/hello_client_version123.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "version123/hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
4 changes: 2 additions & 2 deletions examples/cmake_crosstool/static/src/hello.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "hello.h"

void hello_func(void) {
printf("Hello World!\n");
printf("Hello World!\n");

return;
return;
}
12 changes: 4 additions & 8 deletions examples/cmake_defines/lib_a/lib_a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
#define STR(x) #x
#pragma message "The value of __TIME__: " XSTR(__TIME__)

#define STATIC_ASSERT( condition, name )\
typedef char assert_failed_ ## name [ (condition) ? 1 : -1 ];
#define STATIC_ASSERT(condition, name) \
typedef char assert_failed_##name[(condition) ? 1 : -1];

void foo() {
STATIC_ASSERT(__TIME__ == "redacted", time_must_be_redacted);
}
void foo() { STATIC_ASSERT(__TIME__ == "redacted", time_must_be_redacted); }

// Should return "redacted"
const char *getBuildTime(void) {
return __TIME__;
}
const char *getBuildTime(void) { return __TIME__; }
4 changes: 1 addition & 3 deletions examples/cmake_hello_world_lib/binary/src/hello.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "hello.h"

void hello_func(char* name) {
printf("Hello, %s!", name);
}
void hello_func(char* name) { printf("Hello, %s!", name); }
7 changes: 3 additions & 4 deletions examples/cmake_hello_world_lib/binary/src/main.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hello.h"

int main(int argc, char* argv[])
{
hello_func(argv[1]);
return 0;
int main(int argc, char* argv[]) {
hello_func(argv[1]);
return 0;
}
7 changes: 3 additions & 4 deletions examples/cmake_hello_world_lib/shared/hello_client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
4 changes: 2 additions & 2 deletions examples/cmake_hello_world_lib/shared/src/hello.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "hello.h"

void hello_func(void) {
printf("Hello World!\n");
printf("Hello World!\n");

return;
return;
}
7 changes: 3 additions & 4 deletions examples/cmake_hello_world_lib/static/hello_client.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "version123/hello.h"

int main(int argc, char* argv[])
{
hello_func();
return 0;
int main(int argc, char* argv[]) {
hello_func();
return 0;
}
4 changes: 2 additions & 2 deletions examples/cmake_hello_world_lib/static/src/hello.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "hello.h"

void hello_func(void) {
printf("Hello World!\n");
printf("Hello World!\n");

return;
return;
}
4 changes: 1 addition & 3 deletions examples/cmake_synthetic/liba/src/liba.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "liba.h"

std::string hello_liba(void) {
return "Hello from LIBA!";
}
std::string hello_liba(void) { return "Hello from LIBA!"; }
1 change: 1 addition & 0 deletions examples/cmake_synthetic/liba/src/liba.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIBA_H_ (1)

#include <stdio.h>

#include <string>

std::string hello_liba(void);
Expand Down
4 changes: 1 addition & 3 deletions examples/cmake_synthetic/libb/src/libb.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "libb.h"

std::string hello_libb(void) {
return hello_liba() + " Hello from LIBB!";
}
std::string hello_libb(void) { return hello_liba() + " Hello from LIBB!"; }
2 changes: 2 additions & 0 deletions examples/cmake_synthetic/libb/src/libb.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define LIBB_H_ (1)

#include <stdio.h>

#include <string>

#include "liba.h"

std::string hello_libb(void);
Expand Down
2 changes: 1 addition & 1 deletion examples/cmake_synthetic/libc/src/libc.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "libc.h"

std::string hello_libc(void) {
return hello_liba() + " " + hello_libb() + " Hello from LIBC!";
return hello_liba() + " " + hello_libb() + " Hello from LIBC!";
}
2 changes: 2 additions & 0 deletions examples/cmake_synthetic/libc/src/libc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#define LIBC_H_ (1)

#include <stdio.h>

#include <string>

#include "liba.h"
#include "libb.h"

Expand Down
17 changes: 8 additions & 9 deletions examples/cmake_synthetic/test_libb.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#include "libb.h"

#include <iostream>
#include <stdexcept>
#include <string>

int main(int argc, char* argv[])
{
std:: string result = hello_libb();
if (result != "Hello from LIBA! Hello from LIBB!") {
throw std::runtime_error("Wrong result: " + result);
}
std::cout << "Everything's fine!";
#include "libb.h"

int main(int argc, char* argv[]) {
std::string result = hello_libb();
if (result != "Hello from LIBA! Hello from LIBB!") {
throw std::runtime_error("Wrong result: " + result);
}
std::cout << "Everything's fine!";
}
4 changes: 1 addition & 3 deletions examples/cmake_with_bazel_transitive/libb/src/libb.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "libb.h"

std::string hello_libb(void) {
return hello_liba() + " Hello from LIBB!";
}
std::string hello_libb(void) { return hello_liba() + " Hello from LIBB!"; }
Loading
Loading