Skip to content

Commit

Permalink
Add TypedefAnnoType type alias
Browse files Browse the repository at this point in the history
Summary: Add the type alias to improve clarify.

Reviewed By: ssj933

Differential Revision: D70525847

fbshipit-source-id: 748cb6ed7ea223cbfc0379adc0bc7585b10ed2ed
  • Loading branch information
Wei Zhang (Devinfra) authored and facebook-github-bot committed Mar 5, 2025
1 parent 2474195 commit eb73758
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
52 changes: 28 additions & 24 deletions opt/typedef-anno-checker/TypedefAnnoPatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ bool is_int_or_obj_ref(const type_inference::TypeEnvironment& env, reg_t reg) {

namespace {

bool has_typedef_annos(ParamAnnotations* param_annos,
const std::unordered_set<DexType*>& typedef_annos) {
bool has_typedef_annos(
ParamAnnotations* param_annos,
const std::unordered_set<TypedefAnnoType*>& typedef_annos) {
if (!param_annos) {
return false;
}
Expand Down Expand Up @@ -206,12 +207,12 @@ bool add_annotation_set(DexMember* member,

template <typename DexMember>
bool add_annotation(DexMember* member,
const DexType* anno,
const TypedefAnnoType* anno,
std::mutex& anno_patching_mutex,
Stats& class_stats) {
DexAnnotationSet anno_set = DexAnnotationSet();
anno_set.add_annotation(
std::make_unique<DexAnnotation>(const_cast<DexType*>(anno), DAV_RUNTIME));
anno_set.add_annotation(std::make_unique<DexAnnotation>(
const_cast<TypedefAnnoType*>(anno), DAV_RUNTIME));
return add_annotation_set(member, &anno_set, anno_patching_mutex,
class_stats);
}
Expand Down Expand Up @@ -245,12 +246,12 @@ bool add_param_annotation_set(DexMethod* m,
}

bool add_param_annotation(DexMethod* m,
const DexType* anno,
const TypedefAnnoType* anno,
const int param,
Stats& class_stats) {
DexAnnotationSet anno_set = DexAnnotationSet();
anno_set.add_annotation(
std::make_unique<DexAnnotation>(const_cast<DexType*>(anno), DAV_RUNTIME));
anno_set.add_annotation(std::make_unique<DexAnnotation>(
const_cast<TypedefAnnoType*>(anno), DAV_RUNTIME));
return add_param_annotation_set(m, &anno_set, param, class_stats);
}

Expand All @@ -260,7 +261,7 @@ int find_and_patch_parameter(
DexMethod* m,
IRInstruction* insn,
src_index_t arg_index,
const DexType* typedef_anno,
const TypedefAnnoType* typedef_anno,
live_range::UseDefChains* ud_chains,
Stats& class_stats,
std::vector<std::pair<src_index_t, DexAnnotationSet&>>*
Expand Down Expand Up @@ -299,8 +300,8 @@ void patch_param_from_method_invoke(
IRInstruction* invoke,
live_range::UseDefChains* ud_chains,
Stats& class_stats,
std::vector<std::pair<src_index_t, const DexType*>>* missing_param_annos =
nullptr,
std::vector<std::pair<src_index_t, const TypedefAnnoType*>>*
missing_param_annos = nullptr,
bool patch_accessor = true) {
always_assert(opcode::is_an_invoke(invoke->opcode()));
auto* def_method = resolve_method(caller, invoke);
Expand Down Expand Up @@ -340,13 +341,14 @@ void patch_param_from_method_invoke(
}
}

void patch_setter_method(type_inference::TypeInference& inference,
DexMethod* caller,
IRInstruction* insn,
live_range::UseDefChains* ud_chains,
Stats& class_stats,
std::vector<std::pair<src_index_t, const DexType*>>*
missing_param_annos = nullptr) {
void patch_setter_method(
type_inference::TypeInference& inference,
DexMethod* caller,
IRInstruction* insn,
live_range::UseDefChains* ud_chains,
Stats& class_stats,
std::vector<std::pair<src_index_t, const TypedefAnnoType*>>*
missing_param_annos = nullptr) {
always_assert(opcode::is_an_iput(insn->opcode()) ||
opcode::is_an_sput(insn->opcode()));
auto field_ref = insn->get_field();
Expand Down Expand Up @@ -618,7 +620,7 @@ void patch_synthetic_field_from_local_var_lambda(
const live_range::UseDefChains& ud_chains,
IRInstruction* insn,
const src_index_t src,
const DexType* anno,
const TypedefAnnoType* anno,
std::vector<const DexField*>* patched_fields,
std::mutex& anno_patching_mutex,
Stats& class_stats) {
Expand Down Expand Up @@ -730,7 +732,8 @@ void TypedefAnnoPatcher::patch_lambdas(
// parameters. Find any missing parameter annotations and add them to
// the map of src index to annotation, but don't patch the static
// method since a different visit of that method will patch it
std::vector<std::pair<src_index_t, const DexType*>> missing_param_annos;
std::vector<std::pair<src_index_t, const TypedefAnnoType*>>
missing_param_annos;
patch_parameters_and_returns(static_method, class_stats,
&missing_param_annos);
// Patch missing param annotations
Expand Down Expand Up @@ -899,7 +902,8 @@ void TypedefAnnoPatcher::patch_enclosing_lambda_fields(const DexClass* anon_cls,
void TypedefAnnoPatcher::patch_parameters_and_returns(
DexMethod* m,
Stats& class_stats,
std::vector<std::pair<src_index_t, const DexType*>>* missing_param_annos) {
std::vector<std::pair<src_index_t, const TypedefAnnoType*>>*
missing_param_annos) {
IRCode* code = m->get_code();
if (!code) {
return;
Expand All @@ -915,7 +919,7 @@ void TypedefAnnoPatcher::patch_parameters_and_returns(
live_range::MoveAwareChains chains(m->get_code()->cfg());
live_range::UseDefChains ud_chains = chains.get_use_def_chains();

boost::optional<const DexType*> anno = boost::none;
boost::optional<const TypedefAnnoType*> anno = boost::none;
bool patch_return = missing_param_annos == nullptr;
for (cfg::Block* b : cfg.blocks()) {
for (auto& mie : InstructionIterable(b)) {
Expand All @@ -941,8 +945,8 @@ void TypedefAnnoPatcher::patch_parameters_and_returns(
}

if (patch_return && anno != boost::none) {
add_annotation(m, const_cast<DexType*>(*anno), m_anno_patching_mutex,
class_stats);
add_annotation(m, const_cast<TypedefAnnoType*>(*anno),
m_anno_patching_mutex, class_stats);
auto class_name = type_class(m->get_class())->str();
auto class_name_prefix = class_name.substr(0, class_name.size() - 1);
if (!m_patched_returns.count(class_name_prefix)) {
Expand Down
8 changes: 5 additions & 3 deletions opt/typedef-anno-checker/TypedefAnnoPatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace typedef_anno {
bool is_not_str_nor_int(const type_inference::TypeEnvironment& env, reg_t reg);
} // namespace typedef_anno

using TypedefAnnoType = DexType;

struct Stats {
size_t num_patched_parameters{0};
size_t num_patched_fields_and_methods{0};
Expand Down Expand Up @@ -82,8 +84,8 @@ class TypedefAnnoPatcher {
void patch_parameters_and_returns(
DexMethod* method,
Stats& class_stats,
std::vector<std::pair<src_index_t, const DexType*>>* missing_param_annos =
nullptr);
std::vector<std::pair<src_index_t, const TypedefAnnoType*>>*
missing_param_annos = nullptr);

void patch_enclosing_lambda_fields(const DexClass* cls, Stats& class_stats);

Expand All @@ -102,7 +104,7 @@ class TypedefAnnoPatcher {
void populate_chained_getters(DexClass* cls);
void patch_chained_getters(Stats& class_stats);

std::unordered_set<DexType*> m_typedef_annos;
std::unordered_set<TypedefAnnoType*> m_typedef_annos;
const method_override_graph::Graph& m_method_override_graph;
ConcurrentMap<std::string, std::vector<const DexField*>> m_lambda_anno_map;
InsertOnlyConcurrentSet<std::string_view> m_patched_returns;
Expand Down

0 comments on commit eb73758

Please sign in to comment.