Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Janggun committed Feb 11, 2025
1 parent 0758005 commit a8e0aa5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ target
*.zip
rustfmt.toml
**/*.rs.bk
csmith
2 changes: 2 additions & 0 deletions src/ir/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ pub enum Dtype {
/// TODO(document)
Struct {
/// TODO(document)
// FIXME: Why is this an option?
name: Option<String>,

/// TODO(document)
// FIXME: Just use vec for empty set of fields, no need for option?
fields: Option<Vec<Named<Dtype>>>,

/// TODO(document)
Expand Down
10 changes: 7 additions & 3 deletions src/ir/visualize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::collections::HashMap;

use crate::ir::*;
use crate::{some_or, Translate};
use crate::Translate;

#[derive(Default, Debug)]
pub struct Visualizer {
Expand All @@ -29,7 +29,9 @@ impl Translate<TranslationUnit> for Visualizer {
signature,
definition,
} => {
let definition = some_or!(definition, continue);
let Some(definition) = definition else {
continue;
};
let subgraph = self.translate_function(name, signature, definition)?;
subgraphs.push(subgraph);
}
Expand All @@ -41,7 +43,9 @@ impl Translate<TranslationUnit> for Visualizer {
// Add edges between subgraphs
for (name, decl) in &source.decls {
if let Declaration::Function { definition, .. } = decl {
let definition = some_or!(definition, continue);
let Some(definition) = definition else {
continue;
};

for (bid, block) in &definition.blocks {
for (iid, instruction) in block.instructions.iter().enumerate() {
Expand Down
13 changes: 7 additions & 6 deletions src/ir/write_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::io::{Result, Write};

use crate::ir::*;
use crate::write_base::*;
use crate::*;

impl WriteLine for TranslationUnit {
fn write_line(&self, indent: usize, write: &mut dyn Write) -> Result<()> {
Expand Down Expand Up @@ -36,14 +35,16 @@ impl WriteLine for TranslationUnit {
}

for (name, decl) in &self.decls {
let _ = some_or!(decl.get_variable(), continue);
(name, decl).write_line(indent, write)?;
if decl.get_variable().is_some() {
(name, decl).write_line(indent, write)?;
}
}

for (name, decl) in &self.decls {
let _ = some_or!(decl.get_function(), continue);
writeln!(write)?;
(name, decl).write_line(indent, write)?;
if decl.get_function().is_some() {
writeln!(write)?;
(name, decl).write_line(indent, write)?;
}
}

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/irgen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ fn name_of_params_from_derived_declarators(

#[inline]
fn name_of_parameter_declaration(parameter_declaration: &ParameterDeclaration) -> Option<String> {
let declarator = some_or!(parameter_declaration.declarator.as_ref(), return None);
let declarator = parameter_declaration.declarator.as_ref()?;
Some(name_of_declarator(&declarator.node))
}

Expand Down
2 changes: 1 addition & 1 deletion src/opt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
T: Optimize<ir::FunctionDefinition>,
{
fn optimize(&mut self, code: &mut ir::Declaration) -> bool {
let (_fsig, fdef) = some_or!(code.get_function_mut(), return false);
let (_, fdef) = some_or!(code.get_function_mut(), return false);
let fdef = some_or!(fdef, return false);
self.inner.optimize(fdef)
}
Expand Down
11 changes: 0 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
#[macro_export]
/// Ok or executing the given expression.
macro_rules! ok_or {
($e:expr_2021, $err:expr_2021) => {{
match $e {
Ok(r) => r,
Err(_) => $err,
}
}};
}

#[macro_export]
/// Some or executing the given expression.
macro_rules! some_or {
Expand Down
8 changes: 3 additions & 5 deletions tests/test_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ where
F: Fn(&Path),
{
let dir = path.read_dir().expect("read_dir call failed");
for entry in dir {
let entry = ok_or!(entry, continue);
for entry in dir.filter_map(Result::ok) {
let path = entry.path();

if !(path.is_file() && path.extension() == Some(ext)) {
Expand All @@ -24,8 +23,7 @@ where
fn test_opt_between_dirs<O: Optimize<ir::TranslationUnit>>(from: &Path, to: &Path, opt: &mut O) {
let from_dir = from.read_dir().expect("read_dir call failed");

for entry in from_dir {
let entry = ok_or!(entry, continue);
for entry in from_dir.filter_map(Result::ok) {
let from_file_path = entry.path();

if !(from_file_path.is_file() && from_file_path.extension() == Some(OsStr::new("ir"))) {
Expand All @@ -34,7 +32,7 @@ fn test_opt_between_dirs<O: Optimize<ir::TranslationUnit>>(from: &Path, to: &Pat

let file_name = from_file_path
.strip_prefix(from)
.expect("`from_file _path` must have file name");
.expect("`from_file_path` must have file name");
let to_file_path = to.join(file_name);

assert!(from_file_path.is_file());
Expand Down

0 comments on commit a8e0aa5

Please # to comment.