Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:07

0001 //== CheckerHelpers.h - Helper functions for checkers ------------*- C++ -*--=//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines various utilities used by checkers.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERHELPERS_H
0014 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_CHECKERHELPERS_H
0015 
0016 #include "ProgramState_Fwd.h"
0017 #include "SVals.h"
0018 #include "clang/AST/OperationKinds.h"
0019 #include "clang/AST/Stmt.h"
0020 #include "clang/Basic/OperatorKinds.h"
0021 #include <optional>
0022 #include <tuple>
0023 
0024 namespace clang {
0025 
0026 class Expr;
0027 class VarDecl;
0028 class QualType;
0029 class Preprocessor;
0030 
0031 namespace ento {
0032 
0033 bool containsMacro(const Stmt *S);
0034 bool containsEnum(const Stmt *S);
0035 bool containsStaticLocal(const Stmt *S);
0036 bool containsBuiltinOffsetOf(const Stmt *S);
0037 template <class T> bool containsStmt(const Stmt *S) {
0038   if (isa<T>(S))
0039       return true;
0040 
0041   for (const Stmt *Child : S->children())
0042     if (Child && containsStmt<T>(Child))
0043       return true;
0044 
0045   return false;
0046 }
0047 
0048 std::pair<const clang::VarDecl *, const clang::Expr *>
0049 parseAssignment(const Stmt *S);
0050 
0051 // Do not reorder! The getMostNullable method relies on the order.
0052 // Optimization: Most pointers expected to be unspecified. When a symbol has an
0053 // unspecified or nonnull type non of the rules would indicate any problem for
0054 // that symbol. For this reason only nullable and contradicted nullability are
0055 // stored for a symbol. When a symbol is already contradicted, it can not be
0056 // casted back to nullable.
0057 enum class Nullability : char {
0058   Contradicted, // Tracked nullability is contradicted by an explicit cast. Do
0059                 // not report any nullability related issue for this symbol.
0060                 // This nullability is propagated aggressively to avoid false
0061                 // positive results. See the comment on getMostNullable method.
0062   Nullable,
0063   Unspecified,
0064   Nonnull
0065 };
0066 
0067 /// Get nullability annotation for a given type.
0068 Nullability getNullabilityAnnotation(QualType Type);
0069 
0070 /// Try to parse the value of a defined preprocessor macro. We can only parse
0071 /// simple expressions that consist of an optional minus sign token and then a
0072 /// token for an integer. If we cannot parse the value then std::nullopt is
0073 /// returned.
0074 std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP);
0075 
0076 class OperatorKind {
0077   union {
0078     BinaryOperatorKind Bin;
0079     UnaryOperatorKind Un;
0080   } Op;
0081   bool IsBinary;
0082 
0083 public:
0084   explicit OperatorKind(BinaryOperatorKind Bin) : Op{Bin}, IsBinary{true} {}
0085   explicit OperatorKind(UnaryOperatorKind Un) : IsBinary{false} { Op.Un = Un; }
0086   bool IsBinaryOp() const { return IsBinary; }
0087 
0088   BinaryOperatorKind GetBinaryOpUnsafe() const {
0089     assert(IsBinary && "cannot get binary operator - we have a unary operator");
0090     return Op.Bin;
0091   }
0092 
0093   std::optional<BinaryOperatorKind> GetBinaryOp() const {
0094     if (IsBinary)
0095       return Op.Bin;
0096     return {};
0097   }
0098 
0099   UnaryOperatorKind GetUnaryOpUnsafe() const {
0100     assert(!IsBinary &&
0101            "cannot get unary operator - we have a binary operator");
0102     return Op.Un;
0103   }
0104 
0105   std::optional<UnaryOperatorKind> GetUnaryOp() const {
0106     if (!IsBinary)
0107       return Op.Un;
0108     return {};
0109   }
0110 };
0111 
0112 OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK,
0113                                                  bool IsBinary);
0114 
0115 std::optional<SVal> getPointeeVal(SVal PtrSVal, ProgramStateRef State);
0116 
0117 /// Returns true if declaration \p D is in std namespace or any nested namespace
0118 /// or class scope.
0119 bool isWithinStdNamespace(const Decl *D);
0120 
0121 } // namespace ento
0122 
0123 } // namespace clang
0124 
0125 #endif