aboutsummaryrefslogtreecommitdiffstats
path: root/cparser/Elab.ml
Commit message (Collapse)AuthorAgeFilesLines
* Add a check for the args of unprototyped calls.Bernhard Schommer2019-05-201-3/+8
| | | | | | The arguments that are passed to an unprototyped function must also be checked to be valid types passed to a function, i.e. they must be complete types after argument conversion.
* Reworked elaboration of declarations/definitions.Bernhard Schommer2019-05-101-140/+138
| | | | | | | | | | | | Since a definition/declaration is completed with after the separator to the next init group member it is also possible to use it for example in the next init group member: char s[]="miaou", buf[sizeof s]; In order to ensure that this works the declarations are added to the environment directly during the elaboration of the init member group instead of later.
* Fix various scoping issues (#163)Bernhard Schommer2019-05-101-51/+56
| | | | | | | | | Pass the environment to all expr eval functions since the functions themselve may be called recursively and modify the environment. The other change introduces new scopes that are strict subsets of their surrounding scopes for if, switch, while, do and for statement, as prescribed by ISO C standards.
* Improve overflow check for integer literals (#157)Michael Schmidt2019-03-201-2/+4
| | | | | | The previous check was incomplete for integer literals in base 10. Bug 26119
* Revised attachment of name attributes to structs, unions, enumsXavier Leroy2019-02-251-6/+21
| | | | | | | | | | | | | | | | | | Consider: ``` struct s { ... } __attribute((aligned(N))); struct t { ... } __attribute((aligned(N))) struct t x; ``` In the first case, the aligned attribute should be attached to struct s, so that further references to struct s are aligned. In the second case, the aligned attribute should be attached to the variable x, because if we attach it to struct t, it will be ignored and cause a warning. This commit changes the attachment rule so that it treats both cases right. Extend regression test for "aligned" attribute accordingly, by testing aligned attribute applied to a name of struct type.
* Reject object-related and struct-related attributes on typedefsXavier Leroy2019-02-251-0/+7
| | | | | | | | | | | | | | | | | | | | This commit adds a check to reject type definitions such as ``` typedef __attribute((section "foo")) int fooint; ``` GCC and Clang also reject this as an error. Without the check, the behavior is somewhat surprising: ``` fooint x; // placed in section "foo" fooint * x; // placed in default section, attribute "foo" is ignored ``` Note that the following must be accepted: ``` typedef struct { ... } __attribute((packed)) t; ``` The "packed" attribute is correctly attached to the struct type and should not be checked. This is achieved by using `attribute_of_type_no_expand` to get the attributes of the typedef-ed type, excluding the attributes carried by a struct/union or another typedef.
* Distinguish object-related and name-related attributesXavier Leroy2019-02-251-8/+12
| | | | | | | | | | | | | | | | | This is a second step towards mimicking GCC/Clang's handling of attributes. This commit introduces a distinction between - Object-related attributes, such as "section", which apply to the object (function, variable) being defined; - Name-related attributes, such as "aligned", which apply to the name (object, struct/union member, struct/union/enum tag) being defined. In particular, "aligned" is now attached to "struct" and "union" definitions, while it used to be "floated up" before. The C11 _Alignas modifier is treated like an object-related attribute, so that ``` struct s { ... }; _Alignas(64) struct s x; ``` correctly associates the alignment with "x" and not with "struct s", where it would be ignored because it was not part of the original definition of s.
* Do not expand type names when floating attributes "up" a declarationXavier Leroy2019-02-251-1/+1
| | | | | | | | | | | | | | | | | | | | | During elaboration of type declarators, non-type-related attributes such as "aligned" or "section" are "floated up" so that they apply to the thing being declared. For example, consider: ``` __attribute((aligned(16))) int * p; ``` The attribute is first attached to type `int`, then floated up to type `int *`, so that it finally applies to `p`, giving a 16-aligned pointer to int, and not a naturally-aligned pointer to 16-aligned int. What happens when the non-type-related attribute comes from a typedef? ``` typedef __attribute((aligned(16))) int i16; i16 * p; ``` CompCert used to expand the typedef then float up the attribute, resulting in `p` being a 16-aligned pointer to int. GCC and Clang produce a naturally-aligned pointer, so they do not expand the typedef before floating. The old CompCert behavior is somewhat surprising, and potentially less useful than the GCC/Clang behavior. This commit changes the floating up of non-type-related attributes so that typedefs and struct/union/enum definitions are not expanded when determining which attributes to float up. This is a first step towards mimicking the GCC/Clang behavior.
* Catch exception from elab_attr_arg.Bernhard Schommer2018-10-181-1/+4
| | | | | | Catch the exception from a non constant argument of a packed attribute and print an error. Bug 24748
* Improved diagnostics: spelling, wording, etc (#138)Michael Schmidt2018-09-141-4/+4
| | | | | | | | | | | | | | | | | | * bug 24268: avoid assertion after reporting error for invalid call to builtin_debug * bug 24268, remove duplicated warning tag in lexer messages * bug 24268, fix spelling in array element designator message * bug 24268, unify 'consider adding option ...' messages * bug 24268, add spacing for icbi operands * bug 24268, uniform use of Ignored_attributes class for identical warnings * bug 24268, unify message for 'assignment to const type' to error from error/fatal error * bug 24268, in handcrafted.messages, "a xxx have been recognized" -> "a xxx has been recognized"
* Fatal error instead of error for bit-fields.Bernhard Schommer2018-09-121-1/+1
| | | | | | Since the following offsetof cannot handle bit-fields we should stop earlier. Bug 24480
* Attach _Alignas to names and refactor _Alignas checks (#133)Bernhard Schommer2018-09-101-7/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * Refactor common code of alignas. Instead of working on attributes the function now works directly on the type since the check always performed an extraction of attributes from a type. Bug 23393 * Attach _Alignas to the name. Bug 23393 * Attach "aligned" attributes to names So that __attribute((aligned(N))) remains consistent with _Alignas(N). gcc and clang apply "aligned" attributes to names, with a special case for typedefs: typedef __attribute((aligned(16))) int int_al_16; int_al_16 * p; __attribute((aligned(16))) int * q; For gcc, p is naturally-aligned pointer to 16-aligned int and q is 16-aligned pointer to naturally-aligned int. For CompCert with this commit, both p and q are 16-aligned pointers to naturally-aligned int. * Resurrect the alignment test involving typedef The test was removed because it involved an _Alignas in a typedef, which is no longer supported. However the same effect can be achieved with an "aligned" attribute, which is still supported in typedef.
* Move parameter check.Bernhard Schommer2018-09-031-2/+6
| | | | | | Instead of performing the check only for parameters of function definitions also perform it for function declarations. Bug 23393
* New diagnostic for reduced alignment (#117)Bernhard Schommer2018-08-291-3/+32
| | | | | | | The new diagnostic triggers if an `_Alignas` or an `aligned` attribute or a `packed` attribute requests an alignment smaller than the natural alignment. Bug 23389
* More standard compliant handling of _Alignas.Bernhard Schommer2018-08-241-0/+11
| | | | | | | | | | | The C11 standard disallows the usage of _Alignas for: - Bit-field members of struct or union types - Typedefs - Function Defintions - Parameters of functions It is still allowed to use the gcc attribute for these constructs. Bug 23391
* Preserve attribute(("aligned")) in the AST, don't map it to _AlignasXavier Leroy2018-08-241-2/+1
| | | | | | | | | | | | | | We used to recognize attribute(("aligned"(N))) and map it to _Alignas(N) during elaboration. However, we want to restrict the places where _Alignas can occur, as standardized in ISO C11, while leaving more freedom for the placement of the "aligned" attribute. As a first step in this direction, this commit keeps the "aligned" attribute unchanged in the AST, and distinct from _Alignas attributes. Both attributes are honored when it comes to determining the actual alignment of a type.
* Diagnostic for wrong application of restrict (#119)Bernhard Schommer2018-08-211-4/+23
| | | | | | Restrict is only allowed for pointers whose referenced type is an object type or incomplete type, but not a function type. Bug 23397
* Improve support and diagnostic for type qualified arrays (#118)Bernhard Schommer2018-08-201-0/+6
| | | | | | | | | | | | | | | | * Add diagnostic for type qualified arrays that occur in the wrong place Arrays with type qualifiers (e.g. int t[const 5]) are only allowed as function parameters and for them only the outermost array type derivation. Bug 23400 * Keep attributes from array for argument conversion Type qualifiers of arrays in function parameters are just syntactic sugar to allow adding them to the resulting pointer type. Hence, when a qualified array type such as `int t[const 5]` decays into a pointer type during argument conversion, the pointer type should be qualified, e.g. `int * const t`.
* Added warning for incomplete tentative static defs (#114)Bernhard Schommer2018-08-201-4/+5
| | | | | | Tentative static definitions with incomplete type are not allowed in C99. However most popular compilers support them and warn about them. Bug 23377
* Additional checks for flex arrays in structs (#93)Bernhard Schommer2018-08-201-5/+11
| | | | | | | | | | | | | | | * Error for structs with only flex array member Flexible array members are only allowed if another member exists. Bug 23324 * Added checks for nesting of structs with flex array members Warn if a struct with a flex array member is used as array element or member of another struct. Such usage is dubious. Bug 23324 Don't warn if the struct-with-flex-array is a member of an union.
* Turn error into fatal error for unnamed parameter.Bernhard Schommer2018-08-201-2/+4
| | | | | | Since the parameter name gets used in other error messages it results in messages without names. Bug 24283
* For "packed" attribute, check that 3rd parameter is 0 or 1Xavier Leroy2018-08-171-1/+1
| | | | | | | It's meant as a Boolean (byte-swap or not), so any other value is dangerous. The error message is the generic "ill-formed 'packed' attribute". Maybe we don't need a custom error message.
* Check for bit-fields in __builtin_offsetofXavier Leroy2018-08-171-1/+4
| | | | __builtin_offsetof(struct s, f) is an error if f is a bit-field.
* Issue with packed structs and sizeof, alignof, offsetof in cparser/Xavier Leroy2018-08-171-17/+35
| | | | | | | | | | | | | | | | | | | | | | | | | | | CompCert has two implementations of sizeof, alignof and offsetof (byte offset of a struct field): - the reference implementation, in Coq, from cfrontend/Ctypes.v - the implementation used during elaboration, in OCaml, from cparser/Cutil.ml The reference Coq implementation is used as much as possible, but sometimes during elaboration the size of a type must be computed (e.g. to compute array sizes), or the offset of a field (e.g. to evaluate __builtin_offsetof), in which case the OCaml implementation is used. This causes issues with packed structs. Currently, the cparser/Cutil.ml functions ignore the "packed" attribute on structs. Their results disagree with the "true" sizes, alignments and offsets computed by the cfrontend/Ctypes.v functions after source-to-source transformation of packed structs as done in cparser/PackedStruct.ml. For example: ``` struct __packed__(1) s { char c; short s; int i; }; assert (__builtin_offsetof(struct s, i) == 3); assert (sizeof(struct s) = sizeof(char[sizeof(struct s)])); ``` The two assertions fail. In the first assertion, __builtin_offsetof is elaborated to 4, because the packed attribute is ignored during elaboration. In the second assertion, the type `char[sizeof(struct s)]` is elaborated to `char[8]`, again because the packed attribute is ignored during elaboration, while the other `sizeof(struct s)` is computed as 7 after the source-to-source transformation of packed structs. This commit changes the cparser/Cutil.ml functions so that they take the packed attribute into account when computing sizeof, alignof, offsetof, and struct_layout. Related changes: * cparser/Cutil: add `packing_parameters` function to extract packing info from attributes * cparser/Cutil: refactor and share more code between sizeof_struct, offsetof, and struct_layout * cparser/Elab: check the alignment parameters given in packed attributes. (The check was previously done in cparser/PackedStruct.ml but now it would come too late.) * cparser/Elab: refactor the checking of alignment parameters between _Alignas, attribute((aligned)), __packed__, and attribute((packed)). * cparser/PackedStructs: simplify the code, some functionality was moved to cparser/Cutil, other to cparser/Elab * cfrontend/C2C: raise an "unsupported" error if a packed struct is defined and -fpacked-structs is not given. Before, the packed attribute would be silently ignored, but now doing so would cause inconsistencies between cfrontend/ and cparser/. * test/regression/packedstruct1.c: add tests to compare the sizes and the offsets produced by the elaborator with those obtained after elaboration.
* Added a check for parameters without identifiers. (#128)Bernhard Schommer2018-08-171-5/+7
| | | | | It is not allowed in C to have a parameter in a parameter list without an identifier. Bug 24283
* Earlier check for invalid asm outputs. (#130)Bernhard Schommer2018-08-171-0/+5
| | | | | | Since a non modifiable lvalue is an invalid asm output it should be checked earlier, otherwise this leads to a retyping error later. Bug 24285
* Various improvements in the wording of diagnostics.Michael Schmidt2018-08-021-74/+73
| | | | | | Fix various typos in diagnostic messages and unified wording and capitalization. Bug 23850
* Remove the `_Alignas(expr)` construct (#125)Xavier Leroy2018-06-071-9/+1
| | | | The `_Alignas(expr)` construct is not C11, only `_Alignas(type)` is.
* Warn for defs and uses of static variables in nonstatic inline functionsXavier Leroy2018-06-041-16/+35
| | | | | | | Nonstatic inline functions can be expanded in several compilation units. The static variables in question may differ between different expansions. This is a manual merge and adaptation of pull request #P95 by @bschommer.
* Parameterize elab_expr by the full elaboration contextXavier Leroy2018-06-041-30/+42
| | | | | | | | | | | | Before, we would pass just the `ctx_vararg` component of the context to `elab_expr` as a Boolean argument. For future extensions, we will need to pass more of the context to `elab_expr`, so why not pass the whole context? This is what this commit does. The `stmt_context` type is renamed `elab_context` and its definition is moved earlier. The `ctx` argument is passed as is from `elab_stmt` to `elab_expr`.
* Support redefinition of a typedef in another scope (#122)Xavier Leroy2018-06-041-2/+2
| | | | | | | The current check for redefinition is too strict, ruling out e.g. ``` typedef int t; void f(void) { typedef char t; } ```
* Warn that _Alignas and _Alignof are C11 extensionsXavier Leroy2018-06-041-1/+3
| | | | Consistently with _Noreturn, anonymous structs, etc.
* Allow align attribute of zero. (#120)Bernhard Schommer2018-05-291-2/+2
| | | | | | As the standard says (and is already implemented) an _Alignas(0) does not change the alignment at all. The same holds for the gcc attribute. Bug 23387
* Removed duplicated whitespace. Bug 23660Bernhard Schommer2018-05-291-1/+1
|
* String literals are l-values and have array types (#116)Bernhard Schommer2018-05-271-12/+1
| | | | | | | | | | | | | | | | | * Allow strings literals as lvalues. Strings and WStrings literals are lvalues, thus it is allowed to take their addresses. Bug 23356. * String literals have types "array of (wide) char", not "pointer to (wide) char" The pointer types were a leftover from the early, CIL-based C frontend. * Remove special case for sizeof("string literal") during elaboration No longer needed now that literals have array types.
* Preserve storage class for functions declared within a blockXavier Leroy2018-05-261-7/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is a follow-up to the introduction of the Storage_auto storage class in commit 760e422. For functions declared within a block, we used to set their storage class to "extern": { { int f(void); ---> extern int f(void); } } This helped enter_or_refine_ident understand that those declarations have linkage. Now that we have Storage_auto, this commit teaches enter_or_refine_ident that local declarations have no linkage if auto or static, and have linkage if extern or default. Then, there is no need to change storage class to extern for locally-declared functions. In turn, this improves the "extern-after-definition" warning recently introduced, avoiding a bizarre warning in the following case: int foo(void){return 0;} int main(void){ int foo(void); return foo(); }
* Warning for extern declaration after definition.Bernhard Schommer2018-05-261-1/+4
| | | | | | | | | Warning for change of storage class after the definition of a function from default storage class to extern storage class. This only plays a role if the function is also declared inline, since for inline functions with default storage class no code is generated, but for inline functions with extern storage class code should be generated. Bug 23512
* Revised elaboration of function definitions, part 2Xavier Leroy2018-05-071-65/+143
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Change elab_type_declarator and elab_fundef_name so that the latter returns two environments: the first one takes into account struct/union definitions from the function return type, while the second one also contains bindings for function parameters and struct/union definitions from the function parameter list. To this end the "kr_ok" bool argument of elab_type_declarator is repurposed and renamed "fundef". It controls not just whether K&R function declarators are supported, but also which bindings the returned environment contains. Change elab_fundef to adapt to the changes in elab_fundef_name and to maintain two environments: - the global environment, enriched with struct/union definitions from the function return type, and with the function binding itself; - the local environment, used for elaborating the body of the function, which also contains bindings for function parameters and struct/union definitions from the function parameter list. Change elab_funbody so that it does not open a new scope for elaborating the body, even though the body is represented as a block in the AST. The standard says that the function body is processed in the same scope where function parameters are declared, so that the following is illegal: int f(int x) { double x; ... } Introduce a variant enter_or_refine_function of enter_or_refine_ident where the fresh identifier to use (if no earlier declaration is found) is created in advance in an earlier scope. This helps implement the proper scoping of function names.
* Revised elaboration of function definitions, part 1Xavier Leroy2018-05-071-36/+40
| | | | | | | | | | | | | | | | Partial revert of commit ec95665e087d39e29ece455b90e7d5918dc88cee. That commit introduced a "keep_ty" parameter to type elaboration functions telling them to keep struct/union definitions occurring in function parameter lists and lift them to the outer environment. By setting keep_ty to true, the following could typecheck: int f(struct s { int a; } x) { return x.a; } However, "struct s" would escape the scope of the function definition and leak to the top-level environment, which is not correct. In subsequent commits we'll address the issues above differently, in a way that does not need the "keep_ty = true" behavior.
* Added a diagnostic for attributes dec after defBernhard Schommer2018-05-071-3/+12
| | | | | | The new warning is raise for function declaration after a function definition that introduce new attributes, which are ignored. Bug 23385
* Warning for comparison of incomplete pointers.Bernhard Schommer2018-05-071-0/+4
| | | | | | The C standards says that either both types must be pointer to complete compatible or incomplete compatible types. Bug 23631
* Check that variables declared in 'for' loops are local variables (#104)Bernhard Schommer2018-05-041-11/+11
| | | | | | | | | | | | | | * Check for static and extern variables in for The declaration inside of a for statement is not allowed to have static or extern storage duration. Bug 23330 * Also check that the declared variables don't have function types. * Also checks that no typedefs occur in 'for' declarations. * Simplify the `elab_declarations` function. It's used only to elaborate the whole program, and the resulting declarations and environment are ignored. So, replace `elab_declarations` by a simpler iteration over the program in `elab_program`.
* Reject empty declarations in K&R functions. (#107)Bernhard Schommer2018-05-041-0/+2
| | | | | Empty declarations in K&R function parameters are not allowed by the C standard. Bug 23375
* Reject arrays of incomplete type (#90)Bernhard Schommer2018-05-021-0/+2
| | | | The type of array elements must be complete. Bug 23341
* Also check statement of label statement.Bernhard Schommer2018-04-271-1/+2
|
* Detect duplicate 'case' or 'default' statements within a 'switch'Xavier Leroy2018-04-271-0/+41
| | | | Report an error in this case.
* Record value of constant expression in C.Scase constructorXavier Leroy2018-04-271-5/+5
| | | | | | | | | | | | The Elab pass checks that the argument of 'case' is a compile-time constant expression. This commit records the value of this expression in the C.Scase AST generated by Elab, so that it can be used for further diagnostics, i.e. checking (in Elab) for duplicate cases. Note that C2C ignores the recorded value and recomputes the value of the expression using Ceval.integer_expr. This is intentional: Ceval.integer_expr is more trustworthy, as it is formally verified against the CompCert C semantics.
* Detect 'case' and 'default' outside a 'switch' statementXavier Leroy2018-04-271-1/+7
| | | | Report an error in this case.
* Additional checks on typedefs (#101)Bernhard Schommer2018-04-261-0/+4
| | | | Typedefs should have a name and also should not contain _Noreturn. Bug 23381
* Earlier, more comprehensive check for constant initializers (#88)Xavier Leroy2018-04-261-3/+6
| | | | | | | | | | | | | | | This commit checks *during elaboration* that initializers for global variables or local static variables are compile-time constants. Before this commit, some non-constant initializers were detected later in the C2C pass, but others were eliminated by the Cleanup pass before being checked, and yet others could cause the Rename pass to abort. To determine which variables are constant l-values, we leverage the recent addition of the Storage_auto storage class and base the determination on the storage class of the identifier: 'auto' or 'register' is not constant, the others are constant.