Skip to content

Commit

Permalink
Reduce size of HPos
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 21, 2024
1 parent f76278b commit 3d4a618
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 87 deletions.
3 changes: 1 addition & 2 deletions src/aast_utils/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn get_aast_for_path_and_contents(
return Err(match err {
aast_parser::Error::ParserFatal(err, pos) => ParserError::SyntaxError {
message: err.message.to_string(),
pos: HPos::new(&pos, FilePath(StrId::EMPTY), None),
pos: HPos::new(&pos, FilePath(StrId::EMPTY)),
},
_ => ParserError::NotAHackFile,
})
Expand All @@ -73,7 +73,6 @@ pub fn get_aast_for_path_and_contents(
end_line: line_count as u32,
start_column: (column as u16) + 1,
end_column: (column as u16) + 1,
insertion_start: None,
},
});
}
Expand Down
14 changes: 3 additions & 11 deletions src/analyzer/expr/assertion_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{formula_generator::AssertionContext, function_analysis_data::Functio
use hakana_reflection_info::code_location::HPos;
use hakana_reflection_info::function_context::FunctionLikeIdentifier;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_str::StrId;
use hakana_reflection_info::symbol_references::ReferenceSource;
use hakana_reflection_info::t_atomic::DictKey;
use hakana_reflection_info::{
Expand All @@ -14,6 +13,7 @@ use hakana_reflection_info::{
t_union::populate_union_type,
};
use hakana_reflector::typehint_resolver::get_type_from_hint;
use hakana_str::StrId;
use hakana_type::type_comparator::type_comparison_result::TypeComparisonResult;
use hakana_type::type_comparator::union_type_comparator;
use hakana_type::type_expander::{self, TypeExpansionOptions};
Expand Down Expand Up @@ -291,11 +291,7 @@ fn get_is_assertions(
lhs_type.get_id(Some(interner)),
is_type.get_id(Some(interner)),
),
HPos::new(
var_expr.pos(),
assertion_context.file_source.file_path,
None,
),
HPos::new(var_expr.pos(), assertion_context.file_source.file_path),
&Some(match assertion_context.reference_source {
ReferenceSource::Symbol(_, fn_id) => {
FunctionLikeIdentifier::Function(fn_id)
Expand Down Expand Up @@ -325,11 +321,7 @@ fn get_is_assertions(
lhs_type.get_id(Some(interner)),
is_type.get_id(Some(interner)),
),
HPos::new(
var_expr.pos(),
assertion_context.file_source.file_path,
None,
),
HPos::new(var_expr.pos(), assertion_context.file_source.file_path),
&Some(match assertion_context.reference_source {
ReferenceSource::Symbol(_, fn_id) => {
FunctionLikeIdentifier::Function(fn_id)
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/call/expression_call_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ pub(crate) fn analyze(
.map(|fn_param| {
let mut param = FunctionLikeParameter::new(
"".to_string(),
HPos::new(expr.func.pos(), *statements_analyzer.get_file_path(), None),
HPos::new(expr.func.pos(), *statements_analyzer.get_file_path(), None),
HPos::new(expr.func.pos(), *statements_analyzer.get_file_path()),
HPos::new(expr.func.pos(), *statements_analyzer.get_file_path()),
);
param.signature_type = fn_param.signature_type.as_ref().map(|t| (**t).clone());
param.is_inout = fn_param.is_inout;
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/echo_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) fn analyze(
) -> Result<(), AnalysisError> {
let echo_param = FunctionLikeParameter::new(
"var".to_string(),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
);

for (i, (_, arg_expr)) in args.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/exit_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub(crate) fn analyze(
) -> Result<(), AnalysisError> {
let echo_param = FunctionLikeParameter::new(
"var".to_string(),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
);

for (i, (_, arg_expr)) in args.iter().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/expr/include_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub(crate) fn analyze(
) -> Result<(), AnalysisError> {
let echo_param = FunctionLikeParameter::new(
"var".to_string(),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path(), None),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
HPos::new(call_pos, *statements_analyzer.get_file_path()),
);

expression_analyzer::analyze(statements_analyzer, expr, analysis_data, context, &mut None)?;
Expand Down
4 changes: 2 additions & 2 deletions src/analyzer/function_analysis_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl FunctionAnalysisData {
return;
}

issue.pos.insertion_start = if let Some(expr_fixme_position) = self
issue.insertion_start = if let Some(expr_fixme_position) = self
.expr_fixme_positions
.get(&(issue.pos.start_offset, issue.pos.end_offset))
{
Expand All @@ -129,7 +129,7 @@ impl FunctionAnalysisData {
}

fn add_issue_fixme(&mut self, issue: &Issue) -> bool {
if let Some(insertion_start) = &issue.pos.insertion_start {
if let Some(insertion_start) = &issue.insertion_start {
self.add_replacement(
(insertion_start.offset, insertion_start.offset),
Replacement::Substitute(
Expand Down
16 changes: 3 additions & 13 deletions src/analyzer/functionlike_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl<'a> FunctionLikeAnalyzer<'a> {
HPos::new(
stmt.name.pos(),
self.file_analyzer.get_file_source().file_path,
None,
),
));
};
Expand All @@ -87,7 +86,6 @@ impl<'a> FunctionLikeAnalyzer<'a> {
HPos::new(
stmt.name.pos(),
self.file_analyzer.get_file_source().file_path,
None,
),
));
};
Expand Down Expand Up @@ -144,11 +142,7 @@ impl<'a> FunctionLikeAnalyzer<'a> {
None => {
return Err(AnalysisError::InternalError(
"Cannot get closure storage".to_string(),
HPos::new(
&stmt.span,
self.file_analyzer.get_file_source().file_path,
None,
),
HPos::new(&stmt.span, self.file_analyzer.get_file_source().file_path),
));
}
Some(value) => value,
Expand Down Expand Up @@ -218,11 +212,7 @@ impl<'a> FunctionLikeAnalyzer<'a> {
} else {
return Err(AnalysisError::InternalError(
"Cannot resolve method name".to_string(),
HPos::new(
&stmt.name.0,
self.file_analyzer.get_file_source().file_path,
None,
),
HPos::new(&stmt.name.0, self.file_analyzer.get_file_source().file_path),
));
};

Expand All @@ -247,7 +237,7 @@ impl<'a> FunctionLikeAnalyzer<'a> {
HPos::new(
&stmt.name.0,
self.file_analyzer.get_file_source().file_path,
None,

),
));
};
Expand Down
2 changes: 1 addition & 1 deletion src/analyzer/statements_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl<'a> StatementsAnalyzer<'a> {

#[inline]
pub fn get_hpos(&self, pos: &Pos) -> HPos {
HPos::new(pos, self.file_analyzer.get_file_source().file_path, None)
HPos::new(pos, self.file_analyzer.get_file_source().file_path)
}

#[inline]
Expand Down
5 changes: 1 addition & 4 deletions src/code_info/code_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ pub struct HPos {
pub end_line: u32,
pub start_column: u16,
pub end_column: u16,

pub insertion_start: Option<StmtStart>,
}

impl HPos {
pub fn new(pos: &Pos, file_path: FilePath, stmt_start: Option<StmtStart>) -> HPos {
pub fn new(pos: &Pos, file_path: FilePath) -> HPos {
let (start, end) = pos.to_start_and_end_lnum_bol_offset();
let (start_line, line_start_beginning_offset, start_offset) = start;
let (end_line, line_end_beginning_offset, end_offset) = end;
Expand All @@ -57,7 +55,6 @@ impl HPos {
end_offset: end_offset as u32,
start_column: start_column as u16,
end_column: end_column as u16,
insertion_start: stmt_start,
}
}
}
8 changes: 7 additions & 1 deletion src/code_info/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use rustc_hash::FxHashSet;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumString};

use crate::{code_location::HPos, function_context::FunctionLikeIdentifier, taint::SinkType};
use crate::{
code_location::{HPos, StmtStart},
function_context::FunctionLikeIdentifier,
taint::SinkType,
};

#[derive(Clone, PartialEq, Eq, Hash, Display, Debug, Serialize, Deserialize, EnumString)]
pub enum IssueKind {
Expand Down Expand Up @@ -218,6 +222,7 @@ pub struct Issue {
pub can_fix: bool,
pub fixme_added: bool,
pub symbol: (StrId, StrId),
pub insertion_start: Option<StmtStart>,
}

impl Issue {
Expand All @@ -241,6 +246,7 @@ impl Issue {
pos,
can_fix: false,
fixme_added: false,
insertion_start: None,
}
}

Expand Down
34 changes: 11 additions & 23 deletions src/code_info_builder/classlike_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ pub(crate) fn scan(
ast_nodes: &mut Vec<DefSignatureNode>,
all_uses: &Uses,
) -> bool {
let definition_location = HPos::new(&classlike_node.span, file_source.file_path, None);
let name_location = HPos::new(classlike_node.name.pos(), file_source.file_path, None);
let definition_location = HPos::new(&classlike_node.span, file_source.file_path);
let name_location = HPos::new(classlike_node.name.pos(), file_source.file_path);

let mut meta_start = MetaStart {
start_offset: definition_location.start_offset,
Expand Down Expand Up @@ -706,7 +706,7 @@ fn visit_xhp_attribute(
) {
let mut attribute_type_location = None;
let mut attribute_type = if let Some(hint) = &xhp_attribute.0 .1 {
attribute_type_location = Some(HPos::new(&hint.0, file_source.file_path, None));
attribute_type_location = Some(HPos::new(&hint.0, file_source.file_path));
get_type_from_hint(
&hint.1,
None,
Expand All @@ -733,7 +733,7 @@ fn visit_xhp_attribute(
attribute_type.types.push(TAtomic::TNull);
}

let mut stmt_pos = HPos::new(&xhp_attribute.1.span, file_source.file_path, None);
let mut stmt_pos = HPos::new(&xhp_attribute.1.span, file_source.file_path);

if let Some(type_hint) = &xhp_attribute.0 .1 {
let (line, bol, offset) = type_hint.0.to_start_and_end_lnum_bol_offset().0;
Expand Down Expand Up @@ -781,7 +781,7 @@ fn visit_xhp_attribute(
is_constant: false,
});

let name_pos = HPos::new(xhp_attribute.1.id.pos(), file_source.file_path, None);
let name_pos = HPos::new(xhp_attribute.1.id.pos(), file_source.file_path);

let property_storage = PropertyInfo {
is_static: false,
Expand Down Expand Up @@ -832,14 +832,10 @@ fn visit_class_const_declaration(
supplied_type_hint.0.start_offset() as u32,
);

supplied_type_location = Some(HPos::new(
&supplied_type_hint.0,
file_source.file_path,
None,
));
supplied_type_location = Some(HPos::new(&supplied_type_hint.0, file_source.file_path));
}

let def_pos = HPos::new(&const_node.span, file_source.file_path, None);
let def_pos = HPos::new(&const_node.span, file_source.file_path);

let name = interner.intern(const_node.id.1.clone());

Expand Down Expand Up @@ -927,7 +923,7 @@ fn visit_class_typeconst_declaration(
),
};

let def_pos = HPos::new(&const_node.span, file_source.file_path, None);
let def_pos = HPos::new(&const_node.span, file_source.file_path);

let name = interner.intern(const_node.name.1.clone());

Expand Down Expand Up @@ -982,14 +978,10 @@ fn visit_property_declaration(
property_type_hint.0.start_offset() as u32,
);

property_type_location = Some(HPos::new(
&property_type_hint.0,
file_source.file_path,
None,
));
property_type_location = Some(HPos::new(&property_type_hint.0, file_source.file_path));
}

let def_pos = HPos::new(&property_node.span, file_source.file_path, None);
let def_pos = HPos::new(&property_node.span, file_source.file_path);

let property_ref_id = interner.intern(property_node.id.1.clone());

Expand Down Expand Up @@ -1040,11 +1032,7 @@ fn visit_property_declaration(
}
ast_defs::Visibility::Protected => MemberVisibility::Protected,
},
pos: Some(HPos::new(
property_node.id.pos(),
file_source.file_path,
None,
)),
pos: Some(HPos::new(property_node.id.pos(), file_source.file_path)),
kind: PropertyKind::Property,
stmt_pos: Some(def_pos),
type_pos: property_type_location,
Expand Down
19 changes: 7 additions & 12 deletions src/code_info_builder/functionlike_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub(crate) fn get_functionlike(
file_source: &FileSource,
user_defined: bool,
) -> FunctionLikeInfo {
let definition_location = HPos::new(def_pos, file_source.file_path, None);
let definition_location = HPos::new(def_pos, file_source.file_path);

let mut suppressed_issues = vec![];

Expand Down Expand Up @@ -288,10 +288,6 @@ pub(crate) fn get_functionlike(
}
}

type_context
.template_type_map
.extend(functionlike_info.template_types.clone());

for row in &functionlike_info.template_types {
let mut matched = false;
for existing_template_type in type_context.template_type_map.iter_mut() {
Expand Down Expand Up @@ -416,12 +412,11 @@ pub(crate) fn get_functionlike(
}

if let Some(ret) = &ret.1 {
functionlike_info.return_type_location =
Some(HPos::new(&ret.0, file_source.file_path, None));
functionlike_info.return_type_location = Some(HPos::new(&ret.0, file_source.file_path));
}

if let Some(name_pos) = name_pos {
functionlike_info.name_location = Some(HPos::new(name_pos, file_source.file_path, None));
functionlike_info.name_location = Some(HPos::new(name_pos, file_source.file_path));
}

if !suppressed_issues.is_empty() {
Expand Down Expand Up @@ -593,7 +588,7 @@ pub(crate) fn adjust_location_from_comments(
if let Some(Ok(issue_kind)) =
get_issue_from_comment(trimmed_text, all_custom_issues)
{
let comment_pos = HPos::new(comment_pos, file_source.file_path, None);
let comment_pos = HPos::new(comment_pos, file_source.file_path);
suppressed_issues.push((issue_kind, comment_pos));
}

Expand All @@ -617,7 +612,7 @@ fn convert_param_nodes(
param_nodes
.iter()
.map(|param_node| {
let mut location = HPos::new(&param_node.pos, file_source.file_path, None);
let mut location = HPos::new(&param_node.pos, file_source.file_path);

if let Some(param_type) = &param_node.type_hint.1 {
location.start_offset = param_type.0.start_offset() as u32;
Expand Down Expand Up @@ -652,7 +647,7 @@ fn convert_param_nodes(
let mut param = FunctionLikeParameter::new(
param_node.name.clone(),
location,
HPos::new(&param_node.pos, file_source.file_path, None),
HPos::new(&param_node.pos, file_source.file_path),
);

if !suppressed_issues.is_empty() {
Expand All @@ -677,7 +672,7 @@ fn convert_param_nodes(
.type_hint
.1
.as_ref()
.map(|param_type| HPos::new(&param_type.0, file_source.file_path, None));
.map(|param_type| HPos::new(&param_type.0, file_source.file_path));
for user_attribute in &param_node.user_attributes {
let name = resolved_names
.get(&(user_attribute.name.0.start_offset() as u32))
Expand Down
Loading

0 comments on commit 3d4a618

Please sign in to comment.