Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0526d50b authored by Henri Chataing's avatar Henri Chataing Committed by Cherrypicker Worker
Browse files

pdl: Remove redundant checks from legacy linter

The checks are now performed by the analyzer module.
The linter scope is still used by the generator so
the module cannot be removed entirely.

Test: cargo test
(cherry picked from https://android-review.googlesource.com/q/commit:6481e40e0b04575bc723b3617a9cf5c92b630208)
Merged-In: Idc3cbe34b86bfd544d539c4926976895a8cc1fb3
Change-Id: Idc3cbe34b86bfd544d539c4926976895a8cc1fb3
parent 67485f7e
Loading
Loading
Loading
Loading
+85 −2
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@ pub enum ErrorCode {
    DuplicatePayloadField = 36,
    MissingPayloadField = 37,
    RedundantArraySize = 38,
    InvalidPaddingField = 39,
}

impl From<ErrorCode> for String {
@@ -1037,6 +1038,43 @@ fn check_array_fields(file: &parser_ast::File) -> Result<(), Diagnostics> {
    diagnostics.err_or(())
}

/// Check padding fields.
/// Raises error diagnostics for the following cases:
///      - padding field not following an array field
fn check_padding_fields(file: &parser_ast::File) -> Result<(), Diagnostics> {
    let mut diagnostics: Diagnostics = Default::default();
    for decl in &file.declarations {
        let mut previous_is_array = false;
        for field in decl.fields() {
            match &field.desc {
                FieldDesc::Padding { .. } if !previous_is_array => diagnostics.push(
                    Diagnostic::error()
                        .with_code(ErrorCode::InvalidPaddingField)
                        .with_message("padding field does not follow an array field".to_owned())
                        .with_labels(vec![field.loc.primary()]),
                ),
                FieldDesc::Array { .. } => previous_is_array = true,
                _ => previous_is_array = false,
            }
        }
    }

    diagnostics.err_or(())
}

/// Check checksum fields.
/// Raises error diagnostics for the following cases:
///      - checksum field precedes checksum start
///      - undeclared checksum field
///      - invalid checksum field
fn check_checksum_fields(
    _file: &parser_ast::File,
    _scope: &Scope<parser_ast::Annotation>,
) -> Result<(), Diagnostics> {
    // TODO
    Ok(())
}

/// Check correct definition of packet sizes.
/// Annotate fields and declarations with the size in bits.
fn compute_field_sizes(file: &parser_ast::File) -> ast::File {
@@ -1184,8 +1222,8 @@ pub fn analyze(file: &parser_ast::File) -> Result<ast::File, Diagnostics> {
    check_fixed_fields(file, &scope)?;
    check_payload_fields(file)?;
    check_array_fields(file)?;
    // TODO check_checksum_fields(file, &scope)?;
    // TODO check_padding_fields(file, &scope)?;
    check_padding_fields(file)?;
    check_checksum_fields(file, &scope)?;
    let mut file = compute_field_sizes(file);
    inline_groups(&mut file)?;
    Ok(file)
@@ -1214,6 +1252,15 @@ mod test {
        }};
    }

    macro_rules! valid {
        ($text:literal) => {{
            let mut db = SourceDatabase::new();
            let file = parse_inline(&mut db, "stdin".to_owned(), $text.to_owned())
                .expect("parsing failure");
            assert!(analyzer::analyze(&file).is_ok());
        }};
    }

    #[test]
    fn test_e1() {
        raises!(
@@ -1969,4 +2016,40 @@ mod test {
        "#
        );
    }

    #[test]
    fn test_e39() {
        raises!(
            InvalidPaddingField,
            r#"
        little_endian_packets
        packet A {
            _padding_ [16],
            x : 8[]
        }
        "#
        );

        raises!(
            InvalidPaddingField,
            r#"
        little_endian_packets
        enum A : 8 { X = 0 }
        packet B {
            x : A,
            _padding_ [16]
        }
        "#
        );

        valid!(
            r#"
        little_endian_packets
        packet A {
            x : 8[],
            _padding_ [16]
        }
        "#
        );
    }
}
+52 −836

File changed.

Preview size limit exceeded, changes collapsed.