aboutsummaryrefslogtreecommitdiff
path: root/test/run_translated_c.zig
AgeCommit message (Collapse)Author
2025-09-24delete all the translate-c testsAndrew Kelley
the ziglang/translate-c package has its own test suite, so these are redundant
2025-09-20translate-c: remove cases associated with runtime vector indexingAndrew Kelley
C translation is in the process of switching to be aro-based (see #24497) That codebase will need to gain some kind of helper for translating C code that uses runtime vector indexing.
2024-07-31remove hard tabs from source codeAndrew Kelley
these are illegal according to the spec
2023-10-17tests: translate-c and run-translated-c to the test harnessVeikka Tuominen
2023-09-18translate-c: prevent variable names conflicting with type namesmlugg
This introduces the concept of a "weak global name" into translate-c. translate-c consists of two passes. The first is important, because it discovers all global names, which are used to prevent naming conflicts: whenever we see an identifier in the second pass, we can mangle it if it conflicts with any global or any other in-scope identifier. Unfortunately, this is a bit tricky for structs, unions, and enums. In C, these types are not represented by normal identifers, but by separate tags - `struct foo` does not prevent an unrelated identifier `foo` existing. In general, we want to translate type names to user-friendly ones such as `struct_foo` and `foo` where possible, but we can't guarantee such names will not conflict with real variable names. This is where weak global names come in. In the initial pass, when a global type declaration is seen, `struct_foo` and `foo` are both added as weak global names. This essentially means that we will use these names for the type *if possible*, but if there is another global with the same name, we will mangle the type name instead. Then, when actually translating the declaration, we check whether there's a "true" global with a conflicting name, in which case we mangle our name. If the user-friendly alias `foo` conflicts, we do not attempt to mangle it: we just don't emit it, because a mangled alias isn't particularly helpful.
2023-09-17AstGen: allow closure over known-runtime values within @TypeOfmlugg
AstGen emits an error when a closure over a known-runtime value crosses a namespace boundary. This usually makes sense: however, this usage is actually valid if the capture is within a `@TypeOf` operand. Sema already has a special case to allow such closure within `@TypeOf` when AstGen could not determine a value to be runtime-known. This commit simply introduces analagous logic to AstGen to allow `var`s to cross namespace boundaries within `@TypeOf`.
2023-08-15translate-c: Use canonical type of field for flexible array definitionEvan Haas
This prevents incorrectly casting an ElaboratedType to ArrayType if the flexible field is a typedef'ed zero-length array Fixes #16838
2023-06-25std.cstr: deprecate namespaceEric Joldasov
Signed-off-by: Eric Joldasov <bratishkaerik@getgoogleoff.me>
2022-10-10translate-c: Fix #12263Tau
2022-10-01translate-c: packed struct implies align(1) on every fieldTechcable
Superceeds PR #12735 (now supporting all packed structs in GNU C) Fixes issue #12733 This stops translating C packed struct as a Zig packed struct. Instead use a regular `extern struct` with `align(1)`. This is because (as @Vexu explained) Zig packed structs are really just integers (not structs). Alignment issue is more complicated. I think @ifreund was the first to notice it in his comment on PR #12735 Justification of my interpretion of the C(lang) behavior comes from a careful reading of the GCC docs for type & variable attributes: (clang emulates gnu's packed attribute here) The final line of the documentation for __attribute__ ((aligned)) [on types] says: > When used on a struct, or struct member, *the aligned attribute can only increase the alignment*; in order to decrease it, the packed attribute must be specified as well. This implies that GCC uses the `packed` attribute for alignment purposes in addition to eliminating padding. The documentation for __attribute__((packed)) [on types], states: > This attribute, attached to a struct, union, or C++ class type definition, specifies that each of its members (other than zero-width bit-fields) is placed to minimize the memory required. **This is equivalent to specifying the packed attribute on each of the members**. The key is resolving this indirection, and looking at the documentation for __attribute__((packed)) [on fields (wierdly under "variables" section)]: > The packed attribute specifies that a **structure member should have the smallest possible alignment** — one bit for a bit-field and one byte otherwise, unless a larger value is specified with the aligned attribute. The attribute does not apply to non-member objects. Furthermore, alignment is the only effect of the packed attribute mentioned in the GCC docs (for "common" architecture). Based on this, it seems safe to completely substitute C 'packed' with Zig 'align(1)'. Target-specific or undocumented behavior potentially changes this. Unfortunately, the current implementation of `translate-c` translates as `packed struct` without alignment info. Because Zig packed structs are really integers (as mentioned above), they are the wrong interpretation and we should be using 'extern struct'. Running `translate-c` on the following code: ```c struct foo { char a; int b; } __attribute__((packed)); struct bar { char a; int b; short c; __attribute__((aligned(8))) long d; } __attribute__((packed)); ``` Previously used a 'packed struct' (which was not FFI-safe on stage1). After applying this change, the translated structures have align(1) explicitly applied to all of their fields AS EXPECTED (unless explicitly overriden). This makes Zig behavior for `tranlsate-c` consistent with clang/GCC. Here is the newly produced (correct) output for the above example: ```zig pub const struct_foo = extern struct { a: u8 align(1), b: c_int align(1), }; pub const struct_bar = extern struct { a: u8 align(1), b: c_int align(1), c: c_short align(1), d: c_long align(8), }; ``` Also note for reference: Since the last stable release (0.9.1), there was a change in the language spec related to the alignment of packed structures. The docs for Zig 0.9.1 read: > Packed structs have 1-byte alignment. So the old behavior of translate-c (not specifying any alignment) was possibly correct back then. However the current docs read: > Packed structs have the same alignment as their backing integer Suggsestive both to the change to an integer-backed representation which is incompatible with C's notation.
2022-09-06Merge remote-tracking branch 'origin/master' into llvm15Andrew Kelley
2022-09-03translate-c: do not translate packed C structs as packed Zig structs in stage2Veikka Tuominen
Zig's integer backed packed structs are not compatible with C's packed structs.
2022-08-30fix llvm 15 translate-c regressionsAndrew Kelley
by inserting explicit casts. closes #12682
2022-08-29disable tests failing due to LLVM 15 regressionsAndrew Kelley
2022-08-26coff: do not pull in std.log into coff.zig definitionsJakub Konka
2022-08-25run_translated_c: disable failing test on WindowsJakub Konka
ziglang/zig#12630
2022-07-30translate-c: use correct number of initializers for vectorsEvan Haas
Fixes #12264
2022-07-27run-translated-c: disable two failing testsAndrew Kelley
Issues reported: * #12263 * #12264
2022-07-27translate-c: take address of functions before passing them to @ptrToIntEvan Haas
Fixes #12194
2022-03-08translate-c: use nested scope for comma operator in macrosEvan Haas
Fixes #11040
2022-02-23translate-c: Add support for cast-to-unionEvan Haas
Fixes #10955
2022-02-07std: Allow `mem.zeroes` to work at comptime with extern unionEvan Haas
Fixes #10797
2022-01-11translate-c: Handle typedef'ed void return type for functions.Evan Haas
Fixes #10356
2021-11-20translate-c: coerce boolean results to c_int when negatedEvan Haas
Fixes #10175
2021-11-20translate-c: Allow negative denominator in remainder (%) operatorEvan Haas
Fixes #10176
2021-10-19translate_c: prevent a while under an if from stealing the elseMatthew Borkowski
2021-09-24Spelling corrections (#9833)Josh Soref
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com> Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2021-08-21translate-c: allow string literals to be used as `char *`Evan Haas
In C the type of string literals is `char *`, so when using them in a non-const context we have to cast the const away. Fixes #9126
2021-07-28translate-c: handle signed array subscriptsEvan Haas
A rather complicated workaround for handling signed array subscripts. Once `[*]T + isize` is allowed, this can be removed. Fixes #8556
2021-07-28translate-c: add support for ChooseExprEvan Haas
2021-07-28translate-c: fix import path in translation failure commentEvan Haas
2021-07-19translate-c: Handle underscore when used as an identifierEvan Haas
Use `@` syntax to escape `_` when used as an identifier. Remove the stage1 astgen prohibition against assigning from `_` Note: there a few stage1 bugs preventing `_` from being used as an identifier for a local variable or function parameter; these will be fixed by stage2. They are unlikely to arise in real C code since identifiers starting with underscore are reserved for the implementation.
2021-06-23translate-c: Remove usage of `extern enum`Evan Haas
Translate enum types as the underlying integer type. Translate enum constants as top-level integer constants of the correct type (which does not necessarily match the enum integer type). If an enum constant's type cannot be translated for some reason, omit it. See discussion https://github.com/ziglang/zig/issues/2115#issuecomment-827968279 Fixes #9153
2021-06-13translate-c: don't bother with unwrapping pointersVeikka Tuominen
Dereferencing a c pointer implicitly includes an unwrap, manually adding it just causes bugs.
2021-06-12translate-c: better support for static local variablesEvan Haas
Don't move static local variables into the top-level scope since this can cause name clashes if subsequently-defined variables or parameters in different scopes share the name. Instead, use a variable within a struct so that the variable's lexical scope does not change. This solution was suggested by @LemonBoy Note that a similar name-shadowing problem exists with `extern` variables declared within block scope, but a different solution will be needed since they do need to be moved to the top-level scope and we can't rename them.
2021-06-12translate-c: fix enums that require c_uint typexackus
2021-06-11translate-c: Implement flexible arraysEvan Haas
Fixes #8759
2021-06-07translate-c: properly handle enums used as boolean expressionsEvan Haas
2021-05-19translate-c: add support for __cleanup__ attributeEvan Haas
Use a `defer` statement to implement the C __cleanup__ attribute. See https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html
2021-05-10translate-c: fix typedefs with multiple namesxackus
2021-04-20translate-c: group LHS of array access if necessaryEvan Haas
2021-04-15translate-c: better handling of int -> enum castsEvan Haas
In std.meta.cast when casting to an enum type from an integer type, first do a C-style cast from the source value to the tag type of the enum. This ensures that we don't get an error due to the source value not being representable by the enum. In transCCast() use std.meta.cast instead of directly emitting the cast operation since the enum's underlying type may not be known at translation time due to an MSVC bug, see https://github.com/ziglang/zig/issues/8003 Fixes #6011
2021-04-13translate-c: wrap switch statements in a while (true) loopEvan Haas
This allows `break` statements to be directly translated from the original C. Add a break statement as the last statement of the while loop to ensure we don't have an infinite loop if no breaks / returns are hit in the switch. Fixes #8387
2021-04-06translate-c: Add support for vector expressionsEvan Haas
Includes vector types, __builtin_shufflevector, and __builtin_convertvector
2021-03-28translate-c: intcast compound assignment operand if different-sized integerEvan Haas
Use transCCast to cast the RHS of compound assignment if necessary.
2021-03-22translate-c: Ensure assignments are within a block when necessaryEvan Haas
Ensures that if an assignment statement is the sole statement within a C if statement, for loop, do loop, or do while loop, then when translated it resides within a block, even though it does not in the original C. Fixes the following invalid translation: `if (1) if (1) 2;` -> `if (true) if (true) _ = @as(c_int, 2);` To this: ```zig if (true) if (true) { _ = @as(c_int, 2); }; ``` Fixes #8159
2021-03-18translate-c: preserve zero fractional part in float literalsVeikka Tuominen
2021-03-17translate-c: demote usage of un-implemented builtinsEvan Haas
2021-03-17translate-c: Implement generic selection expressionsEvan Haas
Enables translation of C code that uses the `_Generic` keyword
2021-03-08translate-c: Add compound literal supportEvan Haas