aboutsummaryrefslogtreecommitdiff
path: root/src/zig_clang.cpp
AgeCommit message (Collapse)Author
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-07-31update Target, CPU, OS, ABI, etc. to LLVM 15Andrew Kelley
2022-07-28Add check to verify libc++ is shared by LLVM/ClangCody Tapscott
This check is needed because if static/dynamic linking is mixed incorrectly, it's possible for Clang and LLVM to end up with duplicate "copies" of libc++. This is not benign: Static variables are not shared, so equality comparisons that depend on pointers to static variables will fail. One such failure is std::generic_category(), which causes POSIX error codes to compare as unequal when passed between LLVM and Clang. I believe this is the cause of https://github.com/ziglang/zig/issues/11168 In order to avoid affecting build times when Zig is repeatedly invoked, we only enable this check for "zig env" and "zig version"
2022-07-01Merge remote-tracking branch 'origin/master' into llvm14Andrew Kelley
2022-05-29Revert "reserve correct space for bitfields"Veikka Tuominen
This reverts commit 22cb6938891c73d64b749a2516c8eaf79aa25b03.
2022-05-28reserve correct space for bitfieldsTwoClocks
2022-04-15stage2: fix bugs preventing stage2 from building stage3 with LLVMVeikka Tuominen
2022-02-23translate-c: Add support for cast-to-unionEvan Haas
Fixes #10955
2022-02-03update C API bindings to LLVM 14Andrew Kelley
* zig_clang is fully updated * zig_llvm is fully updated Some initial work on codegen.cpp is in place for upgrading to LLVM's new opaque pointers. However there is much more to be done. A few of zig llvm bindings for deprecated functions have been updated; more need to be updated.
2021-10-20translate-c: create `inline fn` for always_inlineStéphan Kochen
2021-10-01clang API bindings: fix enum detection functionsAndrew Kelley
These functions are intended to emit compile errors when Clang adds new items to its enums. However, two of them were casting to the Zig enum and switching on that, which wouldn't detect anything useful.
2021-09-30Merge remote-tracking branch 'origin/master' into llvm13Andrew Kelley
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-16update src/ to LLVM 13 rc1 APIAndrew Kelley
2021-08-03translate-c: better codegen for pointer index by int literalEvan Haas
#8589 introduced correct handling of signed (possibly negative) array access of pointers. Since unadorned integer literals in C are signed, this resulted in inefficient generated code when indexing a pointer by a non-negative integer literal.
2021-07-28translate-c: add support for ChooseExprEvan Haas
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-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-11translate-c: Implement flexible arraysEvan Haas
Fixes #8759
2021-06-02translate-c: Fix performance hazard in transPreprocessorEntitiesEvan Haas
Fixes O(N^2) behavior of `transPreprocessorEntities` due to repeated calls to `mem.len` Closes #8959
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-15translate-c: translate global (file scope) assemblyEvan Haas
2021-04-24translate-c: Prevent mistranslation of fp literalsLemonBoy
When trying to retrieve 80bit fp values from clang using getValueAsApproximateDouble we'd eventually hit the ceiling value and return infinity, an invalid value for a fp literal. Add some logic to prevent this error and warn the user. Closes #8602
2021-04-11Merge remote-tracking branch 'origin/master' into llvm12Michael Dusan
2021-04-06translate-c: Add support for vector expressionsEvan Haas
Includes vector types, __builtin_shufflevector, and __builtin_convertvector
2021-03-28Merge remote-tracking branch 'origin/master' into llvm12Andrew Kelley
2021-03-17translate-c: Implement generic selection expressionsEvan Haas
Enables translation of C code that uses the `_Generic` keyword
2021-03-12Merge remote-tracking branch 'origin/master' into llvm12Andrew Kelley
2021-03-08translate-c: Add compound literal supportEvan Haas
2021-03-01Merge remote-tracking branch 'origin/master' into llvm12Andrew Kelley
2021-02-28translate-c: add limited OffsetOfExpr supportEvan Haas
Add support for OffsetOfExpr that contain exactly 1 component, when that component is a field. For example, given: ```c struct S { float f; double d; }; struct T { long l; int i; struct S s[10]; }; ``` Then: ```c offsetof(struct T, i) // supported offsetof(struct T, s[2].d) // not supported currently ```
2021-02-27Merge remote-tracking branch 'origin/master' into llvm12Andrew Kelley
2021-02-25translate-c: add typeof supportEvan Haas
2021-02-25Merge remote-tracking branch 'origin/master' into llvm12Andrew Kelley
Conflicts: * src/clang.zig * src/llvm.zig - this file got moved to src/llvm/bindings.zig in master branch so I had to put the new LLVM arch/os enum tags into it. * lib/std/target.zig, src/stage1/target.cpp - haiku had an inconsistency with its default target ABI, gnu vs eabi. In this commit we make it gnu in both places to match the latest changes by @hoanga. * src/translate_c.zig
2021-02-16translate-c: elide some unecessary casts of literalsVeikka Tuominen
2021-02-08translate-c: Improve function pointer handlingEvan Haas
Omit address-of operator if operand is a function. Improve handling of function-call translation when using function pointers Fixes #4124
2021-01-25translate-c: Improve array supportEvan Haas
1. For incomplete arrays with initializer list (`int x[] = {1};`) use the initializer size as the array size. 2. For arrays initialized with a string literal translate it as an array of character literals instead of `[*c]const u8` 3. Don't crash if an empty initializer is used for an incomplete array. 4. Add a test for multi-character character constants Additionally lay some groundwork for supporting wide string literals. fixes #4831 #7832 #7842
2020-12-16Update zig_llvm.cpp and other bitsJakub Konka
Include updates to corresponding zig sources llvm commit b2851aea80e5a8f0cfd6c3c5a56a6b00fb28c6b6
2020-10-27rename ZigClangFloatingLiteral_getValueAsApproximateDoubleAndrew Kelley
2020-09-30Merge remote-tracking branch 'origin/master' into llvm11Andrew Kelley
The changes to install_files.h needed to put into src/libcxx.zig
2020-09-23eliminate dependency of libzigcpp.a on libzigstage1.aAndrew Kelley
This allows us to create a build of self-hosted with LLVM extensions enabled but without the stage1 backend.
2020-08-18Merge remote-tracking branch 'origin/master' into llvm11Andrew Kelley
2020-08-11translate-c: convert int to bool if bool is expectedVexu
2020-08-04Merge remote-tracking branch 'origin/master' into llvm11Andrew Kelley
2020-08-04translate-c: recognize other type trait expressionsVexu
Closes #5979
2020-07-24update LLVM C++ API wrappers from llvm 10 to 11Andrew Kelley
2020-03-13Merge remote-tracking branch 'origin/master' into llvm10Andrew Kelley
2020-03-10translate-c support struct field alignmentVexu
2020-03-08translate-c reject structs with VLAsVexu
2020-03-07stage1: fix compile error on macOS Xcode 11.4Michael Dusan