1 //== CheckerHelpers.h - 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 CheckerVisitor. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERHELPERS_H 14 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERHELPERS_H 15 16 #include "clang/AST/Stmt.h" 17 #include "llvm/ADT/Optional.h" 18 #include <tuple> 19 20 namespace clang { 21 22 class Expr; 23 class VarDecl; 24 class QualType; 25 class AttributedType; 26 class Preprocessor; 27 28 namespace ento { 29 30 bool containsMacro(const Stmt *S); 31 bool containsEnum(const Stmt *S); 32 bool containsStaticLocal(const Stmt *S); 33 bool containsBuiltinOffsetOf(const Stmt *S); containsStmt(const Stmt * S)34template <class T> bool containsStmt(const Stmt *S) { 35 if (isa<T>(S)) 36 return true; 37 38 for (const Stmt *Child : S->children()) 39 if (Child && containsStmt<T>(Child)) 40 return true; 41 42 return false; 43 } 44 45 std::pair<const clang::VarDecl *, const clang::Expr *> 46 parseAssignment(const Stmt *S); 47 48 // Do not reorder! The getMostNullable method relies on the order. 49 // Optimization: Most pointers expected to be unspecified. When a symbol has an 50 // unspecified or nonnull type non of the rules would indicate any problem for 51 // that symbol. For this reason only nullable and contradicted nullability are 52 // stored for a symbol. When a symbol is already contradicted, it can not be 53 // casted back to nullable. 54 enum class Nullability : char { 55 Contradicted, // Tracked nullability is contradicted by an explicit cast. Do 56 // not report any nullability related issue for this symbol. 57 // This nullability is propagated aggressively to avoid false 58 // positive results. See the comment on getMostNullable method. 59 Nullable, 60 Unspecified, 61 Nonnull 62 }; 63 64 /// Get nullability annotation for a given type. 65 Nullability getNullabilityAnnotation(QualType Type); 66 67 /// Try to parse the value of a defined preprocessor macro. We can only parse 68 /// simple expressions that consist of an optional minus sign token and then a 69 /// token for an integer. If we cannot parse the value then None is returned. 70 llvm::Optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP); 71 72 } // namespace ento 73 74 } // namespace clang 75 76 #endif 77