Skip to content

Commit 1ea8fef

Browse files
committed
Add test for compiler attribute validation.
1 parent eb85f5a commit 1ea8fef

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
Attributes: Compiler Attributes can check for target declarations
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zend-test')) {
6+
echo "skip requires zend-test extension\n";
7+
}
8+
--FILE--
9+
<?php
10+
11+
<<ZendTestAttribute>>
12+
function foo() {
13+
}
14+
--EXPECTF--
15+
Fatal error: Only classes can be marked with <<ZendTestAttribute>> in %s

ext/zend_test/test.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@
2323
#include "ext/standard/info.h"
2424
#include "php_test.h"
2525
#include "test_arginfo.h"
26+
#include "zend_attributes.h"
2627

2728
static zend_class_entry *zend_test_interface;
2829
static zend_class_entry *zend_test_class;
2930
static zend_class_entry *zend_test_child_class;
3031
static zend_class_entry *zend_test_trait;
32+
static zend_class_entry *zend_test_attribute;
3133
static zend_object_handlers zend_test_class_handlers;
3234

3335
ZEND_FUNCTION(zend_test_func)
@@ -181,6 +183,13 @@ static zend_function *zend_test_class_static_method_get(zend_class_entry *ce, ze
181183
}
182184
/* }}} */
183185

186+
void zend_attribute_validate_zendtestattribute(zend_attribute *attr, int target)
187+
{
188+
if (target != ZEND_ATTRIBUTE_TARGET_CLASS) {
189+
zend_error(E_COMPILE_ERROR, "Only classes can be marked with <<ZendTestAttribute>>");
190+
}
191+
}
192+
184193
ZEND_METHOD(_ZendTestClass, __toString) /* {{{ */ {
185194
RETURN_EMPTY_STRING();
186195
}
@@ -272,6 +281,12 @@ PHP_MINIT_FUNCTION(zend_test)
272281
zend_register_class_alias("_ZendTestClassAlias", zend_test_class);
273282

274283
REGISTER_LONG_CONSTANT("ZEND_TEST_DEPRECATED", 42, CONST_PERSISTENT | CONST_DEPRECATED);
284+
285+
INIT_CLASS_ENTRY(class_entry, "ZendTestAttribute", NULL);
286+
zend_test_attribute = zend_register_internal_class(&class_entry);
287+
zend_test_attribute->ce_flags |= ZEND_ACC_FINAL;
288+
289+
zend_compiler_attribute_register(zend_test_attribute, zend_attribute_validate_zendtestattribute);
275290
return SUCCESS;
276291
}
277292

0 commit comments

Comments
 (0)