Hi, I am wondering how I can tell whether a field of a struct is introduced by padding or not. For example, if I have a struct: struct foo1 { char *p; /* 8 bytes */ char c; /* 1 byte long x; /* 8 bytes */ }; clang may generate: struct foo1 { char *p; /* 8 bytes */ char c; /* 1 byte char pad[7]; /* 7 bytes */ long x; /* 8 bytes */ }; Is there any way that I can tell the "pad" array is generated by padding? Thanks a lot Hongbin -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170518/ff0f0d67/attachment.html>
Hi Hongbin, You can pass `-Wpadded` to clang. For your particular example it will print something along the lines of ``` warning: padding struct 'foo1' with 7 bytes to align 'x' [-Wpadded] long x; ``` Jonas On Thu, May 18, 2017 at 9:15 AM, Hongbin Zheng via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi, > > I am wondering how I can tell whether a field of a struct is introduced by > padding or not. > > For example, if I have a struct: > > struct foo1 { > char *p; /* 8 bytes */ > char c; /* 1 byte > long x; /* 8 bytes */ > }; > > clang may generate: > > struct foo1 { > char *p; /* 8 bytes */ > char c; /* 1 byte > char pad[7]; /* 7 bytes */ > long x; /* 8 bytes */ > }; > > Is there any way that I can tell the "pad" array is generated by padding? > > Thanks a lot > Hongbin > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170518/de746f41/attachment-0001.html>
Hi Jonas, Thanks a lot. In an LLVM pass, how can I check the related information? will clang emit some metadata table? Thanks Hongbin On Thu, May 18, 2017 at 12:47 AM, Jonas Devlieghere <jonas at devlieghere.com> wrote:> Hi Hongbin, > > You can pass `-Wpadded` to clang. For your particular example it will > print something along the lines of > > ``` > warning: padding struct 'foo1' with 7 bytes to align 'x' [-Wpadded] > long x; > ``` > > Jonas > > On Thu, May 18, 2017 at 9:15 AM, Hongbin Zheng via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> Hi, >> >> I am wondering how I can tell whether a field of a struct is introduced >> by padding or not. >> >> For example, if I have a struct: >> >> struct foo1 { >> char *p; /* 8 bytes */ >> char c; /* 1 byte >> long x; /* 8 bytes */ >> }; >> >> clang may generate: >> >> struct foo1 { >> char *p; /* 8 bytes */ >> char c; /* 1 byte >> char pad[7]; /* 7 bytes */ >> long x; /* 8 bytes */ >> }; >> >> Is there any way that I can tell the "pad" array is generated by padding? >> >> Thanks a lot >> Hongbin >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >> >> >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170518/081efcb9/attachment.html>