Skip to content

Commit

Permalink
fix cli and record usedmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
He1pa committed Aug 8, 2022
1 parent 2740fce commit ba6160b
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 44 deletions.
3 changes: 3 additions & 0 deletions kclvm/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl From<SettingsFile> for ExecProgramArgs {
let mut args = Self::default();
if let Some(cli_configs) = settings.kcl_cli_configs {
args.k_filename_list = cli_configs.files.unwrap_or_default();
if args.k_filename_list.is_empty() {
args.k_filename_list = cli_configs.file.unwrap_or_default();
}
args.strict_range_check = cli_configs.strict_range_check.unwrap_or_default();
args.disable_none = cli_configs.disable_none.unwrap_or_default();
args.verbose = cli_configs.verbose.unwrap_or_default() as i32;
Expand Down
16 changes: 8 additions & 8 deletions kclvm/sema/src/resolver/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
//! Steps to define a new lint:
//! 1. Define a static instance of the `Lint` structure,e.g.,
//!
//! ```rust,no_run
//! ```ignore
//! pub static IMPORT_POSITION: &Lint = &Lint {
//! ...
//! }
//! ```
//!
//! 2. Define a lintpass, which is used to implement the checking process,e.g.,
//!
//! ```rust,no_run
//! ```ignore
//! declare_lint_pass!(ImportPosition => [IMPORT_POSITION]);
//! ```
//!
Expand All @@ -23,7 +23,7 @@
//!
//! 3. Implement the lintpass check process, e.g.,
//!
//! ```rust,no_run
//! ```ignore
//! impl LintPass for ImportPosition {
//! fn check_module(&mut self, handler: &mut Handler, ctx: &mut LintContext,module: &ast::Module){
//! ...
Expand All @@ -33,7 +33,7 @@
//!
//! 4. Add the `check_*` methods in lintpass to the macro `lint_methods`, or skip it if it exists
//!
//! ```rust,no_run
//! ```ignore
//! macro_rules! lint_methods {
//! ($macro:path, $args:tt) => (
//! $macro!($args, [
Expand All @@ -46,7 +46,7 @@
//! 5. Add the new lintpass to the macro `default_lint_passes`, noting that `:` is preceded and followed by
//! the name of the lintpass. e.g.,
//!
//! ```rust,no_run
//! ```ignore
//! macro_rules! default_lint_passes {
//! ($macro:path, $args:tt) => {
//! $macro!(
Expand Down Expand Up @@ -217,7 +217,7 @@ macro_rules! impl_lint_pass {

/// Call the `check_*` method of each lintpass in CombinedLintLass.check_*.
///
/// ```rust,no_run
/// ```ignore
/// fn check_ident(&mut self, handler: &mut Handler, ctx: &mut LintContext, id: &ast::Identifier, ){
/// self.LintPassA.check_ident(handler, ctx, id);
/// self.LintPassB.check_ident(handler, ctx, id);
Expand All @@ -233,7 +233,7 @@ macro_rules! expand_combined_lint_pass_method {

/// Expand all methods defined in macro `lint_methods` in the `CombinedLintLass`.
///
/// ```rust,no_run
/// ```ignore
/// fn check_ident(&mut self, handler: &mut Handler, ctx: &mut LintContext, id: &ast::Identifier){};
/// fn check_stmt(&mut self, handler: &mut Handler, ctx: &mut LintContext, module: &ast::Module){};
/// ...
Expand All @@ -249,7 +249,7 @@ macro_rules! expand_combined_lint_pass_methods {

/// Expand all definitions of `CombinedLintPass`. The results are as follows:
///
/// ```rust,no_run
/// ```ignore
/// pub struct CombinedLintPass {
/// LintPassA: LintPassA;
/// LintPassB: LintPassB;
Expand Down
63 changes: 46 additions & 17 deletions kclvm/sema/src/resolver/lint_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,17 @@ pub static REIMPORT: &Lint = &Lint {
declare_lint_pass!(ReImport => [REIMPORT]);

impl LintPass for ReImport {
fn check_import_stmt(
&mut self,
handler: &mut Handler,
ctx: &mut LintContext,
import_stmt: &ast::ImportStmt,
) {
match ctx.import_names.get_mut(&ctx.filename) {
Some(set) => {
if set.contains(&import_stmt.path) {
fn check_module(&mut self,handler: &mut Handler,ctx: &mut LintContext,module: &ast::Module) {
let mut import_names = IndexSet::<String>::new();
for stmt in &module.body {
if let ast::Stmt::Import(import_stmt) = &stmt.node {
if import_names.contains(&import_stmt.path) {
handler.add_warning(
WarningKind::ReimportWarning,
&[Message {
pos: Position {
filename: ctx.start_pos.filename.clone(),
line: ctx.start_pos.line,
filename: ctx.filename.clone(),
line: stmt.line,
column: None,
},
style: Style::Line,
Expand All @@ -172,14 +168,47 @@ impl LintPass for ReImport {
}],
);
} else {
set.insert(import_stmt.path.clone());
import_names.insert(import_stmt.path.clone());
}
}
None => {
let mut set = IndexSet::<String>::new();
set.insert(import_stmt.path.clone());
ctx.import_names.insert(ctx.filename.clone(), set);

}
}

}
// fn check_import_stmt(
// &mut self,
// handler: &mut Handler,
// ctx: &mut LintContext,
// import_stmt: &ast::ImportStmt,
// ) {
// match ctx.import_names.get_mut(&ctx.filename) {
// Some(set) => {
// if set.contains(&import_stmt.path) {
// handler.add_warning(
// WarningKind::ReimportWarning,
// &[Message {
// pos: Position {
// filename: ctx.start_pos.filename.clone(),
// line: ctx.start_pos.line,
// column: None,
// },
// style: Style::Line,
// message: format!(
// "Module '{}' is reimported multiple times",
// &import_stmt.name
// ),
// note: Some("Consider removing this statement".to_string()),
// }],
// );
// } else {
// set.insert(import_stmt.path.clone());
// }
// }
// None => {
// let mut set = IndexSet::<String>::new();
// set.insert(import_stmt.path.clone());
// ctx.import_names.insert(ctx.filename.clone(), set);
// }
// }
// }
}
5 changes: 4 additions & 1 deletion kclvm/sema/src/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ impl<'ctx> Resolver<'ctx> {
self.lint_check_module(&module);
}
}
for diag in &self.linter.handler.diagnostics {
self.handler.diagnostics.insert(diag.clone());
}
}
None => {}
}
Expand Down Expand Up @@ -138,7 +141,7 @@ pub fn resolve_program(program: &mut Program) -> ProgramScope {
Options {
raise_err: true,
config_auto_fix: false,
lint_check: false,
lint_check: true,
},
);
resolver.resolve_import();
Expand Down
8 changes: 7 additions & 1 deletion kclvm/sema/src/resolver/test_data/record_used_module.k
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import import_test.e
import import_test.f as g
import pkg
import math
import regex


schema Main(d.Parent):
mixin [c.TestOfMixin]
name?: str
age?: int = 18
person?: a.Person
list_union_type: [e.UnionType|int]
dict_union_type: {g.UnionType|int:float}
dict_union_type: {g.UnionType|int:float}

check:
regex.match(name, r"[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*") if name

if a._a > 1:
_c = 1
Expand Down
3 changes: 2 additions & 1 deletion kclvm/sema/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ fn test_resolve_program_cycle_reference_fail() {
"There is a circular reference between schema SchemaSub and SchemaBase",
"There is a circular reference between rule RuleBase and RuleSub",
"There is a circular reference between rule RuleSub and RuleBase",
"Module 'file2' imported but unused",
"Module 'file1' imported but unused",
];
assert_eq!(scope.diagnostics.len(), err_messages.len());
for (diag, msg) in scope.diagnostics.iter().zip(err_messages.iter()) {
Expand Down Expand Up @@ -219,7 +221,6 @@ fn test_lint() {
note: Some("Consider removing this statement".to_string()),
}],
);
resolver.linter.handler.emit();
for (d1, d2) in handler
.diagnostics
.iter()
Expand Down
6 changes: 4 additions & 2 deletions kclvm/sema/src/resolver/var.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::rc::Rc;

use crate::resolver::Resolver;
use crate::ty::TypeKind;
use kclvm_error::*;
Expand Down Expand Up @@ -85,8 +87,8 @@ impl<'ctx> Resolver<'ctx> {
}
} else {
if !pkgpath.is_empty() {
if let Some(module_scope) = self.scope.borrow_mut().elems.get_mut(pkgpath) {
module_scope.borrow_mut().used = true;
if let Some(obj) = self.scope.borrow().lookup(pkgpath) {
obj.borrow_mut().used = true;
}
}
// Load type
Expand Down
81 changes: 67 additions & 14 deletions kclvm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
#[macro_use]
extern crate clap;

use kclvm_error::Handler;
use kclvm_runner::{execute, ExecProgramArgs};

use clap::ArgMatches;
use kclvm_config::settings::{load_file, merge_settings, SettingsFile};
use kclvm_parser::load_program;
use kclvm_sema::resolver::resolve_program;

fn main() {
let matches = clap_app!(kcl =>
(@subcommand run =>
(@arg INPUT: ... "Sets the input file to use")
(@arg OUTPUT: -o --output +takes_value "Sets the LLVM IR/BC output file path")
(@arg SETTING: ... -Y --setting "Sets the input file to use")
(@arg SETTING: ... -Y --setting +takes_value "Sets the input file to use")
(@arg EMIT_TYPE: --emit +takes_value "Sets the emit type, expect (ast)")
(@arg BC_PATH: --bc +takes_value "Sets the linked LLVM bitcode file path")
(@arg verbose: -v --verbose "Print test information verbosely")
Expand All @@ -23,22 +25,73 @@ fn main() {
(@arg sort_key: -k --sort "Sort result keys")
(@arg ARGUMENT: ... -D --argument "Specify the top-level argument")
)
(@subcommand lint =>
(@arg INPUT: ... +takes_value "Sets the input file to use")
(@arg OUTPUT: -o --output +takes_value "Sets the LLVM IR/BC output file path")
(@arg SETTING: ... -Y --setting +takes_value "Sets the input file to use")
(@arg EMIT_TYPE: --emit +takes_value "Sets the emit type, expect (ast)")
(@arg BC_PATH: --bc +takes_value "Sets the linked LLVM bitcode file path")
(@arg verbose: -v --verbose "Print test information verbosely")
(@arg disable_none: -n --disable-none "Disable dumping None values")
(@arg debug: -d --debug "Run in debug mode (for developers only)")
(@arg sort_key: -k --sort "Sort result keys")
(@arg ARGUMENT: ... -D --argument "Specify the top-level argument")
(@arg ALLOW_WARNING: --allow_warning "Allow warning")
)
)
.get_matches();

if let Some(matches) = matches.subcommand_matches("run") {
if let Some(files) = matches.values_of("INPUT") {
let files: Vec<&str> = files.into_iter().collect::<Vec<&str>>();
// Config settings build
let settings = build_settings(&matches);
// Convert settings into execute arguments.
let args: ExecProgramArgs = settings.into();
// Parse AST program.
let program = load_program(&files, Some(args.get_load_program_options())).unwrap();
// Resolve AST program, generate libs, link libs and execute.
// TODO: The argument "plugin_agent" need to be read from python3.
execute(program, 0, &ExecProgramArgs::default()).unwrap();
} else {
println!("{}", matches.usage());
let (files, setting) = (matches.values_of("INPUT"), matches.values_of("SETTING"));
match (files, setting) {
(None, None) => {
println!("{}", matches.usage());
},
(_, _) => {
let mut files: Vec<&str> = match matches.values_of("INPUT") {
Some(files) => files.into_iter().collect::<Vec<&str>>(),
None => vec![],
};
// Config settings build
let settings = build_settings(&matches);
// Convert settings into execute arguments.
let args: ExecProgramArgs = settings.into();
files = if !files.is_empty() {files} else {args.get_files()};
// Parse AST program.
let program = load_program(&files, Some(args.get_load_program_options())).unwrap();
// Resolve AST program, generate libs, link libs and execute.
// TODO: The argument "plugin_agent" need to be read from python3.
execute(program, 0, &ExecProgramArgs::default()).unwrap();
},
}

} else if let Some(matches) = matches.subcommand_matches("lint") {
let (files, setting) = (matches.values_of("INPUT"), matches.values_of("SETTING"));
match (files, setting) {
(None, None) => {
println!("{}", matches.usage());
},
(_, _) => {
let mut files: Vec<&str> = match matches.values_of("INPUT") {
Some(files) => files.into_iter().collect::<Vec<&str>>(),
None => vec![],
};
// Config settings build
let settings = build_settings(&matches);
// Convert settings into execute arguments.
let args: ExecProgramArgs = settings.into();
files = if !files.is_empty() {files} else {args.get_files()};
// Parse AST program.
let mut program = load_program(&files, Some(args.get_load_program_options())).unwrap();
// Resolve AST program, generate libs, link libs and execute.
// TODO: The argument "plugin_agent" need to be read from python3.
let scope = resolve_program(&mut program);
let mut err_handler = Handler::default();
err_handler.diagnostics = scope.diagnostics.clone();
err_handler.emit();
// println!("map {:?}", map);
println!("total {:?} error", err_handler.diagnostics.len());
},
}
} else {
println!("{}", matches.usage());
Expand Down
Empty file added samples/a.json
Empty file.

0 comments on commit ba6160b

Please sign in to comment.