about summary refs log tree commit diff
path: root/src/tools/error_index_generator/build.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools/error_index_generator/build.rs')
-rw-r--r--src/tools/error_index_generator/build.rs64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/tools/error_index_generator/build.rs b/src/tools/error_index_generator/build.rs
new file mode 100644
index 00000000000..2ac7351fce4
--- /dev/null
+++ b/src/tools/error_index_generator/build.rs
@@ -0,0 +1,64 @@
+use walkdir::WalkDir;
+use std::path::PathBuf;
+use std::{env, fs};
+
+fn main() {
+    // The src directory (we are in src/tools/error_index_generator)
+    // Note that we could skip one of the .. but this ensures we at least loosely find the right
+    // directory.
+    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
+    let dest = out_dir.join("error_codes.rs");
+    let mut idx = 0;
+    for entry in WalkDir::new("../../../src") {
+        let entry = entry.unwrap();
+        if entry.file_name() == "error_codes.rs" {
+            println!("cargo:rerun-if-changed={}", entry.path().to_str().unwrap());
+            let file = fs::read_to_string(entry.path()).unwrap()
+                .replace("use syntax::{register_diagnostics, register_long_diagnostics};", "")
+                .replace("use syntax::register_diagnostics;", "")
+                .replace("use syntax::register_long_diagnostics;", "");
+            let contents = format!("(|| {{\n{}\n}})();", file);
+
+            fs::write(&out_dir.join(&format!("error_{}.rs", idx)), &contents).unwrap();
+
+            idx += 1;
+        }
+    }
+
+    let mut all = String::new();
+    all.push_str("fn register_all() -> Vec<(&'static str, Option<&'static str>)> {\n");
+    all.push_str("let mut long_codes: Vec<(&'static str, Option<&'static str>)> = Vec::new();\n");
+    all.push_str(r#"
+macro_rules! register_diagnostics {
+    ($($code:tt),*) => {{
+        long_codes.extend([$(
+            stringify!($code),
+        )*].iter().cloned().map(|s| (s, None)).collect::<Vec<_>>());
+    }};
+    ($($code:tt),*,) => {{
+        long_codes.extend([$(
+            stringify!($code),
+        )*].iter().cloned().map(|s| (s, None)));
+    }}
+}
+
+macro_rules! register_long_diagnostics {
+    ($($code:tt: $description:tt),*) => {
+        {long_codes.extend([$(
+            (stringify!($code), Some(stringify!($description))),
+        )*].iter());}
+    };
+    ($($code:tt: $description:tt),*,) => {
+        {long_codes.extend([$(
+            (stringify!($code), Some(stringify!($description))),
+        )*].iter());}
+    }
+}"#);
+    for idx in 0..idx {
+        all.push_str(&format!(r#"include!(concat!(env!("OUT_DIR"), "/error_{}.rs"));"#, idx));
+    }
+    all.push_str("\nlong_codes\n");
+    all.push_str("}\n");
+
+    fs::write(&dest, all).unwrap();
+}