1 //=== BuiltinFunctionChecker.cpp --------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This checker evaluates clang builtin functions.
12 //===----------------------------------------------------------------------===//
14 #include "ClangSACheckers.h"
15 #include "clang/StaticAnalyzer/Core/Checker.h"
16 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
17 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
18 #include "clang/Basic/Builtins.h"
20 using namespace clang;
25 class BuiltinFunctionChecker : public Checker<eval::Call> {
27 bool evalCall(const CallExpr *CE, CheckerContext &C) const;
32 bool BuiltinFunctionChecker::evalCall(const CallExpr *CE,
33 CheckerContext &C) const{
34 const GRState *state = C.getState();
35 const Expr *Callee = CE->getCallee();
36 SVal L = state->getSVal(Callee);
37 const FunctionDecl *FD = L.getAsFunctionDecl();
42 unsigned id = FD->getBuiltinID();
48 case Builtin::BI__builtin_expect: {
49 // For __builtin_expect, just return the value of the subexpression.
50 assert (CE->arg_begin() != CE->arg_end());
51 SVal X = state->getSVal(*(CE->arg_begin()));
52 C.generateNode(state->BindExpr(CE, X));
56 case Builtin::BI__builtin_alloca: {
57 // FIXME: Refactor into StoreManager itself?
58 MemRegionManager& RM = C.getStoreManager().getRegionManager();
59 const AllocaRegion* R =
60 RM.getAllocaRegion(CE, C.getNodeBuilder().getCurrentBlockCount(),
61 C.getPredecessor()->getLocationContext());
63 // Set the extent of the region in bytes. This enables us to use the
64 // SVal of the argument directly. If we save the extent in bits, we
65 // cannot represent values like symbol*8.
66 DefinedOrUnknownSVal Size =
67 cast<DefinedOrUnknownSVal>(state->getSVal(*(CE->arg_begin())));
69 SValBuilder& svalBuilder = C.getSValBuilder();
70 DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder);
71 DefinedOrUnknownSVal extentMatchesSizeArg =
72 svalBuilder.evalEQ(state, Extent, Size);
73 state = state->assume(extentMatchesSizeArg, true);
75 C.generateNode(state->BindExpr(CE, loc::MemRegionVal(R)));
83 void ento::registerBuiltinFunctionChecker(CheckerManager &mgr) {
84 mgr.registerChecker<BuiltinFunctionChecker>();