clang 20.0.0git
CheckerHelpers.h
Go to the documentation of this file.
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 "ProgramState_Fwd.h"
17#include "SVals.h"
19#include "clang/AST/Stmt.h"
21#include <optional>
22#include <tuple>
23
24namespace clang {
25
26class Expr;
27class VarDecl;
28class QualType;
29class Preprocessor;
30
31namespace ento {
32
33bool containsMacro(const Stmt *S);
34bool containsEnum(const Stmt *S);
35bool containsStaticLocal(const Stmt *S);
36bool containsBuiltinOffsetOf(const Stmt *S);
37template <class T> bool containsStmt(const Stmt *S) {
38 if (isa<T>(S))
39 return true;
40
41 for (const Stmt *Child : S->children())
42 if (Child && containsStmt<T>(Child))
43 return true;
44
45 return false;
46}
47
48std::pair<const clang::VarDecl *, const clang::Expr *>
49parseAssignment(const Stmt *S);
50
51// Do not reorder! The getMostNullable method relies on the order.
52// Optimization: Most pointers expected to be unspecified. When a symbol has an
53// unspecified or nonnull type non of the rules would indicate any problem for
54// that symbol. For this reason only nullable and contradicted nullability are
55// stored for a symbol. When a symbol is already contradicted, it can not be
56// casted back to nullable.
57enum class Nullability : char {
58 Contradicted, // Tracked nullability is contradicted by an explicit cast. Do
59 // not report any nullability related issue for this symbol.
60 // This nullability is propagated aggressively to avoid false
61 // positive results. See the comment on getMostNullable method.
65};
66
67/// Get nullability annotation for a given type.
69
70/// Try to parse the value of a defined preprocessor macro. We can only parse
71/// simple expressions that consist of an optional minus sign token and then a
72/// token for an integer. If we cannot parse the value then std::nullopt is
73/// returned.
74std::optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP);
75
77 union {
80 } Op;
81 bool IsBinary;
82
83public:
84 explicit OperatorKind(BinaryOperatorKind Bin) : Op{Bin}, IsBinary{true} {}
85 explicit OperatorKind(UnaryOperatorKind Un) : IsBinary{false} { Op.Un = Un; }
86 bool IsBinaryOp() const { return IsBinary; }
87
89 assert(IsBinary && "cannot get binary operator - we have a unary operator");
90 return Op.Bin;
91 }
92
93 std::optional<BinaryOperatorKind> GetBinaryOp() const {
94 if (IsBinary)
95 return Op.Bin;
96 return {};
97 }
98
100 assert(!IsBinary &&
101 "cannot get unary operator - we have a binary operator");
102 return Op.Un;
103 }
104
105 std::optional<UnaryOperatorKind> GetUnaryOp() const {
106 if (!IsBinary)
107 return Op.Un;
108 return {};
109 }
110};
111
113 bool IsBinary);
114
115std::optional<SVal> getPointeeVal(SVal PtrSVal, ProgramStateRef State);
116
117} // namespace ento
118
119} // namespace clang
120
121#endif
Defines an enumeration for C++ overloaded operators.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:137
A (possibly-)qualified type.
Definition: Type.h:941
Stmt - This represents one statement.
Definition: Stmt.h:84
The base class of the type hierarchy.
Definition: Type.h:1829
UnaryOperatorKind GetUnaryOpUnsafe() const
BinaryOperatorKind GetBinaryOpUnsafe() const
std::optional< UnaryOperatorKind > GetUnaryOp() const
OperatorKind(UnaryOperatorKind Un)
UnaryOperatorKind Un
OperatorKind(BinaryOperatorKind Bin)
BinaryOperatorKind Bin
std::optional< BinaryOperatorKind > GetBinaryOp() const
bool containsEnum(const Stmt *S)
Nullability getNullabilityAnnotation(QualType Type)
Get nullability annotation for a given type.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
bool containsStaticLocal(const Stmt *S)
std::pair< const clang::VarDecl *, const clang::Expr * > parseAssignment(const Stmt *S)
OperatorKind operationKindFromOverloadedOperator(OverloadedOperatorKind OOK, bool IsBinary)
bool containsBuiltinOffsetOf(const Stmt *S)
std::optional< SVal > getPointeeVal(SVal PtrSVal, ProgramStateRef State)
std::optional< int > tryExpandAsInteger(StringRef Macro, const Preprocessor &PP)
Try to parse the value of a defined preprocessor macro.
bool containsStmt(const Stmt *S)
bool containsMacro(const Stmt *S)
The JSON file list parser is used to communicate input to InstallAPI.
OverloadedOperatorKind
Enumeration specifying the different kinds of C++ overloaded operators.
Definition: OperatorKinds.h:21
BinaryOperatorKind
UnaryOperatorKind
#define true
Definition: stdbool.h:25
#define false
Definition: stdbool.h:26