"...resnet50_tensorflow.git" did not exist on "6ff62233cbecf322a108291f6980e69df5a5869d"
Commit 5b8c89d0 authored by Paul's avatar Paul
Browse files

Format

parent 8f1b688a
import cppcheck, itertools import cppcheck, itertools
from cppcheckdata import simpleMatch, match from cppcheckdata import simpleMatch, match
def skipTokenMatches(tokens, skip=None): def skipTokenMatches(tokens, skip=None):
for tok in tokens: for tok in tokens:
if match(tok, skip): if match(tok, skip):
continue continue
yield tok yield tok
def isTokensEqual(xtokens, ytokens, skip=None): def isTokensEqual(xtokens, ytokens, skip=None):
for x, y in itertools.zip_longest(skipTokenMatches(xtokens, skip), skipTokenMatches(ytokens, skip)): for x, y in itertools.zip_longest(skipTokenMatches(xtokens, skip),
skipTokenMatches(ytokens, skip)):
if not x: if not x:
return False return False
if not y: if not y:
...@@ -17,6 +20,7 @@ def isTokensEqual(xtokens, ytokens, skip=None): ...@@ -17,6 +20,7 @@ def isTokensEqual(xtokens, ytokens, skip=None):
return False return False
return True return True
def getInnerLink(token): def getInnerLink(token):
if not token: if not token:
return [] return []
...@@ -24,6 +28,7 @@ def getInnerLink(token): ...@@ -24,6 +28,7 @@ def getInnerLink(token):
return [] return []
return token.next.forward(token.link) return token.next.forward(token.link)
def getVariableDecl(var): def getVariableDecl(var):
if not var: if not var:
return [] return []
...@@ -32,6 +37,7 @@ def getVariableDecl(var): ...@@ -32,6 +37,7 @@ def getVariableDecl(var):
end = end.next end = end.next
return var.typeStartToken.forward(end) return var.typeStartToken.forward(end)
@cppcheck.checker @cppcheck.checker
def AvoidBranchingStatementAsLastInLoop(cfg, data): def AvoidBranchingStatementAsLastInLoop(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -44,7 +50,11 @@ def AvoidBranchingStatementAsLastInLoop(cfg, data): ...@@ -44,7 +50,11 @@ def AvoidBranchingStatementAsLastInLoop(cfg, data):
if not match(stmt, "break|continue"): if not match(stmt, "break|continue"):
stmt = stmt.astTop() stmt = stmt.astTop()
if match(stmt, "break|continue|return"): if match(stmt, "break|continue|return"):
cppcheck.reportError(stmt, "style", "Branching statement as the last statement inside a loop is very confusing.") cppcheck.reportError(
stmt, "style",
"Branching statement as the last statement inside a loop is very confusing."
)
# @cppcheck.checker # @cppcheck.checker
# def CollapsibleIfStatements(cfg, data): # def CollapsibleIfStatements(cfg, data):
...@@ -53,12 +63,15 @@ def AvoidBranchingStatementAsLastInLoop(cfg, data): ...@@ -53,12 +63,15 @@ def AvoidBranchingStatementAsLastInLoop(cfg, data):
# continue # continue
# cppcheck.reportError(token, "style", "These two if statements can be collapsed into one.") # cppcheck.reportError(token, "style", "These two if statements can be collapsed into one.")
@cppcheck.checker @cppcheck.checker
def ConditionalAssert(cfg, data): def ConditionalAssert(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "if (*) { assert (*) ; }"): if not match(token, "if (*) { assert (*) ; }"):
continue continue
cppcheck.reportError(token, "style", "The if condition should be included in assert.") cppcheck.reportError(token, "style",
"The if condition should be included in assert.")
@cppcheck.checker @cppcheck.checker
def EmptyCatchStatement(cfg, data): def EmptyCatchStatement(cfg, data):
...@@ -67,6 +80,7 @@ def EmptyCatchStatement(cfg, data): ...@@ -67,6 +80,7 @@ def EmptyCatchStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "An empty catch statement.") cppcheck.reportError(token, "style", "An empty catch statement.")
@cppcheck.checker @cppcheck.checker
def EmptyDoWhileStatement(cfg, data): def EmptyDoWhileStatement(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -74,12 +88,15 @@ def EmptyDoWhileStatement(cfg, data): ...@@ -74,12 +88,15 @@ def EmptyDoWhileStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Empty do-while.") cppcheck.reportError(token, "style", "Empty do-while.")
@cppcheck.checker @cppcheck.checker
def EmptyElseBlock(cfg, data): def EmptyElseBlock(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "else { }"): if not match(token, "else { }"):
continue continue
cppcheck.reportError(token, "style", "Empty else statement can be safely removed.") cppcheck.reportError(token, "style",
"Empty else statement can be safely removed.")
@cppcheck.checker @cppcheck.checker
def EmptyForStatement(cfg, data): def EmptyForStatement(cfg, data):
...@@ -88,6 +105,7 @@ def EmptyForStatement(cfg, data): ...@@ -88,6 +105,7 @@ def EmptyForStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Empty for statement.") cppcheck.reportError(token, "style", "Empty for statement.")
@cppcheck.checker @cppcheck.checker
def EmptyIfStatement(cfg, data): def EmptyIfStatement(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -95,6 +113,7 @@ def EmptyIfStatement(cfg, data): ...@@ -95,6 +113,7 @@ def EmptyIfStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Empty if statement.") cppcheck.reportError(token, "style", "Empty if statement.")
@cppcheck.checker @cppcheck.checker
def EmptySwitchStatement(cfg, data): def EmptySwitchStatement(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -102,6 +121,7 @@ def EmptySwitchStatement(cfg, data): ...@@ -102,6 +121,7 @@ def EmptySwitchStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Empty switch statement.") cppcheck.reportError(token, "style", "Empty switch statement.")
@cppcheck.checker @cppcheck.checker
def EmptyWhileStatement(cfg, data): def EmptyWhileStatement(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -109,6 +129,7 @@ def EmptyWhileStatement(cfg, data): ...@@ -109,6 +129,7 @@ def EmptyWhileStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Empty while statement.") cppcheck.reportError(token, "style", "Empty while statement.")
@cppcheck.checker @cppcheck.checker
def ForLoopShouldBeWhileLoop(cfg, data): def ForLoopShouldBeWhileLoop(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -120,7 +141,9 @@ def ForLoopShouldBeWhileLoop(cfg, data): ...@@ -120,7 +141,9 @@ def ForLoopShouldBeWhileLoop(cfg, data):
end = token.next.link end = token.next.link
if not match(end.tokAt(-1), "; )"): if not match(end.tokAt(-1), "; )"):
continue continue
cppcheck.reportError(token, "style", "For loop should be written as a while loop.") cppcheck.reportError(token, "style",
"For loop should be written as a while loop.")
@cppcheck.checker @cppcheck.checker
def GotoStatement(cfg, data): def GotoStatement(cfg, data):
...@@ -129,6 +152,7 @@ def GotoStatement(cfg, data): ...@@ -129,6 +152,7 @@ def GotoStatement(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Goto considered harmful.") cppcheck.reportError(token, "style", "Goto considered harmful.")
# @cppcheck.checker # @cppcheck.checker
# def InvertedLogic(cfg, data): # def InvertedLogic(cfg, data):
# for token in cfg.tokenlist: # for token in cfg.tokenlist:
...@@ -141,12 +165,16 @@ def GotoStatement(cfg, data): ...@@ -141,12 +165,16 @@ def GotoStatement(cfg, data):
# continue # continue
# cppcheck.reportError(cond, "style", "It is cleaner to invert the logic.") # cppcheck.reportError(cond, "style", "It is cleaner to invert the logic.")
@cppcheck.checker @cppcheck.checker
def LambdaAttribute(cfg, data): def LambdaAttribute(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "] __device__|__host__ {|{}}"): if not match(token, "] __device__|__host__ {|{}}"):
continue continue
cppcheck.reportError(token, "style", "Attributes to lambdas must come after parameter list.") cppcheck.reportError(
token, "style",
"Attributes to lambdas must come after parameter list.")
@cppcheck.checker @cppcheck.checker
def MultipleUnaryOperator(cfg, data): def MultipleUnaryOperator(cfg, data):
...@@ -159,14 +187,18 @@ def MultipleUnaryOperator(cfg, data): ...@@ -159,14 +187,18 @@ def MultipleUnaryOperator(cfg, data):
continue continue
if not token.astOperand1.isUnaryOp(token.astOperand1.str): if not token.astOperand1.isUnaryOp(token.astOperand1.str):
continue continue
cppcheck.reportError(token, "style", "Muliple unary operators used together.") cppcheck.reportError(token, "style",
"Muliple unary operators used together.")
@cppcheck.checker @cppcheck.checker
def MutableVariable(cfg, data): def MutableVariable(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "mutable %var%"): if not match(token, "mutable %var%"):
continue continue
cppcheck.reportError(token, "style", "Do not create mutable variables.") cppcheck.reportError(token, "style",
"Do not create mutable variables.")
@cppcheck.checker @cppcheck.checker
def NestedBlocks(cfg, data): def NestedBlocks(cfg, data):
...@@ -178,46 +210,62 @@ def NestedBlocks(cfg, data): ...@@ -178,46 +210,62 @@ def NestedBlocks(cfg, data):
continue continue
cppcheck.reportError(block, "style", "Block directly inside block.") cppcheck.reportError(block, "style", "Block directly inside block.")
@cppcheck.checker @cppcheck.checker
def RedundantCast(cfg, data): def RedundantCast(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
m = match(token, "%var%@decl ; %var%@assign = static_cast <*>@cast (*) ;") m = match(token,
"%var%@decl ; %var%@assign = static_cast <*>@cast (*) ;")
if not m: if not m:
continue continue
if m.decl.varId != m.assign.varId: if m.decl.varId != m.assign.varId:
continue continue
if not match(token.previous, "auto"): if not match(token.previous, "auto"):
if not isTokensEqual(getVariableDecl(m.decl.variable), getInnerLink(m.cast), skip='const|volatile|&|&&|*'): if not isTokensEqual(getVariableDecl(m.decl.variable),
getInnerLink(m.cast),
skip='const|volatile|&|&&|*'):
continue continue
if not match(token, "%var%@decl ; %var%@assign = static_cast <*>@cast (*) ;"): if not match(token,
"%var%@decl ; %var%@assign = static_cast <*>@cast (*) ;"):
continue continue
cppcheck.reportError(token, "style", "Static cast is redundant.") cppcheck.reportError(token, "style", "Static cast is redundant.")
@cppcheck.checker @cppcheck.checker
def RedundantConditionalOperator(cfg, data): def RedundantConditionalOperator(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "? true|false : true|false"): if not match(token, "? true|false : true|false"):
continue continue
cppcheck.reportError(token, "style", "Conditional operator is redundant.") cppcheck.reportError(token, "style",
"Conditional operator is redundant.")
@cppcheck.checker @cppcheck.checker
def RedundantIfStatement(cfg, data): def RedundantIfStatement(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "if (*) { return true|false ; } else { return true|false ; }"): if not match(
token,
"if (*) { return true|false ; } else { return true|false ; }"):
continue continue
cppcheck.reportError(token, "style", "The if statement is redundant.") cppcheck.reportError(token, "style", "The if statement is redundant.")
@cppcheck.checker @cppcheck.checker
def RedundantLocalVariable(cfg, data): def RedundantLocalVariable(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
m = match(token, "%var%@decl ; %var%@assign = **; return %var%@returned ;") m = match(token,
"%var%@decl ; %var%@assign = **; return %var%@returned ;")
if not m: if not m:
continue continue
if m.decl.varId != m.assign.varId: if m.decl.varId != m.assign.varId:
continue continue
if m.decl.varId != m.returned.varId: if m.decl.varId != m.returned.varId:
continue continue
cppcheck.reportError(m.returned, "style", "Variable is returned immediately after its declaration, can be simplified to just return expression.") cppcheck.reportError(
m.returned, "style",
"Variable is returned immediately after its declaration, can be simplified to just return expression."
)
# @cppcheck.checker # @cppcheck.checker
# def UnnecessaryElseStatement(cfg, data): # def UnnecessaryElseStatement(cfg, data):
...@@ -234,6 +282,7 @@ def RedundantLocalVariable(cfg, data): ...@@ -234,6 +282,7 @@ def RedundantLocalVariable(cfg, data):
# continue # continue
# cppcheck.reportError(m.else_statement, "style", "Else statement is not necessary.") # cppcheck.reportError(m.else_statement, "style", "Else statement is not necessary.")
@cppcheck.checker @cppcheck.checker
def UnnecessaryEmptyCondition(cfg, data): def UnnecessaryEmptyCondition(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
...@@ -253,7 +302,10 @@ def UnnecessaryEmptyCondition(cfg, data): ...@@ -253,7 +302,10 @@ def UnnecessaryEmptyCondition(cfg, data):
container_iter = m.for_cond.astOperand2.astOperand2 container_iter = m.for_cond.astOperand2.astOperand2
if container_iter.varId != container.varId: if container_iter.varId != container.varId:
continue continue
cppcheck.reportError(container, "style", "Unnecessary check for empty before for range loop.") cppcheck.reportError(
container, "style",
"Unnecessary check for empty before for range loop.")
@cppcheck.checker @cppcheck.checker
def UseDeviceLaunch(cfg, data): def UseDeviceLaunch(cfg, data):
...@@ -262,19 +314,27 @@ def UseDeviceLaunch(cfg, data): ...@@ -262,19 +314,27 @@ def UseDeviceLaunch(cfg, data):
continue continue
cppcheck.reportError(token, "style", "Use device::launch instead.") cppcheck.reportError(token, "style", "Use device::launch instead.")
@cppcheck.checker @cppcheck.checker
def UseManagePointer(cfg, data): def UseManagePointer(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "fclose|free|hipFree|hipHostFree|hipFreeArray|hipMemFree|hipStreamDestroy|hipEventDestroy|hipArrayDestroy|hipCtxDestroy|hipDestroyTextureObject|hipDestroySurfaceObject|miirDestroyHandle ("): if not match(
token,
"fclose|free|hipFree|hipHostFree|hipFreeArray|hipMemFree|hipStreamDestroy|hipEventDestroy|hipArrayDestroy|hipCtxDestroy|hipDestroyTextureObject|hipDestroySurfaceObject|miirDestroyHandle ("
):
continue continue
cppcheck.reportError(token, "style", "Use manage pointer for resource management.") cppcheck.reportError(token, "style",
"Use manage pointer for resource management.")
@cppcheck.checker @cppcheck.checker
def UseSmartPointer(cfg, data): def UseSmartPointer(cfg, data):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if not match(token, "new %name%"): if not match(token, "new %name%"):
continue continue
cppcheck.reportError(token, "style", "Use manage pointer for resource management.") cppcheck.reportError(token, "style",
"Use manage pointer for resource management.")
@cppcheck.checker @cppcheck.checker
def useStlAlgorithms(cfg, data): def useStlAlgorithms(cfg, data):
...@@ -284,7 +344,7 @@ def useStlAlgorithms(cfg, data): ...@@ -284,7 +344,7 @@ def useStlAlgorithms(cfg, data):
elif match(token, "memset ("): elif match(token, "memset ("):
cppcheck.reportError(token, "style", "Use std::fill instead.") cppcheck.reportError(token, "style", "Use std::fill instead.")
elif match(token, "memcmp ("): elif match(token, "memcmp ("):
cppcheck.reportError(token, "style", "Use std::equal_range instead.") cppcheck.reportError(token, "style",
"Use std::equal_range instead.")
elif match(token, "memchr ("): elif match(token, "memchr ("):
cppcheck.reportError(token, "style", "Use std::find instead.") cppcheck.reportError(token, "style", "Use std::find instead.")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment