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