[PATCH] D32332: Add support for transparent overloadable functions in clang

George Burgess IV via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 20 18:56:36 PDT 2017


george.burgess.iv created this revision.

One of the common use-cases for the `overloadable` attribute is to create small
wrapper functions around an existing function that was previously not
overloadable. For example:

  // C Library v1
  void foo(int i);
  
  
  // C Library v2
  // Note the __asm__("foo"); it ensures that foo isn't mangled. Our docs state
  // that the mangling isn't stable, so we encourage users to either use __asm__
  // to force a stable name, or to only use overloads with internal linkage
  __attribute__((overloadable))
  int foo(int i) __asm__("foo");
  
  __attribute__((overloadable))
  static inline int foo(long l) {
    assert(l <= INT_MAX && l >= INT_MIN && "Out of bounds number!");
    return foo(l);
  }

"Library v2" is suboptimal for a few reasons:

- It can break existing code; if users added `int foo(int);` declarations to their code, upgrading to library v2 requires that the users either remove these declarations or add `__attribute__((overloadable))` to them.
- The `__asm__` name is both redundant and easy to typo (or forget to update, or ...).
- The `__asm__` name also makes it quite a bit more difficult to hide all of this behind a macro, since we need a different name for each function.

This patch aims to fix these usability challenges with the idea of transparent
function overloads.

For any `overloadable` function in C, one of the overloads is allowed to be
transparent. Transparency means that the name will not be mangled, and later
redeclarations of that particular overload don't require
`__attribute__((overloadable))`.

Users can make an overload transparent by using the `transparently_overloadable`
spelling of `overloadable`.

This also includes a bugfix for how we handled `overloadable` functions declared
inside functions. I can factor that out if that would make life easier. :)


https://reviews.llvm.org/D32332

Files:
  include/clang/Basic/Attr.td
  include/clang/Basic/AttrDocs.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Sema.h
  lib/AST/ItaniumMangle.cpp
  lib/AST/MicrosoftMangle.cpp
  lib/Sema/SemaDecl.cpp
  test/CodeGen/mangle-ms.c
  test/CodeGen/mangle.c
  test/CodeGenCXX/mangle-ms.cpp
  test/Sema/overloadable.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32332.96074.patch
Type: text/x-patch
Size: 28537 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170421/54e1b592/attachment-0001.bin>


More information about the cfe-commits mailing list