Skip to content

Commit

Permalink
Stricter treatment of '$' in identifiers.
Browse files Browse the repository at this point in the history
- Reject identifiers consisting of a single '$' sign.
They are rejected by some assemblers.

- Warn on identifiers containing '$' signs
This is an extension, not part of the ISO C standards.
The warning is off by default.
  • Loading branch information
xavierleroy committed Dec 23, 2024
1 parent e503dce commit 6088018
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cparser/Diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type warning_type =
| Reduced_alignment
| Non_linear_cond_expr
| Invalid_UTF8
| Dollar_in_identifier

(* List of all warnings with default status.
"true" means the warning is active by default.
Expand Down Expand Up @@ -142,6 +143,7 @@ let all_warnings =
(Reduced_alignment, false);
(Non_linear_cond_expr, false);
(Invalid_UTF8, true);
(Dollar_in_identifier, false)
]

(* List of active warnings *)
Expand Down Expand Up @@ -185,6 +187,7 @@ let string_of_warning = function
| Reduced_alignment -> "reduced-alignment"
| Non_linear_cond_expr -> "non-linear-cond-expr"
| Invalid_UTF8 -> "invalid-utf8"
| Dollar_in_identifier -> "dollar-in-identifier-extension"

(* Activate the given warning *)
let activate_warning w () =
Expand Down
1 change: 1 addition & 0 deletions cparser/Diagnostics.mli
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type warning_type =
| Reduced_alignment (** alignment reduction *)
| Non_linear_cond_expr (** condition that cannot be linearized *)
| Invalid_UTF8 (** invalid UTF-8 encoding *)
| Dollar_in_identifier (** '$' sign in identifier *)

val warning : (string * int) -> warning_type -> ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
(** [warning (f,c) w fmt arg1 ... argN] formats the arguments [arg1] to [argN] as warining according to
Expand Down
24 changes: 18 additions & 6 deletions cparser/Lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ let warning lb kind fmt =
Diagnostics.warning
(lb.lex_curr_p.pos_fname,lb.lex_curr_p.pos_lnum) kind fmt

(* Identifiers or keywords *)

let ident_or_keyword lb id =
try
let f = Hashtbl.find lexicon id in
f (currentLoc lb)
with Not_found ->
if String.contains id '$' then begin
if id = "$" then
error lb "not supported: identifier consisting of a single '$' sign"
else
warning lb Diagnostics.Dollar_in_identifier "'$' in identifier";
end;
PRE_NAME id

(* Simple character escapes *)

let convert_escape = function
Expand Down Expand Up @@ -416,12 +431,9 @@ rule initial = parse
| ";" { SEMICOLON(currentLoc lexbuf) }
| "," { COMMA(currentLoc lexbuf) }
| "." { DOT(currentLoc lexbuf) }
| identifier as id {
if SSet.mem id !ignored_keywords then
initial lexbuf
else
try Hashtbl.find lexicon id (currentLoc lexbuf)
with Not_found -> PRE_NAME id }
| identifier as id { if SSet.mem id !ignored_keywords
then initial lexbuf
else ident_or_keyword lexbuf id }
| eof { EOF }
| _ as c { fatal_error lexbuf "invalid symbol %C" c }

Expand Down

0 comments on commit 6088018

Please sign in to comment.