mirror of
https://github.com/kmc7468/cs420.git
synced 2025-12-12 21:18:45 +00:00
Nits
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ target
|
||||
*.zip
|
||||
rustfmt.toml
|
||||
**/*.rs.bk
|
||||
csmith
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<()> {
|
||||
@@ -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(())
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
11
src/utils.rs
11
src/utils.rs
@@ -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 {
|
||||
|
||||
@@ -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)) {
|
||||
@@ -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"))) {
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user