Skip to content

Commit

Permalink
refactor: use compiler base session in resolver and remove un-used co…
Browse files Browse the repository at this point in the history
…de in the kclvm error crate. (#442)

* refactor: diagnostic 0-based column

* refactor: use compiler base session in resolver and remove un-used code in the kclvm error crate.

* feat: impl Into for PanicInfo and Diagnostic

* refactor: enhance file not found error message showing.
  • Loading branch information
Peefy committed Mar 10, 2023
1 parent 82c2be0 commit fb2beed
Show file tree
Hide file tree
Showing 34 changed files with 368 additions and 504 deletions.
24 changes: 21 additions & 3 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/ast_pretty/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ indexmap = "1.0"
fancy-regex = "0.7.1"
pretty_assertions = "1.3.0"
compiler_base_session = {path = "../../compiler_base/session", version = "0.0.12"}
compiler_base_macros = "0.0.1"
2 changes: 1 addition & 1 deletion kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::HashSet;

use compiler_base_macros::bug;
use kclvm_ast::{
ast::{self, CallExpr},
token::{DelimToken, TokenKind},
walker::MutSelfTypedResultWalker,
};
use kclvm_error::bug;

use super::{Indentation, Printer};

Expand Down
4 changes: 2 additions & 2 deletions kclvm/capi/src/service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl KclvmService {
let kcl_paths_str = kcl_paths.iter().map(|s| s.as_str()).collect::<Vec<&str>>();
let mut result = ExecProgram_Result::default();
let sess = Arc::new(Session::default());
let mut program = load_program(sess, &kcl_paths_str.as_slice(), Some(opts))?;
let mut program = load_program(sess.clone(), &kcl_paths_str.as_slice(), Some(opts))?;

if let Err(err) = apply_overrides(
&mut program,
Expand All @@ -105,7 +105,7 @@ impl KclvmService {
}

let start_time = SystemTime::now();
let exec_result = kclvm_runner::execute(program, self.plugin_agent, &native_args);
let exec_result = kclvm_runner::execute(sess, program, self.plugin_agent, &native_args);
let escape_time = match SystemTime::now().duration_since(start_time) {
Ok(dur) => dur.as_secs_f32(),
Err(err) => return Err(err.to_string()),
Expand Down
2 changes: 1 addition & 1 deletion kclvm/cmd/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn lint_command(matches: &ArgMatches) -> Result<()> {
(err_handler.diagnostics, warning_handler.diagnostics) =
lint_files(&files, Some(args.get_load_program_options()));
if matches.occurrences_of("emit_warning") > 0 {
warning_handler.emit();
warning_handler.emit()?;
}
err_handler.abort_if_any_errors();
Ok(())
Expand Down
5 changes: 2 additions & 3 deletions kclvm/compiler/src/codegen/llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1750,10 +1750,9 @@ impl<'ctx> LLVMCodeGenContext<'ctx> {
column: None,
},
);
handler.abort_if_errors()
} else {
result
handler.abort_if_any_errors()
}
result
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions kclvm/error/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ edition = "2021"
compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
compiler_base_session = {path = "../../compiler_base/session", version = "0.0.12"}
compiler_base_error = "0.0.8"
compiler_base_macros = "0.0.1"
kclvm-span = {path = "../span", version = "0.4.5"}
kclvm-runtime = {path = "../runtime", version = "0.4.5"}

anyhow = "1.0"
tracing = "0.1"
atty = "0.2"
termcolor = "1.0"
annotate-snippets = "0.8.0"
annotate-snippets = { version = "0.9.0", default-features = false, features = ["color"] }
termize = "0.1.1"
indexmap = "1.0"
48 changes: 0 additions & 48 deletions kclvm/error/src/bug.rs

This file was deleted.

42 changes: 3 additions & 39 deletions kclvm/error/src/diagnostic.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use kclvm_span::Loc;
use std::fmt;
use std::hash::Hash;

use indexmap::IndexSet;
use kclvm_span::Loc;
use termcolor::{Color, ColorSpec};

use crate::{ErrorKind, WarningKind};

/// Diagnostic structure.
Expand All @@ -19,7 +16,7 @@ pub struct Diagnostic {
/// line, and column location.
///
/// A Position is valid if the line number is > 0.
/// The line and column are both 1 based.
/// The line is 1-based and the column is 0-based.
#[derive(PartialEq, Clone, Eq, Hash, Debug, Default)]
pub struct Position {
pub filename: String,
Expand Down Expand Up @@ -89,7 +86,7 @@ impl From<Loc> for Position {
line: loc.line as u64,
column: if loc.col_display > 0 {
// Loc col is the (0-based) column offset.
Some(loc.col.0 as u64 + 1)
Some(loc.col.0 as u64)
} else {
None
},
Expand Down Expand Up @@ -156,22 +153,6 @@ impl Level {
Level::Note => "note",
}
}

pub fn color(&self) -> ColorSpec {
let mut spec = ColorSpec::new();
match self {
Level::Error => {
spec.set_fg(Some(Color::Red)).set_intense(true);
}
Level::Warning => {
spec.set_fg(Some(Color::Yellow)).set_intense(cfg!(windows));
}
Level::Note => {
spec.set_fg(Some(Color::Green)).set_intense(true);
}
}
spec
}
}

impl fmt::Display for Level {
Expand All @@ -189,20 +170,3 @@ pub enum Style {
LineAndColumn,
Line,
}

/// Classify diagnostics into errors and warnings.
pub fn classification(
diagnostics: &IndexSet<Diagnostic>,
) -> (IndexSet<Diagnostic>, IndexSet<Diagnostic>) {
let (mut errs, mut warnings) = (IndexSet::new(), IndexSet::new());
for diag in diagnostics {
if diag.level == Level::Error {
errs.insert(diag.clone());
} else if diag.level == Level::Warning {
warnings.insert(diag.clone());
} else {
continue;
}
}
(errs, warnings)
}
Loading

0 comments on commit fb2beed

Please sign in to comment.