From 3d4a618734c49f350df7afcd97434bd2ae20b1f7 Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Thu, 21 Mar 2024 10:16:51 -0400 Subject: [PATCH] Reduce size of HPos --- src/aast_utils/lib.rs | 3 +- src/analyzer/expr/assertion_finder.rs | 14 ++------ .../expr/call/expression_call_analyzer.rs | 4 +-- src/analyzer/expr/echo_analyzer.rs | 4 +-- src/analyzer/expr/exit_analyzer.rs | 4 +-- src/analyzer/expr/include_analyzer.rs | 4 +-- src/analyzer/function_analysis_data.rs | 4 +-- src/analyzer/functionlike_analyzer.rs | 16 ++------- src/analyzer/statements_analyzer.rs | 2 +- src/code_info/code_location.rs | 5 +-- src/code_info/issue.rs | 8 ++++- src/code_info_builder/classlike_scanner.rs | 34 ++++++------------- src/code_info_builder/functionlike_scanner.rs | 19 ++++------- src/code_info_builder/lib.rs | 7 ++-- src/file_scanner_analyzer/analyzer.rs | 3 -- src/file_scanner_analyzer/lib.rs | 2 -- src/file_scanner_analyzer/wasm.rs | 1 - 17 files changed, 47 insertions(+), 87 deletions(-) diff --git a/src/aast_utils/lib.rs b/src/aast_utils/lib.rs index c1726359..b3f39e9e 100644 --- a/src/aast_utils/lib.rs +++ b/src/aast_utils/lib.rs @@ -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, }) @@ -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, }, }); } diff --git a/src/analyzer/expr/assertion_finder.rs b/src/analyzer/expr/assertion_finder.rs index 45b697d5..62993cd5 100644 --- a/src/analyzer/expr/assertion_finder.rs +++ b/src/analyzer/expr/assertion_finder.rs @@ -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::{ @@ -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}; @@ -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) @@ -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) diff --git a/src/analyzer/expr/call/expression_call_analyzer.rs b/src/analyzer/expr/call/expression_call_analyzer.rs index 7a6572c1..d3c599bc 100644 --- a/src/analyzer/expr/call/expression_call_analyzer.rs +++ b/src/analyzer/expr/call/expression_call_analyzer.rs @@ -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; diff --git a/src/analyzer/expr/echo_analyzer.rs b/src/analyzer/expr/echo_analyzer.rs index f54e00b1..9daafa2c 100644 --- a/src/analyzer/expr/echo_analyzer.rs +++ b/src/analyzer/expr/echo_analyzer.rs @@ -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() { diff --git a/src/analyzer/expr/exit_analyzer.rs b/src/analyzer/expr/exit_analyzer.rs index b70f84d2..6ca76fe0 100644 --- a/src/analyzer/expr/exit_analyzer.rs +++ b/src/analyzer/expr/exit_analyzer.rs @@ -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() { diff --git a/src/analyzer/expr/include_analyzer.rs b/src/analyzer/expr/include_analyzer.rs index 32a19d11..98a39085 100644 --- a/src/analyzer/expr/include_analyzer.rs +++ b/src/analyzer/expr/include_analyzer.rs @@ -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)?; diff --git a/src/analyzer/function_analysis_data.rs b/src/analyzer/function_analysis_data.rs index 074c29f4..ad6fc093 100644 --- a/src/analyzer/function_analysis_data.rs +++ b/src/analyzer/function_analysis_data.rs @@ -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)) { @@ -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( diff --git a/src/analyzer/functionlike_analyzer.rs b/src/analyzer/functionlike_analyzer.rs index d10424db..c3befacb 100644 --- a/src/analyzer/functionlike_analyzer.rs +++ b/src/analyzer/functionlike_analyzer.rs @@ -63,7 +63,6 @@ impl<'a> FunctionLikeAnalyzer<'a> { HPos::new( stmt.name.pos(), self.file_analyzer.get_file_source().file_path, - None, ), )); }; @@ -87,7 +86,6 @@ impl<'a> FunctionLikeAnalyzer<'a> { HPos::new( stmt.name.pos(), self.file_analyzer.get_file_source().file_path, - None, ), )); }; @@ -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, @@ -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), )); }; @@ -247,7 +237,7 @@ impl<'a> FunctionLikeAnalyzer<'a> { HPos::new( &stmt.name.0, self.file_analyzer.get_file_source().file_path, - None, + ), )); }; diff --git a/src/analyzer/statements_analyzer.rs b/src/analyzer/statements_analyzer.rs index c31bdec1..9087ebc1 100644 --- a/src/analyzer/statements_analyzer.rs +++ b/src/analyzer/statements_analyzer.rs @@ -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] diff --git a/src/code_info/code_location.rs b/src/code_info/code_location.rs index 10156c3b..1582b68e 100644 --- a/src/code_info/code_location.rs +++ b/src/code_info/code_location.rs @@ -36,12 +36,10 @@ pub struct HPos { pub end_line: u32, pub start_column: u16, pub end_column: u16, - - pub insertion_start: Option, } impl HPos { - pub fn new(pos: &Pos, file_path: FilePath, stmt_start: Option) -> 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; @@ -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, } } } diff --git a/src/code_info/issue.rs b/src/code_info/issue.rs index 4a31cd65..c39f4004 100644 --- a/src/code_info/issue.rs +++ b/src/code_info/issue.rs @@ -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 { @@ -218,6 +222,7 @@ pub struct Issue { pub can_fix: bool, pub fixme_added: bool, pub symbol: (StrId, StrId), + pub insertion_start: Option, } impl Issue { @@ -241,6 +246,7 @@ impl Issue { pos, can_fix: false, fixme_added: false, + insertion_start: None, } } diff --git a/src/code_info_builder/classlike_scanner.rs b/src/code_info_builder/classlike_scanner.rs index c103b411..51e41b85 100644 --- a/src/code_info_builder/classlike_scanner.rs +++ b/src/code_info_builder/classlike_scanner.rs @@ -43,8 +43,8 @@ pub(crate) fn scan( ast_nodes: &mut Vec, 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, @@ -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, @@ -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; @@ -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, @@ -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()); @@ -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()); @@ -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()); @@ -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, diff --git a/src/code_info_builder/functionlike_scanner.rs b/src/code_info_builder/functionlike_scanner.rs index fe930455..da0a3421 100644 --- a/src/code_info_builder/functionlike_scanner.rs +++ b/src/code_info_builder/functionlike_scanner.rs @@ -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![]; @@ -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() { @@ -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() { @@ -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)); } @@ -617,7 +612,7 @@ fn convert_param_nodes( param_nodes .iter() .map(|param_node| { - let mut location = HPos::new(¶m_node.pos, file_source.file_path, None); + let mut location = HPos::new(¶m_node.pos, file_source.file_path); if let Some(param_type) = ¶m_node.type_hint.1 { location.start_offset = param_type.0.start_offset() as u32; @@ -652,7 +647,7 @@ fn convert_param_nodes( let mut param = FunctionLikeParameter::new( param_node.name.clone(), location, - HPos::new(¶m_node.pos, file_source.file_path, None), + HPos::new(¶m_node.pos, file_source.file_path), ); if !suppressed_issues.is_empty() { @@ -677,7 +672,7 @@ fn convert_param_nodes( .type_hint .1 .as_ref() - .map(|param_type| HPos::new(¶m_type.0, file_source.file_path, None)); + .map(|param_type| HPos::new(¶m_type.0, file_source.file_path)); for user_attribute in ¶m_node.user_attributes { let name = resolved_names .get(&(user_attribute.name.0.start_offset() as u32)) diff --git a/src/code_info_builder/lib.rs b/src/code_info_builder/lib.rs index e645b091..5085861f 100644 --- a/src/code_info_builder/lib.rs +++ b/src/code_info_builder/lib.rs @@ -137,7 +137,7 @@ impl<'ast> Visitor<'ast> for Scanner<'_> { .or_default() .insert(name); - let definition_location = HPos::new(&gc.name.0, self.file_source.file_path, None); + let definition_location = HPos::new(&gc.name.0, self.file_source.file_path); let uses_hash = get_uses_hash(self.uses.symbol_uses.get(&name).unwrap_or(&vec![])); @@ -159,7 +159,7 @@ impl<'ast> Visitor<'ast> for Scanner<'_> { ConstantInfo { pos: definition_location, type_pos: if let Some(t) = &gc.type_ { - Some(HPos::new(&t.0, self.file_source.file_path, None)) + Some(HPos::new(&t.0, self.file_source.file_path)) } else { None }, @@ -264,7 +264,7 @@ impl<'ast> Visitor<'ast> for Scanner<'_> { template_type_map.push((*param_name, h)); } - let mut definition_location = HPos::new(&typedef.span, self.file_source.file_path, None); + let mut definition_location = HPos::new(&typedef.span, self.file_source.file_path); if let Some(user_attribute) = typedef.user_attributes.first() { definition_location.start_line = user_attribute.name.0.line() as u32; @@ -392,7 +392,6 @@ impl<'ast> Visitor<'ast> for Scanner<'_> { HPos::new( shape_source_attribute.name.pos(), self.file_source.file_path, - None, ), source_types, ), diff --git a/src/file_scanner_analyzer/analyzer.rs b/src/file_scanner_analyzer/analyzer.rs index 73a421e8..571409ba 100644 --- a/src/file_scanner_analyzer/analyzer.rs +++ b/src/file_scanner_analyzer/analyzer.rs @@ -223,7 +223,6 @@ fn analyze_file( end_line: 0, start_column: 0, end_column: 0, - insertion_start: None, }, &None, )], @@ -252,7 +251,6 @@ fn analyze_file( end_line: 0, start_column: 0, end_column: 0, - insertion_start: None, }, &None, ), @@ -267,7 +265,6 @@ fn analyze_file( end_line: 0, start_column: 0, end_column: 0, - insertion_start: None, }, &None, ), diff --git a/src/file_scanner_analyzer/lib.rs b/src/file_scanner_analyzer/lib.rs index b6363bfa..a964814c 100644 --- a/src/file_scanner_analyzer/lib.rs +++ b/src/file_scanner_analyzer/lib.rs @@ -462,7 +462,6 @@ fn add_invalid_files(scan_data: &SuccessfulScanData, analysis_result: &mut Analy end_line: 0, start_column: 0, end_column: 0, - insertion_start: None, }, &None, ), @@ -477,7 +476,6 @@ fn add_invalid_files(scan_data: &SuccessfulScanData, analysis_result: &mut Analy end_line: 0, start_column: 0, end_column: 0, - insertion_start: None, }, &None, ), diff --git a/src/file_scanner_analyzer/wasm.rs b/src/file_scanner_analyzer/wasm.rs index b9847cab..407f34f4 100644 --- a/src/file_scanner_analyzer/wasm.rs +++ b/src/file_scanner_analyzer/wasm.rs @@ -289,7 +289,6 @@ pub fn analyze_single_file( end_line: 1, start_column: 1, end_column: 1, - insertion_start: None, }, &None, )],