clang: lib/Sema/SemaBPF.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
20#include "llvm/ADT/APSInt.h"
21#include
22
24
26
29 return false;
30
31
32
33
34
37 isa(Arg->IgnoreParens()));
38}
39
43 return false;
44
45
46
47
48
49
52 return false;
53
54
56 return true;
57
58
61 if (!RT->getDecl()->getDeclName().isEmpty())
62 return true;
63 } else if (const auto *ET = Ty->getAs<EnumType>()) {
64 if (!ET->getDecl()->getDeclName().isEmpty())
65 return true;
66 }
67
68 return false;
69}
70
74 return false;
75
76
77
78
79
80 const auto *UO = dyn_cast(Arg->IgnoreParens());
81 if (!UO)
82 return false;
83
84 const auto *CE = dyn_cast(UO->getSubExpr());
85 if (!CE)
86 return false;
87 if (CE->getCastKind() != CK_IntegralToPointer &&
88 CE->getCastKind() != CK_NullToPointer)
89 return false;
90
91
92 const auto *DR = dyn_cast(CE->getSubExpr());
93 if (!DR)
94 return false;
95
97 dyn_cast(DR->getDecl());
98 if (!Enumerator)
99 return false;
100
101
104 if (!ET)
105 return false;
106
107
108 return llvm::is_contained(ET->getDecl()->enumerators(), Enumerator);
109}
110
113 assert((BuiltinID == BPF::BI__builtin_preserve_field_info ||
114 BuiltinID == BPF::BI__builtin_btf_type_id ||
115 BuiltinID == BPF::BI__builtin_preserve_type_info ||
116 BuiltinID == BPF::BI__builtin_preserve_enum_value) &&
117 "unexpected BPF builtin");
120 return true;
121
122
127 if (BuiltinID == BPF::BI__builtin_preserve_field_info)
128 kind = diag::err_preserve_field_info_not_const;
129 else if (BuiltinID == BPF::BI__builtin_btf_type_id)
130 kind = diag::err_btf_type_id_not_const;
131 else if (BuiltinID == BPF::BI__builtin_preserve_type_info)
132 kind = diag::err_preserve_type_info_not_const;
133 else
134 kind = diag::err_preserve_enum_value_not_const;
136 return true;
137 }
138
139
140 Arg = TheCall->getArg(0);
141 bool InvalidArg = false;
142 bool ReturnUnsignedInt = true;
143 if (BuiltinID == BPF::BI__builtin_preserve_field_info) {
145 InvalidArg = true;
146 kind = diag::err_preserve_field_info_not_field;
147 }
148 } else if (BuiltinID == BPF::BI__builtin_preserve_type_info) {
150 InvalidArg = true;
151 kind = diag::err_preserve_type_info_invalid;
152 }
153 } else if (BuiltinID == BPF::BI__builtin_preserve_enum_value) {
155 InvalidArg = true;
156 kind = diag::err_preserve_enum_value_invalid;
157 }
158 ReturnUnsignedInt = false;
159 } else if (BuiltinID == BPF::BI__builtin_btf_type_id) {
160 ReturnUnsignedInt = false;
161 }
162
163 if (InvalidArg) {
165 return true;
166 }
167
168 if (ReturnUnsignedInt)
169 TheCall->setType(Context.UnsignedIntTy);
170 else
171 TheCall->setType(Context.UnsignedLongTy);
172 return false;
173}
174
176
177 for (auto *D : RD->decls()) {
178 if (D->hasAttr())
179 continue;
180
181 D->addAttr(BPFPreserveAccessIndexAttr::CreateImplicit(getASTContext()));
182 if (auto *Rec = dyn_cast(D))
184 }
185}
186
188 auto *Rec = cast(D);
191 BPFPreserveAccessIndexAttr(getASTContext(), AL));
192}
193
194}
This file declares semantic analysis functions specific to BPF.
Enumerates target-specific builtins in their own namespaces within namespace clang.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Decl - This represents one declaration (or definition), e.g.
An instance of this object exists for each enum constant that is defined.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of enums.
This represents one expression.
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
ExprObjectKind getObjectKind() const
getObjectKind - The object kind that this expression produces.
std::optional< llvm::APSInt > getIntegerConstantExpr(const ASTContext &Ctx, SourceLocation *Loc=nullptr) const
isIntegerConstantExpr - Return the value if this expression is a valid integer constant expression.
ParsedAttr - Represents a syntactic attribute.
A (possibly-)qualified type.
Represents a struct/union/class.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
void handlePreserveAIRecord(RecordDecl *RD)
void handlePreserveAccessIndexAttr(Decl *D, const ParsedAttr &AL)
bool CheckBPFBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall)
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
ASTContext & getASTContext() const
Sema - This implements semantic analysis and AST building for C.
bool checkArgCount(CallExpr *Call, unsigned DesiredArgCount)
Checks that a call expression's argument count is the desired number.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
SourceLocation getBeginLoc() const LLVM_READONLY
The base class of the type hierarchy.
const BuiltinType * getAsPlaceholderType() const
const T * getAs() const
Member-template getAs'.
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
The JSON file list parser is used to communicate input to InstallAPI.
static bool isValidPreserveFieldInfoArg(Expr *Arg)
@ OK_BitField
A bitfield object is a bitfield on a C or C++ record.
static bool isValidPreserveEnumValueArg(Expr *Arg)
static bool isValidPreserveTypeInfoArg(Expr *Arg)