1 //===----------------------------------------------------------------------===// 2 // 3 // Copyright (c) 2016 The University of Utah 4 // All rights reserved. 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See the file COPYING for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 #ifndef EXPRESSION_DETECTOR_H 12 #define EXPRESSION_DETECTOR_H 13 14 #include <vector> 15 #include <map> 16 #include "llvm/ADT/DenseMap.h" 17 #include "llvm/ADT/SmallPtrSet.h" 18 #include "clang/Basic/SourceLocation.h" 19 #include "Transformation.h" 20 21 namespace clang { 22 class DeclGroupRef; 23 class ASTContext; 24 class FunctionDecl; 25 class NamedDecl; 26 class VarDecl; 27 class Stmt; 28 class Expr; 29 } 30 31 class ExprDetectorCollectionVisitor; 32 33 class ExpressionDetector : public Transformation { 34 friend class ExprDetectorCollectionVisitor; 35 friend class ExprDetectorStmtVisitor; 36 friend class ExprDetectorTempVarVisitor; 37 38 public: ExpressionDetector(const char * TransName,const char * Desc)39 ExpressionDetector(const char *TransName, const char *Desc) 40 : Transformation(TransName, Desc), 41 CollectionVisitor(NULL), ControlVarNameQueryWrap(NULL), 42 TmpVarNameQueryWrap(NULL), TheFunc(NULL), TheStmt(NULL), TheExpr(NULL), 43 PrintedVarNamePrefix("__creduce_printed_"), 44 CheckedVarNamePrefix("__creduce_checked_"), ControlVarNamePrefix(""), 45 TmpVarNamePrefix("__creduce_expr_tmp_") 46 { } 47 48 ~ExpressionDetector(void); 49 50 private: 51 struct HeaderFunctionInfo { HeaderFunctionInfoHeaderFunctionInfo52 HeaderFunctionInfo () : HasHeader(false), HasFunction(false) { } 53 54 bool HasHeader; 55 bool HasFunction; 56 clang::SourceLocation HeaderLoc; 57 clang::SourceLocation FunctionLoc; 58 std::string HeaderName; 59 std::string FunctionName; 60 std::string FunctionDeclStr; 61 }; 62 63 typedef std::vector<const clang::Expr *> ExprVector; 64 65 typedef std::map<const clang::Stmt *, ExprVector> StmtToExprMap; 66 67 typedef std::map<const clang::Stmt *, 68 llvm::SmallPtrSet<const clang::Expr *, 10> > StmtToExprSetMap; 69 70 typedef std::map<const clang::Stmt *, 71 llvm::SmallVector<const clang::VarDecl *, 4> > StmtToVarVecMap; 72 73 typedef llvm::DenseMap<const clang::VarDecl *, const clang::Expr *> 74 VarToExprMap; 75 76 virtual void Initialize(clang::ASTContext &context); 77 78 virtual bool HandleTopLevelDecl(clang::DeclGroupRef D); 79 80 virtual void HandleTranslationUnit(clang::ASTContext &Ctx); 81 82 void addOneTempVar(const clang::VarDecl *VD); 83 84 bool refToTmpVar(const clang::NamedDecl *ND); 85 86 bool isValidExpr(clang::Stmt *S, const clang::Expr *E); 87 88 void doRewrite(); 89 90 bool shouldAddFunctionDecl(clang::SourceLocation Loc); 91 92 bool isIdenticalExpr(const clang::Expr *E1, const clang::Expr *E2); 93 94 bool hasIdenticalExpr( 95 const llvm::SmallVector<const clang::VarDecl *, 4> &TmpVars, 96 const clang::Expr *E); 97 98 StmtToExprMap UniqueExprs; 99 100 VarToExprMap ProcessedExprs; 101 102 StmtToExprSetMap InvalidExprsInUOBO; 103 104 StmtToVarVecMap TmpVarsInStmt; 105 106 ExprDetectorCollectionVisitor *CollectionVisitor; 107 108 TransNameQueryWrap *ControlVarNameQueryWrap; 109 110 TransNameQueryWrap *TmpVarNameQueryWrap; 111 112 clang::FunctionDecl *TheFunc; 113 114 clang::Stmt *TheStmt; 115 116 clang::Expr *TheExpr; 117 118 std::string PrintedVarNamePrefix; 119 120 std::string CheckedVarNamePrefix; 121 122 std::string ControlVarNamePrefix; 123 124 std::string TmpVarNamePrefix; 125 126 HeaderFunctionInfo HFInfo; 127 128 // Unimplemented 129 ExpressionDetector(void); 130 131 ExpressionDetector(const ExpressionDetector &); 132 133 void operator=(const ExpressionDetector &); 134 }; 135 #endif // EXPRESSION_DETECTOR_H 136