]> git.lizzy.rs Git - rust.git/blob - src/librustc_interface/interface.rs
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
[rust.git] / src / librustc_interface / interface.rs
1 use crate::queries::Queries;
2 use crate::util;
3 use crate::profile;
4 pub use crate::passes::BoxedResolver;
5
6 use rustc::lint;
7 use rustc::session::config::{self, Input};
8 use rustc::session::{DiagnosticOutput, Session};
9 use rustc::util::common::ErrorReported;
10 use rustc_codegen_utils::codegen_backend::CodegenBackend;
11 use rustc_data_structures::OnDrop;
12 use rustc_data_structures::sync::Lrc;
13 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
14 use rustc_metadata::cstore::CStore;
15 use std::path::PathBuf;
16 use std::result;
17 use std::sync::{Arc, Mutex};
18 use syntax;
19 use syntax::source_map::{FileLoader, SourceMap};
20 use syntax_pos::edition;
21
22 pub type Result<T> = result::Result<T, ErrorReported>;
23
24 /// Represents a compiler session.
25 /// Can be used run `rustc_interface` queries.
26 /// Created by passing `Config` to `run_compiler`.
27 pub struct Compiler {
28     pub(crate) sess: Lrc<Session>,
29     codegen_backend: Lrc<Box<dyn CodegenBackend>>,
30     source_map: Lrc<SourceMap>,
31     pub(crate) input: Input,
32     pub(crate) input_path: Option<PathBuf>,
33     pub(crate) output_dir: Option<PathBuf>,
34     pub(crate) output_file: Option<PathBuf>,
35     pub(crate) queries: Queries,
36     pub(crate) cstore: Lrc<CStore>,
37     pub(crate) crate_name: Option<String>,
38 }
39
40 impl Compiler {
41     pub fn session(&self) -> &Lrc<Session> {
42         &self.sess
43     }
44     pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
45         &self.codegen_backend
46     }
47     pub fn cstore(&self) -> &Lrc<CStore> {
48         &self.cstore
49     }
50     pub fn source_map(&self) -> &Lrc<SourceMap> {
51         &self.source_map
52     }
53     pub fn input(&self) -> &Input {
54         &self.input
55     }
56     pub fn output_dir(&self) -> &Option<PathBuf> {
57         &self.output_dir
58     }
59     pub fn output_file(&self) -> &Option<PathBuf> {
60         &self.output_file
61     }
62 }
63
64 /// The compiler configuration
65 pub struct Config {
66     /// Command line options
67     pub opts: config::Options,
68
69     /// cfg! configuration in addition to the default ones
70     pub crate_cfg: FxHashSet<(String, Option<String>)>,
71
72     pub input: Input,
73     pub input_path: Option<PathBuf>,
74     pub output_dir: Option<PathBuf>,
75     pub output_file: Option<PathBuf>,
76     pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
77     pub diagnostic_output: DiagnosticOutput,
78
79     /// Set to capture stderr output during compiler execution
80     pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
81
82     pub crate_name: Option<String>,
83     pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
84 }
85
86 pub fn run_compiler_in_existing_thread_pool<F, R>(config: Config, f: F) -> R
87 where
88     F: FnOnce(&Compiler) -> R,
89 {
90     let (sess, codegen_backend, source_map) = util::create_session(
91         config.opts,
92         config.crate_cfg,
93         config.diagnostic_output,
94         config.file_loader,
95         config.input_path.clone(),
96         config.lint_caps,
97     );
98
99     let cstore = Lrc::new(CStore::new(codegen_backend.metadata_loader()));
100
101     let compiler = Compiler {
102         sess,
103         codegen_backend,
104         source_map,
105         cstore,
106         input: config.input,
107         input_path: config.input_path,
108         output_dir: config.output_dir,
109         output_file: config.output_file,
110         queries: Default::default(),
111         crate_name: config.crate_name,
112     };
113
114     let _sess_abort_error = OnDrop(|| {
115         compiler.sess.diagnostic().print_error_count(&util::diagnostics_registry());
116     });
117
118     if compiler.sess.profile_queries() {
119         profile::begin(&compiler.sess);
120     }
121
122     let r = f(&compiler);
123
124     if compiler.sess.profile_queries() {
125         profile::dump(&compiler.sess, "profile_queries".to_string())
126     }
127
128     r
129 }
130
131 pub fn run_compiler<F, R>(mut config: Config, f: F) -> R
132 where
133     F: FnOnce(&Compiler) -> R + Send,
134     R: Send,
135 {
136     let stderr = config.stderr.take();
137     util::spawn_thread_pool(
138         config.opts.edition,
139         config.opts.debugging_opts.threads,
140         &stderr,
141         || run_compiler_in_existing_thread_pool(config, f),
142     )
143 }
144
145 pub fn default_thread_pool<F, R>(edition: edition::Edition, f: F) -> R
146 where
147     F: FnOnce() -> R + Send,
148     R: Send,
149 {
150     util::spawn_thread_pool(edition, None, &None, f)
151 }