diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000000..24d97f2b22 --- /dev/null +++ b/.clang-format @@ -0,0 +1,3 @@ +BasedOnStyle: Google +DerivePointerAlignment: false +PointerAlignment: Left diff --git a/.github/workflows/bazel_tests.yml b/.github/workflows/bazel_tests.yml index 062342aeea..b6e9fe24dd 100644 --- a/.github/workflows/bazel_tests.yml +++ b/.github/workflows/bazel_tests.yml @@ -1,4 +1,4 @@ -name: Bazel Tests +name: Check ClangFormat on: push: @@ -10,27 +10,11 @@ on: workflow_dispatch: jobs: - - ubuntu: - runs-on: ${{ matrix.os }} - - strategy: - fail-fast: false # Don't cancel all jobs if one fails. - matrix: - include: - - { CC: clang, os: ubuntu-20.04, flags: "" } - - { CC: clang, os: ubuntu-20.04, flags: "-c opt" } # Some warnings only fire with -c opt - - { CC: gcc, os: ubuntu-20.04, flags: "-c opt" } - - { CC: clang, os: ubuntu-20.04, flags: "--//:fasttable_enabled=true -- -cmake:test_generated_files" } - - { CC: clang, os: ubuntu-20.04, flags: "--config=asan -- -benchmarks:benchmark -python/..." } - - { CC: clang, os: macos-11, flags: "" } - + check_clang_format: + runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 - - name: Setup Python venv - run: rm -rf /tmp/venv && python3 -m venv /tmp/venv - - name: Install dependencies - run: sudo apt install -y ${{ matrix.install }} - if: matrix.install != '' - - name: Run tests - run: cd ${{ github.workspace }} && PATH=/tmp/venv/bin:$PATH CC=${{ matrix.CC }} bazel test --test_output=errors ... ${{ matrix.flags }} + - name: Run ClangFormat + run: find . | grep -E '\.(c|h|cc)$' | grep -E -v '^./(third_party|cmake)' | xargs clang-format -i + - name: Check for differences + run: git diff --exit-code diff --git a/tests/test_cpp.cc b/tests/test_cpp.cc index 36ea9a5307..cbc4b08aa0 100644 --- a/tests/test_cpp.cc +++ b/tests/test_cpp.cc @@ -35,11 +35,11 @@ #include #include +#include "google/protobuf/timestamp.upb.h" +#include "google/protobuf/timestamp.upbdefs.h" #include "gtest/gtest.h" #include "tests/test_cpp.upb.h" #include "tests/test_cpp.upbdefs.h" -#include "google/protobuf/timestamp.upb.h" -#include "google/protobuf/timestamp.upbdefs.h" #include "upb/def.h" #include "upb/def.hpp" #include "upb/json_decode.h" @@ -155,26 +155,30 @@ TEST(Cpp, TimestampEncoder) { upb::SymbolTable symtab; upb::Arena arena; upb::MessageDefPtr md(google_protobuf_Timestamp_getmsgdef(symtab.ptr())); - google_protobuf_Timestamp* timestamp_upb = google_protobuf_Timestamp_new(arena.ptr()); - google_protobuf_Timestamp* timestamp_upb_decoded = google_protobuf_Timestamp_new(arena.ptr()); - + google_protobuf_Timestamp* timestamp_upb = + google_protobuf_Timestamp_new(arena.ptr()); + google_protobuf_Timestamp* timestamp_upb_decoded = + google_protobuf_Timestamp_new(arena.ptr()); long timestamps[] = { - 253402300799, // 9999-12-31T23:59:59Z - 1641006000, // 2022-01-01T03:00:00Z - 0, // 1970-01-01T00:00:00Z - -31525200, // 1969-01-01T03:00:00Z - -2208988800, // 1900-01-01T00:00:00Z - -62135596800, // 0000-01-01T00:00:00Z + 253402300799, // 9999-12-31T23:59:59Z + 1641006000, // 2022-01-01T03:00:00Z + 0, // 1970-01-01T00:00:00Z + -31525200, // 1969-01-01T03:00:00Z + -2208988800, // 1900-01-01T00:00:00Z + -62135596800, // 0000-01-01T00:00:00Z }; - for(long timestamp: timestamps) { + for (long timestamp : timestamps) { google_protobuf_Timestamp_set_seconds(timestamp_upb, timestamp); char json[128]; - size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), NULL, 0, json, sizeof(json), NULL); - bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(), NULL, 0, arena.ptr(), NULL); - const long timestamp_decoded = google_protobuf_Timestamp_seconds(timestamp_upb_decoded); + size_t size = upb_JsonEncode(timestamp_upb, md.ptr(), NULL, 0, json, + sizeof(json), NULL); + bool result = upb_JsonDecode(json, size, timestamp_upb_decoded, md.ptr(), + NULL, 0, arena.ptr(), NULL); + const long timestamp_decoded = + google_protobuf_Timestamp_seconds(timestamp_upb_decoded); ASSERT_TRUE(result); EXPECT_EQ(timestamp, timestamp_decoded); diff --git a/tests/test_generated_code.cc b/tests/test_generated_code.cc index cef9598ccd..4f3d715e0b 100644 --- a/tests/test_generated_code.cc +++ b/tests/test_generated_code.cc @@ -296,12 +296,14 @@ static void check_int32_map_one_entry( EXPECT_EQ( 1, protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_size( msg)); - EXPECT_TRUE(protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get( - msg, test_int32, &val)); + EXPECT_TRUE( + protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get( + msg, test_int32, &val)); EXPECT_EQ(val, test_int32_2); - EXPECT_FALSE(protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get( - msg, test_int32_3, &val)); + EXPECT_FALSE( + protobuf_test_messages_proto3_TestAllTypesProto3_map_int32_int32_get( + msg, test_int32_3, &val)); /* Test that iteration reveals a single k/v pair in the map. */ iter = kUpb_Map_Begin; diff --git a/tests/test_table.cc b/tests/test_table.cc index 5ba2276438..e1a1d40733 100644 --- a/tests/test_table.cc +++ b/tests/test_table.cc @@ -460,7 +460,7 @@ TEST_P(IntTableTest, TestIntTable) { uint32_t largest_key = 0; std::map m; std::unordered_map hm; - for (const auto& key: keys_) { + for (const auto& key : keys_) { largest_key = UPB_MAX((int32_t)largest_key, key); table.Insert(key, key * 2); m[key] = key * 2; diff --git a/upb/json_encode.c b/upb/json_encode.c index a247078c73..e0b35f1320 100644 --- a/upb/json_encode.c +++ b/upb/json_encode.c @@ -162,7 +162,7 @@ static void jsonenc_timestamp(jsonenc* e, const upb_Message* msg, * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for * Processing Calendar Dates," Communications of the Association of * Computing Machines, vol. 11 (1968), p. 657. */ - seconds += 62135596800; // Ensure seconds is positive. + seconds += 62135596800; // Ensure seconds is positive. L = (int)(seconds / 86400) - 719162 + 68569 + 2440588; N = 4 * L / 146097; L = L - (146097 * N + 3) / 4; diff --git a/upb/upb.c b/upb/upb.c index a702d48e55..55730bbe82 100644 --- a/upb/upb.c +++ b/upb/upb.c @@ -341,7 +341,6 @@ static void upb_FixLocale(char* p) { } } - void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size) { assert(size >= kUpb_RoundTripBufferSize); snprintf(buf, size, "%.*g", DBL_DIG, val); diff --git a/upb/upb_internal.h b/upb/upb_internal.h index d14a175836..1eb166f7d3 100644 --- a/upb/upb_internal.h +++ b/upb/upb_internal.h @@ -61,9 +61,7 @@ struct upb_Arena { // the beginning. // // The given buffer size must be at least kUpb_RoundTripBufferSize. -enum { - kUpb_RoundTripBufferSize = 32 -}; +enum { kUpb_RoundTripBufferSize = 32 }; void _upb_EncodeRoundTripDouble(double val, char* buf, size_t size); void _upb_EncodeRoundTripFloat(float val, char* buf, size_t size);