1
Fork 0

rustc_session: Cleanup session creation

This commit is contained in:
Vadim Petrochenkov 2020-05-27 21:34:17 +03:00
parent 2873165725
commit 5369f4aa57
6 changed files with 35 additions and 55 deletions

View file

@ -18,7 +18,7 @@ use rustc_session::lint;
use rustc_session::parse::{CrateConfig, ParseSess}; use rustc_session::parse::{CrateConfig, ParseSess};
use rustc_session::{DiagnosticOutput, Session}; use rustc_session::{DiagnosticOutput, Session};
use rustc_span::edition; use rustc_span::edition;
use rustc_span::source_map::{FileLoader, FileName, SourceMap}; use rustc_span::source_map::{FileLoader, FileName};
use std::path::PathBuf; use std::path::PathBuf;
use std::result; use std::result;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
@ -31,7 +31,6 @@ pub type Result<T> = result::Result<T, ErrorReported>;
pub struct Compiler { pub struct Compiler {
pub(crate) sess: Lrc<Session>, pub(crate) sess: Lrc<Session>,
codegen_backend: Lrc<Box<dyn CodegenBackend>>, codegen_backend: Lrc<Box<dyn CodegenBackend>>,
source_map: Lrc<SourceMap>,
pub(crate) input: Input, pub(crate) input: Input,
pub(crate) input_path: Option<PathBuf>, pub(crate) input_path: Option<PathBuf>,
pub(crate) output_dir: Option<PathBuf>, pub(crate) output_dir: Option<PathBuf>,
@ -49,9 +48,6 @@ impl Compiler {
pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> { pub fn codegen_backend(&self) -> &Lrc<Box<dyn CodegenBackend>> {
&self.codegen_backend &self.codegen_backend
} }
pub fn source_map(&self) -> &Lrc<SourceMap> {
&self.source_map
}
pub fn input(&self) -> &Input { pub fn input(&self) -> &Input {
&self.input &self.input
} }
@ -168,7 +164,7 @@ pub fn run_compiler_in_existing_thread_pool<R>(
f: impl FnOnce(&Compiler) -> R, f: impl FnOnce(&Compiler) -> R,
) -> R { ) -> R {
let registry = &config.registry; let registry = &config.registry;
let (sess, codegen_backend, source_map) = util::create_session( let (sess, codegen_backend) = util::create_session(
config.opts, config.opts,
config.crate_cfg, config.crate_cfg,
config.diagnostic_output, config.diagnostic_output,
@ -181,7 +177,6 @@ pub fn run_compiler_in_existing_thread_pool<R>(
let compiler = Compiler { let compiler = Compiler {
sess, sess,
codegen_backend, codegen_backend,
source_map,
input: config.input, input: config.input,
input_path: config.input_path, input_path: config.input_path,
output_dir: config.output_dir, output_dir: config.output_dir,

View file

@ -7,11 +7,10 @@ use rustc_session::config::{build_configuration, build_session_options, to_crate
use rustc_session::config::{rustc_optgroups, ErrorOutputType, ExternLocation, Options, Passes}; use rustc_session::config::{rustc_optgroups, ErrorOutputType, ExternLocation, Options, Passes};
use rustc_session::config::{CFGuard, ExternEntry, LinkerPluginLto, LtoCli, SwitchWithOptPath}; use rustc_session::config::{CFGuard, ExternEntry, LinkerPluginLto, LtoCli, SwitchWithOptPath};
use rustc_session::config::{Externs, OutputType, OutputTypes, Sanitizer, SymbolManglingVersion}; use rustc_session::config::{Externs, OutputType, OutputTypes, Sanitizer, SymbolManglingVersion};
use rustc_session::getopts;
use rustc_session::lint::Level; use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath; use rustc_session::search_paths::SearchPath;
use rustc_session::utils::NativeLibKind; use rustc_session::utils::NativeLibKind;
use rustc_session::{build_session, Session}; use rustc_session::{build_session, getopts, DiagnosticOutput, Session};
use rustc_span::edition::{Edition, DEFAULT_EDITION}; use rustc_span::edition::{Edition, DEFAULT_EDITION};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::SourceFileHashAlgorithm; use rustc_span::SourceFileHashAlgorithm;
@ -32,7 +31,14 @@ fn build_session_options_and_crate_config(matches: getopts::Matches) -> (Options
fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) { fn mk_session(matches: getopts::Matches) -> (Session, CfgSpecs) {
let registry = registry::Registry::new(&[]); let registry = registry::Registry::new(&[]);
let (sessopts, cfg) = build_session_options_and_crate_config(matches); let (sessopts, cfg) = build_session_options_and_crate_config(matches);
let sess = build_session(sessopts, None, registry); let sess = build_session(
sessopts,
None,
registry,
DiagnosticOutput::Default,
Default::default(),
None,
);
(sess, cfg) (sess, cfg)
} }

View file

@ -23,7 +23,7 @@ use rustc_session::parse::CrateConfig;
use rustc_session::CrateDisambiguator; use rustc_session::CrateDisambiguator;
use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session}; use rustc_session::{early_error, filesearch, output, DiagnosticOutput, Session};
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::source_map::{FileLoader, SourceMap}; use rustc_span::source_map::FileLoader;
use rustc_span::symbol::{sym, Symbol}; use rustc_span::symbol::{sym, Symbol};
use smallvec::SmallVec; use smallvec::SmallVec;
use std::env; use std::env;
@ -65,8 +65,8 @@ pub fn create_session(
input_path: Option<PathBuf>, input_path: Option<PathBuf>,
lint_caps: FxHashMap<lint::LintId, lint::Level>, lint_caps: FxHashMap<lint::LintId, lint::Level>,
descriptions: Registry, descriptions: Registry,
) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>, Lrc<SourceMap>) { ) -> (Lrc<Session>, Lrc<Box<dyn CodegenBackend>>) {
let (mut sess, source_map) = session::build_session_with_source_map( let mut sess = session::build_session(
sopts, sopts,
input_path, input_path,
descriptions, descriptions,
@ -81,7 +81,7 @@ pub fn create_session(
add_configuration(&mut cfg, &mut sess, &*codegen_backend); add_configuration(&mut cfg, &mut sess, &*codegen_backend);
sess.parse_sess.config = cfg; sess.parse_sess.config = cfg;
(Lrc::new(sess), Lrc::new(codegen_backend), source_map) (Lrc::new(sess), Lrc::new(codegen_backend))
} }
const STACK_SIZE: usize = 8 * 1024 * 1024; const STACK_SIZE: usize = 8 * 1024 * 1024;

View file

@ -174,6 +174,10 @@ impl ParseSess {
&self.source_map &self.source_map
} }
pub fn clone_source_map(&self) -> Lrc<SourceMap> {
self.source_map.clone()
}
pub fn buffer_lint( pub fn buffer_lint(
&self, &self,
lint: &'static Lint, lint: &'static Lint,

View file

@ -21,7 +21,7 @@ use rustc_errors::json::JsonEmitter;
use rustc_errors::registry::Registry; use rustc_errors::registry::Registry;
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorReported}; use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticId, ErrorReported};
use rustc_span::edition::Edition; use rustc_span::edition::Edition;
use rustc_span::source_map::{self, FileLoader, MultiSpan, RealFileLoader, SourceMap, Span}; use rustc_span::source_map::{FileLoader, MultiSpan, RealFileLoader, SourceMap, Span};
use rustc_span::{SourceFileHashAlgorithm, Symbol}; use rustc_span::{SourceFileHashAlgorithm, Symbol};
use rustc_target::asm::InlineAsmArch; use rustc_target::asm::InlineAsmArch;
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel}; use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@ -481,7 +481,7 @@ impl Session {
} }
#[inline] #[inline]
pub fn source_map(&self) -> &source_map::SourceMap { pub fn source_map(&self) -> &SourceMap {
self.parse_sess.source_map() self.parse_sess.source_map()
} }
pub fn verbose(&self) -> bool { pub fn verbose(&self) -> bool {
@ -984,26 +984,10 @@ impl Session {
} }
} }
pub fn build_session(
sopts: config::Options,
local_crate_source_file: Option<PathBuf>,
registry: rustc_errors::registry::Registry,
) -> Session {
build_session_with_source_map(
sopts,
local_crate_source_file,
registry,
DiagnosticOutput::Default,
Default::default(),
None,
)
.0
}
fn default_emitter( fn default_emitter(
sopts: &config::Options, sopts: &config::Options,
registry: rustc_errors::registry::Registry, registry: rustc_errors::registry::Registry,
source_map: &Lrc<source_map::SourceMap>, source_map: Lrc<SourceMap>,
emitter_dest: Option<Box<dyn Write + Send>>, emitter_dest: Option<Box<dyn Write + Send>>,
) -> Box<dyn Emitter + sync::Send> { ) -> Box<dyn Emitter + sync::Send> {
let macro_backtrace = sopts.debugging_opts.macro_backtrace; let macro_backtrace = sopts.debugging_opts.macro_backtrace;
@ -1012,17 +996,14 @@ fn default_emitter(
let (short, color_config) = kind.unzip(); let (short, color_config) = kind.unzip();
if let HumanReadableErrorType::AnnotateSnippet(_) = kind { if let HumanReadableErrorType::AnnotateSnippet(_) = kind {
let emitter = AnnotateSnippetEmitterWriter::new( let emitter =
Some(source_map.clone()), AnnotateSnippetEmitterWriter::new(Some(source_map), short, macro_backtrace);
short,
macro_backtrace,
);
Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing)) Box::new(emitter.ui_testing(sopts.debugging_opts.ui_testing))
} else { } else {
let emitter = match dst { let emitter = match dst {
None => EmitterWriter::stderr( None => EmitterWriter::stderr(
color_config, color_config,
Some(source_map.clone()), Some(source_map),
short, short,
sopts.debugging_opts.teach, sopts.debugging_opts.teach,
sopts.debugging_opts.terminal_width, sopts.debugging_opts.terminal_width,
@ -1030,7 +1011,7 @@ fn default_emitter(
), ),
Some(dst) => EmitterWriter::new( Some(dst) => EmitterWriter::new(
dst, dst,
Some(source_map.clone()), Some(source_map),
short, short,
false, // no teach messages when writing to a buffer false, // no teach messages when writing to a buffer
false, // no colors when writing to a buffer false, // no colors when writing to a buffer
@ -1042,20 +1023,14 @@ fn default_emitter(
} }
} }
(config::ErrorOutputType::Json { pretty, json_rendered }, None) => Box::new( (config::ErrorOutputType::Json { pretty, json_rendered }, None) => Box::new(
JsonEmitter::stderr( JsonEmitter::stderr(Some(registry), source_map, pretty, json_rendered, macro_backtrace)
Some(registry), .ui_testing(sopts.debugging_opts.ui_testing),
source_map.clone(),
pretty,
json_rendered,
macro_backtrace,
)
.ui_testing(sopts.debugging_opts.ui_testing),
), ),
(config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new( (config::ErrorOutputType::Json { pretty, json_rendered }, Some(dst)) => Box::new(
JsonEmitter::new( JsonEmitter::new(
dst, dst,
Some(registry), Some(registry),
source_map.clone(), source_map,
pretty, pretty,
json_rendered, json_rendered,
macro_backtrace, macro_backtrace,
@ -1070,14 +1045,14 @@ pub enum DiagnosticOutput {
Raw(Box<dyn Write + Send>), Raw(Box<dyn Write + Send>),
} }
pub fn build_session_with_source_map( pub fn build_session(
sopts: config::Options, sopts: config::Options,
local_crate_source_file: Option<PathBuf>, local_crate_source_file: Option<PathBuf>,
registry: rustc_errors::registry::Registry, registry: rustc_errors::registry::Registry,
diagnostics_output: DiagnosticOutput, diagnostics_output: DiagnosticOutput,
driver_lint_caps: FxHashMap<lint::LintId, lint::Level>, driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>, file_loader: Option<Box<dyn FileLoader + Send + Sync + 'static>>,
) -> (Session, Lrc<SourceMap>) { ) -> Session {
// FIXME: This is not general enough to make the warning lint completely override // FIXME: This is not general enough to make the warning lint completely override
// normal diagnostic warnings, since the warning lint can also be denied and changed // normal diagnostic warnings, since the warning lint can also be denied and changed
// later via the source code. // later via the source code.
@ -1115,7 +1090,7 @@ pub fn build_session_with_source_map(
sopts.file_path_mapping(), sopts.file_path_mapping(),
hash_kind, hash_kind,
)); ));
let emitter = default_emitter(&sopts, registry, &source_map, write_dest); let emitter = default_emitter(&sopts, registry, source_map.clone(), write_dest);
let span_diagnostic = rustc_errors::Handler::with_emitter_and_flags( let span_diagnostic = rustc_errors::Handler::with_emitter_and_flags(
emitter, emitter,
@ -1143,7 +1118,7 @@ pub fn build_session_with_source_map(
None None
}; };
let parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map.clone()); let parse_sess = ParseSess::with_span_handler(span_diagnostic, source_map);
let sysroot = match &sopts.maybe_sysroot { let sysroot = match &sopts.maybe_sysroot {
Some(sysroot) => sysroot.clone(), Some(sysroot) => sysroot.clone(),
None => filesearch::get_or_default_sysroot(), None => filesearch::get_or_default_sysroot(),
@ -1266,7 +1241,7 @@ pub fn build_session_with_source_map(
validate_commandline_args_with_session_available(&sess); validate_commandline_args_with_session_available(&sess);
(sess, source_map) sess
} }
// If it is useful to have a Session available already for validating a // If it is useful to have a Session available already for validating a

View file

@ -114,7 +114,7 @@ pub fn run(options: Options) -> Result<(), String> {
options, options,
false, false,
opts, opts,
Some(compiler.source_map().clone()), Some(compiler.session().parse_sess.clone_source_map()),
None, None,
enable_per_target_ignores, enable_per_target_ignores,
); );