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