Bug 1569681: Part 2 - Add new clang-plugin tests for moz_static_local_class and moz_trivial_destructor attributes; r=Ehsan

These tests are based on `moz_global_class` and `moz_trivial_ctor_dtor` tests,
respectively, but adapted for the semantics of the new attributes.

Differential Revision: https://phabricator.services.mozilla.com/D39718

--HG--
rename : build/clang-plugin/tests/TestGlobalClass.cpp => build/clang-plugin/tests/TestStaticLocalClass.cpp
rename : build/clang-plugin/tests/TestTrivialCtorDtor.cpp => build/clang-plugin/tests/TestTrivialDtor.cpp
extra : moz-landing-system : lando
This commit is contained in:
Aaron Klotz 2019-07-30 18:50:54 +00:00
parent b21e723d2e
commit 20bd2d4b30
3 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,54 @@
#define MOZ_STATIC_LOCAL_CLASS __attribute__((annotate("moz_static_local_class")))
#include <stddef.h>
struct MOZ_STATIC_LOCAL_CLASS StaticLocal {
int i;
void *operator new(size_t x) throw() { return 0; }
void *operator new(size_t blah, char *buffer) { return buffer; }
};
template <class T>
struct MOZ_STATIC_LOCAL_CLASS TemplateClass {
T i;
};
void gobble(void *) { }
void misuseStaticLocalClass(int len) {
StaticLocal notValid; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
StaticLocal alsoNotValid[2]; // expected-error {{variable of type 'StaticLocal [2]' is only valid as a static local}} expected-note {{'StaticLocal [2]' is a static-local type because it is an array of static-local type 'StaticLocal'}} expected-note {{value incorrectly allocated in an automatic variable}}
static StaticLocal valid;
static StaticLocal alsoValid[2];
gobble(&notValid);
gobble(&valid);
gobble(&alsoValid[0]);
gobble(new StaticLocal); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
gobble(new StaticLocal[10]); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
gobble(new TemplateClass<int>); // expected-error {{variable of type 'TemplateClass<int>' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
gobble(len <= 5 ? &valid : new StaticLocal); // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated on the heap}}
char buffer[sizeof(StaticLocal)];
gobble(new (buffer) StaticLocal);
}
StaticLocal notValid; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
struct RandomClass {
StaticLocal nonstaticMember; // expected-note {{'RandomClass' is a static-local type because member 'nonstaticMember' is a static-local type 'StaticLocal'}}
static StaticLocal staticMember; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
};
struct MOZ_STATIC_LOCAL_CLASS RandomStaticLocalClass {
StaticLocal nonstaticMember;
static StaticLocal staticMember; // expected-error {{variable of type 'StaticLocal' is only valid as a static local}} expected-note {{value incorrectly allocated in a global variable}}
};
struct BadInherit : StaticLocal {}; // expected-note {{'BadInherit' is a static-local type because it inherits from a static-local type 'StaticLocal'}}
struct MOZ_STATIC_LOCAL_CLASS GoodInherit : StaticLocal {};
void misuseStaticLocalClassEvenMore(int len) {
BadInherit moreInvalid; // expected-error {{variable of type 'BadInherit' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
RandomClass evenMoreInvalid; // expected-error {{variable of type 'RandomClass' is only valid as a static local}} expected-note {{value incorrectly allocated in an automatic variable}}
}

View File

@ -0,0 +1,53 @@
#define MOZ_TRIVIAL_DTOR __attribute__((annotate("moz_trivial_dtor")))
struct MOZ_TRIVIAL_DTOR EmptyClass{};
template <class T>
struct MOZ_TRIVIAL_DTOR TemplateEmptyClass{};
struct MOZ_TRIVIAL_DTOR NonEmptyClass {
void *m;
};
template <class T>
struct MOZ_TRIVIAL_DTOR TemplateNonEmptyClass {
T* m;
};
struct MOZ_TRIVIAL_DTOR BadUserDefinedDtor { // expected-error {{class 'BadUserDefinedDtor' must have a trivial destructor}}
~BadUserDefinedDtor() {}
};
struct MOZ_TRIVIAL_DTOR BadVirtualDtor { // expected-error {{class 'BadVirtualDtor' must have a trivial destructor}}
virtual ~BadVirtualDtor() {}
};
struct MOZ_TRIVIAL_DTOR OkVirtualMember {
virtual void f();
};
void foo();
struct MOZ_TRIVIAL_DTOR BadNonEmptyCtorDtor { // expected-error {{class 'BadNonEmptyCtorDtor' must have a trivial destructor}}
BadNonEmptyCtorDtor() { foo(); }
~BadNonEmptyCtorDtor() { foo(); }
};
struct NonTrivialDtor {
~NonTrivialDtor() { foo(); }
};
struct VirtualMember {
virtual void f();
};
struct MOZ_TRIVIAL_DTOR BadNonTrivialDtorInBase : NonTrivialDtor { // expected-error {{class 'BadNonTrivialDtorInBase' must have a trivial destructor}}
};
struct MOZ_TRIVIAL_DTOR BadNonTrivialDtorInMember { // expected-error {{class 'BadNonTrivialDtorInMember' must have a trivial destructor}}
NonTrivialDtor m;
};
struct MOZ_TRIVIAL_DTOR OkVirtualMemberInMember {
VirtualMember m;
};

View File

@ -43,8 +43,10 @@ SOURCES += [
'TestRefCountedCopyConstructor.cpp',
'TestSprintfLiteral.cpp',
'TestStackClass.cpp',
'TestStaticLocalClass.cpp',
'TestTemporaryClass.cpp',
'TestTrivialCtorDtor.cpp',
'TestTrivialDtor.cpp',
]
if CONFIG['OS_ARCH'] == 'WINNT':