2019-03-26 19:07:13 +01:00
|
|
|
pub use crate::passes::BoxedResolver;
|
2019-12-22 17:42:04 -05:00
|
|
|
use crate::util;
|
2019-03-26 19:07:13 +01:00
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
use rustc::lint;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::session::config::{self, ErrorOutputType, Input};
|
2019-10-11 23:48:16 +02:00
|
|
|
use rustc::session::early_error;
|
2018-12-08 20:30:23 +01:00
|
|
|
use rustc::session::{DiagnosticOutput, Session};
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc::ty;
|
2018-12-08 20:30:23 +01:00
|
|
|
use rustc::util::common::ErrorReported;
|
|
|
|
use rustc_codegen_utils::codegen_backend::CodegenBackend;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2018-12-08 20:30:23 +01:00
|
|
|
use rustc_data_structures::sync::Lrc;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::OnDrop;
|
2019-11-15 19:41:50 +01:00
|
|
|
use rustc_errors::registry::Registry;
|
2019-10-15 22:48:13 +02:00
|
|
|
use rustc_parse::new_parser_from_source_str;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::edition;
|
2020-01-01 19:25:28 +01:00
|
|
|
use rustc_span::source_map::{FileLoader, FileName, SourceMap};
|
2018-12-08 20:30:23 +01:00
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::result;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2019-10-11 23:48:16 +02:00
|
|
|
use syntax::ast::{self, MetaItemKind};
|
|
|
|
use syntax::sess::ParseSess;
|
2019-12-22 17:42:04 -05:00
|
|
|
use syntax::token;
|
2018-12-08 20:30:23 +01:00
|
|
|
|
|
|
|
pub type Result<T> = result::Result<T, ErrorReported>;
|
|
|
|
|
|
|
|
/// Represents a compiler session.
|
2019-12-19 12:58:42 +01:00
|
|
|
/// Can be used to run `rustc_interface` queries.
|
2018-12-08 20:30:23 +01:00
|
|
|
/// Created by passing `Config` to `run_compiler`.
|
|
|
|
pub struct Compiler {
|
|
|
|
pub(crate) sess: Lrc<Session>,
|
|
|
|
codegen_backend: Lrc<Box<dyn CodegenBackend>>,
|
|
|
|
source_map: Lrc<SourceMap>,
|
|
|
|
pub(crate) input: Input,
|
|
|
|
pub(crate) input_path: Option<PathBuf>,
|
|
|
|
pub(crate) output_dir: Option<PathBuf>,
|
|
|
|
pub(crate) output_file: Option<PathBuf>,
|
|
|
|
pub(crate) crate_name: Option<String>,
|
2019-10-10 19:33:00 -04:00
|
|
|
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
|
2019-11-11 16:09:03 +01:00
|
|
|
pub(crate) override_queries:
|
|
|
|
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Compiler {
|
|
|
|
pub fn session(&self) -> &Lrc<Session> {
|
|
|
|
&self.sess
|
|
|
|
}
|
|
|
|
pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
|
|
|
|
&self.codegen_backend
|
|
|
|
}
|
|
|
|
pub fn source_map(&self) -> &Lrc<SourceMap> {
|
|
|
|
&self.source_map
|
|
|
|
}
|
|
|
|
pub fn input(&self) -> &Input {
|
|
|
|
&self.input
|
|
|
|
}
|
|
|
|
pub fn output_dir(&self) -> &Option<PathBuf> {
|
|
|
|
&self.output_dir
|
|
|
|
}
|
|
|
|
pub fn output_file(&self) -> &Option<PathBuf> {
|
|
|
|
&self.output_file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 23:48:16 +02:00
|
|
|
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
|
|
|
|
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
|
|
|
|
syntax::with_default_globals(move || {
|
2019-12-22 17:42:04 -05:00
|
|
|
let cfg = cfgspecs
|
|
|
|
.into_iter()
|
|
|
|
.map(|s| {
|
|
|
|
let sess = ParseSess::with_silent_emitter();
|
|
|
|
let filename = FileName::cfg_spec_source_code(&s);
|
|
|
|
let mut parser = new_parser_from_source_str(&sess, filename, s.to_string());
|
2019-10-11 23:48:16 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
macro_rules! error {
|
|
|
|
($reason: expr) => {
|
|
|
|
early_error(
|
|
|
|
ErrorOutputType::default(),
|
|
|
|
&format!(concat!("invalid `--cfg` argument: `{}` (", $reason, ")"), s),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
2019-10-11 23:48:16 +02:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
match &mut parser.parse_meta_item() {
|
|
|
|
Ok(meta_item) if parser.token == token::Eof => {
|
|
|
|
if meta_item.path.segments.len() != 1 {
|
|
|
|
error!("argument key must be an identifier");
|
2019-10-11 23:48:16 +02:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
match &meta_item.kind {
|
|
|
|
MetaItemKind::List(..) => {
|
|
|
|
error!(r#"expected `key` or `key="value"`"#);
|
|
|
|
}
|
|
|
|
MetaItemKind::NameValue(lit) if !lit.kind.is_str() => {
|
|
|
|
error!("argument value must be a string");
|
|
|
|
}
|
|
|
|
MetaItemKind::NameValue(..) | MetaItemKind::Word => {
|
|
|
|
let ident = meta_item.ident().expect("multi-segment cfg key");
|
|
|
|
return (ident.name, meta_item.value_str());
|
|
|
|
}
|
2019-10-11 23:48:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
Ok(..) => {}
|
|
|
|
Err(err) => err.cancel(),
|
2019-10-11 23:48:16 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
error!(r#"expected `key` or `key="value"`"#);
|
|
|
|
})
|
|
|
|
.collect::<ast::CrateConfig>();
|
|
|
|
cfg.into_iter().map(|(a, b)| (a.to_string(), b.map(|b| b.to_string()))).collect()
|
2019-10-11 23:48:16 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
/// The compiler configuration
|
|
|
|
pub struct Config {
|
|
|
|
/// Command line options
|
|
|
|
pub opts: config::Options,
|
|
|
|
|
|
|
|
/// cfg! configuration in addition to the default ones
|
|
|
|
pub crate_cfg: FxHashSet<(String, Option<String>)>,
|
|
|
|
|
|
|
|
pub input: Input,
|
|
|
|
pub input_path: Option<PathBuf>,
|
|
|
|
pub output_dir: Option<PathBuf>,
|
|
|
|
pub output_file: Option<PathBuf>,
|
|
|
|
pub file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
|
|
|
|
pub diagnostic_output: DiagnosticOutput,
|
|
|
|
|
|
|
|
/// Set to capture stderr output during compiler execution
|
|
|
|
pub stderr: Option<Arc<Mutex<Vec<u8>>>>,
|
|
|
|
|
|
|
|
pub crate_name: Option<String>,
|
|
|
|
pub lint_caps: FxHashMap<lint::LintId, lint::Level>,
|
2019-10-10 19:33:00 -04:00
|
|
|
|
2019-10-22 16:53:28 -04:00
|
|
|
/// This is a callback from the driver that is called when we're registering lints;
|
|
|
|
/// it is called during plugin registration when we have the LintStore in a non-shared state.
|
|
|
|
///
|
|
|
|
/// Note that if you find a Some here you probably want to call that function in the new
|
|
|
|
/// function being registered.
|
2019-10-10 19:33:00 -04:00
|
|
|
pub register_lints: Option<Box<dyn Fn(&Session, &mut lint::LintStore) + Send + Sync>>,
|
2019-11-11 16:09:03 +01:00
|
|
|
|
|
|
|
/// This is a callback from the driver that is called just after we have populated
|
|
|
|
/// the list of queries.
|
|
|
|
///
|
|
|
|
/// The second parameter is local providers and the third parameter is external providers.
|
|
|
|
pub override_queries:
|
|
|
|
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
|
2019-11-15 19:41:50 +01:00
|
|
|
|
|
|
|
/// Registry of diagnostics codes.
|
|
|
|
pub registry: Registry,
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 19:41:50 +01:00
|
|
|
pub fn run_compiler_in_existing_thread_pool<R>(
|
|
|
|
config: Config,
|
|
|
|
f: impl FnOnce(&Compiler) -> R,
|
|
|
|
) -> R {
|
|
|
|
let registry = &config.registry;
|
2018-12-08 20:30:23 +01:00
|
|
|
let (sess, codegen_backend, source_map) = util::create_session(
|
|
|
|
config.opts,
|
|
|
|
config.crate_cfg,
|
|
|
|
config.diagnostic_output,
|
|
|
|
config.file_loader,
|
|
|
|
config.input_path.clone(),
|
|
|
|
config.lint_caps,
|
2019-11-15 19:41:50 +01:00
|
|
|
registry.clone(),
|
2018-12-08 20:30:23 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
let compiler = Compiler {
|
|
|
|
sess,
|
|
|
|
codegen_backend,
|
|
|
|
source_map,
|
|
|
|
input: config.input,
|
|
|
|
input_path: config.input_path,
|
|
|
|
output_dir: config.output_dir,
|
|
|
|
output_file: config.output_file,
|
|
|
|
crate_name: config.crate_name,
|
2019-10-10 19:33:00 -04:00
|
|
|
register_lints: config.register_lints,
|
2019-11-11 16:09:03 +01:00
|
|
|
override_queries: config.override_queries,
|
2018-12-08 20:30:23 +01:00
|
|
|
};
|
|
|
|
|
2020-01-09 03:48:00 +01:00
|
|
|
let r = {
|
|
|
|
let _sess_abort_error = OnDrop(|| {
|
|
|
|
compiler.sess.diagnostic().print_error_count(registry);
|
|
|
|
});
|
2018-12-08 20:30:23 +01:00
|
|
|
|
2020-01-09 03:48:00 +01:00
|
|
|
f(&compiler)
|
|
|
|
};
|
|
|
|
|
|
|
|
let prof = compiler.sess.prof.clone();
|
|
|
|
prof.generic_activity("drop_compiler").run(move || drop(compiler));
|
|
|
|
r
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 19:41:50 +01:00
|
|
|
pub fn run_compiler<R: Send>(mut config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
|
2019-03-28 18:58:43 +01:00
|
|
|
let stderr = config.stderr.take();
|
|
|
|
util::spawn_thread_pool(
|
2019-04-06 00:15:49 +02:00
|
|
|
config.opts.edition,
|
2019-03-28 18:58:43 +01:00
|
|
|
config.opts.debugging_opts.threads,
|
|
|
|
&stderr,
|
|
|
|
|| run_compiler_in_existing_thread_pool(config, f),
|
|
|
|
)
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 19:41:50 +01:00
|
|
|
pub fn default_thread_pool<R: Send>(edition: edition::Edition, f: impl FnOnce() -> R + Send) -> R {
|
2019-09-30 16:27:28 -04:00
|
|
|
// the 1 here is duplicating code in config.opts.debugging_opts.threads
|
|
|
|
// which also defaults to 1; it ultimately doesn't matter as the default
|
|
|
|
// isn't threaded, and just ignores this parameter
|
|
|
|
util::spawn_thread_pool(edition, 1, &None, f)
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|