From c48037a752c90f0177ae907cfd3a7a320f24de57 Mon Sep 17 00:00:00 2001 From: Douglas Raillard Date: Tue, 2 Jan 2024 19:50:18 +0000 Subject: [PATCH] WIP --- tools/analysis/traceevent/src/cparser.rs | 92 ++++++++++++++++++------ 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/tools/analysis/traceevent/src/cparser.rs b/tools/analysis/traceevent/src/cparser.rs index 2b64d404b9..0ed496903b 100644 --- a/tools/analysis/traceevent/src/cparser.rs +++ b/tools/analysis/traceevent/src/cparser.rs @@ -1328,6 +1328,42 @@ where ) } +fn lexeme_id(inner: P) -> impl nom::Parser +where + E: nom::error::ParseError, + P: for<'a> nom::Parser, + I: nom::AsBytes + + Clone + + nom::InputTake + + nom::Offset + + nom::Slice> + + nom::InputLength + + nom::InputIter + + nom::InputTakeAtPosition + + for<'a> nom::Compare<&'a str>, + ::Item: Clone + nom::AsChar, + E: FromExternalError + nom::error::ParseError, +{ + let mut inner = lexeme(inner); + let mut identifier = recognize(identifier::()); + move |input: I| { + let input2 = input.clone(); + match identifier.parse(input) { + Ok((input, id)) => { + // If we found an identifier, the parser is expected to consume it entirely, so we + // don't accidentally just match a prefix of the identifier. + match all_consuming(|input| inner.parse(input)).parse(id) { + Ok((_, x)) => Ok((input, x)), + Err(err) => Err(err) + } + } + // If we could not recognize an identifier, we just run the parser on unmodified input. + // By construction, this cannot start with an identifier. + Err(_) => inner.parse(input2) + } + } +} + fn escape_sequence() -> impl nom::Parser where I: Clone @@ -1491,7 +1527,7 @@ grammar! { // https://port70.net/~nsz/c/c11/n1570.html#6.7.3p1 rule type_qualifier() -> () { - lexeme(alt(( + lexeme_id(alt(( tag("const"), tag("restrict"), tag("volatile"), @@ -1621,11 +1657,11 @@ grammar! { char('['), preceded( delimited( - lexeme(opt(tag("static"))), + lexeme_id(opt(tag("static"))), many0( Self::type_qualifier() ), - lexeme(opt(tag("static"))), + lexeme_id(opt(tag("static"))), ), lexeme(opt(Self::assignment_expr())), ), @@ -1694,14 +1730,18 @@ grammar! { let discard_parser = || { context( "discarded", - many0_count(lexeme(alt(( - tag("extern").map(|_| ()), - tag("static").map(|_| ()), - tag("auto").map(|_| ()), - tag("register").map(|_| ()), - tag("_Thread_local").map(|_| ()), - Self::type_qualifier(), - )))), + many0_count( + lexeme_id( + alt(( + tag("extern").map(|_| ()), + tag("static").map(|_| ()), + tag("auto").map(|_| ()), + tag("register").map(|_| ()), + tag("_Thread_local").map(|_| ()), + Self::type_qualifier(), + )) + ) + ) ) }; @@ -1789,17 +1829,17 @@ grammar! { State::Unknown(DeclSignedness::Unknown) => lexeme(alt(( context( "struct", - preceded(lexeme(tag("struct")), Self::identifier()) + preceded(lexeme_id(tag("struct")), Self::identifier()) .map(Type::Struct), ), context( "enum", - preceded(lexeme(tag("enum")), Self::identifier()) + preceded(lexeme_id(tag("enum")), Self::identifier()) .map(|id| Type::Enum(Box::new(Type::Unknown), id)), ), context( "union", - preceded(lexeme(tag("union")), Self::identifier()) + preceded(lexeme_id(tag("union")), Self::identifier()) .map(Type::Union), ), context( @@ -1929,7 +1969,7 @@ grammar! { // never yield something sensible. map_res_cut( tuple(( - lexeme(alt(( + lexeme_id(alt(( tag("__data_loc"), tag("__rel_loc"), ))), @@ -2145,7 +2185,7 @@ grammar! { ), context("__builtin_expect", preceded( - lexeme(tag("__builtin_expect")), + lexeme_id(tag("__builtin_expect")), parenthesized( Self::assignment_expr(), ) @@ -2569,7 +2609,7 @@ grammar! { ), context("sizeof type", preceded( - lexeme(tag("sizeof")), + lexeme_id(tag("sizeof")), parenthesized( Self::type_name(), ) @@ -2577,7 +2617,7 @@ grammar! { ), context("sizeof expr", preceded( - lexeme(tag("sizeof")), + lexeme_id(tag("sizeof")), Self::unary_expr(), ).map(|e| Expr::SizeofExpr(Box::new(e))) ), @@ -2610,7 +2650,7 @@ grammar! { pair( Self::grammar_ctx(), preceded( - lexeme(tag("__typeof__")), + lexeme_id(tag("__typeof__")), cut(parenthesized( Self::expr(), )) @@ -2660,7 +2700,7 @@ grammar! { tuple(( context( "char encoding prefix", - lexeme(opt(alt((tag("u8"), tag("u"), tag("U"), tag("L"))))), + lexeme_id(opt(alt((tag("u8"), tag("u"), tag("U"), tag("L"))))), ), delimited( char('\''), @@ -3696,6 +3736,8 @@ mod tests { Type::Pointer(Box::new(Type::Struct("callback_head".into()))), ); + test(b"u64 *static_foo", "static_foo", Type::Pointer(Box::new(u64_typ.clone()))); + // Arrays test( b" u64 foo\t []", @@ -3712,6 +3754,16 @@ mod tests { "foo", Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))), ); + test( + b" u64 foo\t [static 124]", + "foo", + Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))), + ); + test( + b" u64 foo\t [static_foo]", + "foo", + Type::Array(Box::new(u64_typ.clone()), ArrayKind::Fixed(Ok(124))), + ); test( b" u64 (*foo) [1]",