dtc: Add informative error for stray identifier

When a DTS is preprocessed, sometimes the user fails to include the
correct header files, or make a spelling mistake on a macro name. This
leads to a stray identifier in the DTS, like:

  property = <1 2 FOO BAR(3, 4)>;

Give a more helpful error message than "syntax error" by recognizing and
reporting the identifier, like this:

  Lexical error: <stdin>:2.21-24 Unexpected 'FOO'

Also, treat that identifier as literal, which often helps the parser go
further and may generate more helpful error messages.

Signed-off-by: Vivian Wang <wangruikang@iscas.ac.cn>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Vivian Wang 2025-07-04 18:15:20 +08:00 committed by David Gibson
parent 9cabae6b03
commit 52f07dcca4

View file

@ -151,6 +151,21 @@ static void PRINTF(1, 2) lexical_error(const char *fmt, ...);
return DT_LABEL; return DT_LABEL;
} }
<V1>{LABEL} {
/* Missed includes or macro definitions while
* preprocessing can lead to unexpected identifiers in
* the input. Report a slightly more informative error
* in this case */
lexical_error("Unexpected '%s'", yytext);
/* Treat it as a literal which often generates further
* useful error messages */
yylval.integer = 0;
return DT_LITERAL;
}
<V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? { <V1>([0-9]+|0[xX][0-9a-fA-F]+)(U|L|UL|LL|ULL)? {
char *e; char *e;
DPRINT("Integer Literal: '%s'\n", yytext); DPRINT("Integer Literal: '%s'\n", yytext);