clang  15.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 
17 #include "clang/AST/Stmt.h"
19 #include "llvm/ADT/Optional.h"
20 #include <tuple>
21 
22 namespace clang {
23 
24 class Expr;
25 class VarDecl;
26 class QualType;
27 class Preprocessor;
28 
29 namespace ento {
30 
31 bool containsMacro(const Stmt *S);
32 bool containsEnum(const Stmt *S);
33 bool containsStaticLocal(const Stmt *S);
34 bool containsBuiltinOffsetOf(const Stmt *S);
35 template <class T> bool containsStmt(const Stmt *S) {
36  if (isa<T>(S))
37  return true;
38 
39  for (const Stmt *Child : S->children())
40  if (Child && containsStmt<T>(Child))
41  return true;
42 
43  return false;
44 }
45 
46 std::pair<const clang::VarDecl *, const clang::Expr *>
47 parseAssignment(const Stmt *S);
48 
49 // Do not reorder! The getMostNullable method relies on the order.
50 // Optimization: Most pointers expected to be unspecified. When a symbol has an
51 // unspecified or nonnull type non of the rules would indicate any problem for
52 // that symbol. For this reason only nullable and contradicted nullability are
53 // stored for a symbol. When a symbol is already contradicted, it can not be
54 // casted back to nullable.
55 enum class Nullability : char {
56  Contradicted, // Tracked nullability is contradicted by an explicit cast. Do
57  // not report any nullability related issue for this symbol.
58  // This nullability is propagated aggressively to avoid false
59  // positive results. See the comment on getMostNullable method.
60  Nullable,
62  Nonnull
63 };
64 
65 /// Get nullability annotation for a given type.
67 
68 /// Try to parse the value of a defined preprocessor macro. We can only parse
69 /// simple expressions that consist of an optional minus sign token and then a
70 /// token for an integer. If we cannot parse the value then None is returned.
71 llvm::Optional<int> tryExpandAsInteger(StringRef Macro, const Preprocessor &PP);
72 
73 class OperatorKind {
74  union {
77  } Op;
78  bool IsBinary;
79 
80 public:
81  explicit OperatorKind(BinaryOperatorKind Bin) : Op{Bin}, IsBinary{true} {}
82  explicit OperatorKind(UnaryOperatorKind Un) : IsBinary{false} { Op.Un = Un; }
83  bool IsBinaryOp() const { return IsBinary; }
84 
86  assert(IsBinary && "cannot get binary operator - we have a unary operator");
87  return Op.Bin;
88  }
89 
91  if (IsBinary)
92  return Op.Bin;
93  return {};
94  }
95 
97  assert(!IsBinary &&
98  "cannot get unary operator - we have a binary operator");
99  return Op.Un;
100  }
101 
103  if (!IsBinary)
104  return Op.Un;
105  return {};
106  }
107 };
108 
110  bool IsBinary);
111 
112 } // namespace ento
113 
114 } // namespace clang
115 
116 #endif
clang::ento::OperatorKind::OperatorKind
OperatorKind(UnaryOperatorKind Un)
Definition: CheckerHelpers.h:82
clang::ento::Nullability
Nullability
Definition: CheckerHelpers.h:55
clang::ento::OperatorKind::GetUnaryOpUnsafe
UnaryOperatorKind GetUnaryOpUnsafe() const
Definition: CheckerHelpers.h:96
clang::ento::Nullability::Contradicted
@ Contradicted
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
llvm::Optional
Definition: LLVM.h:40
clang::ento::containsStaticLocal
bool containsStaticLocal(const Stmt *S)
Definition: CheckerHelpers.cpp:52
clang::ento::OperatorKind::GetUnaryOp
Optional< UnaryOperatorKind > GetUnaryOp() const
Definition: CheckerHelpers.h:102
clang::ento::containsBuiltinOffsetOf
bool containsBuiltinOffsetOf(const Stmt *S)
Definition: CheckerHelpers.cpp:68
clang::ento::OperatorKind::IsBinaryOp
bool IsBinaryOp() const
Definition: CheckerHelpers.h:83
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:1500
clang::ento::parseAssignment
std::pair< const clang::VarDecl *, const clang::Expr * > parseAssignment(const Stmt *S)
Definition: CheckerHelpers.cpp:81
OperatorKinds.h
clang::ento::OperatorKind::GetBinaryOp
Optional< BinaryOperatorKind > GetBinaryOp() const
Definition: CheckerHelpers.h:90
clang::ento::Nullability::Nonnull
@ Nonnull
clang::ento::Nullability::Nullable
@ Nullable
clang::ento::OperatorKind::GetBinaryOpUnsafe
BinaryOperatorKind GetBinaryOpUnsafe() const
Definition: CheckerHelpers.h:85
clang::ento::OperatorKind::OperatorKind
OperatorKind(BinaryOperatorKind Bin)
Definition: CheckerHelpers.h:81
clang::ento::containsStmt
bool containsStmt(const Stmt *S)
Definition: CheckerHelpers.h:35
OperationKinds.h
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::UnaryOperatorKind
UnaryOperatorKind
Definition: OperationKinds.h:30
clang::BinaryOperatorKind
BinaryOperatorKind
Definition: OperationKinds.h:25
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
clang::ento::OperatorKind::Un
UnaryOperatorKind Un
Definition: CheckerHelpers.h:76
clang::ento::Nullability::Unspecified
@ Unspecified
Stmt.h
clang::ento::OperatorKind::Bin
BinaryOperatorKind Bin
Definition: CheckerHelpers.h:75
clang::Preprocessor
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:131
clang::ento::containsMacro
bool containsMacro(const Stmt *S)
Definition: CheckerHelpers.cpp:23
clang::ento::OperatorKind
Definition: CheckerHelpers.h:73