[cfe-commits] r148857 - in /cfe/trunk: include/clang/Basic/DiagnosticGroups.td include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/SemaCXX/Inputs/warn-new-overaligned-3.h test/SemaCXX/warn-new-overaligned-2.cpp test/SemaCXX/warn-new-overaligned-3.cpp test/SemaCXX/warn-new-overaligned.cpp
Nick Lewycky
nicholas at mxc.ca
Tue Jan 24 13:15:42 PST 2012
Author: nicholas
Date: Tue Jan 24 15:15:41 2012
New Revision: 148857
URL: http://llvm.org/viewvc/llvm-project?rev=148857&view=rev
Log:
Add a new warning, -Wover-aligned, which detects attempts to use the default
allocator to construct an object which declares more alignment than the default
allocator actually provides. Fixes PR9527!
Added:
cfe/trunk/test/SemaCXX/Inputs/warn-new-overaligned-3.h
cfe/trunk/test/SemaCXX/warn-new-overaligned-2.cpp
cfe/trunk/test/SemaCXX/warn-new-overaligned-3.cpp
cfe/trunk/test/SemaCXX/warn-new-overaligned.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaExprCXX.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=148857&r1=148856&r2=148857&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Tue Jan 24 15:15:41 2012
@@ -122,6 +122,7 @@
def NonNull : DiagGroup<"nonnull">;
def : DiagGroup<"nonportable-cfstrings">;
def NonVirtualDtor : DiagGroup<"non-virtual-dtor">;
+def OveralignedType : DiagGroup<"over-aligned">;
def : DiagGroup<"old-style-cast">;
def : DiagGroup<"old-style-definition">;
def OutOfLineDeclaration : DiagGroup<"out-of-line-declaration">;
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=148857&r1=148856&r2=148857&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jan 24 15:15:41 2012
@@ -4024,6 +4024,11 @@
def warn_using_directive_in_header : Warning<
"using namespace directive in global context in header">,
InGroup<HeaderHygiene>, DefaultIgnore;
+def warn_overaligned_type : Warning<
+ "type %0 requires %1 bytes of alignment and the default allocator only "
+ "guarantees %2 bytes">,
+ InGroup<OveralignedType>, DefaultIgnore;
+def note_overaligned_type : Note<"over-aligned here">;
def err_conditional_void_nonvoid : Error<
"%select{left|right}1 operand to ? is void, but %select{right|left}1 operand "
Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=148857&r1=148856&r2=148857&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Jan 24 15:15:41 2012
@@ -20,6 +20,7 @@
#include "clang/Sema/Scope.h"
#include "clang/Sema/TemplateDeduction.h"
#include "clang/AST/ASTContext.h"
+#include "clang/AST/CharUnits.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprCXX.h"
@@ -1104,6 +1105,21 @@
PlaceArgs = &AllPlaceArgs[0];
}
+ // Warn if the type is over-aligned and is being allocated by global operator
+ // new.
+ if (OperatorNew &&
+ (OperatorNew->isImplicit() ||
+ getSourceManager().isInSystemHeader(OperatorNew->getLocStart()))) {
+ if (unsigned Align = Context.getPreferredTypeAlign(AllocType.getTypePtr())){
+ unsigned SuitableAlign = Context.getTargetInfo().getSuitableAlign();
+ if (Align > SuitableAlign)
+ Diag(StartLoc, diag::warn_overaligned_type)
+ << AllocType
+ << unsigned(Align / Context.getCharWidth())
+ << unsigned(SuitableAlign / Context.getCharWidth());
+ }
+ }
+
bool Init = ConstructorLParen.isValid();
// --- Choosing a constructor ---
CXXConstructorDecl *Constructor = 0;
Added: cfe/trunk/test/SemaCXX/Inputs/warn-new-overaligned-3.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/Inputs/warn-new-overaligned-3.h?rev=148857&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/Inputs/warn-new-overaligned-3.h (added)
+++ cfe/trunk/test/SemaCXX/Inputs/warn-new-overaligned-3.h Tue Jan 24 15:15:41 2012
@@ -0,0 +1,12 @@
+#pragma GCC system_header
+
+// This header file pretends to be <new> from the system library, for the
+// purpose of the over-aligned warnings test.
+
+void* operator new(unsigned long) {
+ return 0;
+}
+void* operator new[](unsigned long) {
+ return 0;
+}
+
Added: cfe/trunk/test/SemaCXX/warn-new-overaligned-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-new-overaligned-2.cpp?rev=148857&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-new-overaligned-2.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-new-overaligned-2.cpp Tue Jan 24 15:15:41 2012
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -Wover-aligned -verify %s
+
+// This test verifies that we don't warn when the global operator new is
+// overridden. That's why we can't merge this with the other test file.
+
+void* operator new(unsigned long);
+void* operator new[](unsigned long);
+
+struct Test {
+ template <typename T>
+ struct SeparateCacheLines {
+ T data;
+ } __attribute__((aligned(256)));
+
+ SeparateCacheLines<int> high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test;
+ new Test[10];
+}
Added: cfe/trunk/test/SemaCXX/warn-new-overaligned-3.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-new-overaligned-3.cpp?rev=148857&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-new-overaligned-3.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-new-overaligned-3.cpp Tue Jan 24 15:15:41 2012
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -Wover-aligned %s -isystem %S/Inputs -verify
+
+// This test ensures that we still get the warning even if we #include <new>
+// where the header here simulates <new>.
+#include <warn-new-overaligned-3.h>
+
+struct Test {
+ template <typename T>
+ struct SeparateCacheLines {
+ T data;
+ } __attribute__((aligned(256)));
+
+ SeparateCacheLines<int> high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test; // expected-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+ new Test[10]; // expected-warning {{type 'Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+}
Added: cfe/trunk/test/SemaCXX/warn-new-overaligned.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-new-overaligned.cpp?rev=148857&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/warn-new-overaligned.cpp (added)
+++ cfe/trunk/test/SemaCXX/warn-new-overaligned.cpp Tue Jan 24 15:15:41 2012
@@ -0,0 +1,73 @@
+// RUN: %clang_cc1 -Wover-aligned -verify %s
+
+namespace test1 {
+struct Test {
+ template <typename T>
+ struct SeparateCacheLines {
+ T data;
+ } __attribute__((aligned(256)));
+
+ SeparateCacheLines<int> high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test; // expected-warning {{type 'test1::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+ new Test[10]; // expected-warning {{type 'test1::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+}
+}
+
+namespace test2 {
+class Test {
+ typedef int __attribute__((aligned(256))) aligned_int;
+ aligned_int high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test; // expected-warning {{type 'test2::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+ new Test[10]; // expected-warning {{type 'test2::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+}
+}
+
+namespace test3 {
+struct Test {
+ template <typename T>
+ struct SeparateCacheLines {
+ T data;
+ } __attribute__((aligned(256)));
+
+ void* operator new(unsigned long) {
+ return 0;
+ }
+
+ SeparateCacheLines<int> high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test;
+ new Test[10]; // expected-warning {{type 'test3::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+}
+}
+
+namespace test4 {
+struct Test {
+ template <typename T>
+ struct SeparateCacheLines {
+ T data;
+ } __attribute__((aligned(256)));
+
+ void* operator new[](unsigned long) {
+ return 0;
+ }
+
+ SeparateCacheLines<int> high_contention_data[10];
+};
+
+void helper() {
+ Test t;
+ new Test; // expected-warning {{type 'test4::Test' requires 256 bytes of alignment and the default allocator only guarantees}}
+ new Test[10];
+}
+}
More information about the cfe-commits
mailing list