diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-09-22 14:32:24 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2010-09-22 14:32:24 +0000 |
commit | 78a916ec5ff5b66adec3c499e1b9af7b87668309 (patch) | |
tree | 6963a1c26e97a6d830e04770d504a1b3d0ac9f09 /include/clang/Basic | |
parent | 398e6b90f5e161d520a95cbf34c732a55fd3e476 (diff) |
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
Diffstat (limited to 'include/clang/Basic')
-rw-r--r-- | include/clang/Basic/DiagnosticGroups.td | 3 | ||||
-rw-r--r-- | include/clang/Basic/DiagnosticSemaKinds.td | 13 | ||||
-rw-r--r-- | include/clang/Basic/SourceManager.h | 2 |
3 files changed, 17 insertions, 1 deletions
diff --git a/include/clang/Basic/DiagnosticGroups.td b/include/clang/Basic/DiagnosticGroups.td index fa7c5cfc91..c7585e1ee0 100644 --- a/include/clang/Basic/DiagnosticGroups.td +++ b/include/clang/Basic/DiagnosticGroups.td @@ -84,7 +84,8 @@ def OutOfLineDeclaration : DiagGroup<"out-of-line-declaration">; def : DiagGroup<"overflow">; def OverlengthStrings : DiagGroup<"overlength-strings">; def : DiagGroup<"overloaded-virtual">; -def : DiagGroup<"packed">; +def Packed : DiagGroup<"packed">; +def Padded : DiagGroup<"padded">; def PointerArith : DiagGroup<"pointer-arith">; def PoundWarning : DiagGroup<"#warnings">, DiagCategory<"#warning Directive">; diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 5cb242f9ca..0927de34da 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -1825,6 +1825,19 @@ def err_vm_func_decl : Error< def err_array_too_large : Error< "array is too large (%0 elements)">; +// -Wpadded, -Wpacked +def warn_padded_struct_field : Warning< + "padding %select{struct|class}0 %1 with %2 %select{byte|bit}3%select{|s}4 " + "to align %5">, InGroup<Padded>, DefaultIgnore; +def warn_padded_struct_anon_field : Warning< + "padding %select{struct|class}0 %1 with %2 %select{byte|bit}3%select{|s}4 " + "to align anonymous bit-field">, InGroup<Padded>, DefaultIgnore; +def warn_padded_struct_size : Warning< + "padding size of %0 with %1 %select{byte|bit}2%select{|s}3 " + "to alignment boundary">, InGroup<Padded>, DefaultIgnore; +def warn_unnecessary_packed : Warning< + "packed attribute is unnecessary for %0">, InGroup<Packed>, DefaultIgnore; + def err_typecheck_negative_array_size : Error<"array size is negative">; def warn_typecheck_function_qualifiers : Warning< "qualifier on function type %0 has unspecified behavior">; diff --git a/include/clang/Basic/SourceManager.h b/include/clang/Basic/SourceManager.h index 7a66117f20..fd4f3f4f7a 100644 --- a/include/clang/Basic/SourceManager.h +++ b/include/clang/Basic/SourceManager.h @@ -436,6 +436,8 @@ public: void clearIDTables(); + Diagnostic &getDiagnostics() const { return Diag; } + //===--------------------------------------------------------------------===// // MainFileID creation and querying methods. //===--------------------------------------------------------------------===// |