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
This commit is contained in:
Luni-4 2021-09-29 13:49:30 +02:00 коммит произвёл GitHub
Родитель b9d2ab746d
Коммит 473b40be82
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
18 изменённых файлов: 384 добавлений и 178 удалений

141
Cargo.lock сгенерированный
Просмотреть файл

@ -74,7 +74,7 @@ dependencies = [
"mime",
"percent-encoding",
"pin-project 1.0.8",
"rand 0.7.3",
"rand",
"regex",
"serde",
"serde_json",
@ -351,7 +351,7 @@ dependencies = [
"log",
"mime",
"percent-encoding",
"rand 0.7.3",
"rand",
"serde",
"serde_json",
"serde_urlencoded",
@ -862,18 +862,7 @@ checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.2+wasi-snapshot-preview1",
"wasi",
]
[[package]]
@ -1359,60 +1348,6 @@ dependencies = [
"indexmap",
]
[[package]]
name = "phf"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9fc3db1018c4b59d7d582a739436478b6035138b6aecbce989fc91c3e98409f"
dependencies = [
"phf_macros",
"phf_shared",
"proc-macro-hack",
]
[[package]]
name = "phf_codegen"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb1c3a8bc4dd4e5cfce29b44ffc14bedd2ee294559a294e2a4d4c9e9a6a13cd"
dependencies = [
"phf_generator",
"phf_shared",
]
[[package]]
name = "phf_generator"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand 0.8.4",
]
[[package]]
name = "phf_macros"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58fdf3184dd560f160dd73922bea2d5cd6e8f064bf4b13110abd81b03697b4e0"
dependencies = [
"phf_generator",
"phf_shared",
"proc-macro-hack",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "phf_shared"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6796ad771acdc0123d2a88dc428b5e38ef24456743ddb1744ed628f9815c096"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project"
version = "0.4.28"
@ -1531,23 +1466,11 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"getrandom",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
dependencies = [
"libc",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
"rand_hc 0.3.1",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
@ -1557,17 +1480,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core 0.6.3",
"rand_core",
]
[[package]]
@ -1576,16 +1489,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
]
[[package]]
name = "rand_core"
version = "0.6.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
dependencies = [
"getrandom 0.2.3",
"getrandom",
]
[[package]]
@ -1594,16 +1498,7 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_hc"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
dependencies = [
"rand_core 0.6.3",
"rand_core",
]
[[package]]
@ -1655,8 +1550,6 @@ dependencies = [
"num-format",
"num-traits",
"petgraph",
"phf",
"phf_codegen",
"pretty_assertions",
"regex",
"serde",
@ -1871,12 +1764,6 @@ dependencies = [
"libc",
]
[[package]]
name = "siphasher"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "533494a8f9b724d33625ab53c6c4800f7cc445895924a8ef649222dcb76e938b"
[[package]]
name = "slab"
version = "0.4.4"
@ -2266,7 +2153,7 @@ dependencies = [
"idna",
"lazy_static",
"log",
"rand 0.7.3",
"rand",
"smallvec",
"thiserror",
"tokio",
@ -2378,12 +2265,6 @@ version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.78"

Просмотреть файл

@ -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"
@ -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"

Просмотреть файл

@ -1,33 +0,0 @@
extern crate phf_codegen;
use std::io::{BufWriter, Read, Write};
use std::path::{Path, PathBuf};
use std::{env, fs};
fn mk_predef(data_name: &str, set_name: &str) {
let mut set = phf_codegen::Set::new();
let mut file = fs::File::open(PathBuf::from(format!("./data/{}.txt", data_name))).unwrap();
let mut data = Vec::new();
file.read_to_end(&mut data).unwrap();
for tok in data.split(|c| *c == b'\n') {
let tok = std::str::from_utf8(tok).unwrap().trim();
if !tok.is_empty() {
set.entry(tok);
}
}
let path = Path::new(&env::var("OUT_DIR").unwrap()).join(format!("gen_{}.rs", data_name));
let mut file = BufWriter::new(fs::File::create(&path).unwrap());
writeln!(&mut file, "#[allow(clippy::unreadable_literal)]").unwrap();
writeln!(
&mut file,
"static {}: phf::Set<&'static str> =\n{};\n",
set_name,
set.build()
)
.unwrap();
}
fn main() {
mk_predef("c_macros", "PREDEFINED_MACROS");
mk_predef("c_specials", "SPECIALS");
}

Просмотреть файл

@ -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"

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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 {
@ -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())
}

Просмотреть файл

@ -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)
}

Просмотреть файл

@ -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

Просмотреть файл

@ -0,0 +1,226 @@
// Code generated; DO NOT EDIT.
const PREDEFINED_MACROS: &[&str] = &[
"INT16_C",
"INT16_MAX",
"INT16_MIN",
"INT32_C",
"INT32_MAX",
"INT32_MIN",
"INT64_C",
"INT64_MAX",
"INT64_MIN",
"INT8_C",
"INT8_MAX",
"INT8_MIN",
"INTMAX_MAX",
"INTMAX_MIN",
"INTPTR_MAX",
"INTPTR_MIN",
"INT_FAST16_MAX",
"INT_FAST16_MIN",
"INT_FAST32_MAX",
"INT_FAST32_MIN",
"INT_FAST64_MAX",
"INT_FAST64_MIN",
"INT_FAST8_MAX",
"INT_FAST8_MIN",
"INT_LEAST16_MAX",
"INT_LEAST16_MIN",
"INT_LEAST32_MAX",
"INT_LEAST32_MIN",
"INT_LEAST64_MAX",
"INT_LEAST64_MIN",
"INT_LEAST8_MAX",
"INT_LEAST8_MIN",
"PRIX16",
"PRIX32",
"PRIX64",
"PRIX8",
"PRIXFAST16",
"PRIXFAST32",
"PRIXFAST64",
"PRIXFAST8",
"PRIXLEAST16",
"PRIXLEAST32",
"PRIXLEAST64",
"PRIXLEAST8",
"PRIXMAX",
"PRIXPTR",
"PRId16",
"PRId32",
"PRId64",
"PRId8",
"PRIdFAST16",
"PRIdFAST32",
"PRIdFAST64",
"PRIdFAST8",
"PRIdLEAST16",
"PRIdLEAST32",
"PRIdLEAST64",
"PRIdLEAST8",
"PRIdMAX",
"PRIdPTR",
"PRIi16",
"PRIi32",
"PRIi64",
"PRIi8",
"PRIiFAST16",
"PRIiFAST32",
"PRIiFAST64",
"PRIiFAST8",
"PRIiLEAST16",
"PRIiLEAST32",
"PRIiLEAST64",
"PRIiLEAST8",
"PRIiMAX",
"PRIiPTR",
"PRIo16",
"PRIo32",
"PRIo64",
"PRIo8",
"PRIoFAST16",
"PRIoFAST32",
"PRIoFAST64",
"PRIoFAST8",
"PRIoLEAST16",
"PRIoLEAST32",
"PRIoLEAST64",
"PRIoLEAST8",
"PRIoMAX",
"PRIoPTR",
"PRIu16",
"PRIu32",
"PRIu64",
"PRIu8",
"PRIuFAST16",
"PRIuFAST32",
"PRIuFAST64",
"PRIuFAST8",
"PRIuLEAST16",
"PRIuLEAST32",
"PRIuLEAST64",
"PRIuLEAST8",
"PRIuMAX",
"PRIuPTR",
"PRIx16",
"PRIx32",
"PRIx64",
"PRIx8",
"PRIxFAST16",
"PRIxFAST32",
"PRIxFAST64",
"PRIxFAST8",
"PRIxLEAST16",
"PRIxLEAST32",
"PRIxLEAST64",
"PRIxLEAST8",
"PRIxMAX",
"PRIxPTR",
"SCNd16",
"SCNd32",
"SCNd64",
"SCNd8",
"SCNdFAST16",
"SCNdFAST32",
"SCNdFAST64",
"SCNdFAST8",
"SCNdLEAST16",
"SCNdLEAST32",
"SCNdLEAST64",
"SCNdLEAST8",
"SCNdMAX",
"SCNdPTR",
"SCNi16",
"SCNi32",
"SCNi64",
"SCNi8",
"SCNiFAST16",
"SCNiFAST32",
"SCNiFAST64",
"SCNiFAST8",
"SCNiLEAST16",
"SCNiLEAST32",
"SCNiLEAST64",
"SCNiLEAST8",
"SCNiMAX",
"SCNiPTR",
"SCNo16",
"SCNo32",
"SCNo64",
"SCNo8",
"SCNoFAST16",
"SCNoFAST32",
"SCNoFAST64",
"SCNoFAST8",
"SCNoLEAST16",
"SCNoLEAST32",
"SCNoLEAST64",
"SCNoLEAST8",
"SCNoMAX",
"SCNoPTR",
"SCNu16",
"SCNu32",
"SCNu64",
"SCNu8",
"SCNuFAST16",
"SCNuFAST32",
"SCNuFAST64",
"SCNuFAST8",
"SCNuLEAST16",
"SCNuLEAST32",
"SCNuLEAST64",
"SCNuLEAST8",
"SCNuMAX",
"SCNuPTR",
"SCNx16",
"SCNx32",
"SCNx64",
"SCNx8",
"SCNxFAST16",
"SCNxFAST32",
"SCNxFAST64",
"SCNxFAST8",
"SCNxLEAST16",
"SCNxLEAST32",
"SCNxLEAST64",
"SCNxLEAST8",
"SCNxMAX",
"SCNxPTR",
"UINT16_C",
"UINT16_MAX",
"UINT16_MIN",
"UINT32_C",
"UINT32_MAX",
"UINT32_MIN",
"UINT64_C",
"UINT64_MAX",
"UINT64_MIN",
"UINT8_C",
"UINT8_MAX",
"UINT8_MIN",
"UINTMAX_MAX",
"UINTMAX_MIN",
"UINTPTR_MAX",
"UINTPTR_MIN",
"UINT_FAST16_MAX",
"UINT_FAST16_MIN",
"UINT_FAST32_MAX",
"UINT_FAST32_MIN",
"UINT_FAST64_MAX",
"UINT_FAST64_MIN",
"UINT_FAST8_MAX",
"UINT_FAST8_MIN",
"UINT_LEAST16_MAX",
"UINT_LEAST16_MIN",
"UINT_LEAST32_MAX",
"UINT_LEAST32_MIN",
"UINT_LEAST64_MAX",
"UINT_LEAST64_MIN",
"UINT_LEAST8_MAX",
"UINT_LEAST8_MIN",
];
pub fn is_predefined_macros(mac: &str) -> bool {
PREDEFINED_MACROS.contains(&mac)
}

Просмотреть файл

@ -0,0 +1,67 @@
// Code generated; DO NOT EDIT.
const SPECIALS: &[&str] = &[
"NULL",
"bool",
"char",
"char16_t",
"char32_t",
"char64_t",
"char8_t",
"charptr_t",
"const",
"constexpr",
"double",
"explicit",
"false",
"float",
"inline",
"int",
"int16_t",
"int32_t",
"int64_t",
"int8_t",
"int_fast16_t",
"int_fast32_t",
"int_fast64_t",
"int_fast8_t",
"int_least16_t",
"int_least32_t",
"int_least64_t",
"int_least8_t",
"intmax_t",
"intptr_t",
"long",
"max_align_t",
"mutable",
"namespace",
"nullptr",
"ptrdiff_t",
"restrict",
"short",
"signed",
"size_t",
"ssize_t",
"static",
"true",
"uint16_t",
"uint32_t",
"uint64_t",
"uint8_t",
"uint_fast16_t",
"uint_fast32_t",
"uint_fast64_t",
"uint_fast8_t",
"uint_least16_t",
"uint_least32_t",
"uint_least64_t",
"uint_least8_t",
"uintmax_t",
"uintptr_t",
"unsigned",
"wchar_t",
];
pub fn is_specials(mac: &str) -> bool {
SPECIALS.contains(&mac)
}

Просмотреть файл

@ -0,0 +1,5 @@
mod c_macros;
pub use c_macros::*;
mod c_specials;
pub use c_specials::*;

Просмотреть файл

@ -1,7 +1,8 @@
use std::collections::HashSet;
use crate::c_langs_macros::is_predefined_macros;
const DOLLARS: [u8; 2048] = [b'$'; 2048];
include!(concat!(env!("OUT_DIR"), "/gen_c_macros.rs"));
#[inline(always)]
fn is_identifier_part(c: u8) -> bool {
@ -18,7 +19,7 @@ fn is_identifier_starter(c: u8) -> bool {
#[inline(always)]
fn is_macro<S: ::std::hash::BuildHasher>(mac: &str, macros: &HashSet<String, S>) -> bool {
macros.contains(mac) || PREDEFINED_MACROS.contains(mac)
macros.contains(mac) | is_predefined_macros(mac)
}
pub fn replace<S: ::std::hash::BuildHasher>(

Просмотреть файл

@ -56,6 +56,7 @@ extern crate serde;
#[macro_use]
mod asttools;
mod c_langs_macros;
mod c_macro;
#[macro_use]
mod macros;

Просмотреть файл

@ -4,6 +4,7 @@ use petgraph::{
use std::collections::{hash_map, HashMap, HashSet};
use std::path::{Path, PathBuf};
use crate::c_langs_macros::is_specials;
use crate::node::Node;
use crate::langs::*;
@ -11,8 +12,6 @@ use crate::languages::language_preproc::*;
use crate::tools::*;
use crate::traits::*;
include!(concat!(env!("OUT_DIR"), "/gen_c_specials.rs"));
/// Preprocessor data of a `C/C++` file.
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct PreprocFile {
@ -214,7 +213,7 @@ pub fn preprocess(parser: &PreprocParser, path: &Path, results: &mut PreprocResu
if identifier.kind_id() == Preproc::Identifier {
let r#macro = identifier.utf8_text(code).unwrap();
if !SPECIALS.contains(r#macro) {
if !is_specials(r#macro) {
file_result.macros.insert(r#macro.to_string());
}
}