Skip to content

Commit

Permalink
Move c_macros generation into the enums crate (#676)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Luni-4 authored Sep 29, 2021
1 parent b9d2ab7 commit 473b40b
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 178 deletions.
141 changes: 11 additions & 130 deletions Cargo.lock

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

5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ readme = "README.md"
keywords = ["metrics"]
description = "Tool to compute and export code metrics"
license = "MPL-2.0"
build = "build.rs"

[build-dependencies]
phf_codegen = "^0.10"

[dependencies]
aho-corasick = "^0.7"
Expand All @@ -24,7 +20,6 @@ num = "^0.4"
num-derive = "^0.3"
num-traits = "^0.2"
petgraph = "^0.6"
phf = { version = "^0.10", features = ["macros"] }
regex = "^1.5"
serde = { version = "^1.0", features = ["derive"] }
termcolor = "^1.1"
Expand Down
33 changes: 0 additions & 33 deletions build.rs

This file was deleted.

1 change: 0 additions & 1 deletion enums/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2018"
enum-iterator = "^0.7"
clap = "^2.33"
askama = "^0.10"
phf_codegen = "^0.10"

tree-sitter = "0.19.3"
tree-sitter-java = "0.19.0"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions enums/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ fn main() {
eprintln!("{:?}", err);
}
}
"c_macros" => {
if let Some(err) = generate_macros(output).err() {
eprintln!("{:?}", err);
}
}
_ => {
eprintln!("Invalid target language: {}", language);
}
Expand Down
54 changes: 50 additions & 4 deletions enums/src/rust.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
extern crate phf_codegen;

use askama::Template;
use enum_iterator::IntoEnumIterator;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};

use crate::common::*;
use crate::languages::*;

const MACROS_DEFINITION_DIR: &str = "data";

#[derive(Template)]
#[template(path = "rust.rs", escape = "none")]
struct RustTemplate {
Expand All @@ -35,3 +36,48 @@ pub fn generate_rust(output: &str, file_template: &str) -> std::io::Result<()> {

Ok(())
}

#[derive(Template)]
#[template(path = "c_macros.rs", escape = "none")]
struct CMacrosTemplate {
u_name: String,
l_name: String,
names: Vec<String>,
}

pub fn generate_macros(output: &str) -> std::io::Result<()> {
create_macros_file(output, "c_macros", "PREDEFINED_MACROS")?;
create_macros_file(output, "c_specials", "SPECIALS")
}

fn create_macros_file(output: &str, filename: &str, u_name: &str) -> std::io::Result<()> {
let mut macro_file = File::open(PathBuf::from(format!(
"{}/{}/{}.txt",
&env::var("CARGO_MANIFEST_DIR").unwrap(),
MACROS_DEFINITION_DIR,
filename
)))?;
let mut data = Vec::new();
macro_file.read_to_end(&mut data)?;

let mut names = Vec::new();
for tok in data.split(|c| *c == b'\n') {
let tok = std::str::from_utf8(tok).unwrap().trim();
if !tok.is_empty() {
names.push(tok.to_owned());
}
}
let l_name = u_name.to_lowercase();

let path = Path::new(output).join(format!("{}.rs", filename));

let mut file = File::create(&path)?;

let args = CMacrosTemplate {
u_name: u_name.to_owned(),
l_name,
names,
};

file.write_all(args.render().unwrap().as_bytes())
}
11 changes: 11 additions & 0 deletions enums/templates/c_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Code generated; DO NOT EDIT.

const {{ u_name }}: &[&str] = &[
{% for name in names -%}
"{{ name }}",
{% endfor %}
];

pub fn is_{{ l_name }}(mac: &str) -> bool {
{{ u_name }}.contains(&mac)
}
3 changes: 3 additions & 0 deletions recreate-grammars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ cargo clean --manifest-path ./enums/Cargo.toml
# Recreate all grammars
cargo run --manifest-path ./enums/Cargo.toml -- -lrust -o ./src/languages

# Recreate C macros
cargo run --manifest-path ./enums/Cargo.toml -- -lc_macros -o ./src/c_langs_macros

# Format the code of the recreated grammars
cargo fmt
Loading

0 comments on commit 473b40b

Please sign in to comment.