Skip to content

Commit

Permalink
Build with strict language checks (morganstanley#140)
Browse files Browse the repository at this point in the history
build with strict C++ language checks
  • Loading branch information
kthielen authored Jul 6, 2018
1 parent 9bc8b20 commit c6d5d3b
Show file tree
Hide file tree
Showing 87 changed files with 956 additions and 976 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if(APPLE)
endif()
if(LINUX)
set(sys_libs pthread dl rt)
set(cxx_flags "-Wreorder")
set(cxx_flags "-Werror=old-style-cast -Werror -Wall -Wextra -Winit-self -Wreturn-type -Wunused-variable -Wsign-compare -Warray-bounds -Wunknown-pragmas -Wuninitialized -Wstrict-aliasing -Wunused-value -Wunused-label -Wswitch -Wcast-align -Wno-ctor-dtor-privacy -Wno-missing-noreturn -Wno-unused-parameter -Werror=old-style-cast -Wreorder")
endif()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${cxx_flags}")
Expand Down
14 changes: 8 additions & 6 deletions bin/hi/cio.H
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,29 @@ bool extConsoleCmdsEnabled();

extern ConsoleColors colors;

inline char cchar(uint8_t x) { return static_cast<char>(x); }

inline void sendCmd(std::ostream& out, char c) {
if (extConsoleCmdsEnabled()) {
out << (char)0x1B << '[' << c << std::flush;
out << cchar(0x1B) << '[' << c << std::flush;
}
}

inline void sendCmd(std::ostream& out, int n0, char c) {
if (extConsoleCmdsEnabled()) {
out << (char)0x1B << '[' << n0 << c << std::flush;
out << cchar(0x1B) << '[' << n0 << c << std::flush;
}
}

inline void sendCmd(std::ostream& out, int n0, int n1, char c) {
if (extConsoleCmdsEnabled()) {
out << (char)0x1B << '[' << n0 << ';' << n1 << c << std::flush;
out << cchar(0x1B) << '[' << n0 << ';' << n1 << c << std::flush;
}
}

inline void sendCmd(std::ostream& out, int n0, int n1, int n2, char c) {
if (extConsoleCmdsEnabled()) {
out << (char)0x1B << '[' << n0 << ';' << n1 << ';' << n2 << c << std::flush;
out << cchar(0x1B) << '[' << n0 << ';' << n1 << ';' << n2 << c << std::flush;
}
}

Expand Down Expand Up @@ -87,7 +89,7 @@ struct setfgc {
};

inline std::ostream& operator<<(std::ostream& lhs, const setfgc& c) {
sendCmd(lhs, 38, 5, (int)c.x, 'm');
sendCmd(lhs, 38, 5, static_cast<int>(c.x), 'm');
return lhs;
}

Expand All @@ -97,7 +99,7 @@ struct setbgc {
};

inline std::ostream& operator<<(std::ostream& lhs, const setbgc& c) {
sendCmd(lhs, 48, 5, (int)c.x, 'm');
sendCmd(lhs, 48, 5, static_cast<int>(c.x), 'm');
return lhs;
}

Expand Down
10 changes: 5 additions & 5 deletions bin/hi/evaluator.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace hi {

// allocate a string in global memory
hobbes::array<char>* allocGlobalStr(const char* x, size_t len) {
hobbes::array<char>* r = (hobbes::array<char>*)malloc(sizeof(long) + len * sizeof(char));
hobbes::array<char>* r = reinterpret_cast<hobbes::array<char>*>(malloc(sizeof(long) + len * sizeof(char)));
memcpy(r->data, x, len * sizeof(char));
r->size = len;
return r;
Expand Down Expand Up @@ -48,7 +48,7 @@ void bindArguments(hobbes::cc& ctx, const Args::NameVals& args) {
typedef std::pair<array<char>*, array<char>*> StrPair;
typedef array<StrPair> StrPairs;

StrPairs* arguments = (StrPairs*)malloc(sizeof(long) + args.size() * sizeof(StrPair));
StrPairs* arguments = reinterpret_cast<StrPairs*>(malloc(sizeof(long) + args.size() * sizeof(StrPair)));
arguments->size = 0;
for (auto arg : args) {
arguments->data[arguments->size].first = allocGlobalStr(arg.first);
Expand Down Expand Up @@ -204,7 +204,7 @@ void evaluator::printLLVMModule() {

void evaluator::printAssembly(const std::string& expr, void (*f)(void*,size_t)) {
hobbes::cc::bytes d = this->ctx.machineCodeForExpr(expr);
f((void*)&(*(d.begin())), d.size());
f(reinterpret_cast<void*>(&(*(d.begin()))), d.size());
}

void evaluator::perfTestExpr(const std::string& expr) {
Expand All @@ -214,7 +214,7 @@ void evaluator::perfTestExpr(const std::string& expr) {

const size_t numRuns = 1000;
unsigned long nsCSum = 0;
unsigned long nsCMin = (unsigned long)-1;
unsigned long nsCMin = static_cast<unsigned long>(-1);
unsigned long nsCMax = 0;

for (size_t i = 0; i < numRuns; ++i) {
Expand All @@ -227,7 +227,7 @@ void evaluator::perfTestExpr(const std::string& expr) {
nsCMax = std::max<unsigned long long>(nsCMax, nsC);
}

std::cout << "average over " << numRuns << " runs: " << hobbes::describeNanoTime(((double)nsCSum)/((double)numRuns)) << std::endl
std::cout << "average over " << numRuns << " runs: " << hobbes::describeNanoTime(static_cast<double>(nsCSum)/static_cast<double>(numRuns)) << std::endl
<< "minimum runtime: " << hobbes::describeNanoTime(nsCMin) << std::endl
<< "maximum runtime: " << hobbes::describeNanoTime(nsCMax) << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion bin/hi/funcdefs.C
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const PIO* pexec(const hobbes::array<char>* cmd) {
}
argv.push_back(0);

execv(args[0].c_str(), (char* const*)&argv[0]);
execv(args[0].c_str(), const_cast<char* const*>(&argv[0]));
exit(0);
} else {
close(p2c[0]); p2c[0] = 0;
Expand Down
14 changes: 7 additions & 7 deletions bin/hi/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ void showShellHelp(const CmdDescs& cds) {
<< std::string(2 + llen + 2, '=')
<< std::endl;

double hw = ((double)(llen - header.size())) / 2.0;
size_t lhw = (size_t)floor(hw);
size_t rhw = (size_t)ceil(hw);
double hw = static_cast<double>(llen - header.size()) / 2.0;
size_t lhw = static_cast<size_t>(floor(hw));
size_t rhw = static_cast<size_t>(ceil(hw));

std::cout << "| "
<< std::string(lhw, ' ')
Expand Down Expand Up @@ -183,7 +183,7 @@ evaluator* eval = 0;
str::seq completionMatches;

char* completionStep(const char* pfx, int state) {
if (state < completionMatches.size()) {
if (state >= 0 && size_t(state) < completionMatches.size()) {
return strdup(completionMatches[state].c_str());
} else {
return 0;
Expand All @@ -193,7 +193,7 @@ char* completionStep(const char* pfx, int state) {
char** completions(const char* pfx, int start, int end) {
if (start == 0) {
completionMatches = eval->completionsFor(pfx);
return rl_completion_matches((char*)pfx, &completionStep);
return rl_completion_matches(const_cast<char*>(pfx), &completionStep);
} else {
#ifdef BUILD_LINUX
rl_bind_key('\t', rl_abort);
Expand Down Expand Up @@ -344,7 +344,7 @@ void runProcess(const std::string&, std::ostream&);

unsigned int digitLen(unsigned int x) {
static double log10 = log(10.0);
return (unsigned int)floor(log((double)x) / log10);
return static_cast<unsigned int>(floor(log(static_cast<double>(x)) / log10));
}

template <typename C>
Expand Down Expand Up @@ -468,7 +468,7 @@ std::string saveData(void* d, size_t sz) {
if (!f.is_open()) {
throw std::runtime_error("Failed to open '" + rn + "' for writing.");
}
f.write((const char*)d, sz);
f.write(reinterpret_cast<const char*>(d), sz);
f.close();
return rn;
}
Expand Down
4 changes: 2 additions & 2 deletions bin/hi/www.C
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ const cstr* formatJSTime(long x) {
int64_t us = x % (1000 * 1000);

char b[100];
strftime(b, sizeof(b), "%Y-%m-%d %H:%M:%S.", localtime((time_t*)&s));
strftime(b, sizeof(b), "%Y-%m-%d %H:%M:%S.", localtime(reinterpret_cast<time_t*>(&s)));

return hobbes::makeString(b + hobbes::str::from(us));
}
Expand Down Expand Up @@ -464,7 +464,7 @@ void WWWServer::evalHTTPRequest(const hobbes::HTTPRequest& req, int fd, void* ud
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);

// evaluate it
((WWWServer*)ud)->eval(req, fd);
reinterpret_cast<WWWServer*>(ud)->eval(req, fd);
}

std::string WWWServer::mimeType(const std::string& fpath) {
Expand Down
10 changes: 5 additions & 5 deletions bin/hog/batchrecv.C
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,16 @@ void read(gzbuffer* in, uint8_t* b, size_t n) {
}

#if defined(__APPLE__) && defined(__MACH__)
void read(gzbuffer* in, size_t* n) { read(in, (uint8_t*)n, sizeof(*n)); }
void read(gzbuffer* in, size_t* n) { read(in, reinterpret_cast<uint8_t*>(n), sizeof(*n)); }
#endif
void read(gzbuffer* in, uint32_t* n) { read(in, (uint8_t*)n, sizeof(*n)); }
void read(gzbuffer* in, uint64_t* n) { read(in, (uint8_t*)n, sizeof(*n)); }
void read(gzbuffer* in, uint32_t* n) { read(in, reinterpret_cast<uint8_t*>(n), sizeof(*n)); }
void read(gzbuffer* in, uint64_t* n) { read(in, reinterpret_cast<uint8_t*>(n), sizeof(*n)); }

void read(gzbuffer* in, std::string* x) {
size_t n;
read(in, &n);
x->resize(n);
read(in, (uint8_t*)&(*x)[0], n);
read(in, reinterpret_cast<uint8_t*>(&(*x)[0]), n);
}

void read(gzbuffer* in, std::vector<uint8_t>* x) {
Expand Down Expand Up @@ -163,7 +163,7 @@ void runRecvConnection(SessionGroup* sg, NetConnection* pc, std::string dir) {
storage::statements stmts;
read(&zb, &stmts);

auto txnF = appendStorageSession(sg, instantiateDir(group, dir), (storage::PipeQOS)qos, (storage::CommitMethod)cm, stmts);
auto txnF = appendStorageSession(sg, instantiateDir(group, dir), static_cast<storage::PipeQOS>(qos), static_cast<storage::CommitMethod>(cm), stmts);

connection->send(&ack, sizeof(ack));

Expand Down
14 changes: 7 additions & 7 deletions bin/hog/batchsend.C
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ struct BatchSendSession {
}

void allocFile() {
this->buffer = (gzFile_s*)gzopen(this->tempfilename.c_str(), ("wb" + str::from(this->clevel)).c_str());
this->buffer = reinterpret_cast<gzFile_s*>(gzopen(this->tempfilename.c_str(), ("wb" + str::from(this->clevel)).c_str()));
this->sz = 0;
}

Expand Down Expand Up @@ -276,25 +276,25 @@ void write(BatchSendSession* s, const uint8_t* d, size_t sz) {

void write(BatchSendSession* s, const std::string& x) {
size_t n = x.size();
write(s, (const uint8_t*)&n, sizeof(n));
write(s, (const uint8_t*)x.data(), n);
write(s, reinterpret_cast<const uint8_t*>(&n), sizeof(n));
write(s, reinterpret_cast<const uint8_t*>(x.data()), n);
}

void write(BatchSendSession* s, const std::vector<uint8_t>& x) {
size_t n = x.size();
write(s, (const uint8_t*)&n, sizeof(n));
write(s, reinterpret_cast<const uint8_t*>(&n), sizeof(n));
write(s, &x[0], n);
}

template <typename T>
void write(BatchSendSession* s, T x) {
write(s, (const uint8_t*)&x, sizeof(x));
write(s, reinterpret_cast<const uint8_t*>(&x), sizeof(x));
}

static void initNetSession(BatchSendSession* s, const std::string& groupName, const std::string& dir, storage::PipeQOS qos, storage::CommitMethod cm, const storage::statements& stmts) {
// write init message data to our current batch send file
write(s, (int)qos);
write(s, (int)cm);
write(s, static_cast<int>(qos));
write(s, static_cast<int>(cm));

write(s, stmts.size());
for (const auto& stmt : stmts) {
Expand Down
2 changes: 1 addition & 1 deletion bin/hog/boot/gen/boot.C
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace hog {
#include "bootdata.H"

void compileBootCode(hobbes::cc& ctx) {
hobbes::compile(&ctx, ctx.readModule(std::string((const char*)___bootdata, ___bootdata_len)));
hobbes::compile(&ctx, ctx.readModule(std::string(reinterpret_cast<const char*>(___bootdata), ___bootdata_len)));
}

}
10 changes: 5 additions & 5 deletions bin/hog/main.C
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ RunMode config(int argc, const char** argv) {
exit(0);
}

for (size_t i = 1; i < argc; ++i) {
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];

if (arg == "-?" || arg == "--help") {
Expand Down Expand Up @@ -204,14 +204,14 @@ RunMode config(int argc, const char** argv) {
void evalGroupHostConnection(SessionGroup* sg, const std::string& groupName, const RunMode& m, std::vector<std::thread>* ts, int c) {
try {
uint8_t cmd=0;
hobbes::fdread(c, (char*)&cmd, sizeof(cmd));
hobbes::fdread(c, reinterpret_cast<char*>(&cmd), sizeof(cmd));

auto wp = static_cast<hobbes::storage::WaitPolicy>(0x1 & (cmd >> 1));

uint64_t pid=0,tid=0;
hobbes::fdread(c, (char*)&pid, sizeof(pid));
hobbes::fdread(c, (char*)&tid, sizeof(tid));
out() << "queue registered for group '" << groupName << "' from " << pid << ":" << tid << ", cmd " << (int)cmd << std::endl;
hobbes::fdread(c, reinterpret_cast<char*>(&pid), sizeof(pid));
hobbes::fdread(c, reinterpret_cast<char*>(&tid), sizeof(tid));
out() << "queue registered for group '" << groupName << "' from " << pid << ":" << tid << ", cmd " << static_cast<int>(cmd) << std::endl;

auto qc = hobbes::storage::consumeGroup(groupName, hobbes::storage::ProcThread(pid, tid));

Expand Down
2 changes: 1 addition & 1 deletion bin/hog/session.C
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const uint8_t* hstoreUnsafeReadFixedArray(storage::Transaction& txn, size_t byte

memcpy(result->data, txn.ptr(), bytes);
txn.skip(bytes);
return (const uint8_t*)result;
return reinterpret_cast<const uint8_t*>(result);
}

cc* loggerCompiler() {
Expand Down
Loading

0 comments on commit c6d5d3b

Please sign in to comment.