brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.3 KiB · 6b5b946 Raw
119 lines · cpp
1// RUN: %check_clang_tidy %s altera-struct-pack-align %t -- -header-filter=.*2 3// Struct needs both alignment and packing4struct error {5  char a;6  double b;7  char c;8};9// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'error' is inefficient due to padding; only needs 10 bytes but is using 24 bytes [altera-struct-pack-align]10// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'error'11// CHECK-MESSAGES: :[[@LINE-7]]:8: warning: accessing fields in struct 'error' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]12// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'error' to 16 bytes13// CHECK-FIXES: } __attribute__((packed)) __attribute__((aligned(16)));14 15// Struct is explicitly packed, but needs alignment16struct error_packed {17  char a;18  double b;19  char c;20} __attribute__((packed));21// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'error_packed' is inefficient due to poor alignment; currently aligned to 1 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]22// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'error_packed' to 16 bytes23// CHECK-FIXES: } __attribute__((aligned(16))) __attribute__((packed));24 25// Struct is properly packed, but needs alignment26struct align_only {27  char a;28  char b;29  char c;30  char d;31  int e;32  double f;33};34// CHECK-MESSAGES: :[[@LINE-8]]:8: warning: accessing fields in struct 'align_only' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]35// CHECK-MESSAGES: :[[@LINE-9]]:8: note: use "__attribute__((aligned(16)))" to align struct 'align_only' to 16 bytes36// CHECK-FIXES: } __attribute__((aligned(16)));37 38// Struct is perfectly packed but wrongly aligned39struct bad_align {40  char a;41  double b;42  char c;43} __attribute__((packed)) __attribute__((aligned(8)));44// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]45// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align' to 16 bytes46// CHECK-FIXES: } __attribute__((packed)) __attribute__((aligned(16)));47 48struct bad_align2 {49  char a;50  double b;51  char c;52} __attribute__((packed)) __attribute__((aligned(32)));53// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align2' is inefficient due to poor alignment; currently aligned to 32 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]54// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align2' to 16 bytes55// CHECK-FIXES: } __attribute__((packed)) __attribute__((aligned(16)));56 57struct bad_align3 {58  char a;59  double b;60  char c;61} __attribute__((packed)) __attribute__((aligned(4)));62// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'bad_align3' is inefficient due to poor alignment; currently aligned to 4 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]63// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((aligned(16)))" to align struct 'bad_align3' to 16 bytes64// CHECK-FIXES: } __attribute__((packed)) __attribute__((aligned(16)));65 66// Struct is both perfectly packed and aligned67struct success {68  char a;69  double b;70  char c;71} __attribute__((packed)) __attribute__((aligned(16)));72//Should take 10 bytes and be aligned to 16 bytes73 74// Struct is properly packed, and explicitly aligned75struct success2 {76  int a;77  int b;78  int c;79} __attribute__((aligned(16)));80 81// If struct is properly aligned, packing not needed82struct success3 {83  char a;84  double b;85  char c;86} __attribute__((aligned(16)));87 88// If struct is templated, warnings should not be triggered89template <typename A, typename B>90struct success4 {91  A a;92  B b;93  int c;94};95 96// Warnings should not trigger on struct instantiations97void no_trigger_on_instantiation() {98  struct bad_align3 instantiated { 'a', 0.001, 'b' };99}100 101// Make sure that we don't recommend aligning an empty struct to zero bytes (PR#51620)102struct StructWithNoFields {};103 104struct ContainsStructWithNoFields {105  StructWithNoFields s;106};107 108// Make sure that an empty struct is treated like "char" for padding and alignment purposes109struct ContainsStructWithNoFields2 {110  StructWithNoFields s;111  double d;112  StructWithNoFields t;113};114// CHECK-MESSAGES: :[[@LINE-5]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to padding; only needs 10 bytes but is using 24 bytes [altera-struct-pack-align]115// CHECK-MESSAGES: :[[@LINE-6]]:8: note: use "__attribute__((packed))" to reduce the amount of padding applied to struct 'ContainsStructWithNoFields2'116// CHECK-MESSAGES: :[[@LINE-7]]:8: warning: accessing fields in struct 'ContainsStructWithNoFields2' is inefficient due to poor alignment; currently aligned to 8 bytes, but recommended alignment is 16 bytes [altera-struct-pack-align]117// CHECK-MESSAGES: :[[@LINE-8]]:8: note: use "__attribute__((aligned(16)))" to align struct 'ContainsStructWithNoFields2' to 16 bytes118// CHECK-FIXES: } __attribute__((packed)) __attribute__((aligned(16)));119