[Clang] add support for error+warning fn attrs
authorNick Desaulniers <ndesaulniers@google.com>
Wed, 25 Aug 2021 17:18:13 +0000 (10:18 -0700)
committerNick Desaulniers <ndesaulniers@google.com>
Wed, 25 Aug 2021 17:34:18 +0000 (10:34 -0700)
commit846e562dcc6a9a611d844dc0d123da95629a0567
tree54c30b2732f89f5128fd5b3884e4966206523a35
parentcc4bfd7f59d5a0024ada2a5c2a6f46d53290882b
[Clang] add support for error+warning fn attrs

Add support for the GNU C style __attribute__((error(""))) and
__attribute__((warning(""))). These attributes are meant to be put on
declarations of functions whom should not be called.

They are frequently used to provide compile time diagnostics similar to
_Static_assert, but which may rely on non-ICE conditions (ie. relying on
compiler optimizations). This is also similar to diagnose_if function
attribute, but can diagnose after optimizations have been run.

While users may instead simply call undefined functions in such cases to
get a linkage failure from the linker, these provide a much more
ergonomic and actionable diagnostic to users and do so at compile time
rather than at link time. Users instead may be able use inline asm .err
directives.

These are used throughout the Linux kernel in its implementation of
BUILD_BUG and BUILD_BUG_ON macros. These macros generally cannot be
converted to use _Static_assert because many of the parameters are not
ICEs. The Linux kernel still needs to be modified to make use of these
when building with Clang; I have a patch that does so I will send once
this feature is landed.

To do so, we create a new IR level Function attribute, "dontcall" (both
error and warning boil down to one IR Fn Attr).  Then, similar to calls
to inline asm, we attach a !srcloc Metadata node to call sites of such
attributed callees.

The backend diagnoses these during instruction selection, while we still
know that a call is a call (vs say a JMP that's a tail call) in an arch
agnostic manner.

The frontend then reconstructs the SourceLocation from that Metadata,
and determines whether to emit an error or warning based on the callee's
attribute.

Link: https://bugs.llvm.org/show_bug.cgi?id=16428
Link: https://github.com/ClangBuiltLinux/linux/issues/1173
Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D106030
26 files changed:
clang/docs/ReleaseNotes.rst
clang/include/clang/Basic/Attr.td
clang/include/clang/Basic/AttrDocs.td
clang/include/clang/Basic/DiagnosticFrontendKinds.td
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Sema/Sema.h
clang/lib/CodeGen/CGCall.cpp
clang/lib/CodeGen/CodeGenAction.cpp
clang/lib/CodeGen/CodeGenModule.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclAttr.cpp
clang/test/CodeGen/attr-error.c [new file with mode: 0644]
clang/test/CodeGen/attr-warning.c [new file with mode: 0644]
clang/test/Frontend/backend-attribute-error-warning-optimize.c [new file with mode: 0644]
clang/test/Frontend/backend-attribute-error-warning.c [new file with mode: 0644]
clang/test/Misc/pragma-attribute-supported-attributes-list.test
clang/test/Sema/attr-error.c [new file with mode: 0644]
clang/test/Sema/attr-warning.c [new file with mode: 0644]
llvm/docs/LangRef.rst
llvm/include/llvm/IR/DiagnosticInfo.h
llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
llvm/lib/CodeGen/SelectionDAG/FastISel.cpp
llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/lib/IR/DiagnosticInfo.cpp
llvm/test/CodeGen/X86/attr-dontcall.ll [new file with mode: 0644]
llvm/test/ThinLTO/X86/dontcall.ll [new file with mode: 0644]