blob: 61a218d21180bfde283e1d26c3bc2231c4d6a307 [file] [log] [blame]
Stephen Hines11274a72012-09-26 19:14:20 -07001/*
2 * Copyright 2012, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Matt Walaf5b882c2015-06-15 17:45:45 -070017#include "clang/AST/Attr.h"
18
Stephen Hines11274a72012-09-26 19:14:20 -070019#include "slang_rs_check_ast.h"
20
21#include "slang_assert.h"
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -070022#include "slang.h"
Stephen Hines089cde32012-12-07 19:19:10 -080023#include "slang_rs_export_foreach.h"
Matt Walac0c5dd82015-07-23 17:29:37 -070024#include "slang_rs_export_reduce.h"
Stephen Hines11274a72012-09-26 19:14:20 -070025#include "slang_rs_export_type.h"
26
27namespace slang {
28
29void RSCheckAST::VisitStmt(clang::Stmt *S) {
30 // This function does the actual iteration through all sub-Stmt's within
31 // a given Stmt. Note that this function is skipped by all of the other
32 // Visit* functions if we have already found a higher-level match.
33 for (clang::Stmt::child_iterator I = S->child_begin(), E = S->child_end();
34 I != E;
35 I++) {
36 if (clang::Stmt *Child = *I) {
37 Visit(Child);
38 }
39 }
40}
41
Tobias Grosser61685432013-07-31 14:52:24 -070042void RSCheckAST::WarnOnSetElementAt(clang::CallExpr *E) {
43 clang::FunctionDecl *Decl;
Tobias Grosser61685432013-07-31 14:52:24 -070044 Decl = clang::dyn_cast_or_null<clang::FunctionDecl>(E->getCalleeDecl());
45
46 if (!Decl || Decl->getNameAsString() != std::string("rsSetElementAt")) {
47 return;
48 }
49
50 clang::Expr *Expr;
51 clang::ImplicitCastExpr *ImplCast;
52 Expr = E->getArg(1);
53 ImplCast = clang::dyn_cast_or_null<clang::ImplicitCastExpr>(Expr);
54
55 if (!ImplCast) {
56 return;
57 }
58
59 const clang::Type *Ty;
60 const clang::VectorType *VectorTy;
61 const clang::BuiltinType *ElementTy;
62 Ty = ImplCast->getSubExpr()->getType()->getPointeeType()
63 ->getUnqualifiedDesugaredType();
George Burgess IV85837e92020-02-18 14:04:05 -080064 VectorTy = clang::dyn_cast<clang::VectorType>(Ty);
Tobias Grosser61685432013-07-31 14:52:24 -070065
66 if (VectorTy) {
67 ElementTy = clang::dyn_cast_or_null<clang::BuiltinType>(
68 VectorTy->getElementType()->getUnqualifiedDesugaredType());
69 } else {
70 ElementTy = clang::dyn_cast_or_null<clang::BuiltinType>(
71 Ty->getUnqualifiedDesugaredType());
72 }
73
74 if (!ElementTy) {
75 return;
76 }
77
78 // We only support vectors with 2, 3 or 4 elements.
79 if (VectorTy) {
80 switch (VectorTy->getNumElements()) {
81 default:
82 return;
83 case 2:
84 case 3:
85 case 4:
86 break;
87 }
88 }
89
90 const char *Name;
91
92 switch (ElementTy->getKind()) {
93 case clang::BuiltinType::Float:
94 Name = "float";
95 break;
96 case clang::BuiltinType::Double:
97 Name = "double";
98 break;
99 case clang::BuiltinType::Char_S:
100 Name = "char";
101 break;
102 case clang::BuiltinType::Short:
103 Name = "short";
104 break;
105 case clang::BuiltinType::Int:
106 Name = "int";
107 break;
108 case clang::BuiltinType::Long:
109 Name = "long";
110 break;
111 case clang::BuiltinType::UChar:
112 Name = "uchar";
113 break;
114 case clang::BuiltinType::UShort:
115 Name = "ushort";
116 break;
117 case clang::BuiltinType::UInt:
118 Name = "uint";
119 break;
120 case clang::BuiltinType::ULong:
121 Name = "ulong";
122 break;
123 default:
124 return;
125 }
126
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800127 clang::DiagnosticBuilder DiagBuilder =
128 Context->ReportWarning(E->getLocStart(),
129 "untyped rsSetElementAt() can reduce performance. "
130 "Use rsSetElementAt_%0%1() instead.");
Tobias Grosser61685432013-07-31 14:52:24 -0700131 DiagBuilder << Name;
132
133 if (VectorTy) {
134 DiagBuilder << VectorTy->getNumElements();
135 } else {
136 DiagBuilder << "";
137 }
Tobias Grosser61685432013-07-31 14:52:24 -0700138}
139
140void RSCheckAST::VisitCallExpr(clang::CallExpr *E) {
141 WarnOnSetElementAt(E);
142
143 for (clang::CallExpr::arg_iterator AI = E->arg_begin(), AE = E->arg_end();
144 AI != AE; ++AI) {
145 Visit(*AI);
146 }
147}
148
Stephen Hines11274a72012-09-26 19:14:20 -0700149void RSCheckAST::ValidateFunctionDecl(clang::FunctionDecl *FD) {
150 if (!FD) {
151 return;
152 }
153
Pirama Arumuga Nainarb6a14352016-07-26 11:39:47 -0700154 // Validate that the kernel attribute is not used with static.
155 if (FD->hasAttr<clang::RenderScriptKernelAttr>() &&
156 FD->getStorageClass() == clang::SC_Static) {
157 Context->ReportError(FD->getLocation(),
158 "Invalid use of attribute kernel with "
159 "static function declaration: %0")
160 << FD->getName();
161 mValid = false;
Matt Walaf5b882c2015-06-15 17:45:45 -0700162 }
163
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700164 clang::QualType resultType = FD->getReturnType().getCanonicalType();
165 bool isExtern = (FD->getFormalLinkage() == clang::ExternalLinkage);
Stephen Hines11274a72012-09-26 19:14:20 -0700166
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700167 // We use FD as our NamedDecl in the case of a bad return type.
168 if (!RSExportType::ValidateType(Context, C, resultType, FD,
169 FD->getLocStart(), mTargetAPI,
170 mIsFilterscript, isExtern)) {
171 mValid = false;
172 }
Stephen Hines11274a72012-09-26 19:14:20 -0700173
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700174 size_t numParams = FD->getNumParams();
175 for (size_t i = 0; i < numParams; i++) {
176 clang::ParmVarDecl *PVD = FD->getParamDecl(i);
177 clang::QualType QT = PVD->getType().getCanonicalType();
178 if (!RSExportType::ValidateType(Context, C, QT, PVD, PVD->getLocStart(),
179 mTargetAPI, mIsFilterscript, isExtern)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700180 mValid = false;
181 }
182 }
183
Stephen Hines089cde32012-12-07 19:19:10 -0800184 bool saveKernel = mInKernel;
David Gross8ee018b2016-06-02 14:46:55 -0700185 mInKernel = RSExportForEach::isRSForEachFunc(mTargetAPI, FD);
Stephen Hines089cde32012-12-07 19:19:10 -0800186
Stephen Hines11274a72012-09-26 19:14:20 -0700187 if (clang::Stmt *Body = FD->getBody()) {
188 Visit(Body);
189 }
Stephen Hines089cde32012-12-07 19:19:10 -0800190
191 mInKernel = saveKernel;
Stephen Hines11274a72012-09-26 19:14:20 -0700192}
193
194
195void RSCheckAST::ValidateVarDecl(clang::VarDecl *VD) {
David Gross5e306b92016-02-08 16:45:12 -0800196 if (!VD || RSContext::isSyntheticName(VD->getName())) {
Stephen Hines11274a72012-09-26 19:14:20 -0700197 return;
198 }
199
Stephen Hines089cde32012-12-07 19:19:10 -0800200 clang::QualType QT = VD->getType();
Stephen Hines11274a72012-09-26 19:14:20 -0700201
Stephen Hines44f10062013-06-13 00:33:23 -0700202 if (VD->getFormalLinkage() == clang::ExternalLinkage) {
Stephen Hines11274a72012-09-26 19:14:20 -0700203 llvm::StringRef TypeName;
Stephen Hines089cde32012-12-07 19:19:10 -0800204 const clang::Type *T = QT.getTypePtr();
Stephen Hines13fad852015-11-23 19:32:14 -0800205 if (!RSExportType::NormalizeType(T, TypeName, Context, VD,
206 NotLegacyKernelArgument)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700207 mValid = false;
208 }
209 }
210
Stephen Hines089cde32012-12-07 19:19:10 -0800211 // We don't allow static (non-const) variables within kernels.
212 if (mInKernel && VD->isStaticLocal()) {
213 if (!QT.isConstQualified()) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800214 Context->ReportError(
215 VD->getLocation(),
216 "Non-const static variables are not allowed in kernels: '%0'")
Stephen Hines089cde32012-12-07 19:19:10 -0800217 << VD->getName();
218 mValid = false;
219 }
220 }
221
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800222 if (!RSExportType::ValidateVarDecl(Context, VD, mTargetAPI, mIsFilterscript)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700223 mValid = false;
224 } else if (clang::Expr *Init = VD->getInit()) {
225 // Only check the initializer if the decl is already ok.
226 Visit(Init);
227 }
228}
229
230
231void RSCheckAST::VisitDeclStmt(clang::DeclStmt *DS) {
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700232 if (!Slang::IsLocInRSHeaderFile(DS->getLocStart(), mSM)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700233 for (clang::DeclStmt::decl_iterator I = DS->decl_begin(),
234 E = DS->decl_end();
235 I != E;
236 ++I) {
237 if (clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*I)) {
238 ValidateVarDecl(VD);
239 } else if (clang::FunctionDecl *FD =
240 llvm::dyn_cast<clang::FunctionDecl>(*I)) {
241 ValidateFunctionDecl(FD);
242 }
243 }
244 }
245}
246
247
Stephen Hinesdbb6dc32013-05-08 13:43:13 -0700248void RSCheckAST::VisitCastExpr(clang::CastExpr *CE) {
249 if (CE->getCastKind() == clang::CK_BitCast) {
250 clang::QualType QT = CE->getType();
251 const clang::Type *T = QT.getTypePtr();
252 if (T->isVectorType()) {
Stephen Hinesdbb6dc32013-05-08 13:43:13 -0700253 if (llvm::isa<clang::ImplicitCastExpr>(CE)) {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800254 Context->ReportError(CE->getExprLoc(), "invalid implicit vector cast");
Stephen Hinesdbb6dc32013-05-08 13:43:13 -0700255 } else {
Jean-Luc Brouilletd3f75272014-01-16 18:20:28 -0800256 Context->ReportError(CE->getExprLoc(), "invalid vector cast");
Stephen Hinesdbb6dc32013-05-08 13:43:13 -0700257 }
258 mValid = false;
259 }
260 }
261 Visit(CE->getSubExpr());
262}
263
264
Stephen Hines11274a72012-09-26 19:14:20 -0700265void RSCheckAST::VisitExpr(clang::Expr *E) {
266 // This is where FS checks for code using pointer and/or 64-bit expressions
267 // (i.e. things like casts).
268
269 // First we skip implicit casts (things like function calls and explicit
270 // array accesses rely heavily on them and they are valid.
271 E = E->IgnoreImpCasts();
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700272
273 // Expressions at this point in the checker are not externally visible.
274 static const bool kIsExtern = false;
275
Stephen Hines11274a72012-09-26 19:14:20 -0700276 if (mIsFilterscript &&
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700277 !Slang::IsLocInRSHeaderFile(E->getExprLoc(), mSM) &&
Chris Wailes5abbe0e2014-08-12 15:58:29 -0700278 !RSExportType::ValidateType(Context, C, E->getType(), nullptr, E->getExprLoc(),
Stephen Hinesab94bcc2015-06-11 17:00:04 -0700279 mTargetAPI, mIsFilterscript, kIsExtern)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700280 mValid = false;
281 } else {
282 // Only visit sub-expressions if we haven't already seen a violation.
283 VisitStmt(E);
284 }
285}
286
287
288bool RSCheckAST::Validate() {
289 clang::TranslationUnitDecl *TUDecl = C.getTranslationUnitDecl();
290 for (clang::DeclContext::decl_iterator DI = TUDecl->decls_begin(),
291 DE = TUDecl->decls_end();
292 DI != DE;
293 DI++) {
I-Jui (Ray) Sungb477c352016-11-30 17:08:07 -0800294
295 // Following tests are not applicable to implicitly defined types
296 if (DI->isImplicit())
297 continue;
298
Jean-Luc Brouillet8024ed52015-05-04 23:02:25 -0700299 if (!Slang::IsLocInRSHeaderFile(DI->getLocStart(), mSM)) {
Stephen Hines11274a72012-09-26 19:14:20 -0700300 if (clang::VarDecl *VD = llvm::dyn_cast<clang::VarDecl>(*DI)) {
301 ValidateVarDecl(VD);
302 } else if (clang::FunctionDecl *FD =
303 llvm::dyn_cast<clang::FunctionDecl>(*DI)) {
304 ValidateFunctionDecl(FD);
305 } else if (clang::Stmt *Body = (*DI)->getBody()) {
306 Visit(Body);
307 }
308 }
309 }
310
311 return mValid;
312}
313
314} // namespace slang