From ab5a6da519f91faa1ba5aa281112e547fcd08b88 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Thu, 23 Jan 2020 20:58:59 +0100 Subject: clang-parser: centralize diagnostic building/reporting --- src/parser/clang/parsing.rs | 102 +++++++------------------------------------- 1 file changed, 16 insertions(+), 86 deletions(-) (limited to 'src/parser/clang/parsing.rs') diff --git a/src/parser/clang/parsing.rs b/src/parser/clang/parsing.rs index 6883d06..50ec5d9 100644 --- a/src/parser/clang/parsing.rs +++ b/src/parser/clang/parsing.rs @@ -1,10 +1,10 @@ use super::config::Config; +use super::diagnostics; use super::entities::*; use crate::types::*; use anyhow::{anyhow, Context, Error, Result}; use clang::{Clang, CompilationDatabase, Index, TranslationUnit, Usr}; -use codemap::CodeMap; use thiserror::Error; use std::collections::BTreeMap; @@ -234,10 +234,7 @@ impl FromTopLevel for Struct { } */ -pub(crate) fn parse_compile_commands( - config: &Config, - codemap: &mut CodeMap, -) -> Result> { +pub(crate) fn parse_compile_commands(config: &Config) -> Result> { let clang = Clang::new().unwrap(); let index = Index::new( &clang, /* exclude from pch = */ false, /* print diagnostics = */ false, @@ -268,15 +265,6 @@ pub(crate) fn parse_compile_commands( let filename = command.get_filename(); - let file_map = codemap.add_file( - filename - .to_str() - .context("File is not valid UTF-8")? - .to_owned(), - std::fs::read_to_string(&filename) - .with_context(|| format!("Cannot readfile: {:?}", filename))?, - ); - trace!("Parsing file: {:?}", filename); // The file name is passed as an argument in the compile commands let mut parser = index.parser(""); @@ -293,8 +281,6 @@ pub(crate) fn parse_compile_commands( .with_context(|| format!("Could not parse file: {:?}", filename))?, &mut entities, &toplevel_directory, - file_map.span, - &codemap, )?; trace!("Changing directory to: {:?}", toplevel_directory); @@ -309,11 +295,7 @@ pub(crate) fn parse_compile_commands( Ok(entities.into()) } -pub(crate) fn parse_file( - path: T, - config: &Config, - codemap: &mut CodeMap, -) -> Result> +pub(crate) fn parse_file(path: T, config: &Config) -> Result> where T: Into, T: AsRef, @@ -324,7 +306,6 @@ where // as provided in the command line let filename = path.to_string(); - let file_map = codemap.add_file(filename.clone(), std::fs::read_to_string(&path)?); let path = path.as_ref().canonicalize()?; let toplevel_directory = std::env::current_dir().context("Cannot read current directory")?; @@ -341,14 +322,18 @@ where .unwrap_or_default(); clang_arguments.extend_from_slice(&config.extra_args); - if let Some(command) = maybe_command { + let mut parser = if let Some(command) = maybe_command { let directory = command.get_directory(); trace!("Changing directory to: {:?}", directory); std::env::set_current_dir(&directory) .with_context(|| format!("Cannot change current directory to: {:?}", directory))?; - } + // If it has the parameters provided by the compilation database, + // then it knows which file to parse + index.parser("") + } else { + index.parser(path) + }; - let mut parser = index.parser(""); parser.skip_function_bodies(true); parser.arguments(&clang_arguments); @@ -363,8 +348,6 @@ where .with_context(|| format!("Could not parse file: {:?}", filename))?, &mut entities, &toplevel_directory, - file_map.span, - &codemap, )?; trace!("Changing directory to: {:?}", toplevel_directory); @@ -382,8 +365,6 @@ fn parse_unit( trans_unit: &TranslationUnit, entities: &mut TopLevel, base_dir: impl AsRef, - file_span: codemap::Span, - codemap: &CodeMap, ) -> Result<()> { trans_unit.get_entity().visit_children(|entity, _parent| { if is_in_system_header(entity, &base_dir) { @@ -400,36 +381,18 @@ fn parse_unit( trace!("Entity with USR = {:?}", usr); debug!("Parsing toplevel entity: {:?}", entity); - add_entity(entity, entities, file_span, codemap) + add_entity(entity, entities) }); - use codemap_diagnostic::{ColorConfig, Emitter}; - - let mut emitter = Emitter::stderr(ColorConfig::Auto, Some(&codemap)); - - for diagnostic in trans_unit.get_diagnostics().iter() { - warn!("{}", diagnostic); + for diagnostic in trans_unit.get_diagnostics() { + let diags = diagnostics::clang_diagnostic_to_diagnostics(diagnostic); + diagnostics::emit(&diags); /* - let main_diag = match clang_diag_to_codemap_diag(&diagnostic, file_span) { - Some(diag) => diag, - None => continue, - }; - let sub_diags = diagnostic .get_children() .into_iter() .filter_map(|diagnostic| clang_diag_to_codemap_diag(&diagnostic, file_span)); - let fix_it_diags = diagnostic - .get_fix_its() - .into_iter() - .map(|fix_it| clang_fix_it_to_codemap_diag(&fix_it, file_span)); - - emitter.emit( - &std::iter::once(main_diag) - .chain(sub_diags) - .chain(fix_it_diags) - .collect::>(), ); */ } @@ -462,8 +425,6 @@ fn is_in_system_header(entity: clang::Entity, base_dir: impl AsRef) -> boo fn add_entity( libclang_entity: clang::Entity, toplevel: &mut TopLevel, - file_span: codemap::Span, - codemap: &CodeMap, ) -> clang::EntityVisitResult { if libclang_entity.get_usr().is_none() { return clang::EntityVisitResult::Continue; @@ -472,38 +433,7 @@ fn add_entity( let kind = match ClangEntityKind::try_from(libclang_entity.get_kind()) { Ok(kind) => kind, Err(err) => { - use codemap_diagnostic::{ - ColorConfig, Diagnostic, Emitter, Level, SpanLabel, SpanStyle, - }; - let spans = if let Some(range) = libclang_entity.get_range() { - // TODO: add every file parsed in this translation unit to the - // codemap, so we can properly report errors - if !range.is_in_main_file() { - vec![] - } else { - let begin = range.get_start().get_file_location().offset as u64; - let end = range.get_end().get_file_location().offset as u64; - - vec![SpanLabel { - span: file_span.subspan(begin, end), - label: None, - style: SpanStyle::Primary, - }] - } - } else { - vec![] - }; - - let diag = Diagnostic { - level: Level::Warning, - message: format!("{}", err), - code: None, - spans, - }; - - let mut emitter = Emitter::stderr(ColorConfig::Auto, Some(codemap)); - emitter.emit(&[diag]); - + diagnostics::error(format!("{}", err), libclang_entity); return clang::EntityVisitResult::Continue; } }; @@ -547,7 +477,7 @@ fn add_entity( // TODO: check result if let Err(err) = result { - error!("{}: {:?}", err, libclang_entity); + diagnostics::error(format!("{}", err), libclang_entity); return ::clang::EntityVisitResult::Continue; } } -- cgit v1.2.3