Skip to content

Commit

Permalink
Report parser error for invalid file
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 18, 2024
1 parent a105652 commit eb4ba4a
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 45 deletions.
8 changes: 1 addition & 7 deletions src/aast_utils/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use aast_parser::rust_aast_parser_types::Env as AastParserEnv;

use hakana_reflection_info::code_location::{FilePath, HPos};
use hakana_reflection_info::file_info::ParserError;
use hakana_str::{StrId, ThreadedInterner};
use name_context::NameContext;
use naming_visitor::Scanner;
Expand All @@ -17,13 +18,6 @@ use std::sync::Arc;
pub mod name_context;
mod naming_visitor;

#[derive(Debug)]
pub enum ParserError {
CannotReadFile,
NotAHackFile,
SyntaxError { message: String, pos: HPos },
}

pub fn get_aast_for_path_and_contents(
file_path: FilePath,
file_path_str: &str,
Expand Down
4 changes: 2 additions & 2 deletions src/code_info/codebase_info/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::property_info::PropertyInfo;
use crate::t_atomic::TAtomic;
use crate::t_union::TUnion;
use crate::type_definition_info::TypeDefinitionInfo;
use hakana_str::StrId;
use crate::{class_constant_info::ConstantInfo, code_location::FilePath};
use hakana_str::StrId;
use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -396,7 +396,7 @@ impl CodebaseInfo {

pub fn has_invalid_file(&self, file_path: &FilePath) -> bool {
if let Some(file_info) = self.files.get(file_path) {
!file_info.valid_file
file_info.parser_error.is_some()
} else {
false
}
Expand Down
11 changes: 9 additions & 2 deletions src/code_info/file_info.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
use serde::{Deserialize, Serialize};

use crate::ast_signature::DefSignatureNode;
use crate::{ast_signature::DefSignatureNode, code_location::HPos};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum ParserError {
CannotReadFile,
NotAHackFile,
SyntaxError { message: String, pos: HPos },
}

#[derive(Clone, Serialize, Deserialize, Debug, Default)]
pub struct FileInfo {
pub ast_nodes: Vec<DefSignatureNode>,
pub closure_refs: Vec<u32>,
pub valid_file: bool,
pub parser_error: Option<ParserError>,
}
2 changes: 1 addition & 1 deletion src/code_info_builder/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ pub fn collect_info_for_aast(
FileInfo {
closure_refs: checker.closure_refs,
ast_nodes: checker.ast_nodes,
valid_file: true,
parser_error: None,
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/file_scanner_analyzer/analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::{get_aast_for_path, update_progressbar, SuccessfulScanData};
use hakana_aast_helper::ParserError;
use hakana_analyzer::config::Config;
use hakana_analyzer::file_analyzer;
use hakana_logger::Logger;
use hakana_reflection_info::analysis_result::AnalysisResult;
use hakana_reflection_info::code_location::{FilePath, HPos};
use hakana_reflection_info::codebase_info::CodebaseInfo;
use hakana_reflection_info::file_info::ParserError;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_reflection_info::symbol_references::SymbolReferences;
use hakana_reflection_info::FileSource;
Expand Down
55 changes: 38 additions & 17 deletions src/file_scanner_analyzer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ pub(crate) mod populator;
use analyzer::analyze_files;
use diff::{mark_safe_symbols_from_diff, CachedAnalysis};
use file::{FileStatus, VirtualFileSystem};
use hakana_aast_helper::{get_aast_for_path_and_contents, ParserError};
use hakana_aast_helper::get_aast_for_path_and_contents;
use hakana_analyzer::config::Config;
use hakana_analyzer::dataflow::program_analyzer::{find_connections, find_tainted_data};
use hakana_logger::Logger;
use hakana_reflection_info::analysis_result::AnalysisResult;
use hakana_reflection_info::code_location::{FilePath, HPos};
use hakana_reflection_info::codebase_info::CodebaseInfo;
use hakana_reflection_info::data_flow::graph::{GraphKind, WholeProgramKind};
use hakana_reflection_info::file_info::ParserError;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_reflection_info::symbol_references::SymbolReferences;
use hakana_str::{Interner, StrId};
Expand Down Expand Up @@ -446,24 +447,44 @@ fn update_progressbar(percentage: u64, bar: Option<Arc<ProgressBar>>) {

fn add_invalid_files(scan_data: &SuccessfulScanData, analysis_result: &mut AnalysisResult) {
for (file_path, file_info) in &scan_data.codebase.files {
if !file_info.valid_file {
if let Some(parser_error) = &file_info.parser_error {
analysis_result.emitted_issues.insert(
*file_path,
vec![Issue::new(
IssueKind::InvalidHackFile,
"Invalid Hack file".to_string(),
HPos {
file_path: *file_path,
start_offset: 1,
end_offset: 1,
start_line: 1,
end_line: 1,
start_column: 1,
end_column: 1,
insertion_start: None,
},
&None,
)],
vec![match parser_error {
ParserError::NotAHackFile => Issue::new(
IssueKind::InvalidHackFile,
"Invalid Hack file".to_string(),
HPos {
file_path: *file_path,
start_offset: 0,
end_offset: 0,
start_line: 0,
end_line: 0,
start_column: 0,
end_column: 0,
insertion_start: None,
},
&None,
),
ParserError::CannotReadFile => Issue::new(
IssueKind::InvalidHackFile,
"Cannot read file".to_string(),
HPos {
file_path: *file_path,
start_offset: 0,
end_offset: 0,
start_line: 0,
end_line: 0,
start_column: 0,
end_column: 0,
insertion_start: None,
},
&None,
),
ParserError::SyntaxError { message, pos } => {
Issue::new(IssueKind::InvalidHackFile, message.clone(), *pos, &None)
}
}],
);
}
}
Expand Down
29 changes: 19 additions & 10 deletions src/file_scanner_analyzer/scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ use crate::get_aast_for_path;
use crate::SuccessfulScanData;
use ast_differ::get_diff;
use hakana_aast_helper::name_context::NameContext;
use hakana_aast_helper::ParserError;
use hakana_analyzer::config::Config;
use hakana_logger::Logger;
use hakana_reflection_info::code_location::FilePath;
use hakana_reflection_info::codebase_info::symbols::SymbolKind;
use hakana_reflection_info::codebase_info::CodebaseInfo;
use hakana_reflection_info::diff::CodebaseDiff;
use hakana_reflection_info::file_info::FileInfo;
use hakana_reflection_info::file_info::ParserError;
use hakana_reflection_info::FileSource;
use hakana_str::Interner;
use hakana_str::StrId;
Expand Down Expand Up @@ -306,7 +306,7 @@ pub fn scan_files(
.lookup(&file_path.0)
.to_string();

if let Ok(scanner_result) = scan_file(
match scan_file(
&str_path,
**file_path,
&config.all_custom_issues,
Expand All @@ -317,14 +317,23 @@ pub fn scan_files(
!test_patterns.iter().any(|p| p.matches(&str_path)),
&logger,
) {
resolved_names
.lock()
.unwrap()
.insert(**file_path, scanner_result);
} else {
resolved_names.lock().unwrap().remove(*file_path);
new_codebase.files.insert(**file_path, FileInfo::default());
invalid_files.lock().unwrap().push(**file_path);
Ok(scanner_result) => {
resolved_names
.lock()
.unwrap()
.insert(**file_path, scanner_result);
}
Err(parser_error) => {
resolved_names.lock().unwrap().remove(*file_path);
new_codebase.files.insert(
**file_path,
FileInfo {
parser_error: Some(parser_error),
..FileInfo::default()
},
);
invalid_files.lock().unwrap().push(**file_path);
}
}

update_progressbar(i as u64, bar.clone());
Expand Down
3 changes: 2 additions & 1 deletion src/file_scanner_analyzer/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hakana_aast_helper::get_aast_for_path_and_contents;
use hakana_aast_helper::name_context::NameContext;
use hakana_aast_helper::{get_aast_for_path_and_contents, ParserError};
use hakana_analyzer::config::Config;
use hakana_analyzer::dataflow::program_analyzer::find_tainted_data;
use hakana_analyzer::file_analyzer;
Expand All @@ -8,6 +8,7 @@ use hakana_reflection_info::analysis_result::AnalysisResult;
use hakana_reflection_info::code_location::{FilePath, HPos};
use hakana_reflection_info::codebase_info::CodebaseInfo;
use hakana_reflection_info::data_flow::graph::{GraphKind, WholeProgramKind};
use hakana_reflection_info::file_info::ParserError;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_reflection_info::symbol_references::SymbolReferences;
use hakana_reflection_info::FileSource;
Expand Down
7 changes: 5 additions & 2 deletions src/ttype/type_comparator/atomic_type_comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,11 @@ pub(crate) fn can_be_identical<'a>(
if let Some(as_type) = as_type {
type2_part = as_type.get_single();
} else if inside_assertion {
let type_alias_info = codebase.type_definitions.get(name).unwrap();
type2_part = type_alias_info.actual_type.get_single();
if let Some(type_alias_info) = codebase.type_definitions.get(name) {
type2_part = type_alias_info.actual_type.get_single();
} else {
break;
}
} else {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/diff/fileWithGitMarkersIncluded/output.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ERROR: InvalidHackFile - input.hack:1:1 - Invalid Hack file
ERROR: InvalidHackFile - input.hack:7:0 - A name is expected here.
ERROR: NonExistentFunction - main.hack:3:5 - Function foo is not defined
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
ERROR: InvalidHackFile - input.hack:1:1 - Invalid Hack file
ERROR: InvalidHackFile - input.hack:7:0 - A name is expected here.
ERROR: NonExistentFunction - main.hack:4:5 - Function foo is not defined

0 comments on commit eb4ba4a

Please sign in to comment.