bool isInvertedLogic(Expr *condExpr) { BinaryOperator *binaryOperator = dyn_cast<BinaryOperator>(condExpr); UnaryOperator *unaryOperator = dyn_cast<UnaryOperator>(condExpr); return (binaryOperator && binaryOperator->getOpcode() == BO_NE) || (unaryOperator && unaryOperator->getOpcode() == UO_LNot); }
void FakeDirectiveHandler::HandleUnaryOperator(DeclRefExpr * Original, Expr * Var, Expr * Next, vector<LocalStmtPair> WritePairs, bool ActualVar, string Struct) { UnaryOperator * UO = dyn_cast<UnaryOperator>(Next); // Var++ etc. if (UO->isIncrementDecrementOp()) { InsertAccess(Var, Original, false, WritePairs, ActualVar, Struct); InsertAccess(Var, Original, true, WritePairs, ActualVar, Struct); // *Var } else if (UO->getOpcode() == clang::UO_Deref) { InsertAccess(Var, Original, false, WritePairs, ActualVar, Struct); ActualVar = true; } else { InsertAccess(Var, Original, false, WritePairs, ActualVar, Struct); ActualVar = false; } if (IsPointerType(Var)) { WalkUpExpr(Original, UO, WritePairs, ActualVar, Struct); } }
ValueDecl *valueDeclFromIncExpr(Expr *incExpr) { UnaryOperator *unaryOperator = dyn_cast_or_null<UnaryOperator>(incExpr); if (unaryOperator) { Expr *unaryOpSubExpr = unaryOperator->getSubExpr(); if (unaryOpSubExpr && isa<DeclRefExpr>(unaryOpSubExpr)) { DeclRefExpr *declRefExpr = dyn_cast<DeclRefExpr>(unaryOpSubExpr); return declRefExpr->getDecl(); } } return nullptr; }
bool isRedundantNilCheck(BinaryOperator *binaryOperator) { if(binaryOperator->getOpcode() == BO_LAnd && isNeNullCheck(binaryOperator->getLHS())) { UnaryOperator *unaryOperator = dyn_cast_or_null<UnaryOperator>(binaryOperator->getRHS()); if (unaryOperator) { return unaryOperator->getOpcode() != UO_LNot; } return true; } return false; }
int WalkAST::ReturnDefLevel(Expr *E){ int levelflag = 0; // 1 for *, -1 for & // deflevel++; E = E->IgnoreParenLValueCasts(); switch (E->getStmtClass()){ case Stmt::ArraySubscriptExprClass: { // ArraySubscriptExpr *AE = cast<ArraySubscriptExpr>(E->getLHS()); //UnaryOperator *U = cast<UnaryOperator>(E->getLHS()); //U->getSubExpr()->IgnoreParens()->dump(); E = (cast<ArraySubscriptExpr>(E))->getBase()->IgnoreParenCasts(); levelflag = 1; break; } case Stmt::UnaryOperatorClass: { UnaryOperator *UO = dyn_cast<UnaryOperator>(E); if (UO->getOpcode() == UO_Deref){ //E->dump(); levelflag = 1; } else if(UO->getOpcode() == UO_AddrOf){ //E->dump(); levelflag = -1; } E = (cast<UnaryOperator>(E))->getSubExpr()->IgnoreParens(); //E = ICE->getSubExpr(); break; } case Stmt::MemberExprClass: { E = (cast<MemberExpr>(E))->getBase()->IgnoreParenCasts(); levelflag = 1; break; } default: break; } if (DeclRefExpr *drE = dyn_cast<DeclRefExpr>(E->IgnoreParenLValueCasts())){ // std::cerr<<" at level "<<deflevel<<" of var "<<drE->getDecl()->getNameAsString()<<std::endl; std::cerr<< " of var "<<drE->getDecl()->getNameAsString() ; return levelflag; } //std::cerr<<"#### at level " << levelflag << std::endl; //E->dump(); return levelflag + WalkAST::ReturnDefLevel(E); }
string NetworkDriverRewriteVisitor::GetSharedStructStr(CallExpr *callExpr) { string shared_struct_str = ""; Expr *callee = callExpr->getCallee(); if (!isa<ImplicitCastExpr>(callee)) return shared_struct_str; ImplicitCastExpr *calleeImplExpr = cast<ImplicitCastExpr>(callee); if (!isa<DeclRefExpr>(calleeImplExpr->getSubExpr())) return shared_struct_str; DeclRefExpr *calleeDeclExpr = cast<DeclRefExpr>(calleeImplExpr->getSubExpr()); Stmt *body = callExpr->getCalleeDecl()->getBody(); if (calleeDeclExpr->getNameInfo().getAsString() != "alloc_etherdev") shared_struct_str = GetSharedStructStrInFunctionBody(body, false); if (calleeDeclExpr->getNameInfo().getAsString() != "alloc_etherdev") return shared_struct_str; for (auto i = callExpr->arg_begin(), e = callExpr->arg_end(); i != e; ++i) { if (!isa<ImplicitCastExpr>(*i)) continue; ImplicitCastExpr *argImplExpr = cast<ImplicitCastExpr>(*i); if (!isa<UnaryExprOrTypeTraitExpr>(argImplExpr->getSubExpr())) continue; UnaryExprOrTypeTraitExpr *argExpr = cast<UnaryExprOrTypeTraitExpr>(argImplExpr->getSubExpr()); ParenExpr *parenExpr = cast<ParenExpr>(argExpr->getArgumentExpr()); UnaryOperator *uop = cast<UnaryOperator>(parenExpr->getSubExpr()); ImplicitCastExpr *implExpr = cast<ImplicitCastExpr>(uop->getSubExpr()); DeclRefExpr *declExpr = cast<DeclRefExpr>(implExpr->getSubExpr()); ValueDecl *valueDecl = cast<ValueDecl>(declExpr->getDecl()); shared_struct_str = valueDecl->getType().getAsString(Context->getPrintingPolicy()); break; } return shared_struct_str; }
//--------------------------------------------------------- VarDecl* isFortranLoop(ForStmt* Node, const char*& errMsg) { // cond must be of form "x >/</!=/ expr": BinaryOperator* condCmp = dyn_cast_or_null<BinaryOperator>(Node->getCond()); if (condCmp == 0 || ((!condCmp->isRelationalOp()) && condCmp->getOpcode() != BO_NE)) { errMsg = "for-cond not fortran-like (must be x rel expr)"; return 0; } DeclRefExpr* condVar = dyn_cast<DeclRefExpr>(stripParenCasts(condCmp->getLHS())); if (condVar == 0 || !condVar->getType()->isIntegerType()) { errMsg = "no integer for-init variable"; return 0; } VarDecl* VD = dyn_cast<VarDecl>(condVar->getDecl()); if (VD == 0) { errMsg = "strange unrecognized lhs in for-condition"; return 0; } // inc must be of form "++x/x++": UnaryOperator* incStmt = dyn_cast_or_null<UnaryOperator>(Node->getInc()); if (incStmt == 0 || (!incStmt->isIncrementOp())) { errMsg = "for-inc not fortran-like (must be ++x/x++)"; return 0; } DeclRefExpr* incVar = dyn_cast<DeclRefExpr>(incStmt->getSubExpr()); if (incVar == 0 || incVar->getDecl() != VD) { errMsg = "for-inc doesn't refer to for-cond variable"; return 0; } return VD; }
/* this helper function is called when the traversal reaches a node of type Stmt */ void StmtHelper(Stmt *x){ //variable used for <cond> </cond> //bool condition = false; bool isElse = false; if(x != NULL){ string output = ""; //find current level and next level int intLevel = getLevelStmt(x); int intNextLevel = intLevel+1; //convert them both to strings to use for output string level; string nextLevel; stringstream ss; ss << intLevel; level = ss.str(); stringstream ss2; ss2 << intNextLevel; nextLevel = ss2.str(); const Stmt* parent = getStmtParent(x, Context); //PROBLEM if(x->getStmtClassName() != std::string("ForStmt") && isFlowControl(x, Context)){ //return; } //if the parent is calling any type of funciton then this node should be enclosed in <args> </args> string filename; if(callStackDebug && !callStack.empty()){ cerr << "stmt: call stack top: " << callStack.top()->getStmtClassName() << endl; } while(!callStack.empty() && numClosingArgsNeeded > 0 && !isParentStmt(parent, callStack.top()->getStmtClassName())){ if(debugPrint){ cerr << "adding args" << endl; } numClosingArgsNeeded--; output += "</args,1>\n"; callStack.pop(); if(callStackDebug){ cerr << "popping" << endl; printCallStack(); } } if(isParentStmtInCurFile(x,"CXXConstructExpr") && isParentStmt(x, "CXXConstructExpr")){ if(debugPrint){ cerr << "setting previousConstructorCall to true" << endl; } }else if(isParentStmtInCurFile(x,"CXXTemporaryObjectExpr") && isParentStmt(x, "CXXTemporaryObjectExpr")){ if(debugPrint){ cerr << "setting previousTempConstructorCallArg" << endl; } }else if(isParentStmt(x, "CallExpr")){ if(debugPrint){ cerr << "setting previousCallArgs to true" << endl; } }else if(isParentStmt(x, "CXXMemberCallExpr")){ if(debugPrint){ cerr << "setting previousMemberCallArgs to true" << endl; } } //if the parent is a variable declaration then this node should be encolsed in <decl> </decl> if(isParentStmt(x, "Var")){ previousRhsDecl = true; if(debugPrint){ cout << "setting prev var to true" << endl; } }else if(previousRhsDecl && numClosingVarsNeeded > 0){ //if the current node is not a child of a variable declaration //but the previous node was a child of a variable declation //then we know to print a </decl> output +="</variableDecl,1>\n"; numClosingVarsNeeded--; previousRhsDecl = false; } if(parent != NULL && strcmp(parent->getStmtClassName(), "IfStmt") == 0){ if(debugPrint){ cerr << "possibly an if statement" << endl; } //find the first child of the if statemt const Stmt* firstChild = NULL; auto children = parent->children(); for(const Stmt* child : children){ if(child != NULL){ firstChild = child; break; } } //if the first child is the current node, then we know it is part of the condition if(firstChild != NULL && x->getLocStart() == firstChild->getLocStart()){ if(debugPrint){ cerr << "part of the condition" << endl; } prevCondition = true; }else if(prevCondition){ output +="</cond,1>\n"; prevCondition = false; } //find if else const IfStmt* ifstmt = (IfStmt*) parent; const Stmt* elseStmt = ifstmt->getElse(); if(elseStmt != NULL){ if(debugPrint){ cout << "checking if " << x->getLocStart().printToString(Context->getSourceManager()); cout << " == " << elseStmt->getLocStart().printToString(Context->getSourceManager()); cout << " : " << (x->getLocStart() == elseStmt->getLocStart()) << endl; } if(x->getLocStart() == elseStmt->getLocStart()){ isElse = true; } } } string node = x->getStmtClassName(); if(node == "ReturnStmt"){ output += "<return"; }else if(node == "ForStmt"){ output += "<forLoop"; }else if(node == "WhileStmt"){ output += "<whileLoop"; }else if(node == "DoStmt"){ output += "<do"; }else if(node == "IfStmt"){ if(parent->getStmtClassName() != std::string("IfStmt")){ stringstream ssminus; ssminus << (intLevel-1); output += "<ifBlock," + ssminus.str() + ">\n"; intLevel += 1; stringstream ssif; ssif << intLevel; level = ssif.str(); } output += "<ifStatement"; }else if(node == "SwitchStmt"){ output += "<switch"; }else if(node == "CaseStmt"){ output += "<case"; }else if(node == "CXXMemberCallExpr"){ CXXMemberCallExpr* ce = (CXXMemberCallExpr*) x; Expr* obj = ce->getImplicitObjectArgument(); CallExpr* expr = (CallExpr*) x; output += "<object: "; QualType qt = obj->getType(); output += qt.getBaseTypeIdentifier()->getName().str(); output += "; calling func: "; output += expr->getDirectCallee()->getNameInfo().getAsString(); output += ", " + level + ">\n"; output += "<args"; numClosingArgsNeeded++; callStack.push(x); if(callStackDebug){ cerr << "pushing" << endl; printCallStack(); } }else if(node == "CallExpr"){ CallExpr* expr = (CallExpr*) x; output += "<calling func: "; output += expr->getDirectCallee()->getNameInfo().getAsString(); output += ", " + level + ">\n"; output += "<args"; numClosingArgsNeeded++; callStack.push(x); if(callStackDebug){ cerr << "pushing" << endl; printCallStack(); } }else if(node == "CXXConstructExpr"){ CXXConstructExpr* ce = (CXXConstructExpr*) x; //Decl* CD = ce->getConstructor(); string filename; //if(isInCurFile(Context, CD, filename)){ CXXMethodDecl* MD = ce->getConstructor(); output += "<calling func: "; output += MD->getNameInfo().getAsString(); output += "," + level + ">\n"; output += "<args"; numClosingArgsNeeded++; callStack.push(x); if(callStackDebug){ cerr << "pushing" << endl; printCallStack(); } //} }else if(node == "BinaryOperator"){ BinaryOperator* binaryOp = (BinaryOperator*) x; if(binaryOp->isAssignmentOp()){ output += "<assignment"; }else if(binaryOp->isComparisonOp()){ output += "<comparison"; }else{ output += "<binaryOp"; } }else if(node == "UnaryOperator"){ UnaryOperator* uo = (UnaryOperator*) x; string op = uo->getOpcodeStr(uo->getOpcode()).str(); if(op != "-"){ output += "<unaryOp"; } }else if(node == "CompoundAssignOperator"){ output += "<augAssign"; }else if(node == "CompoundStmt"){ if(isElse){ output += "<elseStatement"; }else{ output += "<compoundStmt"; } }else if(node == "CXXThrowExpr"){ output += "<raisingException"; }else if(node == "CXXTryStmt"){ output += "<try"; }else if(node == "CXXCatchStmt"){ output += "<except"; }else if(node == "CXXOperatorCallExpr"){ CXXOperatorCallExpr* ce = (CXXOperatorCallExpr*) x; if(ce->isAssignmentOp()){ output += "<assignment"; } }else if(node == "CXXTemporaryObjectExpr"){ CXXTemporaryObjectExpr* ce = (CXXTemporaryObjectExpr*) x; Decl* CD = ce->getConstructor(); string filename; if(isInCurFile(Context, CD, filename)){ CXXMethodDecl* MD = ce->getConstructor(); output += "<calling func: "; output += MD->getNameInfo().getAsString(); output += "," + level + ">\n"; output += "<args"; numClosingArgsNeeded++; callStack.push(x); if(callStackDebug){ cerr << "pushing" << endl; printCallStack(); } } }else if(node == "DeclRefExpr"){ if(parent != NULL && parent->getStmtClassName() == std::string("ImplicitCastExpr")){ DeclRefExpr* dr = (DeclRefExpr*) x; ValueDecl* d = (ValueDecl*) dr->getDecl(); //cout << d->getQualType().getAsString() << endl; if(d != NULL){ QualType qt = d->getType(); //cout << qt.getAsString() << endl; if(qt.getAsString() == "std::vector<int, class std::allocator<int> >::const_reference (std::vector::size_type) const noexcept"){ //string type = io->getName().str(); //cout << type << endl; //if(type == "vector"){ output += "<expr"; //} } } } }else{ if(allNodes){ output += "<"; output += node; output += ">"; } } if(output.size() != 0 && !endsIn(output, "</cond,1>\n") && !endsIn(output,"</variableDecl,1>\n") && !endsIn(output,"</args,1>\n") && !endsIn(output,">") && !endsIn(output, ">\n")){ output += ", " + level + ">"; cout << output << endl; output = ""; }else if(output.size() != 0){ cout << output << endl; output = ""; if(debugPrint){ cerr << "printing output" << endl; } } } }