[analyzer] When failing to evaluate a __builtin_constant_p, presume it's false.
authorArtem Dergachev <artem.dergachev@gmail.com>
Wed, 3 Apr 2019 01:53:40 +0000 (01:53 +0000)
committerArtem Dergachev <artem.dergachev@gmail.com>
Wed, 3 Apr 2019 01:53:40 +0000 (01:53 +0000)
commitf7887d41cbd73cad2e92eea60c710e4f263cdf18
tree643c54ab95c14152bcd9a8a234579cc31f132ee3
parent30d9af1b247ee3c0a3ea66e1f14a21eb1268b169
[analyzer] When failing to evaluate a __builtin_constant_p, presume it's false.

__builtin_constant_p(x) is a compiler builtin that evaluates to 1 when
its argument x is a compile-time constant and to 0 otherwise. In CodeGen
it is simply lowered to the respective LLVM intrinsic. In the Analyzer
we've been trying to delegate modeling to Expr::EvaluateAsInt, which is
allowed to sometimes fail for no apparent reason.

When it fails, let's conservatively return false. Modeling it as false
is pretty much never wrong, and it is only required to return true
on a best-effort basis, which every user should expect.

Fixes VLAChecker false positives on code that tries to emulate
static asserts in C by constructing a VLA of dynamic size -1 under the
assumption that this dynamic size is actually a constant
in the sense of __builtin_constant_p.

Differential Revision: https://reviews.llvm.org/D60110

llvm-svn: 357557
clang/lib/StaticAnalyzer/Checkers/BuiltinFunctionChecker.cpp
clang/test/Analysis/builtin-functions.cpp