#error-reporting #diagnostics #preprocessor #cpp #source #m4 #preprocessed

codespan_preprocessed

Beautiful diagnostic reporting for M4 (or cpp) preprocessed text files

5 unstable releases

0.7.12 Nov 7, 2024
0.7.11 Nov 27, 2023
0.7.5 Oct 30, 2023
0.7.3 Apr 20, 2023
0.2.1 Oct 18, 2021

#693 in Text processing

Download history 20/week @ 2024-07-30 184/week @ 2024-08-06 228/week @ 2024-08-13 223/week @ 2024-08-20 257/week @ 2024-08-27 113/week @ 2024-09-03 43/week @ 2024-09-10 13/week @ 2024-09-17 167/week @ 2024-09-24 198/week @ 2024-10-01 88/week @ 2024-10-08 371/week @ 2024-10-15 345/week @ 2024-10-22 269/week @ 2024-10-29 331/week @ 2024-11-05 243/week @ 2024-11-12

1,269 downloads per month

MIT license

33KB
730 lines

codespan_preprocessed

Crates.io Crates.io License Docs

This is an extension for the very useful crate codespan_reporting to deal with preprocessed file through the well-knownm4 or cpp.

Using such a preprocessor allows, among a lost of things, the inclusion of many files which are identified in the bytes sequence with preprecessor directive as:

#line 42 "/my/preprocessed/file"

This directive breaks the location of the source and so should be correctly processed to make correct location for error reporting.

This is the purpose of this crate: taking a preprocessor output and managing the different underlying locations inside it.

Example

use codespan_reporting::diagnostic::Diagnostic;
use codespan_reporting::term;
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use codespan_preprocessed::PreprocessedFile;

fn main()
{
    let file = PreprocessedFile::new(   
        unindent::unindent(
            r#"
                #line 1 "top_file"
                a first statement;
                another one
    
                #line 1 "included_file"
                continue...
    
                #line 5
                another line
                the last one
            "#,
        ),
    );
   
    let diagnostic = Diagnostic::note()
            .with_message("this is just an example")
            .with_labels(vec![
                file.primary_label(113..117).with_message("do you see that ?"),
                file.secondary_label(21..26).with_message("is it related to this ?")
            ]);
   
    // We now set up the writer and configuration, and then finally render the
    // diagnostic to standard error.
    // (see `codespan_reporting` documention for more details)
   
    let writer = StandardStream::stderr(ColorChoice::Always);
    let config = codespan_reporting::term::Config::default();
   
    term::emit(&mut writer.lock(), &config, &file, &diagnostic);
}

The previous code will produce:

note: this is just an example
  ┌─ included_file:6:5
  │
6 │ the last one
  │     ^^^^ do you see that ?
  │
  ┌─ top_file:1:3
  │
1 │ a first statement;
  │   ----- is it related to this ?

Dependencies

~1.3–8MB
~59K SLoC