1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/build/clang-plugin/tests/TestStackClass.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,50 @@ 1.4 +#define MOZ_STACK_CLASS __attribute__((annotate("moz_stack_class"))) 1.5 +#include <stddef.h> 1.6 + 1.7 +struct MOZ_STACK_CLASS Stack { 1.8 + int i; 1.9 + void *operator new(size_t x) { return 0; } 1.10 + void *operator new(size_t blah, char *buffer) { return buffer; } 1.11 +}; 1.12 + 1.13 +template <class T> 1.14 +struct MOZ_STACK_CLASS TemplateClass { 1.15 + T i; 1.16 +}; 1.17 + 1.18 +void gobble(void *) { } 1.19 + 1.20 +void misuseStackClass(int len) { 1.21 + Stack valid; 1.22 + Stack alsoValid[2]; 1.23 + static Stack notValid; // expected-error {{variable of type 'Stack' only valid on the stack}} 1.24 + static Stack alsoNotValid[2]; // expected-error {{variable of type 'Stack [2]' only valid on the stack}} 1.25 + 1.26 + gobble(&valid); 1.27 + gobble(¬Valid); 1.28 + gobble(&alsoValid[0]); 1.29 + 1.30 + gobble(new Stack); // expected-error {{variable of type 'Stack' only valid on the stack}} 1.31 + gobble(new Stack[10]); // expected-error {{variable of type 'Stack' only valid on the stack}} 1.32 + gobble(new TemplateClass<int>); // expected-error {{variable of type 'TemplateClass<int>' only valid on the stack}} 1.33 + gobble(len <= 5 ? &valid : new Stack); // expected-error {{variable of type 'Stack' only valid on the stack}} 1.34 + 1.35 + char buffer[sizeof(Stack)]; 1.36 + gobble(new (buffer) Stack); 1.37 +} 1.38 + 1.39 +Stack notValid; // expected-error {{variable of type 'Stack' only valid on the stack}} 1.40 +struct RandomClass { 1.41 + Stack nonstaticMember; // expected-note {{'RandomClass' is a stack class because member 'nonstaticMember' is a stack class 'Stack'}} 1.42 + static Stack staticMember; // expected-error {{variable of type 'Stack' only valid on the stack}} 1.43 +}; 1.44 +struct MOZ_STACK_CLASS RandomStackClass { 1.45 + Stack nonstaticMember; 1.46 + static Stack staticMember; // expected-error {{variable of type 'Stack' only valid on the stack}} 1.47 +}; 1.48 + 1.49 +struct BadInherit : Stack {}; // expected-note {{'BadInherit' is a stack class because it inherits from a stack class 'Stack'}} 1.50 +struct MOZ_STACK_CLASS GoodInherit : Stack {}; 1.51 + 1.52 +BadInherit moreInvalid; // expected-error {{variable of type 'BadInherit' only valid on the stack}} 1.53 +RandomClass evenMoreInvalid; // expected-error {{variable of type 'RandomClass' only valid on the stack}}