[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits

https://github.com/aloisklink updated 
https://github.com/llvm/llvm-project/pull/68059

>From a76561f522f628b0882572f8dabee6f7e4abd5f5 Mon Sep 17 00:00:00 2001
From: Alois Klink 
Date: Mon, 2 Oct 2023 19:59:06 +0100
Subject: [PATCH 1/5] [clang] Ignore GCC 11 [[malloc(x)]] attribute

Ignore the `[[malloc(x)]]` or `[[malloc(x, 1)]]` function attribute
syntax added in [GCC 11][1] and print a warning instead of an error.

Unlike `[[malloc]]` with no arguments (which is supported by Clang),
GCC uses the one or two argument form to specify a deallocator for
GCC's static analyzer.

Code currently compiled with `[[malloc(x)]]` or
`__attribute((malloc(x)))` fails with the following error:
`'malloc' attribute takes no arguments`.

[1]: 
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;f=gcc/doc/extend.texi;h=dce6c58db87ebf7f4477bd3126228e73e497#patch6

Fixes: https://github.com/llvm/llvm-project/issues/51607
Partial-Bug: https://github.com/llvm/llvm-project/issues/53152
---
 clang/docs/ReleaseNotes.rst   |  5 +
 clang/include/clang/Basic/Attr.td |  2 ++
 clang/include/clang/Basic/AttrDocs.td |  3 +++
 .../clang/Basic/DiagnosticCommonKinds.td  |  4 
 clang/lib/Sema/SemaDeclAttr.cpp   | 20 ---
 clang/test/Sema/attr-args.c   |  6 --
 clang/test/SemaCXX/attr-print.cpp |  8 
 7 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6be824771c583be..3cbeed044ada396 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -289,6 +289,11 @@ Bug Fixes to Compiler Builtins
 Bug Fixes to Attribute Support
 ^^
 
+- Clang now emits a warning instead of an error when using the one or two
+  argument form of GCC 11's ``__attribute__((malloc(deallocator)))``
+  or ``__attribute__((malloc(deallocator, ptr-index)))``
+  (`#51607 `).
+
 Bug Fixes to C++ Support
 
 
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 7a6ec77ae84b15a..db1f332efdb7653 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1629,6 +1629,8 @@ def IFunc : Attr, TargetSpecificAttr {
 
 def Restrict : InheritableAttr {
   let Spellings = [Declspec<"restrict">, GCC<"malloc">];
+  let Args = [IdentifierArgument<"Deallocator", /*opt*/ 1>,
+  ParamIdxArgument<"DeallocatorPtrArgIndex", /*opt*/ 1>];
   let Subjects = SubjectList<[Function]>;
   let Documentation = [RestrictDocs];
 }
diff --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index 8d928dcc146b254..1e498aeea6b7832 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -4122,6 +4122,9 @@ def RestrictDocs : Documentation {
 The ``malloc`` attribute indicates that the function acts like a system memory
 allocation function, returning a pointer to allocated storage disjoint from the
 storage for any other object accessible to the caller.
+
+The form of ``malloc`` with one or two arguments (supported by GCC 11) is
+currently ignored by Clang.
   }];
 }
 
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td 
b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index f2df283c74829f6..35472b92e460850 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -177,6 +177,10 @@ def warn_unknown_attribute_ignored : Warning<
   "unknown attribute %0 ignored">, InGroup;
 def warn_attribute_ignored : Warning<"%0 attribute ignored">,
   InGroup;
+def warn_multiarg_malloc_attribute_ignored: Warning<
+  "'malloc' attribute ignored because"
+  " Clang does not support the one/two argument form">,
+  InGroup;
 def err_keyword_not_supported_on_target : Error<
   "%0 is not supported on this target">;
 def err_use_of_tag_name_without_tag : Error<
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ed0b4d29b056397..9fe190b74db58fe 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2064,13 +2064,27 @@ static void handleTLSModelAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
 
 static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   QualType ResultType = getFunctionOrMethodResultType(D);
-  if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
+  if (!ResultType->isAnyPointerType() && !ResultType->isBlockPointerType()) {
+S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
+<< AL << getFunctionOrMethodResultSourceRange(D);
+return;
+  }
+
+  if (getNumAttributeArgs(AL) == 0) {
 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
 return;
   }
 
-  S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
-  << AL << getFuncti

[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits


@@ -177,6 +177,10 @@ def warn_unknown_attribute_ignored : Warning<
   "unknown attribute %0 ignored">, InGroup;
 def warn_attribute_ignored : Warning<"%0 attribute ignored">,
   InGroup;
+def warn_multiarg_malloc_attribute_ignored: Warning<
+  "'malloc' attribute ignored because"
+  " Clang does not support the one/two argument form">,
+  InGroup;

aloisklink wrote:

Fixed in 02a153d9be1007b9603bcd14bf3f80d4bf900806, although I've very slightly 
changed the wording to match other warnings by using `because` instead of the 
`;`.

I've also added a couple of other error diagnostics in other commit, so please 
let me know if you think it's worth generalizing them too.

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits


@@ -1629,6 +1629,8 @@ def IFunc : Attr, TargetSpecificAttr {
 
 def Restrict : InheritableAttr {
   let Spellings = [Declspec<"restrict">, GCC<"malloc">];
+  let Args = [IdentifierArgument<"Deallocator", /*opt*/ 1>,
+  ParamIdxArgument<"DeallocatorPtrArgIndex", /*opt*/ 1>];

aloisklink wrote:

:+1: Fixed in b7e125882fe895f0c4fb346a5ae1acddb70f6739

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits


@@ -289,6 +289,11 @@ Bug Fixes to Compiler Builtins
 Bug Fixes to Attribute Support
 ^^
 
+- Clang now emits a warning instead of an error when using the one or two
+  argument form of GCC 11's ``__attribute__((malloc(deallocator)))``
+  or ``__attribute__((malloc(deallocator, ptr-index)))``
+  (`#51607 `).

aloisklink wrote:

Sorry! That's my fault for being lazy and skipping build the docs! Fixed in 
5f6f893c867e73a2a8e04e94a2fa6e2104b9b85e

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits


@@ -1,12 +1,14 @@
-// RUN: %clang_cc1 -verify -Wunused -Wused-but-marked-unused 
-Wunused-parameter -fsyntax-only %s
+// RUN: %clang_cc1 -verify -Wunused -Wused-but-marked-unused 
-Wunused-parameter -fsyntax-only -fdeclspec %s
 int a;
 
 inline __attribute__((noreturn(a))) void *f1(void);  // expected-error 
{{'noreturn' attribute takes no arguments}}
 inline __attribute__((always_inline(a))) void *f2(void);  // expected-error 
{{'always_inline' attribute takes no arguments}}
 inline __attribute__((cdecl(a))) void *f3(void);  // expected-error {{'cdecl' 
attribute takes no arguments}}
 inline __attribute__((const(a))) void *f4(void);  // expected-error {{'const' 
attribute takes no arguments}}
 inline __attribute__((fastcall(a))) void *f5(void);  // expected-error 
{{'fastcall' attribute takes no arguments}}
-inline __attribute__((malloc(a))) void *f5(void);  // expected-error 
{{'malloc' attribute takes no arguments}}
+inline __declspec(restrict(a)) void *f6_a(void);  // expected-error 
{{'restrict' attribute takes no arguments}}
+inline __attribute__((malloc(a, 1, a))) void *f6_b(void);  // expected-error 
{{'malloc' attribute takes no more than 2 arguments}}
+inline __attribute__((malloc(a, 1))) void *f6_c(void);  // expected-warning 
{{'malloc' attribute ignored because Clang does not support the one/two 
argument form}}

aloisklink wrote:

Fixed in 0659620002757ab8bbd892a717fb47478d22aecd, although with a different 
error diagnostic. I've also added a bunch more error checking (adapting the 
code from 
[`handleCleanupAttr`](https://github.com/llvm/llvm-project/blob/0659620002757ab8bbd892a717fb47478d22aecd/clang/lib/Sema/SemaDeclAttr.cpp#L3793-L3818)),
 so now we should even be stricter than GCC on what args we accept:

https://github.com/llvm/llvm-project/blob/0659620002757ab8bbd892a717fb47478d22aecd/clang/test/Sema/attr-args.c#L2-L6

https://github.com/llvm/llvm-project/blob/0659620002757ab8bbd892a717fb47478d22aecd/clang/test/Sema/attr-args.c#L13-L21

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-11-01 Thread Alois Klink via cfe-commits

aloisklink wrote:

I've fixed reviewer comments! Sorry for the delay! I didn't have much time, and 
my PC isn't the fastest, so building Clang + regression tests takes a while!

As recommended by 
https://github.com/llvm/llvm-project/pull/68059#discussion_r1355449108, I added 
type checking for the attribute arguments so that we should throw an error on 
any invalid args that GCC throws an error (or even a warning on). Since this is 
a pretty substantial change, I made it all as `fixup!` commits to make the 
changes easier to review.

---

> gcc to have the same name for basically two different attributes

I agree, it's not ideal! Having a separate name for this attribute (e.g. 
`[[dealloc(func)]]` or `[[deallocator(func)]]` would be my preference. Although 
I think it might be worth asking the GCC team to implement it first. After all, 
this PR doesn't actually add support for the `[[malloc(deallocator)]]` 
attribute form in Clang, it just ignores it, instead of throwing an error.


https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-10-02 Thread Alois Klink via cfe-commits

https://github.com/aloisklink created 
https://github.com/llvm/llvm-project/pull/68059

Ignore the `[[malloc(x)]]` or `[[malloc(x, 1)]]` function attribute syntax 
added in [GCC 11][1].

Unlike `[[malloc]]` with no arguments (which is supported by Clang), GCC uses 
the one or two argument form to specify a deallocator for GCC's static analyzer.

Code currently compiled with `[[malloc(x)]]` or `__attribute__((malloc(x)))` 
fails with the following error: `'malloc' attribute takes no arguments`.

[1]: 
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;f=gcc/doc/extend.texi;h=dce6c58db87ebf7f4477bd3126228e73e497#patch6

Fixes: https://github.com/llvm/llvm-project/issues/51607
Partial-Bug: https://github.com/llvm/llvm-project/issues/53152 (this PR only 
ignores `__attribute__((malloc(x)))` and allows it to compile, so only 
partially fixes the problem).

---

In the future, we can add this attribute to the AST as well. This will let us 
improve the clang static analyzer's 
[`unix.MismatchedDeallocator`](https://clang.llvm.org/docs/analyzer/checkers.html#id93)
 checker.

>From f9c914729a5f5ac7f8b61ea2d39509ff0236a228 Mon Sep 17 00:00:00 2001
From: Alois Klink 
Date: Mon, 2 Oct 2023 19:59:06 +0100
Subject: [PATCH] [clang] Ignore GCC 11 [[malloc(x)]] attribute

Ignore the `[[malloc(x)]]` or `[[malloc(x, 1)]]` function attribute
syntax added in [GCC 11][1].

Unlike `[[malloc]]` with no arguments (which is supported by Clang),
GCC uses the one or two argument form to specify a deallocator for
GCC's static analyzer.

Code currently compiled with `[[malloc(x)]]` or
`__attribute((malloc(x)))` fails with the following error:
`'malloc' attribute takes no arguments`.

[1]: 
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;f=gcc/doc/extend.texi;h=dce6c58db87ebf7f4477bd3126228e73e497#patch6

Fixes: https://github.com/llvm/llvm-project/issues/51607
Partial-Bug: https://github.com/llvm/llvm-project/issues/53152
---
 clang/include/clang/Basic/Attr.td |  2 ++
 clang/include/clang/Basic/AttrDocs.td |  3 +++
 clang/lib/Sema/SemaDeclAttr.cpp   | 19 ---
 clang/test/Sema/attr-args.c   |  5 +++--
 clang/test/SemaCXX/attr-print.cpp |  8 
 5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 7a6ec77ae84b15a..db1f332efdb7653 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1629,6 +1629,8 @@ def IFunc : Attr, TargetSpecificAttr {
 
 def Restrict : InheritableAttr {
   let Spellings = [Declspec<"restrict">, GCC<"malloc">];
+  let Args = [IdentifierArgument<"Deallocator", /*opt*/ 1>,
+  ParamIdxArgument<"DeallocatorPtrArgIndex", /*opt*/ 1>];
   let Subjects = SubjectList<[Function]>;
   let Documentation = [RestrictDocs];
 }
diff --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index 8d928dcc146b254..1e498aeea6b7832 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -4122,6 +4122,9 @@ def RestrictDocs : Documentation {
 The ``malloc`` attribute indicates that the function acts like a system memory
 allocation function, returning a pointer to allocated storage disjoint from the
 storage for any other object accessible to the caller.
+
+The form of ``malloc`` with one or two arguments (supported by GCC 11) is
+currently ignored by Clang.
   }];
 }
 
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ed0b4d29b056397..fcc63cc27c1b537 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2064,13 +2064,26 @@ static void handleTLSModelAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
 
 static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   QualType ResultType = getFunctionOrMethodResultType(D);
-  if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
+  if (!ResultType->isAnyPointerType() && !ResultType->isBlockPointerType()) {
+S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
+<< AL << getFunctionOrMethodResultSourceRange(D);
+return;
+  }
+
+  if (getNumAttributeArgs(AL) == 0) {
 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
 return;
   }
 
-  S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
-  << AL << getFunctionOrMethodResultSourceRange(D);
+  if (AL.getAttributeSpellingListIndex() == RestrictAttr::Declspec_restrict) {
+// __declspec(restrict) accepts no arguments
+S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 0;
+return;
+  }
+
+  // FIXME: GCC uses [[malloc(my_func)]] to specify a deallocator for the
+  // returned pointer, but this isn't currently supported in LLVM
+  // see https://github.com/llvm/llvm-project/issues/51607
 }
 
 static void handleCPUSpecificAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
diff --git a/clang/test/Sema/attr-args.c b/clang/te

[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-10-03 Thread Alois Klink via cfe-commits


@@ -4122,6 +4122,9 @@ def RestrictDocs : Documentation {
 The ``malloc`` attribute indicates that the function acts like a system memory
 allocation function, returning a pointer to allocated storage disjoint from the
 storage for any other object accessible to the caller.
+
+The form of ``malloc`` with one or two arguments (supported by GCC 11) is
+currently ignored by Clang.

aloisklink wrote:

In this PR, it's completely ignored, it's not even added to the Clang AST.

It just reaches the end of this function where nothing happens (let me know if 
I can improve the comment!):

https://github.com/llvm/llvm-project/blob/f9c914729a5f5ac7f8b61ea2d39509ff0236a228/clang/lib/Sema/SemaDeclAttr.cpp#L2084-L2087

We can't treat it as if has no arguments because in GCC it means two different 
things:
  - `__attribute__((malloc))` with no args means the return value is guaranteed 
not to alias to any other pointer (aka like [`__declspec(restrict)` in 
MSVC](https://learn.microsoft.com/en-us/cpp/cpp/restrict?view=msvc-170)) and 
that the function will normally return non-`NULL`, allowing optimizations,
  - `__attribute__((malloc(deallocator)))` instead says the returned pointer 
should be deallocated by the specified deallocator, allowing a static analyzer 
to print warnings.

See the `malloc` section of 
https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html for more 
info.

To be honest, I feel like GCC should have given the one/two argument form a 
different attribute name to make things less confusing, but GCC 11 has already 
been out for years unfortunately.

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-10-07 Thread Alois Klink via cfe-commits

https://github.com/aloisklink updated 
https://github.com/llvm/llvm-project/pull/68059

>From a76561f522f628b0882572f8dabee6f7e4abd5f5 Mon Sep 17 00:00:00 2001
From: Alois Klink 
Date: Mon, 2 Oct 2023 19:59:06 +0100
Subject: [PATCH] [clang] Ignore GCC 11 [[malloc(x)]] attribute

Ignore the `[[malloc(x)]]` or `[[malloc(x, 1)]]` function attribute
syntax added in [GCC 11][1] and print a warning instead of an error.

Unlike `[[malloc]]` with no arguments (which is supported by Clang),
GCC uses the one or two argument form to specify a deallocator for
GCC's static analyzer.

Code currently compiled with `[[malloc(x)]]` or
`__attribute((malloc(x)))` fails with the following error:
`'malloc' attribute takes no arguments`.

[1]: 
https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;f=gcc/doc/extend.texi;h=dce6c58db87ebf7f4477bd3126228e73e497#patch6

Fixes: https://github.com/llvm/llvm-project/issues/51607
Partial-Bug: https://github.com/llvm/llvm-project/issues/53152
---
 clang/docs/ReleaseNotes.rst   |  5 +
 clang/include/clang/Basic/Attr.td |  2 ++
 clang/include/clang/Basic/AttrDocs.td |  3 +++
 .../clang/Basic/DiagnosticCommonKinds.td  |  4 
 clang/lib/Sema/SemaDeclAttr.cpp   | 20 ---
 clang/test/Sema/attr-args.c   |  6 --
 clang/test/SemaCXX/attr-print.cpp |  8 
 7 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6be824771c583be..3cbeed044ada396 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -289,6 +289,11 @@ Bug Fixes to Compiler Builtins
 Bug Fixes to Attribute Support
 ^^
 
+- Clang now emits a warning instead of an error when using the one or two
+  argument form of GCC 11's ``__attribute__((malloc(deallocator)))``
+  or ``__attribute__((malloc(deallocator, ptr-index)))``
+  (`#51607 `).
+
 Bug Fixes to C++ Support
 
 
diff --git a/clang/include/clang/Basic/Attr.td 
b/clang/include/clang/Basic/Attr.td
index 7a6ec77ae84b15a..db1f332efdb7653 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -1629,6 +1629,8 @@ def IFunc : Attr, TargetSpecificAttr {
 
 def Restrict : InheritableAttr {
   let Spellings = [Declspec<"restrict">, GCC<"malloc">];
+  let Args = [IdentifierArgument<"Deallocator", /*opt*/ 1>,
+  ParamIdxArgument<"DeallocatorPtrArgIndex", /*opt*/ 1>];
   let Subjects = SubjectList<[Function]>;
   let Documentation = [RestrictDocs];
 }
diff --git a/clang/include/clang/Basic/AttrDocs.td 
b/clang/include/clang/Basic/AttrDocs.td
index 8d928dcc146b254..1e498aeea6b7832 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -4122,6 +4122,9 @@ def RestrictDocs : Documentation {
 The ``malloc`` attribute indicates that the function acts like a system memory
 allocation function, returning a pointer to allocated storage disjoint from the
 storage for any other object accessible to the caller.
+
+The form of ``malloc`` with one or two arguments (supported by GCC 11) is
+currently ignored by Clang.
   }];
 }
 
diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td 
b/clang/include/clang/Basic/DiagnosticCommonKinds.td
index f2df283c74829f6..35472b92e460850 100644
--- a/clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -177,6 +177,10 @@ def warn_unknown_attribute_ignored : Warning<
   "unknown attribute %0 ignored">, InGroup;
 def warn_attribute_ignored : Warning<"%0 attribute ignored">,
   InGroup;
+def warn_multiarg_malloc_attribute_ignored: Warning<
+  "'malloc' attribute ignored because"
+  " Clang does not support the one/two argument form">,
+  InGroup;
 def err_keyword_not_supported_on_target : Error<
   "%0 is not supported on this target">;
 def err_use_of_tag_name_without_tag : Error<
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index ed0b4d29b056397..9fe190b74db58fe 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -2064,13 +2064,27 @@ static void handleTLSModelAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
 
 static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   QualType ResultType = getFunctionOrMethodResultType(D);
-  if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
+  if (!ResultType->isAnyPointerType() && !ResultType->isBlockPointerType()) {
+S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
+<< AL << getFunctionOrMethodResultSourceRange(D);
+return;
+  }
+
+  if (getNumAttributeArgs(AL) == 0) {
 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
 return;
   }
 
-  S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
-  << AL << getFunctionOr

[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-10-07 Thread Alois Klink via cfe-commits

aloisklink wrote:

> We generally want ignored attributes to be diagnosed as being ignored; 
> otherwise users have a much harder time determining whether the attribute is 
> working or not. I think we should issue an "attribute ignored" warning when 
> we're dropping the attribute in the AST.

Personally, I think there shouldn't be a warning, because even if we add the 
attribute to the AST, Clang will do nothing with it; the attribute would only 
be used by Clang's static analyzer. But I've added a warning anyway in 
https://github.com/llvm/llvm-project/commit/a76561f522f628b0882572f8dabee6f7e4abd5f5.

I think a generic `'malloc' attribute ignored` warning would have been pretty 
confusing, (especially because most of the time, people use both the 
no-argument form and the one/two argument form on the same function, e.g. 
`__attribute__((malloc, malloc(my_cleanup))) void *my_func();`) so instead I've 
made a custom warning that says: `'malloc' attribute ignored because Clang does 
not support the one/two argument form`.

> Also, the changes should come with a release note so that users know about 
> the bug fix.

:+1: That makes sense! I've added a note in the **Bug Fixes to Attribute 
Support** section in 
https://github.com/llvm/llvm-project/commit/a76561f522f628b0882572f8dabee6f7e4abd5f5.

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Ignore GCC 11 `[[malloc(x)]]` attribute (PR #68059)

2023-10-07 Thread Alois Klink via cfe-commits


@@ -2064,13 +2064,26 @@ static void handleTLSModelAttr(Sema &S, Decl *D, const 
ParsedAttr &AL) {
 
 static void handleRestrictAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
   QualType ResultType = getFunctionOrMethodResultType(D);
-  if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) {
+  if (!ResultType->isAnyPointerType() && !ResultType->isBlockPointerType()) {
+S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
+<< AL << getFunctionOrMethodResultSourceRange(D);
+return;
+  }
+
+  if (getNumAttributeArgs(AL) == 0) {
 D->addAttr(::new (S.Context) RestrictAttr(S.Context, AL));
 return;
   }
 
-  S.Diag(AL.getLoc(), diag::warn_attribute_return_pointers_only)
-  << AL << getFunctionOrMethodResultSourceRange(D);
+  if (AL.getAttributeSpellingListIndex() == RestrictAttr::Declspec_restrict) {
+// __declspec(restrict) accepts no arguments
+S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << AL << 0;
+return;
+  }
+
+  // FIXME: GCC uses [[malloc(my_func)]] to specify a deallocator for the

aloisklink wrote:

I slightly disagree, since even if we do add support for this attribute, only 
the Clang analyzer would use it, but I've added a `'malloc' attribute ignored 
because Clang does not support the one/two argument form` warning in commit 
https://github.com/llvm/llvm-project/commit/a76561f522f628b0882572f8dabee6f7e4abd5f5.

https://github.com/llvm/llvm-project/pull/68059
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits