cocci: Improve C parsing of attributes
v4 cover cocci: Improve C parsing of attributes
This patch series aims to improve parsing of attributes in C by Coccinelle's C parser.
These parsing errors were discovered by running a build of Coccinelle's master branch on gitlab to parse the source code of Linux v5.5-rc4 and Linux v5.6-rc7. Coccinelle currently manages attributes similar to comments, so to explicity state what the attributes are to the C parser, a MACROANNOTATION hint was used in Coccinelle's standard.h file. The hint enables the parser to correctly identify attributes, and thus potentially handle them differently in the AST.
Upon collection of these parsing errors, The GNU C Compiler's grammar was taken as a reference for introducing attributes in correct places in Coccinelle's C parser.
Upon applying the MACROANNOTATION hint to a majority of the attributes in Coccinelle's standard.h file and parsing the source code of Linux v5.6-rc7, the difference between the stats (of --parse-c) before and after applying this patch series are as follows:
Before:
NB total files = 28268; perfect = 25697; pbs = 2570; timeout = 0; =========> 90%
nb good = 19160664, nb passed = 135235 =========> 0.70% passed
nb good = 19160664, nb bad = 129685 =========> 99.33% good or passedAfter:
NB total files = 28268; perfect = 25699; pbs = 2568; timeout = 0; =========> 90%
nb good = 19160856, nb passed = 71716 =========> 0.37% passed
nb good = 19160856, nb bad = 129493 =========> 99.33% good or passedWhat this patch series does not do:
- Add attributes from all the new productions into the C AST. Some of these are dropped.
- Reflect changes from the C parser in the SmPL parser.
Separate patches will be sent for the above.
Changes in v4:
-------------
Remove the following patches from the series and add/merge them to [1].
parsing_c: visitor_c: Add vk_attribute and vk_attribute_s
parsing_c: lib_parsing_c: Add ii_of_attr
engine: Add distrf_attr to PARAM functor
engine: cocci_vs_c: Add allminus argument to attribute_list
parsing_c: parser: Make abstract_declarator pass attributes
cocci: Add parameter attributes to C AST
parsing_c: parser: Place parameter attributes in C AST
parsing_c: visitor_c: Visit parameter attributes
parsing_c: unparse_hrule: Add parameter attributes in record
parsing_c: type_c: Add parameter attributes to record
engine: cocci_vs_c: "Match" parameter attributes
engine: c_vs_c: Match parameter attributes
parsing_c: parser: Add rule for optional end attributes
- Add to series.
parsing_c: parser: Add field declaration end attributes production
- Use end_attributes_opt to avoid duplicate code.
parsing_c: parser: Handle struct/union/enum end attributes
- Use end_attributes_opt to avoid duplicate code.
parsing_c: parser: Add MacroDecl end attributes production
- Use end_attributes_opt to avoid duplicate code.
parsing_c: parser: cpp_other end attributes production
- Use end_attributes_opt to avoid duplicate code.
Changes in v3:
-------------parsing_c: parser: Place parameter attributes in C AST
- Remove the "attr is ignored" comments in the declaratorp rule.
- Place the attributes from the "declarator attributes" production in the declaratorp rule in the C AST as well.
Changes in v2:
--------------parsing_c: parser: Add attribute production in spec_qualif_list
- This patch applied on its own causes a build error. Fix the error by removing the let binding for abstract_declaratort in the type_name rule
parsing_c: parser: Make abstract_declarator pass attributes
- Reflect changes from the above build error fix.
Jaskaran Singh (14):
parsing_c: parse_c: Ignore TMacroAttr and TMacroEndAttr in passed tokens
parsing_c: parsing_hacks: Label end attributes correctly
parsing_c: parsing_hacks: Commentize attributes before qualif/type
parsing_c: parser: Add rule for multiple end attributes
parsing_c: parser: Add expression statement attributes
parsing_c: parser: Add rule for optional end attributes
parsing_c: parser: Add attribute production in spec_qualif_list
parsing_c: parser: Add init_declarator_attrs rule
parsing_c: parser: Add field declaration end attributes production
parsing_c: parser: Handle struct/union/enum end attributes
parsing_c: parser: Add MacroDecl end attributes production
parsing_c: parser: cpp_other end attributes production
parsing_c: cpp_token_c: Introduce MACROANNOTATION hint
cocci: standard.h: Annotate attributes with MACROANNOTATION
parsing_c/cpp_token_c.ml | 2 parsing_c/parse_c.ml | 4 + parsing_c/parser_c.mly | 91 ++++++++++++++++++++++++----------- parsing_c/parsing_hacks.ml | 37 ++++++++++++++ standard.h | 116 ++++++++++++++++++++++----------------------- 5 files changed, 164 insertions(+), 86 deletions(-)
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 1/14 parsing_c: parse_c: Ignore TMacroAttr and TMacroEndAttr in passed tokens
Most cases in parsing_hacks do not consider attributes before or after the token in question. So, do not pass TMacroAttr or TMacroEndAttr tokens to parsing_hacks in the before list.
--- parsing_c/parse_c.ml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
parsing_c/parse_c.ml +3 -1
@@ -612,13 +612,15 @@ let rec lexer_function ~pass tr = fun lexbuf -> | x -> x in + let passed_before = filter_noise 10 tr.passed_clean in + let v = if !in_exec then v else Parsing_hacks.lookahead ~pass (clean_for_lookahead (v::tr.rest_clean)) - tr.passed_clean in + passed_before in tr.passed <- v::tr.passed;
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 2/14 parsing_c: parsing_hacks: Label end attributes correctly
In reply to Jaskaran SinghDue to certain conflicts in the grammar, a separate token (TMacroEndAttr) is required for attributes before a semicolon or an assignment operator. Prior to this, multiple end attributes were not labeled correctly, i.e., only the single attribute before the semicolon or assignment operator would be.
Add a case to label multiple end attributes correctly.
--- parsing_c/parsing_hacks.ml | 4 ++++ 1 file changed, 4 insertions(+)
parsing_c/parsing_hacks.ml +4 -0
@@ -2771,6 +2771,10 @@ let lookahead2 ~pass next before = msg_attribute s1; TMacroAttr (s1, i1) + | (TMacroAttr(s1,i1)::(TPtVirg(ii2)|TEq(ii2))::rest,_) + -> + TMacroEndAttr (s1, i1) + (* (* christia: here insert support for macros on top level *) | TIdent (s, ii) :: tl :: _, _ when can_be_on_top_level tl && LP.current_context () = InTopLevel ->
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 3/14 parsing_c: parsing_hacks: Commentize attributes before qualif/type
In reply to Jaskaran SinghFor the following example from fs/btrfs/ctree.h in Linux v5.6-rc7:
__cold __noreturn
static inline void assertfail(const char *expr, const char *file, int line)
{
pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
BUG();
}__cold and __noreturn are not labeled correctly, leading to C parsing errors. Add a case to commentize attributes before a storage qualifier/type qualifier/type correctly.
--- parsing_c/parsing_hacks.ml | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
parsing_c/parsing_hacks.ml +33 -0
@@ -1952,6 +1952,29 @@ let is_type = function | Tshort _ -> true | _ -> false +let is_type_qualif = function + | Tconst _ + | Tvolatile _ -> true + | _ -> false + +let is_storage_spec = function + | Tstatic _ + | Tregister _ + | Textern _ + | Tauto _ -> true + | _ -> false + +let rec is_idents ?(followed_by=fun _ -> true) ts = + let rec loop l = + match l with + | x::xs when ident x -> loop xs + | x::xs -> followed_by x + | [] -> failwith "unexpected end of token stream" in + match ts with + | x::xs when ident x -> loop xs + | x::xs -> followed_by x + | _ -> false + let is_cparen = function (TCPar _) -> true | _ -> false let is_oparen = function (TOPar _) -> true | _ -> false
@@ -2134,6 +2157,16 @@ let lookahead2 ~pass next before = else TCommentCpp (Token_c.CppDirective, i1) + (* tt xx yy *) + | (TIdent (s, i1)::rest, _) + when not_struct_enum before + && is_idents + ~followed_by: + (function x -> + is_type x || is_storage_spec x || is_type_qualif x) rest + && s ==~ regexp_annot -> + TCommentCpp (Token_c.CppMacro, i1) + | (TIdent (s2, i2)::_ , TIdent (s, i1)::seen::_) when not_struct_enum before && is_macro s2 && is_type seen ->
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 4/14 parsing_c: parser: Add rule for multiple end attributes
In reply to Jaskaran SinghThere is no rule for productions of multiple end attributes. Add a case similar to that of attribute_list for end attributes.
--- parsing_c/parser_c.mly | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
parsing_c/parser_c.mly +4 -1
@@ -2401,8 +2401,11 @@ attribute_list: attributes: attribute_list { $1 } -end_attributes: +end_attribute_list: | TMacroEndAttr { [Attribute (fst $1), [snd $1]] } + | end_attribute_list TMacroEndAttr { $1 @ [Attribute(fst $2), [snd $2]] } + +end_attributes: end_attribute_list { $1 } comma_opt: | TComma { [$1] }
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 5/14 parsing_c: parser: Add expression statement attributes
In reply to Jaskaran SinghAs per GCC's C grammar, the expression statement rule has the following production:
expression-statement:
gnu-attributes ;
Add this production to the expr_statement rule of Coccinelle's C parser. If attributes are recognized, this parses the following code successfully:
label: attribute;
--- parsing_c/parser_c.mly | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
parsing_c/parser_c.mly +3 -2
@@ -1082,8 +1082,9 @@ stat_or_decl: expr_statement: - | TPtVirg { None, [$1] } - | expr TPtVirg { Some $1, [$2] } + | TPtVirg { None, [$1] } + | end_attributes TPtVirg { None, [$2] } + | expr TPtVirg { Some $1, [$2] } selection: | Tif TOPar expr TCPar cpp_ifdef_statement %prec SHIFTHERE
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 6/14 parsing_c: parser: Add rule for optional end attributes
In reply to Jaskaran SinghA few attribute centric productions cause a lot of duplicate code. Add an 'end_attributes_opt' rule to dispel duplicate code caused by an additional attribute centric productions.
--- parsing_c/parser_c.mly | 4 ++++ 1 file changed, 4 insertions(+)
parsing_c/parser_c.mly +4 -0
@@ -2408,6 +2408,10 @@ end_attribute_list: end_attributes: end_attribute_list { $1 } +end_attributes_opt: + | end_attributes { $1 } + | /*(* empty *)*/ { [] } + comma_opt: | TComma { [$1] } | /*(* empty *)*/ { [] }
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 7/14 parsing_c: parser: Add attribute production in spec_qualif_list
In reply to Jaskaran SinghAs per GCC's C grammar, the specifier-qualifier-list has the following production:
specifier-qualifier-list:
gnu-attributes specifier-qualifier-list[opt]
Add this production to the spec_qualif_list rule of Coccinelle's C parser. This parses attributes in struct fields and casts successfully. Some code examples from Linux v5.5-rc4:
kernel/rcu/tree.h:
struct rcu_node {
...
raw_spinlock_t __private lock;
...
}; kernel/sysctl.c:
char __user **buffer = (char __user **)buf;
--- parsing_c/parser_c.mly | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-)
parsing_c/parser_c.mly +15 -8
@@ -1500,10 +1500,12 @@ abstract_declaratorp: /*(* for struct and also typename *)*/ /*(* can't put decl_spec cos no storage is allowed for field struct *)*/ spec_qualif_list2: - | type_spec { addTypeD ($1, nullDecl) } - | type_qualif { {nullDecl with qualifD = (fst $1,[snd $1])}} - | type_spec spec_qualif_list { addTypeD ($1,$2) } - | type_qualif spec_qualif_list { addQualifD ($1,$2) } + | type_spec { ([], addTypeD ($1, nullDecl)) } + | type_qualif { ([], {nullDecl with qualifD = (fst $1,[snd $1])})} + | attribute { ([$1], nullDecl) } + | type_spec spec_qualif_list { (fst $2, addTypeD ($1,snd $2)) } + | type_qualif spec_qualif_list { (fst $2, addQualifD ($1,snd $2)) } + | attribute spec_qualif_list { ([$1]@(fst $2), snd $2) } spec_qualif_list: spec_qualif_list2 { dt "spec_qualif" (); $1 }
@@ -1521,9 +1523,12 @@ type_qualif_list: type_name: | spec_qualif_list - { let (returnType, _) = fixDeclSpecForDecl $1 in returnType } + { let (attrs, ds) = $1 in + let (returnType, _) = fixDeclSpecForDecl ds in returnType } | spec_qualif_list abstract_declaratort - { let (returnType, _) = fixDeclSpecForDecl $1 in $2 returnType } + { let (attrs1, ds) = $1 in + let (returnType, _) = fixDeclSpecForDecl ds in + $2 returnType }
@@ -1808,7 +1813,8 @@ struct_decl2: field_declaration: | spec_qualif_list struct_declarator_list TPtVirg { - let (returnType,storage) = fixDeclSpecForDecl $1 in + let (attrs, ds) = $1 in + let (returnType,storage) = fixDeclSpecForDecl ds in if fst (unwrap storage) <> NoSto then internal_error "parsing don't allow this";
@@ -1823,8 +1829,9 @@ field_declaration: | spec_qualif_list TPtVirg { + let (attrs, ds) = $1 in (* gccext: allow empty elements if it is a structdef or enumdef *) - let (returnType,storage) = fixDeclSpecForDecl $1 in + let (returnType,storage) = fixDeclSpecForDecl ds in if fst (unwrap storage) <> NoSto then internal_error "parsing don't allow this";
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 8/14 parsing_c: parser: Add init_declarator_attrs rule
In reply to Jaskaran SinghAs per GCC's C grammar, the init-declarator rule has the following production:
init-declarator:
declarator simple-asm-expr[opt] gnu-attributes[opt] declarator simple-asm-expr[opt] gnu-attributes[opt] = initializer
Due to conflicts in Coccinelle's C grammar, adding productions with attributes to init_declarator is not possible. Create init_declarator_attrs and use this rule in init_declarator_list for handling attributes after commas.
Following is an example of C code that is parsed successfully:
struct mxser_mstatus ms, __user *msu = argp;
--- parsing_c/parser_c.mly | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-)
parsing_c/parser_c.mly +21 -2
@@ -1667,6 +1667,23 @@ init_declarator2: | declaratori TOParCplusplusInit argument_list TCPar { ($1, ConstrInit($3,[$2;$4])) } +/*(*-----------------------------------------------------------------------*)*/ +/*(* declarators (right part of type and variable). *)*/ +/*(* This is a workaround for the following case: *)*/ +/*(* <type> <declarator>, <attribute> <declarator> ...*)*/ +/*(* The normal init_declarator rule does not handle this, and adding *)*/ +/*(* attributes to it causes conflicts, thus the need for this rule. *)*/ +/*(*-----------------------------------------------------------------------*)*/ +init_declarator_attrs2: + | declaratori { ($1, NoInit) } + | attributes declaratori { ($2, NoInit) } + | declaratori teq initialize { ($1, ValInit($2, $3)) } + | attributes declaratori teq initialize { ($2, ValInit($3, $4)) } + /* C++ only */ + | declaratori TOParCplusplusInit argument_list TCPar + { ($1, ConstrInit($3,[$2;$4])) } + | attributes declaratori TOParCplusplusInit argument_list TCPar + { ($2, ConstrInit($4,[$3;$5])) } /*(*----------------------------*)*/ /*(* workarounds *)*/
@@ -1675,6 +1692,8 @@ teq: TEq { et "teq" (); $1 } init_declarator: init_declarator2 { dt "init" (); $1 } +init_declarator_attrs: init_declarator_attrs2 { dt "init_attrs" (); $1 } + /*(*----------------------------*)*/ /*(* gccext: *)*/
@@ -2372,9 +2391,9 @@ enumerator_list: init_declarator_list: | init_declarator { [$1, []] } - | init_declarator_list TComma cpp_directive_list init_declarator + | init_declarator_list TComma cpp_directive_list init_declarator_attrs { $1 @ [$4, [$2]] } - | init_declarator_list TComma init_declarator { $1 @ [$3, [$2]] } + | init_declarator_list TComma init_declarator_attrs { $1 @ [$3, [$2]] } parameter_list:
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 9/14 parsing_c: parser: Add field declaration end attributes production
In reply to Jaskaran SinghAs per GCC's C grammar, the struct-declarator rule has the following productions:
struct-declarator:
declarator gnu-attributes[opt]
declarator[opt] : constant-expression gnu-attributes[opt]
While these productions are handled in the struct_declarator rule of Coccinelle's C grammar, end attributes are not.
Add productions for end attributes in the field_declaration rule of Coccinelle's C parser. This parses the following C code from Linux v5.6-rc7 successfully:
kernel/sched/sched.h:
struct task_group {
...
atomic_t load_avg __cacheline_aligned;
...
};--- parsing_c/parser_c.mly | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
parsing_c/parser_c.mly +4 -4
@@ -1830,7 +1830,7 @@ struct_decl2: field_declaration: - | spec_qualif_list struct_declarator_list TPtVirg + | spec_qualif_list struct_declarator_list end_attributes_opt TPtVirg { let (attrs, ds) = $1 in let (returnType,storage) = fixDeclSpecForDecl ds in
@@ -1840,13 +1840,13 @@ field_declaration: let iistart = Ast_c.fakeInfo () in (* for parallelism with DeclList *) FieldDeclList ($2 +> (List.map (fun (f, iivirg) -> f returnType, iivirg)) - ,[$3;iistart]) + ,[$4;iistart]) (* don't need to check if typedef or func initialised cos * grammar don't allow typedef nor initialiser in struct *) } - | spec_qualif_list TPtVirg + | spec_qualif_list end_attributes_opt TPtVirg { let (attrs, ds) = $1 in (* gccext: allow empty elements if it is a structdef or enumdef *)
@@ -1855,7 +1855,7 @@ field_declaration: then internal_error "parsing don't allow this"; let iistart = Ast_c.fakeInfo () in (* for parallelism with DeclList *) - FieldDeclList ([(Simple (None, returnType)) , []], [$2;iistart]) + FieldDeclList ([(Simple (None, returnType)) , []], [$3;iistart]) }
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 10/14 parsing_c: parser: Handle struct/union/enum end attributes
In reply to Jaskaran SinghAs per GCC's C grammar, the struct-or-union-specifier and enum-specifier have the following productions:
struct-or-union-specifier:
struct-or-union attribute-specifier-sequence[opt] gnu-attributes[opt]
identifier[opt] { struct-contents } gnu-attributes[opt]
enum-specifier:
enum gnu-attributes[opt] identifier[opt] { enumerator-list }
gnu-attributes[opt]
enum gnu-attributes[opt] identifier[opt] { enumerator-list , }
gnu-attributes[opt]Add a production to the decl2 rule of Coccinelle's C parser to handle the end attributes here (i.e. gnu-attributes after the } ). This parses the following C code from Linux v5.6-rc7:
drivers/net/wireless/broadcom/b43legacy/b43legacy.h
struct b43legacy_iv {
__be16 offset_size;
union {
__be16 d16;
__be32 d32;
} data __packed;
} __packed; drivers/scsi/myrs.h:
enum myrs_cmd_opcode {
MYRS_CMD_OP_MEMCOPY = 0x01,
MYRS_CMD_OP_SCSI_10_PASSTHRU = 0x02,
MYRS_CMD_OP_SCSI_255_PASSTHRU = 0x03,
MYRS_CMD_OP_SCSI_10 = 0x04,
MYRS_CMD_OP_SCSI_256 = 0x05,
MYRS_CMD_OP_IOCTL = 0x20,
} __packed;--- parsing_c/parser_c.mly | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
parsing_c/parser_c.mly +3 -3
@@ -1542,16 +1542,16 @@ abstract_declaratort: /*(*************************************************************************)*/ decl2: - | decl_spec TPtVirg + | decl_spec end_attributes_opt TPtVirg { function local -> let (returnType,storage) = fixDeclSpecForDecl (snd $1) in let iistart = Ast_c.fakeInfo () in DeclList ([{v_namei = None; v_type = returnType; v_storage = unwrap storage; v_local = local; - v_attr = fst $1; v_endattr = Ast_c.noattr; + v_attr = fst $1; v_endattr = $2; v_type_bis = ref None; },[]], - ($2::iistart::snd storage)) + ($3::iistart::snd storage)) } | decl_spec init_declarator_list TPtVirg { function local ->
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 11/14 parsing_c: parser: Add MacroDecl end attributes production
In reply to Jaskaran SinghAdd a production for macro declaration end attributes. This parses the following C code from Linux v5.6-rc7:
arch/x86/kernel/nmi_selftest.c:
static DECLARE_BITMAP(nmi_ipi_mask, NR_CPUS) __initdata;
--- parsing_c/parser_c.mly | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
parsing_c/parser_c.mly +4 -3
@@ -1576,15 +1576,16 @@ decl2: } /*(* cppext: *)*/ - | storage_const_opt TMacroDecl TOPar argument_list TCPar TPtVirg + | storage_const_opt TMacroDecl TOPar argument_list TCPar end_attributes_opt + TPtVirg { function _ -> match $1 with Some (sto,stoii) -> MacroDecl - ((sto, fst $2, $4, true), (snd $2::$3::$5::$6::fakeInfo()::stoii)) + ((sto, fst $2, $4, true), (snd $2::$3::$5::$7::fakeInfo()::stoii)) | None -> MacroDecl - ((NoSto, fst $2, $4, true), [snd $2;$3;$5;$6;fakeInfo()]) } + ((NoSto, fst $2, $4, true), [snd $2;$3;$5;$7;fakeInfo()]) } | storage_const_opt TMacroDecl TOPar argument_list TCPar teq initialize TPtVirg
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 12/14 parsing_c: parser: cpp_other end attributes production
In reply to Jaskaran SinghAdd a production for a macro call in the cpp_other rule of Coccinelle's C parser. This parses the following C code from Linux v5.6-rc7 successfully:
arch/x86/kernel/irq_64.c:
DEFINE_PER_CPU_PAGE_ALIGNED(struct irq_stack, irq_stack_backing_store) __visible;
--- parsing_c/parser_c.mly | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
parsing_c/parser_c.mly +6 -4
@@ -2182,7 +2182,8 @@ cpp_other: * the rule are slightly different, they can't be statement and so expr * at the top, only decl or function definition. *)*/ - | identifier TOPar argument_list TCPar TPtVirg + + | identifier TOPar argument_list TCPar end_attributes_opt TPtVirg { if args_are_params $3 then
@@ -2202,16 +2203,17 @@ cpp_other: Declaration( DeclList ([{v_namei = Some (id,NoInit); v_type = ty; v_storage = unwrap sto; v_local = NotLocalDecl; - v_attr = attrs; v_endattr = Ast_c.noattr; + v_attr = attrs; v_endattr = $5; v_type_bis = ref None; },[]], - ($5::iistart::snd sto))) + ($6::iistart::snd sto))) else Declaration - (MacroDecl((NoSto, fst $1, $3, true), [snd $1;$2;$4;$5;fakeInfo()])) + (MacroDecl((NoSto, fst $1, $3, true), [snd $1;$2;$4;$6;fakeInfo()])) (* old: MacroTop (fst $1, $3, [snd $1;$2;$4;$5]) *) } + /* cheap solution for functions with no return type. Not really a cpp_other, but avoids conflicts */ | identifier TOPar argument_list TCPar compound {
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 13/14 parsing_c: cpp_token_c: Introduce MACROANNOTATION hint
In reply to Jaskaran SinghA better way of denoting attributes is to pass attribute information from SmPL to the C parser. However, a temporary solution is to introduce a MACROANNOTATION hint to cpp_token_c.ml so that the parser can identify attributes easily.
This hint can be used as follows in standard.h, the user provided macro-defs file or the given C file itself:
#define __attribute_name MACROANNOTATION
By default, __attribute_name would be identified as a comment. Using the above hint, __attribute_name would be identified as an attribute and transformations from SmPL would be performed as if __attribute_name is an attribute, not a comment.
--- parsing_c/cpp_token_c.ml | 2 ++ 1 file changed, 2 insertions(+)
parsing_c/cpp_token_c.ml +2 -0
@@ -81,6 +81,8 @@ let assoc_hint_string = [ "YACFE_END_ATTRIBUTE" , HintEndAttribute; "YACFE_IDENT_BUILDER" , HintMacroIdentBuilder; + "MACROANNOTATION" , HintAttribute; + "MACROSTATEMENT" , HintMacroStatement; (* backward compatibility *) ]
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
PATCH v4 14/14 cocci: standard.h: Annotate attributes with MACROANNOTATION
In reply to Jaskaran SinghApply the MACROANNOTATION hint to select attributes in standard.h. These hints can be replaced, removed or added by the user as their use case demands.
--- standard.h | 116 ++++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 58 deletions(-)
standard.h +58 -58
@@ -87,46 +87,46 @@// ---------------------------------------------------------------------------- // Attributes. could perhaps generalize via "__.*" // ---------------------------------------------------------------------------- -#define __init -#define __initconst -#define __page_aligned_data -#define __page_aligned_bss -#define __always_unused -#define __visible -#define __exit -#define __user -#define __iomem -#define __initdata -#define __exitdata -#define __devinit -#define __devexit -#define __devinitdata -#define __cpuinit -#define __cpuinitdata -#define __init_or_module -#define __initdata_or_module -#define __pminit -#define __pminitdata -#define __irq_entry - -#define __cacheline_aligned -#define ____cacheline_aligned -#define __cacheline_aligned_in_smp -#define ____cacheline_aligned_in_smp -#define ____cacheline_internodealigned_in_smp +#define __init MACROANNOTATION +#define __initconst MACROANNOTATION +#define __page_aligned_data MACROANNOTATION +#define __page_aligned_bss MACROANNOTATION +#define __always_unused MACROANNOTATION +#define __visible MACROANNOTATION +#define __exit MACROANNOTATION +#define __user MACROANNOTATION +#define __iomem MACROANNOTATION +#define __initdata MACROANNOTATION +#define __exitdata MACROANNOTATION +#define __devinit MACROANNOTATION +#define __devexit MACROANNOTATION +#define __devinitdata MACROANNOTATION +#define __cpuinit MACROANNOTATION +#define __cpuinitdata MACROANNOTATION +#define __init_or_module MACROANNOTATION +#define __initdata_or_module MACROANNOTATION +#define __pminit MACROANNOTATION +#define __pminitdata MACROANNOTATION +#define __irq_entry MACROANNOTATION + +#define __cacheline_aligned MACROANNOTATION +#define ____cacheline_aligned MACROANNOTATION +#define __cacheline_aligned_in_smp MACROANNOTATION +#define ____cacheline_aligned_in_smp MACROANNOTATION +#define ____cacheline_internodealigned_in_smp MACROANNOTATION#define __ALIGNED__#define __3xp_aligned -#define __pmac -#define __force -#define __nocast -#define __read_mostly +#define __pmac MACROANNOTATION +#define __force MACROANNOTATION +#define __nocast MACROANNOTATION +#define __read_mostly MACROANNOTATION -#define __must_check +#define __must_check MACROANNOTATION// pb -#define __unused -#define __maybe_unused +#define __unused MACROANNOTATION +#define __maybe_unused MACROANNOTATION#define __attribute_used__
@@ -139,27 +139,27 @@#define __xipram// in the other part of the kernel, in arch/, mm/, etc -#define __sched -#define __initmv -#define __exception -#define __cpuexit -#define __kprobes -#define __meminit -#define __meminitdata -#define __nosavedata -#define __kernel -#define __nomods_init -#define __apicdebuginit -#define __ipc_init -#define __modinit -#define __lockfunc -#define __weak -#define __tlb_handler_align -#define __lock_aligned -#define __force_data -#define __nongprelbss -#define __nongpreldata -#define __noreturn +#define __sched MACROANNOTATION +#define __initmv MACROANNOTATION +#define __exception MACROANNOTATION +#define __cpuexit MACROANNOTATION +#define __kprobes MACROANNOTATION +#define __meminit MACROANNOTATION +#define __meminitdata MACROANNOTATION +#define __nosavedata MACROANNOTATION +#define __kernel MACROANNOTATION +#define __nomods_init MACROANNOTATION +#define __apicdebuginit MACROANNOTATION +#define __ipc_init MACROANNOTATION +#define __modinit MACROANNOTATION +#define __lockfunc MACROANNOTATION +#define __weak MACROANNOTATION +#define __tlb_handler_align MACROANNOTATION +#define __lock_aligned MACROANNOTATION +#define __force_data MACROANNOTATION +#define __nongprelbss MACROANNOTATION +#define __nongpreldata MACROANNOTATION +#define __noreturn MACROANNOTATION#define __section_jiffies#define __vsyscall_fn
@@ -193,9 +193,9 @@// last last -#define __net_init -#define __net_exit -#define __net_initdata +#define __net_init MACROANNOTATION +#define __net_exit MACROANNOTATION +#define __net_initdata MACROANNOTATION#define __paginginit// in mm
Signature · 6 lines
2.21.1 _______________________________________________ Linux-kernel-mentees mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
Quoted 102 lines · Jaskaran Singh
This patch series aims to improve parsing of attributes in C by Coccinelle's C parser.
These parsing errors were discovered by running a build of Coccinelle's
master branch on gitlab to parse the source code of Linux v5.5-rc4 and Linux
v5.6-rc7. Coccinelle currently manages attributes similar to comments, so
to explicity state what the attributes are to the C parser, a MACROANNOTATION hint was used in Coccinelle'sstandard.hfile. The hint
enables the parser to correctly identify attributes, and thus potentially
handle them differently in the AST.Upon collection of these parsing errors, The GNU C Compiler's grammar was taken as a reference for introducing attributes in correct
places in Coccinelle's C parser.Upon applying the MACROANNOTATION hint to a majority of the attributes in
Coccinelle'sstandard.hfile and parsing the source code of Linux v5.6-rc7, the difference between the stats (of --parse-c) before and after applying this patch series are as follows:Before:
NB total files = 28268; perfect = 25697; pbs = 2570; timeout = 0; =========> 90% nb good = 19160664, nb passed = 135235 =========> 0.70% passed nb good = 19160664, nb bad = 129685 =========> 99.33% goodor passed
After:
NB total files = 28268; perfect = 25699; pbs = 2568; timeout = 0; =========> 90% nb good = 19160856, nb passed = 71716 =========> 0.37% passed nb good = 19160856, nb bad = 129493 =========> 99.33% goodor passed
What this patch series does not do:
- Add attributes from all the new productions into the C AST. Some of these are dropped.
- Reflect changes from the C parser in the SmPL parser.Separate patches will be sent for the above.
Changes in v4:
-------------
Remove the following patches from the series and add/merge them to [1].
parsing_c:visitor_c: Addvk_attributeandvk_attribute_s
parsing_c:lib_parsing_c: Addii_of_attr
engine: Adddistrf_attrto PARAM functor
engine:cocci_vs_c: Add allminus argument toattribute_list
parsing_c: parser: Makeabstract_declaratorpass attributes
cocci: Add parameter attributes to C AST
parsing_c: parser: Place parameter attributes in C AST
parsing_c:visitor_c: Visit parameter attributes
parsing_c:unparse_hrule: Add parameter attributes in record
parsing_c:type_c: Add parameter attributes to record
engine:cocci_vs_c: "Match" parameter attributes
engine:c_vs_c: Match parameter attributes
parsing_c: parser: Add rule for optional end attributes
- Add to series.
parsing_c: parser: Add field declaration end attributes production
- Useend_attributes_optto avoid duplicate code.
parsing_c: parser: Handle struct/union/enum end attributes
- Useend_attributes_optto avoid duplicate code.
parsing_c: parser: Add MacroDecl end attributes production
- Useend_attributes_optto avoid duplicate code.
parsing_c: parser:cpp_otherend attributes production
- Useend_attributes_optto avoid duplicate code.Changes in v3:
-------------parsing_c: parser: Place parameter attributes in C AST
- Remove the "attr is ignored" comments in the declaratorp rule.
- Place the attributes from the "declarator attributes" production in the declaratorp rule in the C AST as well.Changes in v2:
--------------parsing_c: parser: Add attribute production inspec_qualif_list
- This patch applied on its own causes a build error. Fix the error by
removing the let binding forabstract_declaratortin thetype_namerule
Quoted 3 lines
parsing_c: parser: Makeabstract_declaratorpass attributes
- Reflect changes from the above build error fix.
[1]
[PATCH v3 00/23] cocci: Add parameter attributes to SmPL
https://lore.kernel.org/cocci/[email protected]/
Quoted 20 lines
Jaskaran Singh (14):
parsing_c:parse_c: Ignore TMacroAttr and TMacroEndAttr in
passed tokens
parsing_c:parsing_hacks: Label end attributes correctly
parsing_c:parsing_hacks: Commentize attributes before
qualif/type
parsing_c: parser: Add rule for multiple end attributes
parsing_c: parser: Add expression statement attributes
parsing_c: parser: Add rule for optional end attributes
parsing_c: parser: Add attribute production inspec_qualif_list
parsing_c: parser: Addinit_declarator_attrsrule
parsing_c: parser: Add field declaration end attributes
production
parsing_c: parser: Handle struct/union/enum end attributes
parsing_c: parser: Add MacroDecl end attributes production
parsing_c: parser:cpp_otherend attributes production
parsing_c:cpp_token_c: Introduce MACROANNOTATION hint
cocci:standard.h: Annotate attributes with MACROANNOTATION
Quoted 10 lines
parsing_c/cpp_token_c.ml | 2 parsing_c/parse_c.ml | 4 + parsing_c/parser_c.mly | 91 ++++++++++++++++++++++++-------- --- parsing_c/parsing_hacks.ml | 37 ++++++++++++++ standard.h | 116 ++++++++++++++++++++++---------- ------------- 5 files changed, 164 insertions(+), 86 deletions(-)
_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees