[PATCH] D27424: Add the diagnose_if attribute to clang.

George Burgess IV via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 5 12:12:11 PST 2016


george.burgess.iv created this revision.
george.burgess.iv added reviewers: rsmith, aaron.ballman.
george.burgess.iv added subscribers: EricWF, cfe-commits.

This patch adds the `diagnose_if` attribute to clang.

`diagnose_if` is meant to be a less powerful version of `enable_if`: it doesn't interact with overload resolution at all, and if the condition in the `diagnose_if` attribute can't be evaluated, compilation proceeds as though said `diagnose_if` attribute was not present. It can be used to emit either errors or warnings, like so:

  void foo(int a) __attribute__((diagnose_if(a > 10, "a is too large!", "error")));
  void bar(int a) __attribute__((diagnose_if(a > 10, "this takes a long time if a is larger than 10.", "warning")));
  void baz(int a) __attribute__((diagnose_if(1, "don't use me anymore", "warning")));
  
  void run(int a) {
    foo(10);
    foo(11); // error: a is too large
    foo(a);
    bar(10);
    bar(11); // warning: this takes a long time if a is larger than 10.
    bar(a);
  
    void (*b)(int) = baz; // warning: don't use me anymore.
  }

Under the hood, we have two kinds of `diagnose_if` attributes: ones that are dependent on the value of arguments (which are handled a lot like `enable_if`), and ones that aren't (which are treated like `unavailable`/`deprecated` attributes). This split made this attribute *substantially* less complex to implement, since it lets us reuse existing code


https://reviews.llvm.org/D27424

Files:
  include/clang/Basic/Attr.td
  include/clang/Basic/AttrDocs.td
  include/clang/Basic/DiagnosticCommonKinds.td
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticSemaKinds.td
  include/clang/Sema/Overload.h
  include/clang/Sema/Sema.h
  lib/Parse/ParseDecl.cpp
  lib/Sema/SemaDeclAttr.cpp
  lib/Sema/SemaExpr.cpp
  lib/Sema/SemaOverload.cpp
  lib/Sema/SemaTemplateInstantiateDecl.cpp
  test/Sema/diagnose_if.c
  test/SemaCXX/diagnose_if.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27424.80307.patch
Type: text/x-patch
Size: 61608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20161205/10f1e2b3/attachment-0001.bin>


More information about the cfe-commits mailing list