Skip to content

Commit f73960d

Browse files
authored
Merge pull request github#5788 from Marcono1234/marcono1234/stmt-toString
Java: Override toString() for statements
2 parents 60377a8 + 6003b6e commit f73960d

File tree

73 files changed

+2940
-2942
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+2940
-2942
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* The CodeQL predicate `toString()` has been overridden for subclasses of `Stmt` to be more descriptive.

java/ql/src/semmle/code/java/Statement.qll

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ class BlockStmt extends Stmt, @block {
7373
/** Gets the last statement in this block. */
7474
Stmt getLastStmt() { result = getStmt(getNumStmt() - 1) }
7575

76-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
7776
override string pp() { result = "{ ... }" }
7877

79-
/** This statement's Halstead ID (used to compute Halstead metrics). */
78+
override string toString() { result = "{ ... }" }
79+
8080
override string getHalsteadID() { result = "BlockStmt" }
8181

8282
override string getAPrimaryQlClass() { result = "BlockStmt" }
@@ -130,14 +130,14 @@ class IfStmt extends ConditionalStmt, @ifstmt {
130130
/** Gets the `else` branch of this `if` statement. */
131131
Stmt getElse() { result.isNthChildOf(this, 2) }
132132

133-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
134133
override string pp() {
135134
result = "if (...) " + this.getThen().pp() + " else " + this.getElse().pp()
136135
or
137136
not exists(this.getElse()) and result = "if (...) " + this.getThen().pp()
138137
}
139138

140-
/** This statement's Halstead ID (used to compute Halstead metrics). */
139+
override string toString() { result = "if (...)" }
140+
141141
override string getHalsteadID() { result = "IfStmt" }
142142

143143
override string getAPrimaryQlClass() { result = "IfStmt" }
@@ -201,10 +201,10 @@ class ForStmt extends ConditionalStmt, @forstmt {
201201
getCondition().getAChildExpr*() = result.getAnAccess()
202202
}
203203

204-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
205204
override string pp() { result = "for (...;...;...) " + this.getStmt().pp() }
206205

207-
/** This statement's Halstead ID (used to compute Halstead metrics). */
206+
override string toString() { result = "for (...;...;...)" }
207+
208208
override string getHalsteadID() { result = "ForStmt" }
209209

210210
override string getAPrimaryQlClass() { result = "ForStmt" }
@@ -221,10 +221,10 @@ class EnhancedForStmt extends Stmt, @enhancedforstmt {
221221
/** Gets the body of this enhanced `for` loop. */
222222
Stmt getStmt() { result.getParent() = this }
223223

224-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
225-
override string pp() { result = "for (...) " + this.getStmt().pp() }
224+
override string pp() { result = "for (... : ...) " + this.getStmt().pp() }
225+
226+
override string toString() { result = "for (... : ...)" }
226227

227-
/** This statement's Halstead ID (used to compute Halstead metrics). */
228228
override string getHalsteadID() { result = "EnhancedForStmt" }
229229

230230
override string getAPrimaryQlClass() { result = "EnhancedForStmt" }
@@ -244,10 +244,10 @@ class WhileStmt extends ConditionalStmt, @whilestmt {
244244
*/
245245
deprecated override Stmt getTrueSuccessor() { result = getStmt() }
246246

247-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
248247
override string pp() { result = "while (...) " + this.getStmt().pp() }
249248

250-
/** This statement's Halstead ID (used to compute Halstead metrics). */
249+
override string toString() { result = "while (...)" }
250+
251251
override string getHalsteadID() { result = "WhileStmt" }
252252

253253
override string getAPrimaryQlClass() { result = "WhileStmt" }
@@ -267,10 +267,10 @@ class DoStmt extends ConditionalStmt, @dostmt {
267267
*/
268268
deprecated override Stmt getTrueSuccessor() { result = getStmt() }
269269

270-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
271270
override string pp() { result = "do " + this.getStmt().pp() + " while (...)" }
272271

273-
/** This statement's Halstead ID (used to compute Halstead metrics). */
272+
override string toString() { result = "do ... while (...)" }
273+
274274
override string getHalsteadID() { result = "DoStmt" }
275275

276276
override string getAPrimaryQlClass() { result = "DoStmt" }
@@ -356,10 +356,10 @@ class TryStmt extends Stmt, @trystmt {
356356
result = getAResourceExpr().getVariable()
357357
}
358358

359-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
360359
override string pp() { result = "try " + this.getBlock().pp() + " catch (...)" }
361360

362-
/** This statement's Halstead ID (used to compute Halstead metrics). */
361+
override string toString() { result = "try ..." }
362+
363363
override string getHalsteadID() { result = "TryStmt" }
364364

365365
override string getAPrimaryQlClass() { result = "TryStmt" }
@@ -387,10 +387,10 @@ class CatchClause extends Stmt, @catchclause {
387387
)
388388
}
389389

390-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
391390
override string pp() { result = "catch (...) " + this.getBlock().pp() }
392391

393-
/** This statement's Halstead ID (used to compute Halstead metrics). */
392+
override string toString() { result = "catch (...)" }
393+
394394
override string getHalsteadID() { result = "CatchClause" }
395395

396396
override string getAPrimaryQlClass() { result = "CatchClause" }
@@ -422,10 +422,10 @@ class SwitchStmt extends Stmt, @switchstmt {
422422
/** Gets the expression of this `switch` statement. */
423423
Expr getExpr() { result.getParent() = this }
424424

425-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
426425
override string pp() { result = "switch (...)" }
427426

428-
/** This statement's Halstead ID (used to compute Halstead metrics). */
427+
override string toString() { result = "switch (...)" }
428+
429429
override string getHalsteadID() { result = "SwitchStmt" }
430430

431431
override string getAPrimaryQlClass() { result = "SwitchStmt" }
@@ -485,10 +485,10 @@ class ConstCase extends SwitchCase {
485485
*/
486486
Expr getValue(int i) { result.getParent() = this and result.getIndex() = i and i >= 0 }
487487

488-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
489488
override string pp() { result = "case ..." }
490489

491-
/** This statement's Halstead ID (used to compute Halstead metrics). */
490+
override string toString() { result = "case ..." }
491+
492492
override string getHalsteadID() { result = "ConstCase" }
493493

494494
override string getAPrimaryQlClass() { result = "ConstCase" }
@@ -498,10 +498,10 @@ class ConstCase extends SwitchCase {
498498
class DefaultCase extends SwitchCase {
499499
DefaultCase() { not exists(Expr e | e.getParent() = this | e.getIndex() >= 0) }
500500

501-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
502501
override string pp() { result = "default" }
503502

504-
/** This statement's Halstead ID (used to compute Halstead metrics). */
503+
override string toString() { result = "default" }
504+
505505
override string getHalsteadID() { result = "DefaultCase" }
506506

507507
override string getAPrimaryQlClass() { result = "DefaultCase" }
@@ -515,10 +515,10 @@ class SynchronizedStmt extends Stmt, @synchronizedstmt {
515515
/** Gets the block of this `synchronized` statement. */
516516
Stmt getBlock() { result.getParent() = this }
517517

518-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
519518
override string pp() { result = "synchronized (...) " + this.getBlock().pp() }
520519

521-
/** This statement's Halstead ID (used to compute Halstead metrics). */
520+
override string toString() { result = "synchronized (...)" }
521+
522522
override string getHalsteadID() { result = "SynchronizedStmt" }
523523

524524
override string getAPrimaryQlClass() { result = "SynchronizedStmt" }
@@ -529,10 +529,10 @@ class ReturnStmt extends Stmt, @returnstmt {
529529
/** Gets the expression returned by this `return` statement, if any. */
530530
Expr getResult() { result.getParent() = this }
531531

532-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
533532
override string pp() { result = "return ..." }
534533

535-
/** This statement's Halstead ID (used to compute Halstead metrics). */
534+
override string toString() { result = "return ..." }
535+
536536
override string getHalsteadID() { result = "ReturnStmt" }
537537

538538
override string getAPrimaryQlClass() { result = "ReturnStmt" }
@@ -543,10 +543,10 @@ class ThrowStmt extends Stmt, @throwstmt {
543543
/** Gets the expression thrown by this `throw` statement. */
544544
Expr getExpr() { result.getParent() = this }
545545

546-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
547546
override string pp() { result = "throw ..." }
548547

549-
/** This statement's Halstead ID (used to compute Halstead metrics). */
548+
override string toString() { result = "throw ..." }
549+
550550
override string getHalsteadID() { result = "ThrowStmt" }
551551

552552
/** Gets the type of the expression thrown by this `throw` statement. */
@@ -638,12 +638,12 @@ class BreakStmt extends Stmt, @breakstmt {
638638
/** Holds if this `break` statement has an explicit label. */
639639
predicate hasLabel() { exists(string s | s = this.getLabel()) }
640640

641-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
642641
override string pp() {
643642
if this.hasLabel() then result = "break " + this.getLabel() else result = "break"
644643
}
645644

646-
/** This statement's Halstead ID (used to compute Halstead metrics). */
645+
override string toString() { result = "break" }
646+
647647
override string getHalsteadID() { result = "BreakStmt" }
648648

649649
override string getAPrimaryQlClass() { result = "BreakStmt" }
@@ -660,6 +660,8 @@ class YieldStmt extends Stmt, @yieldstmt {
660660

661661
override string pp() { result = "yield ..." }
662662

663+
override string toString() { result = "yield ..." }
664+
663665
override string getHalsteadID() { result = "YieldStmt" }
664666

665667
override string getAPrimaryQlClass() { result = "YieldStmt" }
@@ -673,23 +675,23 @@ class ContinueStmt extends Stmt, @continuestmt {
673675
/** Holds if this `continue` statement has an explicit label. */
674676
predicate hasLabel() { exists(string s | s = this.getLabel()) }
675677

676-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
677678
override string pp() {
678679
if this.hasLabel() then result = "continue " + this.getLabel() else result = "continue"
679680
}
680681

681-
/** This statement's Halstead ID (used to compute Halstead metrics). */
682+
override string toString() { result = "continue" }
683+
682684
override string getHalsteadID() { result = "ContinueStmt" }
683685

684686
override string getAPrimaryQlClass() { result = "ContinueStmt" }
685687
}
686688

687689
/** The empty statement. */
688690
class EmptyStmt extends Stmt, @emptystmt {
689-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
690691
override string pp() { result = ";" }
691692

692-
/** This statement's Halstead ID (used to compute Halstead metrics). */
693+
override string toString() { result = ";" }
694+
693695
override string getHalsteadID() { result = "EmptyStmt" }
694696

695697
override string getAPrimaryQlClass() { result = "EmptyStmt" }
@@ -704,10 +706,10 @@ class ExprStmt extends Stmt, @exprstmt {
704706
/** Gets the expression of this expression statement. */
705707
Expr getExpr() { result.getParent() = this }
706708

707-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
708-
override string pp() { result = "...;" }
709+
override string pp() { result = "<Expr>;" }
710+
711+
override string toString() { result = "<Expr>;" }
709712

710-
/** This statement's Halstead ID (used to compute Halstead metrics). */
711713
override string getHalsteadID() { result = "ExprStmt" }
712714

713715
/** Holds if this statement represents a field declaration with an initializer. */
@@ -733,12 +735,12 @@ class LabeledStmt extends Stmt, @labeledstmt {
733735
/** Gets the label of this labeled statement. */
734736
string getLabel() { namestrings(result, _, this) }
735737

736-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
737738
override string pp() { result = this.getLabel() + ": " + this.getStmt().pp() }
738739

739-
/** This statement's Halstead ID (used to compute Halstead metrics). */
740740
override string getHalsteadID() { result = this.getLabel() + ":" }
741741

742+
override string toString() { result = "<Label>: ..." }
743+
742744
override string getAPrimaryQlClass() { result = "LabeledStmt" }
743745
}
744746

@@ -750,12 +752,12 @@ class AssertStmt extends Stmt, @assertstmt {
750752
/** Gets the assertion message expression, if any. */
751753
Expr getMessage() { exprs(result, _, _, this, _) and result.getIndex() = 1 }
752754

753-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
754755
override string pp() {
755756
if exists(this.getMessage()) then result = "assert ... : ..." else result = "assert ..."
756757
}
757758

758-
/** This statement's Halstead ID (used to compute Halstead metrics). */
759+
override string toString() { result = "assert ..." }
760+
759761
override string getHalsteadID() { result = "AssertStmt" }
760762

761763
override string getAPrimaryQlClass() { result = "AssertStmt" }
@@ -775,10 +777,10 @@ class LocalVariableDeclStmt extends Stmt, @localvariabledeclstmt {
775777
/** Gets an index of a variable declared in this local variable declaration statement. */
776778
int getAVariableIndex() { exists(getVariable(result)) }
777779

778-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
779-
override string pp() { result = "local variable declaration" }
780+
override string pp() { result = "var ...;" }
781+
782+
override string toString() { result = "var ...;" }
780783

781-
/** This statement's Halstead ID (used to compute Halstead metrics). */
782784
override string getHalsteadID() { result = "LocalVariableDeclStmt" }
783785

784786
override string getAPrimaryQlClass() { result = "LocalVariableDeclStmt" }
@@ -789,10 +791,10 @@ class LocalClassDeclStmt extends Stmt, @localclassdeclstmt {
789791
/** Gets the local class declared by this statement. */
790792
LocalClass getLocalClass() { isLocalClass(result, this) }
791793

792-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
793-
override string pp() { result = "local class declaration: " + this.getLocalClass().toString() }
794+
override string pp() { result = "class " + this.getLocalClass().toString() }
795+
796+
override string toString() { result = "class ..." }
794797

795-
/** This statement's Halstead ID (used to compute Halstead metrics). */
796798
override string getHalsteadID() { result = "LocalClassDeclStmt" }
797799

798800
override string getAPrimaryQlClass() { result = "LocalClassDeclStmt" }
@@ -829,13 +831,10 @@ class ThisConstructorInvocationStmt extends Stmt, ConstructorCall, @constructori
829831
/** Gets the immediately enclosing statement of this constructor invocation. */
830832
override Stmt getEnclosingStmt() { result = this }
831833

832-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
833834
override string pp() { result = "this(...)" }
834835

835-
/** Gets a printable representation of this statement. */
836-
override string toString() { result = pp() }
836+
override string toString() { result = "this(...)" }
837837

838-
/** This statement's Halstead ID (used to compute Halstead metrics). */
839838
override string getHalsteadID() { result = "ConstructorInvocationStmt" }
840839

841840
override string getAPrimaryQlClass() { result = "ThisConstructorInvocationStmt" }
@@ -873,13 +872,10 @@ class SuperConstructorInvocationStmt extends Stmt, ConstructorCall, @superconstr
873872
/** Gets the immediately enclosing statement of this constructor invocation. */
874873
override Stmt getEnclosingStmt() { result = this }
875874

876-
/** Gets a printable representation of this statement. May include more detail than `toString()`. */
877875
override string pp() { result = "super(...)" }
878876

879-
/** Gets a printable representation of this statement. */
880-
override string toString() { result = pp() }
877+
override string toString() { result = "super(...)" }
881878

882-
/** This statement's Halstead ID (used to compute Halstead metrics). */
883879
override string getHalsteadID() { result = "SuperConstructorInvocationStmt" }
884880

885881
override string getAPrimaryQlClass() { result = "SuperConstructorInvocationStmt" }

0 commit comments

Comments
 (0)