]> granicus.if.org Git - clang/commit
Implement -Wpadded and -Wpacked.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 22 Sep 2010 14:32:24 +0000 (14:32 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 22 Sep 2010 14:32:24 +0000 (14:32 +0000)
commit78a916ec5ff5b66adec3c499e1b9af7b87668309
tree6963a1c26e97a6d830e04770d504a1b3d0ac9f09
parent398e6b90f5e161d520a95cbf34c732a55fd3e476
Implement -Wpadded and -Wpacked.

-Wpadded warns when undesired padding is introduced in a struct. (rdar://7469556)
-Wpacked warns if a struct is given the packed attribute, but the packed attribute has no effect
  on the layout or the size of the struct. Such structs may be mis-aligned for little benefit.

The warnings are emitted at the point where layout is calculated, that is at RecordLayoutBuilder.
To avoid calculating the layouts of all structs regardless of whether they are needed or not,
I let the layouts be lazily constructed when needed. This has the disadvantage that the above warnings
will be emitted only when they are used for IR gen, and not e.g with -fsyntax-only:

$ cat t.c
struct S {
  char c;
  int i;
};
void f(struct S* s) {}

$ clang -fsyntax-only -Wpadded t.c
$ clang -c -Wpadded t.c -o t.o
t.c:3:7: warning: padding struct 'struct S' with 3 bytes to align 'i' [-Wpadded]
  int i;
      ^
1 warning generated.

This is a good tradeoff between providing the warnings and not calculating layouts for all
structs in case the user has enabled a couple of rarely used warnings.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114544 91177308-0d34-0410-b5e6-96231b3b80d8
include/clang/AST/ASTContext.h
include/clang/Basic/DiagnosticGroups.td
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Basic/SourceManager.h
lib/AST/ASTContext.cpp
lib/AST/RecordLayoutBuilder.cpp
test/CodeGenCXX/warn-padded-packed.cpp [new file with mode: 0644]