[PATCH] D14274: Add alloc_size attribute to clang

George Burgess IV via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 28 13:29:23 PDT 2016


george.burgess.iv added inline comments.

================
Comment at: include/clang/Basic/Attr.td:753
@@ +752,3 @@
+  let Args = [IntArgument<"ElemSizeParam">, IntArgument<"NumElemsParam", 1>];
+  let TemplateDependent = 1;
+  let Documentation = [AllocSizeDocs];
----------------
aaron.ballman wrote:
> I don't see any C++ tests involving templates; can you add some?
Good catch

================
Comment at: test/SemaCXX/constant-expression-cxx11.cpp:1171
@@ -1170,3 +1170,3 @@
   int l : n3; // expected-error {{constant expression}} expected-note {{read of non-const variable}}
-  int m : t.n; // expected-error {{constant expression}} expected-note {{read of non-constexpr variable}}
+  int m : t.n; // expected-warning{{width of bit-field 'm' (42 bits)}}
 };
----------------
aaron.ballman wrote:
> This change seems out of place for a test that doesn't use alloc_size anywhere. Can you explain why this test has changed?
Yup!

Clang's constexpr evaluator is conservative about `const` variables. If it kept this attitude, `alloc_size` evaluation can't happen in non-constexpr contexts in clang, even in cases like:

```
int foo() {
  void *const p = malloc(10);
  return __builtin_object_size(p, 0);
}
```

The change I made at ExprConstant.cpp:2789 (which Richard okayed when I talked with him offline) makes the constexpr evaluator more willing to look at const variables. With that, we can fold calls to `__builtin_object_size` like above in clang.

It also means that there are a few cases outside of `alloc_size` where the constexpr evaluator can be more aggressive. As it turns out, this is one of them (as is the change in test/CodeGenCXX/global-init.cpp, IIRC)


http://reviews.llvm.org/D14274





More information about the cfe-commits mailing list