Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
Line
Count
Source (jump to first uncovered line)
1
//== CheckerContext.cpp - Context info for path-sensitive checkers-----------=//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines CheckerContext that provides contextual info for
10
//  path-sensitive checkers.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
15
#include "clang/Basic/Builtins.h"
16
#include "clang/Lex/Lexer.h"
17
18
using namespace clang;
19
using namespace ento;
20
21
62.6k
const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
22
62.6k
  const Expr *Callee = CE->getCallee();
23
62.6k
  SVal L = Pred->getSVal(Callee);
24
62.6k
  return L.getAsFunctionDecl();
25
62.6k
}
26
27
29.5k
StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
28
29.5k
  if (!FunDecl)
29
70
    return StringRef();
30
29.4k
  IdentifierInfo *funI = FunDecl->getIdentifier();
31
29.4k
  if (!funI)
32
1.97k
    return StringRef();
33
27.5k
  return funI->getName();
34
27.5k
}
35
36
32
StringRef CheckerContext::getDeclDescription(const Decl *D) {
37
32
  if (isa<ObjCMethodDecl>(D) || 
isa<CXXMethodDecl>(D)26
)
38
6
    return "method";
39
26
  if (isa<BlockDecl>(D))
40
0
    return "anonymous block";
41
26
  return "function";
42
26
}
43
44
bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
45
383k
                                        StringRef Name) {
46
383k
  // To avoid false positives (Ex: finding user defined functions with
47
383k
  // similar names), only perform fuzzy name matching when it's a builtin.
48
383k
  // Using a string compare is slow, we might want to switch on BuiltinID here.
49
383k
  unsigned BId = FD->getBuiltinID();
50
383k
  if (BId != 0) {
51
46.7k
    if (Name.empty())
52
2.81k
      return true;
53
43.8k
    StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
54
43.8k
    if (BName.find(Name) != StringRef::npos)
55
2.26k
      return true;
56
378k
  }
57
378k
58
378k
  const IdentifierInfo *II = FD->getIdentifier();
59
378k
  // If this is a special C++ name without IdentifierInfo, it can't be a
60
378k
  // C library function.
61
378k
  if (!II)
62
25.3k
    return false;
63
352k
64
352k
  // Look through 'extern "C"' and anything similar invented in the future.
65
352k
  // If this function is not in TU directly, it is not a C library function.
66
352k
  if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
67
49.8k
    return false;
68
303k
69
303k
  // If this function is not externally visible, it is not a C library function.
70
303k
  // Note that we make an exception for inline functions, which may be
71
303k
  // declared in header files without external linkage.
72
303k
  if (!FD->isInlined() && 
!FD->isExternallyVisible()263k
)
73
7.83k
    return false;
74
295k
75
295k
  if (Name.empty())
76
37.2k
    return true;
77
258k
78
258k
  StringRef FName = II->getName();
79
258k
  if (FName.equals(Name))
80
1.37k
    return true;
81
256k
82
256k
  if (FName.startswith("__inline") && 
(FName.find(Name) != StringRef::npos)7
)
83
1
    return true;
84
256k
85
256k
  if (FName.startswith("__") && 
FName.endswith("_chk")13.8k
&&
86
256k
      
FName.find(Name) != StringRef::npos2.09k
)
87
122
    return true;
88
256k
89
256k
  return false;
90
256k
}
91
92
12
StringRef CheckerContext::getMacroNameOrSpelling(SourceLocation &Loc) {
93
12
  if (Loc.isMacroID())
94
12
    return Lexer::getImmediateMacroName(Loc, getSourceManager(),
95
12
                                             getLangOpts());
96
0
  SmallVector<char, 16> buf;
97
0
  return Lexer::getSpelling(Loc, buf, getSourceManager(), getLangOpts());
98
0
}
99
100
/// Evaluate comparison and return true if it's known that condition is true
101
static bool evalComparison(SVal LHSVal, BinaryOperatorKind ComparisonOp,
102
287
                           SVal RHSVal, ProgramStateRef State) {
103
287
  if (LHSVal.isUnknownOrUndef())
104
0
    return false;
105
287
  ProgramStateManager &Mgr = State->getStateManager();
106
287
  if (!LHSVal.getAs<NonLoc>()) {
107
5
    LHSVal = Mgr.getStoreManager().getBinding(State->getStore(),
108
5
                                              LHSVal.castAs<Loc>());
109
5
    if (LHSVal.isUnknownOrUndef() || !LHSVal.getAs<NonLoc>())
110
0
      return false;
111
287
  }
112
287
113
287
  SValBuilder &Bldr = Mgr.getSValBuilder();
114
287
  SVal Eval = Bldr.evalBinOp(State, ComparisonOp, LHSVal, RHSVal,
115
287
                             Bldr.getConditionType());
116
287
  if (Eval.isUnknownOrUndef())
117
0
    return false;
118
287
  ProgramStateRef StTrue, StFalse;
119
287
  std::tie(StTrue, StFalse) = State->assume(Eval.castAs<DefinedSVal>());
120
287
  return StTrue && 
!StFalse260
;
121
287
}
122
123
251
bool CheckerContext::isGreaterOrEqual(const Expr *E, unsigned long long Val) {
124
251
  DefinedSVal V = getSValBuilder().makeIntVal(Val, getASTContext().LongLongTy);
125
251
  return evalComparison(getSVal(E), BO_GE, V, getState());
126
251
}
127
128
36
bool CheckerContext::isNegative(const Expr *E) {
129
36
  DefinedSVal V = getSValBuilder().makeIntVal(0, false);
130
36
  return evalComparison(getSVal(E), BO_LT, V, getState());
131
36
}