Merge pull request #4222 from jbj/BlockStmt

C++/Java/JS: Rename Block -> BlockStmt
This commit is contained in:
Mathias Vorreiter Pedersen
2020-09-09 10:02:37 +02:00
committed by GitHub
75 changed files with 474 additions and 437 deletions

View File

@@ -27,7 +27,7 @@ predicate oneLineStatement(Stmt s, File f, int line, int col) {
col = l.getStartColumn()
) and
// Exclude blocks: `{break;}` is not really a violation.
not s instanceof Block and
not s instanceof BlockStmt and
// Exclude implicit super constructor invocations.
not s instanceof SuperConstructorInvocationStmt and
// Java enums are desugared to a whole bunch of generated statements.

View File

@@ -20,6 +20,6 @@ class ComplexStmt extends Stmt {
}
}
from Block b, int n
from BlockStmt b, int n
where n = count(ComplexStmt s | s = b.getAStmt()) and n > 3
select b, "Block with too many statements (" + n.toString() + " complex statements in the block)."

View File

@@ -51,9 +51,9 @@ class ImpureStmt extends Stmt {
/**
* Get any non-block stmt in the block, including those nested within blocks.
*/
private Stmt getANestedStmt(Block block) {
private Stmt getANestedStmt(BlockStmt block) {
// Any non-block statement
not result instanceof Block and result = block.getAStmt()
not result instanceof BlockStmt and result = block.getAStmt()
or
// Or any statement nested in a block
result = getANestedStmt(block.getAStmt())

View File

@@ -42,7 +42,7 @@ predicate hasTypeTest(Variable v) {
*/
class ReferenceEquals extends EqualsMethod {
ReferenceEquals() {
exists(Block b, ReturnStmt ret, EQExpr eq |
exists(BlockStmt b, ReturnStmt ret, EQExpr eq |
this.getBody() = b and
b.getStmt(0) = ret and
ret.getResult() = eq and

View File

@@ -13,7 +13,7 @@
import semmle.code.java.Statement
/** A block without statements or comments. */
private Block emptyBlock() {
private BlockStmt emptyBlock() {
result.getNumStmt() = 0 and
result.getLocation().getNumberOfCommentLines() = 0
}
@@ -48,8 +48,8 @@ predicate blockParent(Stmt empty, string msg) {
or
empty.getParent() instanceof LoopStmt and msg = "The body of a loop should not be empty."
or
empty.getParent() instanceof Block and
empty instanceof Block and
empty.getParent() instanceof BlockStmt and
empty instanceof BlockStmt and
msg = "This block should not be empty."
)
}

View File

@@ -15,11 +15,11 @@ import java
/**
* A control structure for which the trailing body (the syntactically last part)
* is not a `Block`. This is either an `IfStmt` or a `LoopStmt`, but not a
* is not a `BlockStmt`. This is either an `IfStmt` or a `LoopStmt`, but not a
* `DoStmt`, since do-while statements don't have a trailing body.
*/
predicate unbracedTrailingBody(Stmt ctrlStructure, Stmt trailingBody) {
not trailingBody instanceof Block and
not trailingBody instanceof BlockStmt and
(
exists(IfStmt c | c = ctrlStructure |
trailingBody = c.getElse() and not trailingBody instanceof IfStmt
@@ -33,15 +33,15 @@ predicate unbracedTrailingBody(Stmt ctrlStructure, Stmt trailingBody) {
/*
* The body of a `SwitchStmt` is a block, but it isn't represented explicitly
* in the AST as a `Block`, so we have to take it into account directly in the
* in the AST as a `BlockStmt`, so we have to take it into account directly in the
* following two predicates.
*/
/**
* Two consecutive statements in a `Block` statement or `SwitchStmt`.
* Two consecutive statements in a `BlockStmt` statement or `SwitchStmt`.
*/
Stmt nextInBlock(Stmt s) {
exists(Block b, int i |
exists(BlockStmt b, int i |
b.getStmt(i) = s and
b.getStmt(i + 1) = result
)
@@ -52,10 +52,10 @@ Stmt nextInBlock(Stmt s) {
)
}
/** The `Stmt.getParent()` relation restricted to not pass through `Block`s or `SwitchStmt`s. */
/** The `Stmt.getParent()` relation restricted to not pass through `BlockStmt`s or `SwitchStmt`s. */
Stmt nonBlockParent(Stmt s) {
result = s.getParent() and
not result instanceof Block and
not result instanceof BlockStmt and
not result instanceof SwitchStmt
}
@@ -64,7 +64,7 @@ predicate ifElseIf(IfStmt s, IfStmt elseif) { s.getElse() = elseif }
/**
* The statement `body` is an unbraced trailing body of a control structure and
* `succ` is the next statement in the surrounding `Block` (or `SwitchStmt`).
* `succ` is the next statement in the surrounding `BlockStmt` (or `SwitchStmt`).
*/
predicate shouldOutdent(
Stmt ctrl, Stmt body, Stmt succ, int bodycol, int succcol, int bodyline, int succline
@@ -79,7 +79,7 @@ predicate shouldOutdent(
/**
* The statement `body` is an unbraced trailing body of a control structure and
* `succ` is the next statement in the surrounding `Block` (or `SwitchStmt`).
* `succ` is the next statement in the surrounding `BlockStmt` (or `SwitchStmt`).
* The indentation of statement `succ` is suspect because it is indented
* the same way as `body` and thus visually suggests to be part of the same
* syntactic scope as `body`.

View File

@@ -25,7 +25,7 @@ class ComparisonOrEqTestExpr extends Expr {
class Empty extends Stmt {
Empty() {
this instanceof EmptyStmt or
this.(Block).getNumStmt() = 0
this.(BlockStmt).getNumStmt() = 0
}
}

View File

@@ -26,7 +26,7 @@ predicate skipParent(Stmt s) {
exists(Stmt parent | parent = s.getParent() |
s instanceof IfStmt and parent.(IfStmt).getElse() = s
or
parent instanceof Block
parent instanceof BlockStmt
)
}

View File

@@ -44,7 +44,7 @@ class PointlessLoop extends WhileStmt {
getCondition().(BooleanLiteral).getBooleanValue() = true and
// The only `break` must be the last statement.
forall(BreakStmt break | break.(JumpStmt).getTarget() = this |
this.getStmt().(Block).getLastStmt() = break
this.getStmt().(BlockStmt).getLastStmt() = break
) and
// No `continue` statements.
not exists(ContinueStmt continue | continue.(JumpStmt).getTarget() = this)

View File

@@ -16,5 +16,5 @@ import java
from FinalizeMethod finalize
where
finalize.fromSource() and
not exists(Stmt s | s.getEnclosingCallable() = finalize | not s instanceof Block)
not exists(Stmt s | s.getEnclosingCallable() = finalize | not s instanceof BlockStmt)
select finalize, "Empty finalize method."

View File

@@ -14,14 +14,14 @@
import java
Block finallyBlock() { exists(TryStmt try | try.getFinally() = result) }
BlockStmt finallyBlock() { exists(TryStmt try | try.getFinally() = result) }
Stmt statementIn(Block finally) {
Stmt statementIn(BlockStmt finally) {
finallyBlock() = finally and
result.getParent+() = finally
}
predicate banned(Stmt s, Block finally) {
predicate banned(Stmt s, BlockStmt finally) {
s = statementIn(finally) and
(
s instanceof ReturnStmt
@@ -32,6 +32,6 @@ predicate banned(Stmt s, Block finally) {
)
}
from Stmt s, Block finally
from Stmt s, BlockStmt finally
where banned(s, finally)
select s, "Leaving a finally-block with this statement can cause exceptions to silently disappear."

View File

@@ -77,7 +77,7 @@ private predicate blockCoversStatement(int equivClass, int first, int last, Stmt
private Stmt statementInMethod(Method m) {
result.getEnclosingCallable() = m and
not result instanceof Block
not result instanceof BlockStmt
}
private predicate duplicateStatement(Method m1, Method m2, Stmt s1, Stmt s2) {

View File

@@ -338,7 +338,7 @@ private module ControlFlowGraphImpl {
/** Holds if a call to `m` indicates that `m` is expected to return. */
private predicate expectedReturn(EffectivelyNonVirtualMethod m) {
exists(Stmt s, Block b |
exists(Stmt s, BlockStmt b |
m.getAnAccess().getEnclosingStmt() = s and
b.getAStmt() = s and
not b.getLastStmt() = s
@@ -352,7 +352,7 @@ private module ControlFlowGraphImpl {
result instanceof MethodExit
or
not result.isOverridable() and
exists(Block body |
exists(BlockStmt body |
body = result.getBody() and
not exists(ReturnStmt ret | ret.getEnclosingCallable() = result)
|
@@ -388,7 +388,7 @@ private module ControlFlowGraphImpl {
or
result.(ExprStmt).getExpr() = nonReturningMethodAccess()
or
result.(Block).getLastStmt() = nonReturningStmt()
result.(BlockStmt).getLastStmt() = nonReturningStmt()
or
exists(IfStmt ifstmt | ifstmt = result |
ifstmt.getThen() = nonReturningStmt() and
@@ -450,7 +450,7 @@ private module ControlFlowGraphImpl {
or
this instanceof SuperAccess
or
this.(Block).getNumStmt() = 0
this.(BlockStmt).getNumStmt() = 0
or
this instanceof SwitchCase and not this.(SwitchCase).isRule()
or
@@ -723,7 +723,7 @@ private module ControlFlowGraphImpl {
or
// The last statement in a block is any statement that does not complete normally,
// or the last statement.
exists(Block blk | blk = n |
exists(BlockStmt blk | blk = n |
last(blk.getAStmt(), last, completion) and completion != NormalCompletion()
or
last(blk.getStmt(blk.getNumStmt() - 1), last, completion)
@@ -943,9 +943,9 @@ private module ControlFlowGraphImpl {
)
or
// Statements within a block execute sequentially.
result = first(n.(Block).getStmt(0)) and completion = NormalCompletion()
result = first(n.(BlockStmt).getStmt(0)) and completion = NormalCompletion()
or
exists(Block blk, int i |
exists(BlockStmt blk, int i |
last(blk.getStmt(i), n, completion) and
completion = NormalCompletion() and
result = first(blk.getStmt(i + 1))

View File

@@ -1022,7 +1022,7 @@ class LambdaExpr extends FunctionalExpr, @lambdaexpr {
}
/** Gets the body of this lambda expression, if it is a statement. */
Block getStmtBody() { hasStmtBody() and result = asMethod().getBody() }
BlockStmt getStmtBody() { hasStmtBody() and result = asMethod().getBody() }
/** Gets a printable representation of this expression. */
override string toString() { result = "...->..." }

View File

@@ -215,7 +215,7 @@ class Callable extends StmtParent, Member, @callable {
Call getAReference() { result.getCallee() = this }
/** Gets the body of this callable, if any. */
Block getBody() { result.getParent() = this }
BlockStmt getBody() { result.getParent() = this }
/**
* Gets the source declaration of this callable.

View File

@@ -469,7 +469,7 @@ private class PpWildcardTypeAccess extends PpAst, WildcardTypeAccess {
* Statements
*/
private class PpBlock extends PpAst, Block {
private class PpBlock extends PpAst, BlockStmt {
override string getPart(int i) {
i = 0 and result = "{"
or
@@ -493,26 +493,26 @@ private class PpIfStmt extends PpAst, IfStmt {
or
i = 2 and result = ")"
or
i = 3 and result = " " and this.getThen() instanceof Block
i = 3 and result = " " and this.getThen() instanceof BlockStmt
or
exists(this.getElse()) and
(
i = 5 and result = " " and this.getThen() instanceof Block
i = 5 and result = " " and this.getThen() instanceof BlockStmt
or
i = 6 and result = "else"
or
i = 7 and result = " " and this.getElse() instanceof Block
i = 7 and result = " " and this.getElse() instanceof BlockStmt
)
}
override predicate newline(int i) {
i = 3 and not this.getThen() instanceof Block
i = 3 and not this.getThen() instanceof BlockStmt
or
exists(this.getElse()) and
(
i = 5 and not this.getThen() instanceof Block
i = 5 and not this.getThen() instanceof BlockStmt
or
i = 7 and not this.getElse() instanceof Block
i = 7 and not this.getElse() instanceof BlockStmt
)
}
@@ -525,9 +525,9 @@ private class PpIfStmt extends PpAst, IfStmt {
}
override predicate indents(int i) {
i = 4 and not this.getThen() instanceof Block
i = 4 and not this.getThen() instanceof BlockStmt
or
i = 8 and not this.getElse() instanceof Block
i = 8 and not this.getElse() instanceof BlockStmt
}
}
@@ -549,7 +549,7 @@ private class PpForStmt extends PpAst, ForStmt {
or
i = 1 + lastUpdateIndex() and result = ")"
or
i = 2 + lastUpdateIndex() and result = " " and this.getStmt() instanceof Block
i = 2 + lastUpdateIndex() and result = " " and this.getStmt() instanceof BlockStmt
}
private int lastInitIndex() { result = 3 + 2 * max(int j | exists(this.getInit(j))) }
@@ -559,7 +559,7 @@ private class PpForStmt extends PpAst, ForStmt {
}
override predicate newline(int i) {
i = 2 + lastUpdateIndex() and not this.getStmt() instanceof Block
i = 2 + lastUpdateIndex() and not this.getStmt() instanceof BlockStmt
}
override PpAst getChild(int i) {
@@ -575,7 +575,7 @@ private class PpForStmt extends PpAst, ForStmt {
}
override predicate indents(int i) {
i = 3 + lastUpdateIndex() and not this.getStmt() instanceof Block
i = 3 + lastUpdateIndex() and not this.getStmt() instanceof BlockStmt
}
}
@@ -588,7 +588,7 @@ private class PpEnhancedForStmt extends PpAst, EnhancedForStmt {
i = 4 and result = " : "
or
i = 6 and
if this.getStmt() instanceof Block then result = ") " else result = ")"
if this.getStmt() instanceof BlockStmt then result = ") " else result = ")"
}
override PpAst getChild(int i) {
@@ -601,7 +601,7 @@ private class PpEnhancedForStmt extends PpAst, EnhancedForStmt {
i = 7 and result = this.getStmt()
}
override predicate indents(int i) { i = 7 and not this.getStmt() instanceof Block }
override predicate indents(int i) { i = 7 and not this.getStmt() instanceof BlockStmt }
}
private class PpWhileStmt extends PpAst, WhileStmt {
@@ -610,10 +610,10 @@ private class PpWhileStmt extends PpAst, WhileStmt {
or
i = 2 and result = ")"
or
i = 3 and result = " " and this.getStmt() instanceof Block
i = 3 and result = " " and this.getStmt() instanceof BlockStmt
}
override predicate newline(int i) { i = 3 and not this.getStmt() instanceof Block }
override predicate newline(int i) { i = 3 and not this.getStmt() instanceof BlockStmt }
override PpAst getChild(int i) {
i = 1 and result = this.getCondition()
@@ -621,21 +621,21 @@ private class PpWhileStmt extends PpAst, WhileStmt {
i = 4 and result = this.getStmt()
}
override predicate indents(int i) { i = 4 and not this.getStmt() instanceof Block }
override predicate indents(int i) { i = 4 and not this.getStmt() instanceof BlockStmt }
}
private class PpDoStmt extends PpAst, DoStmt {
override string getPart(int i) {
i = 0 and result = "do"
or
i in [1, 3] and result = " " and this.getStmt() instanceof Block
i in [1, 3] and result = " " and this.getStmt() instanceof BlockStmt
or
i = 4 and result = "while ("
or
i = 6 and result = ");"
}
override predicate newline(int i) { i in [1, 3] and not this.getStmt() instanceof Block }
override predicate newline(int i) { i in [1, 3] and not this.getStmt() instanceof BlockStmt }
override PpAst getChild(int i) {
i = 2 and result = this.getStmt()
@@ -643,7 +643,7 @@ private class PpDoStmt extends PpAst, DoStmt {
i = 5 and result = this.getCondition()
}
override predicate indents(int i) { i = 2 and not this.getStmt() instanceof Block }
override predicate indents(int i) { i = 2 and not this.getStmt() instanceof BlockStmt }
}
private class PpTryStmt extends PpAst, TryStmt {

View File

@@ -60,7 +60,7 @@ class Stmt extends StmtParent, ExprParent, @stmt {
class StmtParent extends @stmtparent, Top { }
/** A block of statements. */
class Block extends Stmt, @block {
class BlockStmt extends Stmt, @block {
/** Gets a statement that is an immediate child of this block. */
Stmt getAStmt() { result.getParent() = this }
@@ -77,11 +77,17 @@ class Block extends Stmt, @block {
override string pp() { result = "{ ... }" }
/** This statement's Halstead ID (used to compute Halstead metrics). */
override string getHalsteadID() { result = "Block" }
override string getHalsteadID() { result = "BlockStmt" }
}
/**
* DEPRECATED: This is now called `BlockStmt` to avoid confusion with
* `BasicBlock`.
*/
deprecated class Block = BlockStmt;
/** A block with only a single statement. */
class SingletonBlock extends Block {
class SingletonBlock extends BlockStmt {
SingletonBlock() { this.getNumStmt() = 1 }
/** Gets the single statement in this block. */
@@ -304,7 +310,7 @@ class TryStmt extends Stmt, @trystmt {
}
/** Gets the `finally` block, if any. */
Block getFinally() { result.isNthChildOf(this, -2) }
BlockStmt getFinally() { result.isNthChildOf(this, -2) }
/** Gets a resource variable declaration, if any. */
LocalVariableDeclStmt getAResourceDecl() { result.getParent() = this and result.getIndex() <= -3 }
@@ -348,7 +354,7 @@ class TryStmt extends Stmt, @trystmt {
/** A `catch` clause in a `try` statement. */
class CatchClause extends Stmt, @catchclause {
/** Gets the block of this `catch` clause. */
Block getBlock() { result.getParent() = this }
BlockStmt getBlock() { result.getParent() = this }
/** Gets the `try` statement in which this `catch` clause occurs. */
TryStmt getTry() { this = result.getACatchClause() }

View File

@@ -14,7 +14,7 @@ private predicate flowEntry(Stmt entry) {
exists(Callable c | entry = c.getBody())
or
// This disjunct is technically superfluous, but safeguards against extractor problems.
entry instanceof Block and
entry instanceof BlockStmt and
not exists(entry.getEnclosingCallable()) and
not entry.getParent() instanceof Stmt
}

View File

@@ -293,7 +293,7 @@ private predicate impossibleEdge(BasicBlock bb1, BasicBlock bb2) {
/** A control flow edge that leaves a finally-block. */
private predicate leavingFinally(BasicBlock bb1, BasicBlock bb2, boolean normaledge) {
exists(TryStmt try, Block finally |
exists(TryStmt try, BlockStmt finally |
try.getFinally() = finally and
bb1.getABBSuccessor() = bb2 and
bb1.getEnclosingStmt().getEnclosingStmt*() = finally and

View File

@@ -93,8 +93,8 @@ class SuppressedConstructor extends Constructor {
not isDefaultConstructor() and
// Verify that there is only one statement, which is the `super()` call. This exists
// even for empty constructors.
getBody().(Block).getNumStmt() = 1 and
getBody().(Block).getAStmt().(SuperConstructorInvocationStmt).getNumArgument() = 0 and
getBody().(BlockStmt).getNumStmt() = 1 and
getBody().(BlockStmt).getAStmt().(SuperConstructorInvocationStmt).getNumArgument() = 0 and
// A constructor that is called is not acting to suppress the default constructor. We permit
// calls from suppressed and default constructors - in both cases, they can only come from
// sub-class constructors.