-
-
Save andrep/f94d432ec9b207deac13a22d9b710071 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example & test for FTP-015 | |
cat > empty_struct_test.c << EOF | |
#include <stdio.h> | |
// FIDL: | |
// struct Empty {}; | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wzero-length-array" | |
typedef int FIDLEmptyStructInternal[0]; | |
#pragma clang diagnostic pop | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wc++-compat" | |
#pragma clang diagnostic ignored "-Wextern-c-compat" | |
typedef struct { | |
FIDLEmptyStructInternal reserved; | |
} Empty; | |
#pragma clang diagnostic pop | |
int main() { | |
Empty e; | |
printf("sizeof(Empty): %zu\n", sizeof(e)); | |
return 0; | |
} | |
EOF | |
clang empty_struct_test.c -Weverything -O0 -o empty_struct_test_c_O0 && ./empty_struct_test_c_O0 | |
clang empty_struct_test.c -Weverything -O3 -o empty_struct_test_c_O3 && ./empty_struct_test_c_O3 | |
cat > empty_struct_test.cc << EOF | |
#include <stdio.h> | |
// FIDL: | |
// struct Empty {}; | |
extern "C" { // test including header files intended for C translation units | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wzero-length-array" | |
typedef int FIDLEmptyStructInternal[0]; | |
#pragma clang diagnostic pop | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wc++-compat" | |
#pragma clang diagnostic ignored "-Wextern-c-compat" | |
typedef struct { | |
FIDLEmptyStructInternal reserved; | |
} CEmpty; | |
#pragma clang diagnostic pop | |
} | |
typedef struct { | |
private: | |
#pragma clang diagnostic push | |
#pragma clang diagnostic ignored "-Wzero-length-array" | |
#pragma clang diagnostic ignored "-Wunused-private-field" | |
int reserved[0]; | |
#pragma clang diagnostic pop | |
public: | |
void Foo() { | |
// Can attach methods to an empty struct too, just like a normal struct. | |
printf("Foo called\n"); | |
} | |
} CPPEmpty; | |
int main() { | |
CPPEmpty cpp_e; | |
CEmpty c_e; | |
printf("sizeof(CPPEmpty): %zu\n", sizeof(cpp_e)); | |
cpp_e.Foo(); | |
printf("sizeof(CEmpty): %zu\n", sizeof(c_e)); | |
return 0; | |
} | |
EOF | |
clang++ empty_struct_test.cc -Weverything -O0 -o empty_struct_test_cpp_O0 -std=gnu++17 && ./empty_struct_test_cpp_O0 | |
clang++ empty_struct_test.cc -Weverything -O3 -o empty_struct_test_cpp_O3 -std=gnu++17 && ./empty_struct_test_cpp_O3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment