[Sema] Add some compile time _FORTIFY_SOURCE diagnostics
authorErik Pilkington <erik.pilkington@gmail.com>
Mon, 18 Mar 2019 19:23:45 +0000 (19:23 +0000)
committerErik Pilkington <erik.pilkington@gmail.com>
Mon, 18 Mar 2019 19:23:45 +0000 (19:23 +0000)
commitb6e16ea006a262c77ed44afc72b94a9b7c9cf440
treea632d8849ea69fef6f7564e76ae57b4647512897
parent8627178d4680af6a49459659a71f690dfefc2783
[Sema] Add some compile time _FORTIFY_SOURCE diagnostics

These diagnose overflowing calls to subset of fortifiable functions. Some
functions, like sprintf or strcpy aren't supported right not, but we should
probably support these in the future. We previously supported this kind of
functionality with -Wbuiltin-memcpy-chk-size, but that diagnostic doesn't work
with _FORTIFY implementations that use wrapper functions. Also unlike that
diagnostic, we emit these warnings regardless of whether _FORTIFY_SOURCE is
actually enabled, which is nice for programs that don't enable the runtime
checks.

Why not just use diagnose_if, like Bionic does? We can get better diagnostics in
the compiler (i.e. mention the sizes), and we have the potential to diagnose
sprintf and strcpy which is impossible with diagnose_if (at least, in languages
that don't support C++14 constexpr). This approach also saves standard libraries
from having to add diagnose_if.

rdar://48006655

Differential revision: https://reviews.llvm.org/D58797

llvm-svn: 356397
16 files changed:
clang/include/clang/AST/Decl.h
clang/include/clang/Basic/DiagnosticGroups.td
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Sema.h
clang/lib/AST/Decl.cpp
clang/lib/Sema/SemaChecking.cpp
clang/lib/Sema/SemaExpr.cpp
clang/test/Analysis/bstring.c
clang/test/Analysis/null-deref-ps-region.c
clang/test/Analysis/pr22954.c
clang/test/Analysis/string.c
clang/test/Sema/builtin-object-size.c
clang/test/Sema/builtins.c
clang/test/Sema/transpose-memset.c
clang/test/Sema/warn-fortify-source.c [new file with mode: 0644]
clang/test/Sema/warn-strncat-size.c