Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ck3-tiger/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,23 @@ struct Config {
sample_size: Option<usize>,
}

fn workspace_path(s: &str) -> PathBuf {
let p = PathBuf::from(s);
if p.is_relative() {
PathBuf::from("..").join(p)
} else {
p
}
}

fn bench_multiple(c: &mut Criterion) {
let content = fs::read_to_string(CONFIG_PATH).unwrap();
let config: Config = toml::from_str(&content).unwrap();
let mut modfile_paths = config.modfile_paths.iter().map(PathBuf::from).collect::<Vec<_>>();
let mut modfile_paths =
config.modfile_paths.iter().map(|p| workspace_path(p)).collect::<Vec<_>>();

if let Some(modfile_dir) = config.modfile_dir {
let modfile_dir = workspace_path(&modfile_dir);
let iter =
fs::read_dir(modfile_dir).unwrap().filter_map(|entry| entry.ok()).filter_map(|entry| {
entry.file_name().to_string_lossy().ends_with(".mod").then(|| entry.path())
Expand Down
2 changes: 1 addition & 1 deletion src/config_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ pub fn assert_one_key(assert_key: &str, block: &Block) {
.map(|(index, key)| PointedMessage {
loc: key.into_loc(),
length: 1,
msg: Some((if index == 0 { "It occurs here" } else { "and here" }).to_owned()),
msg: Some((if index == 0 { "It occurs here" } else { "and here" }).into()),
})
.collect();
err(ErrorKey::Config)
Expand Down
44 changes: 22 additions & 22 deletions src/report/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,32 +316,32 @@ pub fn add_loaded_dlc_root(label: String) {
}

/// Store an error report to be emitted when [`emit_reports`] is called.
pub fn log((report, mut pointers): LogReport) {
let mut vec = Vec::new();
pointers.drain(..).for_each(|pointer| {
let index = vec.len();
recursive_pointed_msg_expansion(&mut vec, &pointer);
vec.insert(index, pointer);
});
pointers.extend(vec);
pub fn log((report, pointers): LogReport) {
let pointers = pointed_msg_expansion(pointers);
Errors::get_mut().push_report(report, pointers);
}

/// Expand `PointedMessage` recursively.
/// That is; for the given `PointedMessage`, follow its location's link until such link is no
/// longer available, adding a newly created `PointedMessage` to the given `Vec` for each linked
/// Expand `Vec<PointedMessage>`.
/// That is; for each `PointedMessage`, follow its location's link until such link is no
/// longer available, adding a newly created `PointedMessage` to the returned `Vec` for each linked
/// location.
fn recursive_pointed_msg_expansion(vec: &mut LogReportPointers, pointer: &PointedMessage) {
if let Some(link) = pointer.loc.link_idx {
let from_here = PointedMessage {
loc: MACRO_MAP.get_loc(link).unwrap(),
length: 1,
msg: Some("from here".to_owned()),
};
let index = vec.len();
recursive_pointed_msg_expansion(vec, &from_here);
vec.insert(index, from_here);
}
fn pointed_msg_expansion(pointers: Vec<PointedMessage>) -> Vec<PointedMessage> {
pointers
.into_iter()
.flat_map(|p| {
let mut next_loc = Some(p.loc);
std::iter::from_fn(move || match next_loc {
Some(mut stack) => {
next_loc = stack.link_idx.and_then(|idx| MACRO_MAP.get_loc(idx));
stack.link_idx = None;
let next =
PointedMessage { loc: stack, length: 1, msg: Some("from here".into()) };
Some(next)
}
None => None,
})
})
.collect()
}

/// Tests whether the report might be printed. If false, the report will definitely not be printed.
Expand Down
25 changes: 23 additions & 2 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! in the game files they came from.

use std::borrow::{Borrow, Cow};
use std::cmp::Ordering;
use std::ffi::OsStr;
use std::fmt::{Debug, Display, Error, Formatter};
use std::hash::Hash;
Expand All @@ -14,11 +15,11 @@ use bumpalo::Bump;

use crate::date::Date;
use crate::fileset::{FileEntry, FileKind};
use crate::macros::MacroMapIndex;
use crate::macros::{MacroMapIndex, MACRO_MAP};
use crate::pathtable::{PathTable, PathTableIndex};
use crate::report::{err, untidy, ErrorKey};

#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
pub struct Loc {
pub(crate) idx: PathTableIndex,
pub kind: FileKind,
Expand Down Expand Up @@ -58,6 +59,26 @@ impl Loc {
}
}

impl PartialOrd for Loc {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for Loc {
fn cmp(&self, other: &Self) -> Ordering {
self.idx
.cmp(&other.idx)
.then(self.line.cmp(&other.line))
.then(self.column.cmp(&other.column))
.then(
self.link_idx
.map(|link| MACRO_MAP.get_loc(link))
.cmp(&other.link_idx.map(|link| MACRO_MAP.get_loc(link))),
)
}
}

impl From<&FileEntry> for Loc {
fn from(entry: &FileEntry) -> Self {
if let Some(idx) = entry.path_idx() {
Expand Down
12 changes: 11 additions & 1 deletion vic3-tiger/benches/criterion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,23 @@ struct Config {
sample_size: Option<usize>,
}

fn workspace_path(s: &str) -> PathBuf {
let p = PathBuf::from(s);
if p.is_relative() {
PathBuf::from("..").join(p)
} else {
p
}
}

fn bench_multiple(c: &mut Criterion) {
Game::set(Game::Vic3).unwrap();
let content = fs::read_to_string(CONFIG_PATH).unwrap();
let config: Config = toml::from_str(&content).unwrap();
let mut mod_paths = config.mod_paths.iter().map(PathBuf::from).collect::<Vec<_>>();
let mut mod_paths = config.mod_paths.iter().map(|p| workspace_path(p)).collect::<Vec<_>>();

if let Some(mod_dir) = config.mod_dir {
let mod_dir = workspace_path(&mod_dir);
let iter =
fs::read_dir(mod_dir).unwrap().filter_map(|entry| entry.ok()).filter_map(|entry| {
entry.path().join(".metadata/metadata.json").is_file().then(|| entry.path())
Expand Down
Loading