Fix clang-tidy findings in the binpac lib code

This commit is contained in:
Tim Wojtulewicz 2025-08-13 15:48:29 -07:00
parent 4ae8bb856d
commit cd1414ab69
5 changed files with 28 additions and 26 deletions

View file

@ -17,7 +17,7 @@ public:
int contract_threshold;
};
enum LineBreakStyle {
enum LineBreakStyle : uint8_t {
CR_OR_LF, // CR or LF or CRLF
STRICT_CRLF, // CR followed by LF
CR_LF_NUL, // CR or LF or CR-LF or CR-NUL
@ -142,13 +142,13 @@ protected:
LineBreakStyle linebreak_style_default;
unsigned char linebreaker_;
enum {
enum : uint8_t {
UNKNOWN_MODE,
LINE_MODE,
FRAME_MODE,
} mode_;
enum {
enum : uint8_t {
CR_OR_LF_0,
CR_OR_LF_1,
STRICT_CRLF_0,
@ -163,7 +163,7 @@ protected:
static Policy policy;
};
typedef FlowBuffer* flow_buffer_t;
using flow_buffer_t = FlowBuffer*;
} // namespace binpac

View file

@ -3,7 +3,7 @@
#ifndef binpac_bytestring_h
#define binpac_bytestring_h
#include <string.h>
#include <cstring>
#include <string>
#include "binpac.h"
@ -16,7 +16,7 @@ class datastring;
template<class T>
class const_datastring {
public:
const_datastring() : begin_(0), end_(0) {}
const_datastring() : begin_(nullptr), end_(nullptr) {}
const_datastring(T const* data, int length) : begin_(data), end_(data + length) {}
@ -49,7 +49,7 @@ private:
T const* end_;
};
typedef const_datastring<uint8> const_bytestring;
using const_bytestring = const_datastring<uint8>;
template<class T>
class datastring {
@ -65,6 +65,9 @@ public:
explicit datastring(const_datastring<T> const& x) { set_const(x.begin(), x.length()); }
datastring const& operator=(datastring<T> const& x) {
if ( this == &x )
return *this;
BINPAC_ASSERT(! data_);
set(x.data(), x.length());
return *this;
@ -76,7 +79,7 @@ public:
}
void clear() {
data_ = 0;
data_ = nullptr;
length_ = 0;
}
@ -119,13 +122,11 @@ private:
int length_;
};
typedef datastring<uint8> bytestring;
using bytestring = datastring<uint8>;
inline const char* c_str(bytestring const& s) { return (const char*)s.begin(); }
inline std::string std_str(const_bytestring const& s) {
return std::string((const char*)s.begin(), (const char*)s.end());
}
inline std::string std_str(const_bytestring const& s) { return {(const char*)s.begin(), (const char*)s.end()}; }
inline bool operator==(bytestring const& s1, const char* s2) { return strcmp(c_str(s1), s2) == 0; }

View file

@ -3,25 +3,26 @@
#ifndef binpac_exception_h
#define binpac_exception_h
#include <inttypes.h>
#include <stdint.h>
#include <cinttypes>
#include <cstdint>
#include <string>
namespace binpac {
class Exception {
public:
Exception(const char* m = 0) : msg_("binpac exception: ") {
Exception(const char* m = nullptr) : msg_("binpac exception: ") {
if ( m )
append(m);
// abort();
}
void append(string m) { msg_ += m; }
string msg() const { return msg_; }
void append(std::string m) { msg_ += m; }
std::string msg() const { return msg_; }
const char* c_msg() const { return msg_.c_str(); }
protected:
string msg_;
std::string msg_;
};
class ExceptionEnforceViolation : public Exception {
@ -46,7 +47,7 @@ public:
protected:
const char* location_;
int64_t index_;
string expected_;
std::string expected_;
};
class ExceptionInvalidCaseIndex : public Exception {

View file

@ -1,6 +1,10 @@
// See the file "COPYING" in the main distribution directory for copyright.
#ifndef binpac_regex_h
#define binpac_regex_h
#include <vector>
#include "zeek/RE.h"
#include "binpac.h"
@ -16,7 +20,7 @@ namespace binpac {
// Note, this must be declared/defined here, and inline, because the RE
// functionality can only be used when compiling from inside Zeek.
// A copy is made of any FlowBuffer policy struct data passed.
inline void init(FlowBuffer::Policy* fbp = 0);
inline void init(FlowBuffer::Policy* fbp = nullptr);
// Internal vector recording not yet compiled matchers.
extern std::vector<zeek::RE_Matcher*>* uncompiled_re_matchers;
@ -50,8 +54,8 @@ inline void RegExMatcher::init() {
if ( ! uncompiled_re_matchers )
return;
for ( size_t i = 0; i < uncompiled_re_matchers->size(); ++i ) {
if ( ! (*uncompiled_re_matchers)[i]->Compile() ) {
for ( const auto& matcher : *uncompiled_re_matchers ) {
if ( ! matcher->Compile() ) {
fprintf(stderr, "binpac: cannot compile regular expression\n");
exit(1);
}

View file

@ -3,7 +3,6 @@
#include <ctype.h>
#include <unistd.h>
#include "config.h"
#include "pac_common.h"
#include "pac_decl.h"
#include "pac_exception.h"
@ -177,9 +176,6 @@ int compile(const char* filename) {
}
void usage() {
#ifdef BINPAC_VERSION
fprintf(stderr, "binpac version %s\n", BINPAC_VERSION);
#endif
fprintf(stderr, "usage: binpac [options] <pac files>\n");
fprintf(stderr, " <pac files> | pac-language input files\n");
fprintf(stderr, " -d <dir> | use given directory for compiler output\n");