clang  14.0.0git
CheckerHelpers.cpp
Go to the documentation of this file.
1 //===---- CheckerHelpers.cpp - Helper functions for checkers ----*- C++ -*-===//
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 several static functions for use in checkers.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/Expr.h"
16 #include "clang/Lex/Preprocessor.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 = cast<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 
114  const Preprocessor &PP) {
115  const auto *MacroII = PP.getIdentifierInfo(Macro);
116  if (!MacroII)
117  return llvm::None;
118  const MacroInfo *MI = PP.getMacroInfo(MacroII);
119  if (!MI)
120  return llvm::None;
121 
122  // Filter out parens.
123  std::vector<Token> FilteredTokens;
124  FilteredTokens.reserve(MI->tokens().size());
125  for (auto &T : MI->tokens())
126  if (!T.isOneOf(tok::l_paren, tok::r_paren))
127  FilteredTokens.push_back(T);
128 
129  // Parse an integer at the end of the macro definition.
130  const Token &T = FilteredTokens.back();
131  // FIXME: EOF macro token coming from a PCH file on macOS while marked as
132  // literal, doesn't contain any literal data
133  if (!T.isLiteral() || !T.getLiteralData())
134  return llvm::None;
135  StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength());
136  llvm::APInt IntValue;
137  constexpr unsigned AutoSenseRadix = 0;
138  if (ValueStr.getAsInteger(AutoSenseRadix, IntValue))
139  return llvm::None;
140 
141  // Parse an optional minus sign.
142  size_t Size = FilteredTokens.size();
143  if (Size >= 2) {
144  if (FilteredTokens[Size - 2].is(tok::minus))
145  IntValue = -IntValue;
146  }
147 
148  return IntValue.getSExtValue();
149 }
150 
152  bool IsBinary) {
153  llvm::StringMap<BinaryOperatorKind> BinOps{
154 #define BINARY_OPERATION(Name, Spelling) {Spelling, BO_##Name},
155 #include "clang/AST/OperationKinds.def"
156  };
157  llvm::StringMap<UnaryOperatorKind> UnOps{
158 #define UNARY_OPERATION(Name, Spelling) {Spelling, UO_##Name},
159 #include "clang/AST/OperationKinds.def"
160  };
161 
162  switch (OOK) {
163 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
164  case OO_##Name: \
165  if (IsBinary) { \
166  auto BinOpIt = BinOps.find(Spelling); \
167  if (BinOpIt != BinOps.end()) \
168  return OperatorKind(BinOpIt->second); \
169  else \
170  llvm_unreachable("operator was expected to be binary but is not"); \
171  } else { \
172  auto UnOpIt = UnOps.find(Spelling); \
173  if (UnOpIt != UnOps.end()) \
174  return OperatorKind(UnOpIt->second); \
175  else \
176  llvm_unreachable("operator was expected to be unary but is not"); \
177  } \
178  break;
179 #include "clang/Basic/OperatorKinds.def"
180  default:
181  llvm_unreachable("unexpected operator kind");
182  }
183 }
184 
185 } // namespace ento
186 } // namespace clang
clang::interp::APInt
llvm::APInt APInt
Definition: Integral.h:27
clang::AttributedType
An attributed type is a type to which a type attribute has been applied.
Definition: Type.h:4668
clang::ento::Nullability
Nullability
Definition: CheckerHelpers.h:56
clang::Token::getLiteralData
const char * getLiteralData() const
getLiteralData - For a literal token (numeric constant, string, etc), this returns a pointer to the s...
Definition: Token.h:217
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:673
clang::MacroInfo::tokens
ArrayRef< Token > tokens() const
Definition: MacroInfo.h:245
llvm::Optional
Definition: LLVM.h:40
clang::ento::containsStaticLocal
bool containsStaticLocal(const Stmt *S)
Definition: CheckerHelpers.cpp:52
clang::MacroInfo
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
clang::ento::containsBuiltinOffsetOf
bool containsBuiltinOffsetOf(const Stmt *S)
Definition: CheckerHelpers.cpp:68
clang::Token
Token - This structure provides full information about a lexed token.
Definition: Token.h:34
clang::ento::tryExpandAsInteger
llvm::Optional< int > tryExpandAsInteger(StringRef Macro, const Preprocessor &PP)
Try to parse the value of a defined preprocessor macro.
Definition: CheckerHelpers.cpp:113
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
Preprocessor.h
Decl.h
clang::ento::parseAssignment
std::pair< const clang::VarDecl *, const clang::Expr * > parseAssignment(const Stmt *S)
Definition: CheckerHelpers.cpp:81
clang::ento::Nullability::Nonnull
@ Nonnull
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7161
clang::ento::Nullability::Nullable
@ Nullable
clang::DeclRefExpr::getDecl
ValueDecl * getDecl()
Definition: Expr.h:1289
Expr.h
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::Preprocessor::getMacroInfo
const MacroInfo * getMacroInfo(const IdentifierInfo *II) const
Definition: Preprocessor.h:1153
clang::OverloadedOperatorKind
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
clang::ento::operationKindFromOverloadedOperator
OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK, bool IsBinary)
Definition: CheckerHelpers.cpp:151
clang::Token::isLiteral
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition: Token.h:115
clang::Token::getLength
unsigned getLength() const
Definition: Token.h:129
clang::VarDecl::getAnyInitializer
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition: Decl.h:1275
clang::ento::getNullabilityAnnotation
Nullability getNullabilityAnnotation(QualType Type)
Get nullability annotation for a given type.
Definition: CheckerHelpers.cpp:102
clang
Definition: CalledOnceCheck.h:17
clang::ento::containsEnum
bool containsEnum(const Stmt *S)
Definition: CheckerHelpers.cpp:38
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
CheckerHelpers.h
clang::ento::Nullability::Unspecified
@ Unspecified
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:130
clang::ento::containsMacro
bool containsMacro(const Stmt *S)
Definition: CheckerHelpers.cpp:23
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1217
clang::Preprocessor::getIdentifierInfo
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
Definition: Preprocessor.h:1243
clang::ento::OperatorKind
Definition: CheckerHelpers.h:74