clang  8.0.0svn
CheckerHelpers.cpp
Go to the documentation of this file.
1 //===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines several static functions for use in checkers.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "clang/AST/Decl.h"
16 #include "clang/AST/Expr.h"
17 
18 namespace clang {
19 
20 namespace ento {
21 
22 // Recursively find any substatements containing macros
23 bool containsMacro(const Stmt *S) {
24  if (S->getBeginLoc().isMacroID())
25  return true;
26 
27  if (S->getEndLoc().isMacroID())
28  return true;
29 
30  for (const Stmt *Child : S->children())
31  if (Child && containsMacro(Child))
32  return true;
33 
34  return false;
35 }
36 
37 // Recursively find any substatements containing enum constants
38 bool containsEnum(const Stmt *S) {
39  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
40 
41  if (DR && isa<EnumConstantDecl>(DR->getDecl()))
42  return true;
43 
44  for (const Stmt *Child : S->children())
45  if (Child && containsEnum(Child))
46  return true;
47 
48  return false;
49 }
50 
51 // Recursively find any substatements containing static vars
52 bool containsStaticLocal(const Stmt *S) {
53  const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S);
54 
55  if (DR)
56  if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
57  if (VD->isStaticLocal())
58  return true;
59 
60  for (const Stmt *Child : S->children())
61  if (Child && containsStaticLocal(Child))
62  return true;
63 
64  return false;
65 }
66 
67 // Recursively find any substatements containing __builtin_offsetof
68 bool containsBuiltinOffsetOf(const Stmt *S) {
69  if (isa<OffsetOfExpr>(S))
70  return true;
71 
72  for (const Stmt *Child : S->children())
73  if (Child && containsBuiltinOffsetOf(Child))
74  return true;
75 
76  return false;
77 }
78 
79 // Extract lhs and rhs from assignment statement
80 std::pair<const clang::VarDecl *, const clang::Expr *>
81 parseAssignment(const Stmt *S) {
82  const VarDecl *VD = nullptr;
83  const Expr *RHS = nullptr;
84 
85  if (auto Assign = dyn_cast_or_null<BinaryOperator>(S)) {
86  if (Assign->isAssignmentOp()) {
87  // Ordinary assignment
88  RHS = Assign->getRHS();
89  if (auto DE = dyn_cast_or_null<DeclRefExpr>(Assign->getLHS()))
90  VD = dyn_cast_or_null<VarDecl>(DE->getDecl());
91  }
92  } else if (auto PD = dyn_cast_or_null<DeclStmt>(S)) {
93  // Initialization
94  assert(PD->isSingleDecl() && "We process decls one by one");
95  VD = dyn_cast_or_null<VarDecl>(PD->getSingleDecl());
96  RHS = VD->getAnyInitializer();
97  }
98 
99  return std::make_pair(VD, RHS);
100 }
101 
103  const auto *AttrType = Type->getAs<AttributedType>();
104  if (!AttrType)
106  if (AttrType->getAttrKind() == attr::TypeNullable)
107  return Nullability::Nullable;
108  else if (AttrType->getAttrKind() == attr::TypeNonNull)
109  return Nullability::Nonnull;
111 }
112 
113 
114 } // end namespace ento
115 } // end namespace clang
A (possibly-)qualified type.
Definition: Type.h:642
bool containsStaticLocal(const Stmt *S)
Stmt - This represents one statement.
Definition: Stmt.h:66
The base class of the type hierarchy.
Definition: Type.h:1415
Represents a variable declaration or definition.
Definition: Decl.h:812
const T * getAs() const
Member-template getAs<specific type>&#39;.
Definition: Type.h:6590
bool containsBuiltinOffsetOf(const Stmt *S)
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Stmt.cpp:280
child_range children()
Definition: Stmt.cpp:229
std::pair< const clang::VarDecl *, const clang::Expr * > parseAssignment(const Stmt *S)
This represents one expression.
Definition: Expr.h:105
ValueDecl * getDecl()
Definition: Expr.h:1056
SourceLocation getEndLoc() const LLVM_READONLY
Definition: Stmt.cpp:293
Dataflow Directional Tag Classes.
Nullability getNullabilityAnnotation(QualType Type)
Get nullability annotation for a given type.
An attributed type is a type to which a type attribute has been applied.
Definition: Type.h:4308
bool containsMacro(const Stmt *S)
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:971
bool containsEnum(const Stmt *S)