Coverage Report

Created: 2020-02-15 09:57

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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
77.1k
const FunctionDecl *CheckerContext::getCalleeDecl(const CallExpr *CE) const {
22
77.1k
  const Expr *Callee = CE->getCallee();
23
77.1k
  SVal L = Pred->getSVal(Callee);
24
77.1k
  return L.getAsFunctionDecl();
25
77.1k
}
26
27
38.5k
StringRef CheckerContext::getCalleeName(const FunctionDecl *FunDecl) const {
28
38.5k
  if (!FunDecl)
29
70
    return StringRef();
30
38.4k
  IdentifierInfo *funI = FunDecl->getIdentifier();
31
38.4k
  if (!funI)
32
2.45k
    return StringRef();
33
36.0k
  return funI->getName();
34
36.0k
}
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
536k
                                        StringRef Name) {
46
536k
  // To avoid false positives (Ex: finding user defined functions with
47
536k
  // similar names), only perform fuzzy name matching when it's a builtin.
48
536k
  // Using a string compare is slow, we might want to switch on BuiltinID here.
49
536k
  unsigned BId = FD->getBuiltinID();
50
536k
  if (BId != 0) {
51
48.1k
    if (Name.empty())
52
3.01k
      return true;
53
45.1k
    StringRef BName = FD->getASTContext().BuiltinInfo.getName(BId);
54
45.1k
    if (BName.find(Name) != StringRef::npos)
55
2.32k
      return true;
56
531k
  }
57
531k
58
531k
  const IdentifierInfo *II = FD->getIdentifier();
59
531k
  // If this is a special C++ name without IdentifierInfo, it can't be a
60
531k
  // C library function.
61
531k
  if (!II)
62
52.1k
    return false;
63
479k
64
479k
  // Look through 'extern "C"' and anything similar invented in the future.
65
479k
  // If this function is not in TU directly, it is not a C library function.
66
479k
  if (!FD->getDeclContext()->getRedeclContext()->isTranslationUnit())
67
97.6k
    return false;
68
381k
69
381k
  // If this function is not externally visible, it is not a C library function.
70
381k
  // Note that we make an exception for inline functions, which may be
71
381k
  // declared in header files without external linkage.
72
381k
  if (!FD->isInlined() && 
!FD->isExternallyVisible()342k
)
73
7.85k
    return false;
74
373k
75
373k
  if (Name.empty())
76
45.1k
    return true;
77
328k
78
328k
  StringRef FName = II->getName();
79
328k
  if (FName.equals(Name))
80
1.51k
    return true;
81
327k
82
327k
  if (FName.startswith("__inline") && 
(FName.find(Name) != StringRef::npos)7
)
83
1
    return true;
84
327k
85
327k
  if (FName.startswith("__") && 
FName.endswith("_chk")14.4k
&&
86
327k
      
FName.find(Name) != StringRef::npos2.09k
)
87
122
    return true;
88
327k
89
327k
  return false;
90
327k
}
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
}