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 13 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h" 14 #include "clang/AST/Decl.h" 15 #include "clang/AST/Expr.h" 16 #include "clang/Lex/Preprocessor.h" 17 #include <optional> 18 19 namespace clang { 20 21 namespace ento { 22 23 // Recursively find any substatements containing macros 24 bool containsMacro(const Stmt *S) { 25 if (S->getBeginLoc().isMacroID()) 26 return true; 27 28 if (S->getEndLoc().isMacroID()) 29 return true; 30 31 for (const Stmt *Child : S->children()) 32 if (Child && containsMacro(Child)) 33 return true; 34 35 return false; 36 } 37 38 // Recursively find any substatements containing enum constants 39 bool containsEnum(const Stmt *S) { 40 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); 41 42 if (DR && isa<EnumConstantDecl>(DR->getDecl())) 43 return true; 44 45 for (const Stmt *Child : S->children()) 46 if (Child && containsEnum(Child)) 47 return true; 48 49 return false; 50 } 51 52 // Recursively find any substatements containing static vars 53 bool containsStaticLocal(const Stmt *S) { 54 const DeclRefExpr *DR = dyn_cast<DeclRefExpr>(S); 55 56 if (DR) 57 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) 58 if (VD->isStaticLocal()) 59 return true; 60 61 for (const Stmt *Child : S->children()) 62 if (Child && containsStaticLocal(Child)) 63 return true; 64 65 return false; 66 } 67 68 // Recursively find any substatements containing __builtin_offsetof 69 bool containsBuiltinOffsetOf(const Stmt *S) { 70 if (isa<OffsetOfExpr>(S)) 71 return true; 72 73 for (const Stmt *Child : S->children()) 74 if (Child && containsBuiltinOffsetOf(Child)) 75 return true; 76 77 return false; 78 } 79 80 // Extract lhs and rhs from assignment statement 81 std::pair<const clang::VarDecl *, const clang::Expr *> 82 parseAssignment(const Stmt *S) { 83 const VarDecl *VD = nullptr; 84 const Expr *RHS = nullptr; 85 86 if (auto Assign = dyn_cast_or_null<BinaryOperator>(S)) { 87 if (Assign->isAssignmentOp()) { 88 // Ordinary assignment 89 RHS = Assign->getRHS(); 90 if (auto DE = dyn_cast_or_null<DeclRefExpr>(Assign->getLHS())) 91 VD = dyn_cast_or_null<VarDecl>(DE->getDecl()); 92 } 93 } else if (auto PD = dyn_cast_or_null<DeclStmt>(S)) { 94 // Initialization 95 assert(PD->isSingleDecl() && "We process decls one by one"); 96 VD = cast<VarDecl>(PD->getSingleDecl()); 97 RHS = VD->getAnyInitializer(); 98 } 99 100 return std::make_pair(VD, RHS); 101 } 102 103 Nullability getNullabilityAnnotation(QualType Type) { 104 const auto *AttrType = Type->getAs<AttributedType>(); 105 if (!AttrType) 106 return Nullability::Unspecified; 107 if (AttrType->getAttrKind() == attr::TypeNullable) 108 return Nullability::Nullable; 109 else if (AttrType->getAttrKind() == attr::TypeNonNull) 110 return Nullability::Nonnull; 111 return Nullability::Unspecified; 112 } 113 114 llvm::Optional<int> tryExpandAsInteger(StringRef Macro, 115 const Preprocessor &PP) { 116 const auto *MacroII = PP.getIdentifierInfo(Macro); 117 if (!MacroII) 118 return std::nullopt; 119 const MacroInfo *MI = PP.getMacroInfo(MacroII); 120 if (!MI) 121 return std::nullopt; 122 123 // Filter out parens. 124 std::vector<Token> FilteredTokens; 125 FilteredTokens.reserve(MI->tokens().size()); 126 for (auto &T : MI->tokens()) 127 if (!T.isOneOf(tok::l_paren, tok::r_paren)) 128 FilteredTokens.push_back(T); 129 130 // Parse an integer at the end of the macro definition. 131 const Token &T = FilteredTokens.back(); 132 // FIXME: EOF macro token coming from a PCH file on macOS while marked as 133 // literal, doesn't contain any literal data 134 if (!T.isLiteral() || !T.getLiteralData()) 135 return std::nullopt; 136 StringRef ValueStr = StringRef(T.getLiteralData(), T.getLength()); 137 llvm::APInt IntValue; 138 constexpr unsigned AutoSenseRadix = 0; 139 if (ValueStr.getAsInteger(AutoSenseRadix, IntValue)) 140 return std::nullopt; 141 142 // Parse an optional minus sign. 143 size_t Size = FilteredTokens.size(); 144 if (Size >= 2) { 145 if (FilteredTokens[Size - 2].is(tok::minus)) 146 IntValue = -IntValue; 147 } 148 149 return IntValue.getSExtValue(); 150 } 151 152 OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK, 153 bool IsBinary) { 154 llvm::StringMap<BinaryOperatorKind> BinOps{ 155 #define BINARY_OPERATION(Name, Spelling) {Spelling, BO_##Name}, 156 #include "clang/AST/OperationKinds.def" 157 }; 158 llvm::StringMap<UnaryOperatorKind> UnOps{ 159 #define UNARY_OPERATION(Name, Spelling) {Spelling, UO_##Name}, 160 #include "clang/AST/OperationKinds.def" 161 }; 162 163 switch (OOK) { 164 #define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \ 165 case OO_##Name: \ 166 if (IsBinary) { \ 167 auto BinOpIt = BinOps.find(Spelling); \ 168 if (BinOpIt != BinOps.end()) \ 169 return OperatorKind(BinOpIt->second); \ 170 else \ 171 llvm_unreachable("operator was expected to be binary but is not"); \ 172 } else { \ 173 auto UnOpIt = UnOps.find(Spelling); \ 174 if (UnOpIt != UnOps.end()) \ 175 return OperatorKind(UnOpIt->second); \ 176 else \ 177 llvm_unreachable("operator was expected to be unary but is not"); \ 178 } \ 179 break; 180 #include "clang/Basic/OperatorKinds.def" 181 default: 182 llvm_unreachable("unexpected operator kind"); 183 } 184 } 185 186 } // namespace ento 187 } // namespace clang 188