1/*
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
17#ifndef _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CHECK_AST_H_  // NOLINT
18#define _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CHECK_AST_H_
19
20#include "slang_assert.h"
21#include "clang/AST/ASTContext.h"
22#include "clang/AST/StmtVisitor.h"
23
24namespace slang {
25
26// This class is designed to walk a Renderscript/Filterscript AST looking for
27// violations. Examples of violations for FS are pointer declarations and
28// casts (i.e. no pointers allowed in FS whatsoever).
29class RSCheckAST : public clang::StmtVisitor<RSCheckAST> {
30 private:
31  clang::ASTContext &C;
32  clang::DiagnosticsEngine &mDiagEngine;
33  clang::SourceManager &mSM;
34  bool mValid;
35  unsigned int mTargetAPI;
36  bool mIsFilterscript;
37  bool mInKernel;
38
39  /// @brief Emit warnings for inapproriate uses of rsSetElementAt
40  ///
41  /// We warn in case generic rsSetElementAt() is used even though the user
42  /// could have used a typed rsSetElementAt_<type>() call. Typed calls
43  /// allow more aggressive optimization (e.g. due to better alias analysis
44  /// results). Hence, we want to steer the users to use them.
45  void WarnOnSetElementAt(clang::CallExpr*);
46
47 public:
48  explicit RSCheckAST(clang::ASTContext &Con, unsigned int TargetAPI,
49                      bool IsFilterscript)
50      : C(Con), mDiagEngine(Con.getDiagnostics()), mSM(C.getSourceManager()),
51        mValid(true), mTargetAPI(TargetAPI), mIsFilterscript(IsFilterscript),
52        mInKernel(false) {
53    return;
54  }
55
56  void VisitStmt(clang::Stmt *S);
57
58  void VisitCallExpr(clang::CallExpr *CE);
59
60  void VisitCastExpr(clang::CastExpr *CE);
61
62  void VisitExpr(clang::Expr *E);
63
64  void VisitDeclStmt(clang::DeclStmt *DS);
65
66  void ValidateFunctionDecl(clang::FunctionDecl *FD);
67
68  void ValidateVarDecl(clang::VarDecl *VD);
69
70  bool Validate();
71};
72
73}  // namespace slang
74
75#endif  // _FRAMEWORKS_COMPILE_SLANG_SLANG_RS_CHECK_AST_H_  NOLINT
76