Skip to content

Commit

Permalink
fix warnings in CI, as well as bugs (#460)
Browse files Browse the repository at this point in the history
* fix warnings in CI, as well as bugs
  • Loading branch information
lihuiba authored Apr 22, 2024
1 parent 83a3f39 commit 1db53d9
Show file tree
Hide file tree
Showing 68 changed files with 344 additions and 307 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ endif ()
if (PHOTON_BUILD_TESTING)
include(CTest)

add_compile_options(-Wno-error)
# add_compile_options(-Wno-error)

add_library(ci-tools STATIC test/ci-tools.cpp)

Expand Down
3 changes: 3 additions & 0 deletions common/PMF.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ struct pmf_map
T* obj; // may be adjusted for virtual function call
};

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
template<typename PF, typename T, typename MF>
inline auto __get_mfa__(T* obj, MF f)
-> pmf_map<PF, T>
Expand Down Expand Up @@ -65,6 +67,7 @@ inline auto __get_mfa__(T* obj, MF f)
auto addr = pmf.get_function_address((void*&)obj);
return pmf_map<PF, T>{(PF)addr, obj};
}
#pragma GCC diagnostic pop

template<typename T, typename R, typename...ARGS>
inline auto get_member_function_address(T* obj, R (T::*f)(ARGS...))
Expand Down
3 changes: 1 addition & 2 deletions common/checksum/test/test_checksum.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <gtest/gtest.h>
#include <photon/common/checksum/crc32c.h>

#include <chrono>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "../../../test/gtest.h"

#ifndef DATA_DIR
#define DATA_DIR ""
Expand Down
4 changes: 2 additions & 2 deletions common/estring.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ class estring_view : public std::string_view
bool to_double_check(double* v = nullptr)
{
char buf[32];
auto len = std::max(this->size(), sizeof(buf) - 1 );
auto len = std::min(this->size(), sizeof(buf) - 1 );
memcpy(buf, data(), len);
buf[len] = '0';
buf[len] = '\0';
return sscanf(buf, "%lf", v) == 1;
}
double to_double(double default_val = NAN)
Expand Down
3 changes: 1 addition & 2 deletions common/executor/test/test_async.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ limitations under the License.
*/

#include <fcntl.h>
#include <gtest/gtest.h>
#include <photon/common/alog.h>
#include <photon/common/executor/executor.h>
#include <photon/common/utility.h>
Expand All @@ -25,9 +24,9 @@ limitations under the License.
#include <photon/thread/thread.h>
#include <sched.h>
#include <immintrin.h>

#include <chrono>
#include <thread>
#include "../../../test/gtest.h"

using namespace photon;

Expand Down
3 changes: 1 addition & 2 deletions common/executor/test/test_easy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ limitations under the License.
*/

#include <fcntl.h>
#include <gtest/gtest.h>

#include <photon/common/alog.h>
#include <photon/fs/filesystem.h>
#include <photon/fs/localfs.h>
#include <photon/common/utility.h>
#include <photon/common/executor/executor.h>
#include <photon/common/executor/easyawaiter.h>
#include <photon/thread/thread.h>
#include "../../test/gtest.h"

using namespace photon;

Expand Down
3 changes: 1 addition & 2 deletions common/executor/test/test_easyexport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ limitations under the License.
*/

#include <fcntl.h>
#include <gtest/gtest.h>

#include <photon/fs/async_filesystem.h>
#include <photon/fs/exportfs.h>
#include <photon/fs/filesystem.h>
Expand All @@ -25,6 +23,7 @@ limitations under the License.
#include <photon/thread/thread.h>
#include <photon/common/executor/executor.h>
#include <photon/common/executor/easyawaiter.h>
#include "../../../test/gtest.h"

using namespace photon;

Expand Down
3 changes: 1 addition & 2 deletions common/executor/test/test_export_as_executor.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#include <gtest/gtest.h>
#include <photon/common/executor/executor.h>
#include <photon/common/utility.h>
#include <photon/common/alog.h>
#include <photon/photon.h>
#include <photon/thread/thread.h>

#include <thread>
#include "../../test/gtest.h"

TEST(enter_as_executor, test) {
// set global default logger output to null
Expand Down
3 changes: 1 addition & 2 deletions common/executor/test/test_std.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ limitations under the License.
*/

#include <fcntl.h>
#include <gtest/gtest.h>

#include <photon/common/alog.h>
#include <photon/fs/exportfs.h>
#include <photon/fs/filesystem.h>
#include <photon/fs/localfs.h>
#include <photon/common/utility.h>
#include <photon/common/executor/executor.h>
#include "../../../test/gtest.h"

using namespace photon;

Expand Down
4 changes: 2 additions & 2 deletions common/generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ inline void ___example_of_generator____()
};

{
int i = 0;
int i = 0; (void)i;
for (auto x: example_generator1(10))
{
assert(x == i); i++;
Expand Down Expand Up @@ -198,7 +198,7 @@ inline void ___example_of_generator____()
};

{
int i = 0;
int i = 0; (void)i;
for (auto x: example_generator2(10))
{
assert(x == i); i++;
Expand Down
2 changes: 1 addition & 1 deletion common/iovector.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,8 @@ class iovector
}
void copy(IOAlloc* rhs, uint16_t nbases)
{
*(IOAlloc*)this = *rhs;
auto rhs_ = (IOVAllocation_*)rhs;
*this = *rhs_;
memcpy(bases, rhs_->bases, sizeof(bases[0]) * nbases);
}
};
Expand Down
2 changes: 1 addition & 1 deletion common/lockfree_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ class LockfreeSPSCRingQueue : public LockfreeRingQueueBase<T, N> {
n = std::min(n, tail.load(std::memory_order_acquire) - h);
if (n == 0) return 0;
auto first_idx = idx(h);
auto last_idx = idx(h + n - 1);
auto last_idx = idx(h + n - 1); (void)last_idx;
auto part_length = Base::capacity - first_idx;
if (likely(part_length >= n)) {
memcpy(x, &slots[first_idx], sizeof(T) * n);
Expand Down
2 changes: 1 addition & 1 deletion common/memory-stream/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ limitations under the License.
#include <memory>
#include <algorithm>
#include <string>
#include <gtest/gtest.h>
#include <photon/thread/thread.h>
#include <photon/common/utility.h>
#include <photon/common/alog.h>
#include <photon/common/alog-stdstring.h>
#include "../../../test/gtest.h"

using namespace std;
using namespace photon;
Expand Down
12 changes: 9 additions & 3 deletions common/range-lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class RangeLock
it = m_index.emplace_hint(it, r);
assert(it != m_index.end());
static_assert(sizeof(it) == sizeof(LockHandle*), "...");
return (LockHandle*&)it;
return __reinterpret_cast<LockHandle*>(it);
}
}

Expand All @@ -87,7 +87,7 @@ class RangeLock
if (!h) return -1;
range_t r1(offset, length);
SCOPED_LOCK(m_lock);
auto it = (iterator&)h;
auto it = __reinterpret_cast<iterator>(h);
auto r0 = (range_t*) &*it;
if ((r1.offset < r0->offset && r1.offset < prev_end(it)) ||
(r1.end() > it->end() && r1.end() > next_offset(it)))
Expand All @@ -100,7 +100,7 @@ class RangeLock
void unlock(LockHandle* h)
{
SCOPED_LOCK(m_lock);
auto it = (iterator&)h;
auto it = __reinterpret_cast<iterator>(h);
m_index.erase(it);
}

Expand Down Expand Up @@ -147,6 +147,12 @@ class RangeLock
{
return (it == m_index.begin()) ? 0 : (--it)->end();
}
template<typename T, typename P>
T& __reinterpret_cast(P& x) {
static_assert(sizeof(P) == sizeof(T), "...");
auto y = (T*)&x;
return *y;
}
};

class ScopedRangeLock
Expand Down
2 changes: 1 addition & 1 deletion common/stream-messenger/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ limitations under the License.
#include <memory>
#include <algorithm>
#include <string>
#include <gtest/gtest.h>
#include <photon/thread/thread.h>
#include <photon/common/memory-stream/memory-stream.h>
#include <photon/common/utility.h>
#include <photon/common/alog.h>
#include "../../../test/gtest.h"
using namespace std;
using namespace photon;
using namespace StreamMessenger;
Expand Down
27 changes: 10 additions & 17 deletions common/string-keyed.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ class basic_map_string_kv : public M
mutable bool _has_val = false;

iterator(base_it b_it) : _b_it(b_it) { }
iterator(typename base::const_iterator b_it) {
auto x = (base_it*) &b_it;
_b_it = *x;
}

mutable_value_type& _init_val() const {
if (_has_val) return _val;
Expand Down Expand Up @@ -321,11 +325,9 @@ class basic_map_string_kv : public M
{
return base::begin();
}
using B_IT = typename base::iterator;
const_iterator end() const
{
auto it = base::end();
return (B_IT&)it;
return {base::end()};
}
iterator end()
{
Expand Down Expand Up @@ -353,22 +355,19 @@ class basic_map_string_kv : public M
}
const_iterator find ( const key_type& k ) const
{
auto it = base::find((const skvm&)k);
return (B_IT&)it;
return {base::find((const skvm&)k)};
}
iterator find ( const key_type& k )
{
auto it = base::find((const skvm&)k);
return (B_IT&)it;
return {base::find((const skvm&)k)};
}
size_type count ( const key_type& k ) const
{
return base::count((const skvm&)k);
}
std::pair<const_iterator,const_iterator> equal_range ( const key_type& k ) const
{
auto r = base::equal_range((const skvm&)k);
return {(B_IT&) r.first, (B_IT&) r.second};
return {base::equal_range((const skvm&)k)};
}
std::pair<iterator, bool> emplace (const key_type& k, const mapped_type& v )
{
Expand Down Expand Up @@ -437,18 +436,12 @@ class map_string_kv : public basic_map_string_kv<std::map<skvm, size_t>> {
{
return base::upper_bound((const skvm&)k);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
using typename base::B_IT;
const_iterator lower_bound (const key_type& k) const
{
auto it = base::lower_bound((const skvm&)k);
return (B_IT&)it;
return {base::lower_bound((const skvm&)k)};
}
const_iterator upper_bound (const key_type& k) const
{
auto it = base::upper_bound((const skvm&)k);
return (B_IT&)it;
return {base::upper_bound((const skvm&)k)};
}
#pragma GCC diagnostic pop
};
20 changes: 10 additions & 10 deletions common/test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ limitations under the License.
#include <memory>
#include <string>
//#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <gtest/gtest-spi.h>
//#include <malloc.h>
#ifndef __clang__
#include <gnu/libc-version.h>
#endif
#include "../../test/gtest.h"

using namespace std;

Expand Down Expand Up @@ -872,8 +871,8 @@ TEST(estring, test)
EXPECT_EQ(estring_view("234423").to_uint64(), 234423);
EXPECT_EQ(estring_view("-234423").to_int64(), -234423);
EXPECT_EQ(estring_view("asfdsf").to_uint64(32), 32);
EXPECT_EQ(estring_view("-3.14").to_double(), -3.14);
EXPECT_EQ(estring_view("1e10").to_double(), 1e10);
EXPECT_NEAR(estring_view("-3.14").to_double(), -3.14, 1e-5);
EXPECT_NEAR(estring_view("1e10").to_double(), 1e10, 1e-5);

EXPECT_EQ(estring_view("1").hex_to_uint64(), 0x1);
EXPECT_EQ(estring_view("1a2b3d4e5f").hex_to_uint64(), 0x1a2b3d4e5f);
Expand Down Expand Up @@ -920,7 +919,8 @@ TEST(retval, basic) {
EXPECT_EQ(rvs[2], -1234);
EXPECT_EQ(rvs[3], -5234);

for (int i = 0; i < LEN(rvs); ++i) {
for (auto i: xrange(LEN(rvs))) {
static_assert(std::is_same<decltype(i), size_t>::value, "...");
auto ret = foo(i);
LOG_DEBUG("got ", ret);
EXPECT_EQ(ret, rvs[i]);
Expand Down Expand Up @@ -954,15 +954,15 @@ void basic_map_test(T &test_map) {
char xname[1000];
auto p = test_map.begin();
for (int i = 200000; i < 300000; i++) if (i % 2 == 0) {
sprintf(xname, "%s%d", prefix.c_str(), i);
snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i);
auto s = std::string_view(xname).substr(prefix.size());
test_map.insert(p, make_pair(xname, s));
ASSERT_EQ(test_map.size(), i/2+1);
}

// LOG_DEBUG("asdf");
for (int i = 300000; i < 400000; i++) if (i % 2 == 0) {
sprintf(xname, "%s%d", prefix.c_str(), i);
snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i);
auto s = std::string_view(xname).substr(prefix.size());
test_map[xname] = s;
EXPECT_EQ(test_map[xname], s);
Expand All @@ -971,7 +971,7 @@ void basic_map_test(T &test_map) {

// LOG_DEBUG("asdf");
for (int i = 400000; i < 500000; i++) if (i % 2 == 0) {
sprintf(xname, "%s%d", prefix.c_str(), i);
snprintf(xname, sizeof(xname), "%s%d", prefix.c_str(), i);
auto s = std::string_view(xname).substr(prefix.size());
test_map.insert(pair<string_view, string_view>{xname, s});
EXPECT_EQ(test_map.size(), i/2+1);
Expand Down Expand Up @@ -1087,7 +1087,7 @@ TEST(string_key, unordered_map_string_key) {
std::string s = std::to_string(i);
// string_view view(s);
char chars[1000];
sprintf(chars, "%d", i);
snprintf(chars, sizeof(chars), "%d", i);
// std::pair<const std::string_view, int> pr = make_pair(string_view(s), i);
// unordered_map_string_key<int>::value_type x = make_pair(s, i);
const std::pair<string, int> x = make_pair(s, i);//{s, i};
Expand All @@ -1100,7 +1100,7 @@ TEST(string_key, unordered_map_string_key) {
//std::string
std::string s = std::to_string(i);
char chars[1000];
sprintf(chars, "%d", i);
snprintf(chars, sizeof(chars), "%d", i);
// string_key k(s);
// string_view view(s);
string_view sv(chars);
Expand Down
Loading

0 comments on commit 1db53d9

Please sign in to comment.