bool VisitSwitchStmt(SwitchStmt *switchStmt) { CompoundStmt *compoundStmt = dyn_cast<CompoundStmt>(switchStmt->getBody()); if (compoundStmt) { bool breakFound = true; for (CompoundStmt::body_iterator body = compoundStmt->body_begin(), bodyEnd = compoundStmt->body_end(); body != bodyEnd; body++) { Stmt *bodyStmt = dyn_cast<Stmt>(*body); if (isBreakingPoint(bodyStmt)) { breakFound = true; continue; } if (isSwitchCase(bodyStmt)) { if (!breakFound) { addViolation(switchStmt, this); break; } FindingBreak findingBreak; breakFound = findingBreak.findBreak(dyn_cast<SwitchCase>(bodyStmt)); } } if (!breakFound) { addViolation(switchStmt, this); } } return true; }
void AbstractASTRuleBase::addViolation(const clang::Stmt *stmt, RuleBase *rule, const std::string& message) { if (stmt && !shouldSuppress(stmt, *_carrier->getASTContext(), rule)) { addViolation(stmt->getLocStart(), stmt->getLocEnd(), rule, message); } }
void AbstractASTRuleBase::addViolation(const clang::Decl *decl, RuleBase *rule, const std::string& message) { if (decl && !shouldSuppress(decl, *_carrier->getASTContext(), rule)) { addViolation(decl->getLocStart(), decl->getLocEnd(), rule, message); } }
bool VisitVarDecl(VarDecl *varDecl) { if (varDecl && !varDecl->isUsed() && varDecl->isLocalVarDecl() && !varDecl->isStaticDataMember()) { addViolation(varDecl, this); } return true; }
bool VisitIfStmt(IfStmt *ifStmt) { if (ifStmt->getElse() && isInvertedLogic(ifStmt->getCond())) { addViolation(ifStmt->getCond(), this); } return true; }
bool VisitConditionalOperator(ConditionalOperator *conditionalOperator) { if (isInvertedLogic(conditionalOperator->getCond())) { addViolation(conditionalOperator->getCond(), this); } return true; }
virtual void eachLine(int lineNumber, string line) { int threshold = RuleConfiguration::intForKey("LONG_LINE", 100); int currentLineSize = line.size(); if (currentLineSize > threshold) { string description = "Line with " + intToString(currentLineSize) + " characters exceeds limit of " + intToString(threshold); addViolation(lineNumber, 1, lineNumber, currentLineSize, this, description); } }
bool VisitBinaryOperator(BinaryOperator *binaryOperator) { Expr *leftExpr = binaryOperator->getLHS(); Expr *rightExpr = binaryOperator->getRHS(); if (binaryOperator->getOpcode() == BO_EQ && isRemainderEqualsOne(leftExpr, rightExpr)) { addViolation(binaryOperator, this); } return true; }
bool VisitCXXRecordDecl(CXXRecordDecl *decl) { int fieldCount = distance(decl->field_begin(), decl->field_end()); if (fieldCount > _threshold) { string description = "C++ class with " + toString<int>(fieldCount) + " fields exceeds limit of " + toString<int>(_threshold); addViolation(decl, this, description); } return true; }
bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *decl) { int fieldCount = decl->ivar_size(); if (fieldCount > _threshold) { string description = "Objective-C interface with " + toString<int>(fieldCount) + " fields exceeds limit of " + toString<int>(_threshold); addViolation(decl, this, description); } return true; }
bool VisitCXXRecordDecl(CXXRecordDecl *decl) { int length = getNumberOfLines(decl->getSourceRange()); if (length > _threshold) { string description = "C++ class with " + intToString(length) + " lines exceeds limit of " + intToString(_threshold); addViolation(decl, this, description); } return true; }
bool VisitObjCImplDecl(ObjCImplDecl *decl) { int length = getNumberOfLines(decl->getSourceRange()); if (length > _threshold) { string description = "Objective-C implementation with " + intToString(length) + " lines exceeds limit of " + intToString(_threshold); addViolation(decl, this, description); } return true; }
void applyDecl(T *decl) { unsigned numOfParams = decl->param_size(); if (!markedAsSuppress(decl, this) && decl->hasBody() && numOfParams > _threshold) { string description = "Method with " + toString<int>(numOfParams) + " parameters exceeds limit of " + toString<int>(_threshold); addViolation(decl, this, description); } }
bool VisitCompoundStmt(CompoundStmt *compoundStmt) { int depth = getStmtDepth(compoundStmt); int threshold = RuleConfiguration::intForKey("NESTED_BLOCK_DEPTH", 5); if (depth > threshold) { string description = "Block depth of " + toString<int>(depth) + " exceeds limit of " + toString<int>(threshold); addViolation(compoundStmt, this, description); } return true; }
bool VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *finallyStmt) { vector<ObjCAtThrowStmt*> *throws = new vector<ObjCAtThrowStmt*>(); ExtractObjCAtThrowStmts extractThrowStmts; extractThrowStmts.extract(finallyStmt, throws); for (int index = 0; index < throws->size(); index++) { ObjCAtThrowStmt *throwStmt = throws->at(index); addViolation(throwStmt, this); } vector<ObjCMessageExpr*> *exceptionRaisers = new vector<ObjCMessageExpr*>(); ExtractNSExceptionRaiser extractExceptions; extractExceptions.extract(finallyStmt, exceptionRaisers); for (int index = 0; index < exceptionRaisers->size(); index++) { ObjCMessageExpr *raiseExpr = exceptionRaisers->at(index); addViolation(raiseExpr, this); } return true; }
bool VisitParmVarDecl(ParmVarDecl *varDecl) { if (markedAsSuppress(varDecl, this)) { return true; } if (!varDecl->isUsed() && hasVariableName(varDecl) && !isExistingByContract(varDecl)) { addViolation(varDecl, this); } return true; }
bool VisitObjCMessageExpr(ObjCMessageExpr *objCMsgExpr) { Expr *receiverExpr = objCMsgExpr->getInstanceReceiver(); string selectorString = objCMsgExpr->getSelector().getAsString(); if (receiverExpr && isa<ImplicitCastExpr>(receiverExpr) && (isArrayGetSelector(receiverExpr, selectorString) || isArraySetSelector(receiverExpr, selectorString) || isDictionaryGetSelector(receiverExpr, selectorString) || isDictionarySetSelector(receiverExpr, selectorString))) { addViolation(objCMsgExpr, this); } return true; }
bool VisitSwitchStmt(SwitchStmt *switchStmt) { // SwitchCaseList has a linked data structure in the reversed order SwitchCase *currentSwitchCase = switchStmt->getSwitchCaseList(); while (currentSwitchCase && isa<CaseStmt>(currentSwitchCase)) { SwitchCase *nextSwitchCase = currentSwitchCase->getNextSwitchCase(); if (nextSwitchCase && isa<DefaultStmt>(nextSwitchCase)) { addViolation(nextSwitchCase, this); } currentSwitchCase = nextSwitchCase; } return true; }
void applyDecl(Decl *decl) { if (markedAsSuppress(decl, this)) { return; } int ccn = getCyclomaticComplexity(decl); // In McBABE, 1976, A Complexity Measure, he suggested a reasonable number of 10 int threshold = RuleConfiguration::intForKey("CYCLOMATIC_COMPLEXITY", 10); if (ccn > threshold) { string description = "Cyclomatic Complexity Number " + toString<int>(ccn) + " exceeds limit of " + toString<int>(threshold); addViolation(decl, this, description); } }
bool VisitUnaryOperator(UnaryOperator *unaryOperator) { Expr *subExpr = unaryOperator->getSubExpr(); while (isa<ParenExpr>(subExpr)) { ParenExpr *parenExpr = dyn_cast<ParenExpr>(subExpr); subExpr = parenExpr->getSubExpr(); } if (isa<UnaryOperator>(subExpr)) { UnaryOperator *subUnaryOperator = dyn_cast<UnaryOperator>(subExpr); if (bothUOLNot(unaryOperator, subUnaryOperator) || bothUONot(unaryOperator, subUnaryOperator)) { addViolation(unaryOperator, this); } } return true; }