Skip to content

Commit 6088018

Browse files
committed
Stricter treatment of '$' in identifiers.
- 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.
1 parent e503dce commit 6088018

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

cparser/Diagnostics.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type warning_type =
105105
| Reduced_alignment
106106
| Non_linear_cond_expr
107107
| Invalid_UTF8
108+
| Dollar_in_identifier
108109

109110
(* List of all warnings with default status.
110111
"true" means the warning is active by default.
@@ -142,6 +143,7 @@ let all_warnings =
142143
(Reduced_alignment, false);
143144
(Non_linear_cond_expr, false);
144145
(Invalid_UTF8, true);
146+
(Dollar_in_identifier, false)
145147
]
146148

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

189192
(* Activate the given warning *)
190193
let activate_warning w () =

cparser/Diagnostics.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type warning_type =
5858
| Reduced_alignment (** alignment reduction *)
5959
| Non_linear_cond_expr (** condition that cannot be linearized *)
6060
| Invalid_UTF8 (** invalid UTF-8 encoding *)
61+
| Dollar_in_identifier (** '$' sign in identifier *)
6162

6263
val warning : (string * int) -> warning_type -> ('a, Format.formatter, unit, unit, unit, unit) format6 -> 'a
6364
(** [warning (f,c) w fmt arg1 ... argN] formats the arguments [arg1] to [argN] as warining according to

cparser/Lexer.mll

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,21 @@ let warning lb kind fmt =
150150
Diagnostics.warning
151151
(lb.lex_curr_p.pos_fname,lb.lex_curr_p.pos_lnum) kind fmt
152152

153+
(* Identifiers or keywords *)
154+
155+
let ident_or_keyword lb id =
156+
try
157+
let f = Hashtbl.find lexicon id in
158+
f (currentLoc lb)
159+
with Not_found ->
160+
if String.contains id '$' then begin
161+
if id = "$" then
162+
error lb "not supported: identifier consisting of a single '$' sign"
163+
else
164+
warning lb Diagnostics.Dollar_in_identifier "'$' in identifier";
165+
end;
166+
PRE_NAME id
167+
153168
(* Simple character escapes *)
154169

155170
let convert_escape = function
@@ -416,12 +431,9 @@ rule initial = parse
416431
| ";" { SEMICOLON(currentLoc lexbuf) }
417432
| "," { COMMA(currentLoc lexbuf) }
418433
| "." { DOT(currentLoc lexbuf) }
419-
| identifier as id {
420-
if SSet.mem id !ignored_keywords then
421-
initial lexbuf
422-
else
423-
try Hashtbl.find lexicon id (currentLoc lexbuf)
424-
with Not_found -> PRE_NAME id }
434+
| identifier as id { if SSet.mem id !ignored_keywords
435+
then initial lexbuf
436+
else ident_or_keyword lexbuf id }
425437
| eof { EOF }
426438
| _ as c { fatal_error lexbuf "invalid symbol %C" c }
427439

0 commit comments

Comments
 (0)