Skip to content

Commit

Permalink
SizedType: Change "type" member to private
Browse files Browse the repository at this point in the history
The "type" member of the SizedType class is currently public, making it
possible to modify it without updating required fields. This commit changes
it to private "type_", and you can access it through the "GetTy()" getter.
  • Loading branch information
arthurshau authored and viktormalik committed Feb 14, 2024
1 parent 72d54f2 commit 81615a2
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 171 deletions.
2 changes: 1 addition & 1 deletion src/ast/dibuilderbpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ DIType *DIBuilderBPF::GetType(const SizedType &stype)
return getInt8Ty();
default:
LOG(FATAL) << "Cannot generate debug info for type "
<< typestr(stype.type) << " (" << stype.GetSize()
<< typestr(stype.GetTy()) << " (" << stype.GetSize()
<< " is not a valid type size)";
return nullptr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/ast/passes/codegen_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@ void CodegenLLVM::unop_int(Unop &unop)
}
case Operator::MUL: {
// When dereferencing a 32-bit integer, only read in 32-bits, etc.
auto dst_type = SizedType(type.type, type.GetSize());
auto dst_type = SizedType(type.GetTy(), type.GetSize());
AllocaInst *dst = b_.CreateAllocaBPF(dst_type, "deref");
b_.CreateProbeRead(ctx_, dst, type, expr_, unop.loc);
expr_ = b_.CreateIntCast(b_.CreateLoad(b_.GetType(dst_type), dst),
Expand Down Expand Up @@ -3225,8 +3225,8 @@ void CodegenLLVM::createPrintNonMapCall(Call &call, int &id)

auto elements = AsyncEvent::PrintNonMap().asLLVMType(b_, arg.type.GetSize());
std::ostringstream struct_name;
struct_name << call.func << "_" << arg.type.type << "_" << arg.type.GetSize()
<< "_t";
struct_name << call.func << "_" << arg.type.GetTy() << "_"
<< arg.type.GetSize() << "_t";
StructType *print_struct = b_.GetStructType(struct_name.str(),
elements,
true);
Expand Down
4 changes: 2 additions & 2 deletions src/ast/passes/config_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void ConfigAnalyser::log_type_error(SizedType &type,
AssignConfigVarStatement &assignment)
{
LOG(ERROR, assignment.loc, err_)
<< "Invalid type for " << assignment.config_var << ". Type: " << type.type
<< ". Expected Type: " << expected_type;
<< "Invalid type for " << assignment.config_var
<< ". Type: " << type.GetTy() << ". Expected Type: " << expected_type;
}

void ConfigAnalyser::set_uint64_config(AssignConfigVarStatement &assignment,
Expand Down
54 changes: 27 additions & 27 deletions src/ast/passes/semantic_analyser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ void SemanticAnalyser::visit(Call &call)
LOG(ERROR, call.loc, err_)
<< call.func
<< "() expects an integer, string, or array argument but saw "
<< typestr(arg.type.type);
<< typestr(arg.type.GetTy());
}

size_t max_buffer_size = bpftrace_.config_.get(ConfigKeyInt::max_strlen);
Expand All @@ -663,7 +663,7 @@ void SemanticAnalyser::visit(Call &call)
else if (is_final_pass())
LOG(ERROR, call.loc, err_)
<< call.func << "() expects a length argument for non-array type "
<< typestr(arg.type.type);
<< typestr(arg.type.GetTy());
} else {
if (is_final_pass())
check_arg(call, Type::integer, 1, false);
Expand Down Expand Up @@ -726,7 +726,7 @@ void SemanticAnalyser::visit(Call &call)
!arg->type.IsArrayTy())
LOG(ERROR, call.loc, err_)
<< call.func << "() expects an integer or array argument, got "
<< arg->type.type;
<< arg->type.GetTy();

// Kind of:
//
Expand Down Expand Up @@ -791,7 +791,7 @@ void SemanticAnalyser::visit(Call &call)
auto &arg = *call.vargs->at(0);
if (!(arg.type.IsIntTy() || arg.type.IsPtrTy())) {
LOG(ERROR, call.loc, err_) << "() only supports int or pointer arguments"
<< " (" << arg.type.type << " provided)";
<< " (" << arg.type.GetTy() << " provided)";
}

if (call.vargs && call.vargs->size() > 1)
Expand Down Expand Up @@ -1058,7 +1058,7 @@ void SemanticAnalyser::visit(Call &call)
<< std::to_string(*sig)
<< " is not a valid signal, allowed range: [1,64]";
}
} else if (arg.type.type != Type::integer) {
} else if (!arg.type.IsIntTy()) {
LOG(ERROR, call.loc, err_)
<< "signal only accepts string literals or integers";
}
Expand All @@ -1080,7 +1080,7 @@ void SemanticAnalyser::visit(Call &call)

LOG(ERROR, call.loc, err_)
<< "path() only supports pointer or record argument ("
<< arg.type.type << " provided)";
<< arg.type.GetTy() << " provided)";
}

call.type = SizedType(Type::string,
Expand Down Expand Up @@ -1142,10 +1142,10 @@ void SemanticAnalyser::visit(Call &call)

// kptr should accept both integer or pointer. Consider case: kptr($1)
auto &arg = *call.vargs->at(0);
if (arg.type.type != Type::integer && arg.type.type != Type::pointer) {
LOG(ERROR, call.loc, err_)
<< call.func << "() only supports "
<< "integer or pointer arguments (" << arg.type.type << " provided)";
if (!arg.type.IsIntTy() && !arg.type.IsPtrTy()) {
LOG(ERROR, call.loc, err_) << call.func << "() only supports "
<< "integer or pointer arguments ("
<< arg.type.GetTy() << " provided)";
return;
}

Expand All @@ -1162,7 +1162,7 @@ void SemanticAnalyser::visit(Call &call)
!arg->type.IsByteArray() && !arg->type.IsPtrTy())
LOG(ERROR, call.loc, err_)
<< call.func << "() only supports array or pointer arguments"
<< " (" << arg->type.type << " provided)";
<< " (" << arg->type.GetTy() << " provided)";

auto type = arg->type;
if ((type.IsArrayTy() || type.IsByteArray()) && type.GetSize() != 6)
Expand All @@ -1185,10 +1185,10 @@ void SemanticAnalyser::visit(Call &call)
return;

Expression *arg = call.vargs->at(0);
if (arg->type.type != Type::integer) {
if (!arg->type.IsIntTy()) {
LOG(ERROR, call.loc, err_)
<< call.func << "() only supports integer arguments ("
<< arg->type.type << " provided)";
<< arg->type.GetTy() << " provided)";
return;
}

Expand Down Expand Up @@ -1354,7 +1354,7 @@ void SemanticAnalyser::visit(Map &map)
} else if (expr->type.IsPtrTy() && expr->type.IsCtxAccess()) {
// map functions only accepts a pointer to a element in the stack
LOG(ERROR, map.loc, err_) << "context cannot be used as a map key";
} else if (expr->type.type == Type::tuple) {
} else if (expr->type.IsTupleTy()) {
LOG(ERROR, map.loc, err_)
<< "tuple cannot be used as a map key. Try a multi-key associative"
" array instead (eg `@map[$1, $2] = ...)`.";
Expand Down Expand Up @@ -1417,7 +1417,7 @@ void SemanticAnalyser::visit(ArrayAccess &arr)
if (!type.IsArrayTy() && !type.IsPtrTy()) {
LOG(ERROR, arr.loc, err_) << "The array index operator [] can only be "
"used on arrays and pointers, found "
<< type.type << ".";
<< type.GetTy() << ".";
return;
}

Expand Down Expand Up @@ -1700,7 +1700,7 @@ void SemanticAnalyser::visit(Binop &binop)
}
// Compare type here, not the sized type as we it needs to work on strings of
// different lengths
else if (lht.type != rht.type) {
else if (lht.GetTy() != rht.GetTy()) {
LOG(ERROR, binop.left->loc + binop.right->loc, err_)
<< "Type mismatch for '" << opstr(binop) << "': comparing '" << lht
<< "' with '" << rht << "'";
Expand Down Expand Up @@ -1813,9 +1813,9 @@ void SemanticAnalyser::visit(Ternary &ternary)
ternary.cond->accept(*this);
ternary.left->accept(*this);
ternary.right->accept(*this);
Type &cond = ternary.cond->type.type;
Type &lhs = ternary.left->type.type;
Type &rhs = ternary.right->type.type;
const Type &cond = ternary.cond->type.GetTy();
const Type &lhs = ternary.left->type.GetTy();
const Type &rhs = ternary.right->type.GetTy();
if (is_final_pass()) {
if (lhs != rhs) {
LOG(ERROR, ternary.loc, err_)
Expand All @@ -1842,7 +1842,7 @@ void SemanticAnalyser::visit(If &if_block)
if_block.cond->accept(*this);

if (is_final_pass()) {
Type &cond = if_block.cond->type.type;
const Type &cond = if_block.cond->type.GetTy();
if (cond != Type::integer)
LOG(ERROR, if_block.loc, err_) << "Invalid condition in if(): " << cond;
}
Expand Down Expand Up @@ -2283,7 +2283,7 @@ void SemanticAnalyser::visit(AssignVarStatement &assignment)
}

if (is_final_pass()) {
auto &ty = assignTy.type;
const auto &ty = assignTy.GetTy();
if (ty == Type::none)
LOG(ERROR, assignment.expr->loc, err_)
<< "Invalid expression for assignment: " << ty;
Expand All @@ -2302,7 +2302,7 @@ void SemanticAnalyser::visit(Predicate &pred)
SizedType &ty = pred.expr->type;
if (!ty.IsIntTy() && !ty.IsPtrTy()) {
LOG(ERROR, pred.loc, err_)
<< "Invalid type for predicate: " << pred.expr->type.type;
<< "Invalid type for predicate: " << pred.expr->type.GetTy();
}
}
}
Expand Down Expand Up @@ -2763,11 +2763,11 @@ bool SemanticAnalyser::check_arg(const Call &call,
return false;

auto &arg = *call.vargs->at(arg_num);
if (want_literal && (!arg.is_literal || arg.type.type != type)) {
if (want_literal && (!arg.is_literal || arg.type.GetTy() != type)) {
if (fail) {
LOG(ERROR, call.loc, err_)
<< call.func << "() expects a " << type << " literal ("
<< arg.type.type << " provided)";
<< arg.type.GetTy() << " provided)";
if (type == Type::string) {
// If the call requires a string literal and a positional parameter is
// given, tell user to use str()
Expand All @@ -2778,11 +2778,11 @@ bool SemanticAnalyser::check_arg(const Call &call,
}
}
return false;
} else if (is_final_pass() && arg.type.type != type) {
} else if (is_final_pass() && arg.type.GetTy() != type) {
if (fail) {
LOG(ERROR, call.loc, err_)
<< call.func << "() only supports " << type << " arguments ("
<< arg.type.type << " provided)";
<< arg.type.GetTy() << " provided)";
}
return false;
}
Expand Down Expand Up @@ -2922,7 +2922,7 @@ void SemanticAnalyser::assign_map_type(const Map &map, const SizedType &type)
LOG(ERROR, map.loc, err_) << "Undefined map: " + map_ident;
else
*maptype = type;
} else if (maptype->type != type.type) {
} else if (maptype->GetTy() != type.GetTy()) {
LOG(ERROR, map.loc, err_)
<< "Type mismatch for " << map_ident << ": "
<< "trying to assign value of type '" << type
Expand Down
2 changes: 1 addition & 1 deletion src/bpftrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ std::vector<std::unique_ptr<IPrintable>> BPFtrace::get_arg_values(
std::vector<std::unique_ptr<IPrintable>> arg_values;

for (auto arg : args) {
switch (arg.type.type) {
switch (arg.type.GetTy()) {
case Type::integer:
if (arg.type.IsSigned()) {
int64_t val = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/btf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ SizedType BTF::get_stype(const BTFId &btf_id, bool resolve_structs)
auto *array = btf_array(t);
const auto &elem_type = get_stype(
BTFId{ .btf = btf_id.btf, .id = array->type });
if (elem_type.type == Type::integer && elem_type.GetSize() == 1) {
if (elem_type.IsIntTy() && elem_type.GetSize() == 1) {
stype = CreateString(array->nelems);
} else {
stype = CreateArray(array->nelems, elem_type);
Expand Down
2 changes: 1 addition & 1 deletion src/format_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ std::string validate_format_string(const std::string &fmt,

auto token_iter = tokens_begin;
for (int i = 0; i < num_args; i++, token_iter++) {
Type arg_type = args.at(i).type.type;
Type arg_type = args.at(i).type.GetTy();
if (arg_type == Type::ksym || arg_type == Type::usym ||
arg_type == Type::probe || arg_type == Type::username ||
arg_type == Type::kstack || arg_type == Type::ustack ||
Expand Down
2 changes: 1 addition & 1 deletion src/mapkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ std::string MapKey::argument_value(BPFtrace &bpftrace,
{
auto arg_data = static_cast<const uint8_t *>(data);
std::ostringstream ptr;
switch (arg.type) {
switch (arg.GetTy()) {
case Type::integer:
switch (arg.GetSize()) {
case 1:
Expand Down
7 changes: 3 additions & 4 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace bpftrace {
namespace {
bool is_quoted_type(const SizedType &ty)
{
switch (ty.type) {
switch (ty.GetTy()) {
case Type::buffer:
case Type::cgroup_path:
case Type::inet:
Expand Down Expand Up @@ -669,9 +669,8 @@ std::string TextOutput::map_keyval_to_str(IMap &map,

std::string TextOutput::map_elem_delim_to_str(IMap &map) const
{
if (map.type_.type != Type::kstack && map.type_.type != Type::ustack &&
map.type_.type != Type::ksym && map.type_.type != Type::usym &&
map.type_.type != Type::inet)
if (!map.type_.IsKstackTy() && !map.type_.IsUstackTy() &&
!map.type_.IsKsymTy() && !map.type_.IsUsymTy() && !map.type_.IsInetTy())
return "\n";

return "";
Expand Down
26 changes: 13 additions & 13 deletions src/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::ostream &operator<<(std::ostream &os, const SizedType &type)
} else if (type.IsArrayTy()) {
os << *type.GetElementTy() << "[" << type.GetNumElements() << "]";
} else if (type.IsStringTy() || type.IsBufferTy()) {
os << type.type << "[" << type.GetSize() << "]";
os << type.GetTy() << "[" << type.GetSize() << "]";
} else if (type.IsTupleTy()) {
os << "(";
size_t n = type.GetFieldCount();
Expand All @@ -51,15 +51,15 @@ std::ostream &operator<<(std::ostream &os, const SizedType &type)
}
os << ")";
} else {
os << type.type;
os << type.GetTy();
}

return os;
}

bool SizedType::IsSameType(const SizedType &t) const
{
if (t.type != type)
if (t.GetTy() != type_)
return false;

if (IsRecordTy())
Expand All @@ -68,12 +68,12 @@ bool SizedType::IsSameType(const SizedType &t) const
if (IsPtrTy() && t.IsPtrTy())
return GetPointeeTy()->IsSameType(*t.GetPointeeTy());

return type == t.type;
return type_ == t.GetTy();
}

bool SizedType::IsEqual(const SizedType &t) const
{
if (t.type != type)
if (t.GetTy() != type_)
return false;

if (IsRecordTy())
Expand All @@ -89,7 +89,7 @@ bool SizedType::IsEqual(const SizedType &t) const
if (IsTupleTy())
return *t.GetStruct().lock() == *GetStruct().lock();

return type == t.type && GetSize() == t.GetSize() &&
return type_ == t.GetTy() && GetSize() == t.GetSize() &&
is_signed_ == t.is_signed_;
}

Expand All @@ -105,10 +105,10 @@ bool SizedType::operator==(const SizedType &t) const

bool SizedType::IsByteArray() const
{
return type == Type::string || type == Type::usym || type == Type::ustack ||
type == Type::inet || type == Type::buffer ||
type == Type::timestamp || type == Type::mac_address ||
type == Type::cgroup_path;
return type_ == Type::string || type_ == Type::usym ||
type_ == Type::ustack || type_ == Type::inet ||
type_ == Type::buffer || type_ == Type::timestamp ||
type_ == Type::mac_address || type_ == Type::cgroup_path;
}

bool SizedType::IsAggregate() const
Expand All @@ -118,7 +118,7 @@ bool SizedType::IsAggregate() const

bool SizedType::IsStack() const
{
return type == Type::ustack || type == Type::kstack;
return type_ == Type::ustack || type_ == Type::kstack;
}

std::string addrspacestr(AddrSpace as)
Expand Down Expand Up @@ -565,10 +565,10 @@ namespace std {
size_t hash<bpftrace::SizedType>::operator()(
const bpftrace::SizedType &type) const
{
auto hash = std::hash<unsigned>()(static_cast<unsigned>(type.type));
auto hash = std::hash<unsigned>()(static_cast<unsigned>(type.GetTy()));
bpftrace::hash_combine(hash, type.GetSize());

switch (type.type) {
switch (type.GetTy()) {
case bpftrace::Type::integer:
bpftrace::hash_combine(hash, type.IsSigned());
break;
Expand Down
Loading

0 comments on commit 81615a2

Please sign in to comment.