|
| 1 | +#![feature(rustc_private)] |
| 2 | +extern crate rustc_ast; |
| 3 | +extern crate rustc_driver; |
| 4 | +extern crate rustc_error_codes; |
| 5 | +extern crate rustc_errors; |
| 6 | +extern crate rustc_hash; |
| 7 | +extern crate rustc_hir; |
| 8 | +extern crate rustc_interface; |
| 9 | +extern crate rustc_session; |
| 10 | +extern crate rustc_span; |
| 11 | + |
| 12 | +use rustc_errors::registry; |
| 13 | +use rustc_hash::{FxHashMap, FxHashSet}; |
| 14 | +use rustc_session::config; |
| 15 | +use rustc_session::config::CrateType; |
| 16 | +use rustc_span::source_map; |
| 17 | +use std::rc::Rc; |
| 18 | +use std::sync::Mutex; |
| 19 | + |
| 20 | +mod funtions_extractor; |
| 21 | + |
| 22 | +pub struct Source { |
| 23 | + code: String, |
| 24 | + filename: String, |
| 25 | +} |
| 26 | +impl Source { |
| 27 | + pub fn new(code: String, filename: String) -> Source { |
| 28 | + Source { code, filename } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +pub fn extract_functions(source: Source) { |
| 33 | + let Source { code, filename } = source; |
| 34 | + |
| 35 | + let config = rustc_interface::Config { |
| 36 | + // Command line options |
| 37 | + opts: config::Options { |
| 38 | + crate_types: vec![CrateType::Cdylib], |
| 39 | + ..config::Options::default() |
| 40 | + }, |
| 41 | + // cfg! configuration in addition to the default ones |
| 42 | + crate_cfg: FxHashSet::default(), // FxHashSet<(String, Option<String>)> |
| 43 | + input: config::Input::Str { |
| 44 | + name: source_map::FileName::Custom(filename.clone()), |
| 45 | + input: code.clone(), |
| 46 | + }, |
| 47 | + input_path: None, // Option<PathBuf> |
| 48 | + output_dir: None, // Option<PathBuf> |
| 49 | + output_file: None, // Option<PathBuf> |
| 50 | + file_loader: None, // Option<Box<dyn FileLoader + Send + Sync>> |
| 51 | + diagnostic_output: rustc_session::DiagnosticOutput::Default, |
| 52 | + // Set to capture stderr output during compiler execution |
| 53 | + stderr: None, // Option<Arc<Mutex<Vec<u8>>>> |
| 54 | + lint_caps: FxHashMap::default(), // FxHashMap<lint::LintId, lint::Level> |
| 55 | + // This is a callback from the driver that is called when [`ParseSess`] is created. |
| 56 | + parse_sess_created: None, //Option<Box<dyn FnOnce(&mut ParseSess) + Send>> |
| 57 | + // This is a callback from the driver that is called when we're registering lints; |
| 58 | + // it is called during plugin registration when we have the LintStore in a non-shared state. |
| 59 | + // |
| 60 | + // Note that if you find a Some here you probably want to call that function in the new |
| 61 | + // function being registered. |
| 62 | + register_lints: None, // Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>> |
| 63 | + // This is a callback from the driver that is called just after we have populated |
| 64 | + // the list of queries. |
| 65 | + // |
| 66 | + // The second parameter is local providers and the third parameter is external providers. |
| 67 | + override_queries: None, // Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)> |
| 68 | + // Registry of diagnostics codes. |
| 69 | + registry: registry::Registry::new(&rustc_error_codes::DIAGNOSTICS), |
| 70 | + make_codegen_backend: None, |
| 71 | + }; |
| 72 | + |
| 73 | + let rc = Rc::new(Mutex::new<Option<Vec<funtions_extractor::ParsedRustFunction>>>(None)); |
| 74 | + |
| 75 | + let rc_compiler = Rc::clone(&rc); |
| 76 | + |
| 77 | + rustc_interface::run_compiler(config, move |compiler| { |
| 78 | + compiler.enter(move |queries| { |
| 79 | + // Parse the program and print the syntax tree. |
| 80 | + let parsed_source: rustc_ast::Crate = queries.parse().unwrap().take(); |
| 81 | + |
| 82 | + let extracted_functions = rc_compiler.lock().unwrap(); |
| 83 | + |
| 84 | + extracted_functions = funtions_extractor::functionss_extractor(parsed_source) |
| 85 | + |
| 86 | + // tx.send(extracted_functions).unwrap(); |
| 87 | + }) |
| 88 | + }); |
| 89 | +} |
0 commit comments