Skip to content

Commit 473b40b

Browse files
authored
Move c_macros generation into the enums crate (#676)
* Move the c_macros generator in the enums crate In this way macros for the C language are not generated during rust-code-analysis build * Create macros file automatically * Apply generated macros to the code * Format macros * Replace c_macros phf_sets with a const array * Regenerate c_macros * Remove phf dependency
1 parent b9d2ab7 commit 473b40b

18 files changed

Lines changed: 384 additions & 178 deletions

‎Cargo.lock‎

Lines changed: 11 additions & 130 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ readme = "README.md"
99
keywords = ["metrics"]
1010
description = "Tool to compute and export code metrics"
1111
license = "MPL-2.0"
12-
build = "build.rs"
13-
14-
[build-dependencies]
15-
phf_codegen = "^0.10"
1612

1713
[dependencies]
1814
aho-corasick = "^0.7"
@@ -24,7 +20,6 @@ num = "^0.4"
2420
num-derive = "^0.3"
2521
num-traits = "^0.2"
2622
petgraph = "^0.6"
27-
phf = { version = "^0.10", features = ["macros"] }
2823
regex = "^1.5"
2924
serde = { version = "^1.0", features = ["derive"] }
3025
termcolor = "^1.1"

‎build.rs‎

Lines changed: 0 additions & 33 deletions
This file was deleted.

‎enums/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = "2018"
88
enum-iterator = "^0.7"
99
clap = "^2.33"
1010
askama = "^0.10"
11-
phf_codegen = "^0.10"
1211

1312
tree-sitter = "0.19.3"
1413
tree-sitter-java = "0.19.0"
File renamed without changes.
File renamed without changes.

‎enums/src/main.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ fn main() {
5959
eprintln!("{:?}", err);
6060
}
6161
}
62+
"c_macros" => {
63+
if let Some(err) = generate_macros(output).err() {
64+
eprintln!("{:?}", err);
65+
}
66+
}
6267
_ => {
6368
eprintln!("Invalid target language: {}", language);
6469
}

‎enums/src/rust.rs‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
extern crate phf_codegen;
2-
31
use askama::Template;
42
use enum_iterator::IntoEnumIterator;
3+
use std::env;
54
use std::fs::File;
6-
use std::io::Write;
7-
use std::path::PathBuf;
5+
use std::io::{Read, Write};
6+
use std::path::{Path, PathBuf};
87

98
use crate::common::*;
109
use crate::languages::*;
1110

11+
const MACROS_DEFINITION_DIR: &str = "data";
12+
1213
#[derive(Template)]
1314
#[template(path = "rust.rs", escape = "none")]
1415
struct RustTemplate {
@@ -35,3 +36,48 @@ pub fn generate_rust(output: &str, file_template: &str) -> std::io::Result<()> {
3536

3637
Ok(())
3738
}
39+
40+
#[derive(Template)]
41+
#[template(path = "c_macros.rs", escape = "none")]
42+
struct CMacrosTemplate {
43+
u_name: String,
44+
l_name: String,
45+
names: Vec<String>,
46+
}
47+
48+
pub fn generate_macros(output: &str) -> std::io::Result<()> {
49+
create_macros_file(output, "c_macros", "PREDEFINED_MACROS")?;
50+
create_macros_file(output, "c_specials", "SPECIALS")
51+
}
52+
53+
fn create_macros_file(output: &str, filename: &str, u_name: &str) -> std::io::Result<()> {
54+
let mut macro_file = File::open(PathBuf::from(format!(
55+
"{}/{}/{}.txt",
56+
&env::var("CARGO_MANIFEST_DIR").unwrap(),
57+
MACROS_DEFINITION_DIR,
58+
filename
59+
)))?;
60+
let mut data = Vec::new();
61+
macro_file.read_to_end(&mut data)?;
62+
63+
let mut names = Vec::new();
64+
for tok in data.split(|c| *c == b'\n') {
65+
let tok = std::str::from_utf8(tok).unwrap().trim();
66+
if !tok.is_empty() {
67+
names.push(tok.to_owned());
68+
}
69+
}
70+
let l_name = u_name.to_lowercase();
71+
72+
let path = Path::new(output).join(format!("{}.rs", filename));
73+
74+
let mut file = File::create(&path)?;
75+
76+
let args = CMacrosTemplate {
77+
u_name: u_name.to_owned(),
78+
l_name,
79+
names,
80+
};
81+
82+
file.write_all(args.render().unwrap().as_bytes())
83+
}

0 commit comments

Comments
 (0)