Skip to content

Commit 72f7212

Browse files
committed
Add QLDoc
1 parent b33d41c commit 72f7212

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

go/extractor/dbscheme/tables.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ var UsesTable = NewTable("uses",
11021102
// declare a new variable in the guard to the variable that is implicitly
11031103
// declared at the beginning of the case clause to represent that variable
11041104
// with the correct type
1105-
var ImplicitVarDeclTable = NewTable("implicitvardecl",
1105+
var ImplicitCaseVarDeclTable = NewTable("implicitcasevardecl",
11061106
EntityColumn(CaseClauseType, "caseclause"),
11071107
EntityColumn(ObjectType, "object"),
11081108
)

go/extractor/extractor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ func emitImplicitObjects(tw *trap.Writer, ts *ast.TypeSwitchStmt) {
862862
log.Fatalf("getTypeLabel() called for implicit variable %s", obj.Name())
863863
return trap.InvalidLabel
864864
})
865-
dbscheme.ImplicitVarDeclTable.Emit(tw, cclbl, objlbl)
865+
dbscheme.ImplicitCaseVarDeclTable.Emit(tw, cclbl, objlbl)
866866
}
867867
}
868868
}

go/ql/lib/go.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ defs(int ident: @ident ref, int object: @object ref);
190190

191191
uses(int ident: @ident ref, int object: @object ref);
192192

193-
implicitvardecl(int caseclause: @caseclause ref, int object: @object ref);
193+
implicitcasevardecl(int caseclause: @caseclause ref, int object: @object ref);
194194

195195
types(unique int id: @type, int kind: int ref);
196196

go/ql/lib/semmle/go/Scopes.qll

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ class Entity extends @object {
122122
/**
123123
* Gets the declaring identifier for this entity, if any.
124124
*
125-
* Note that type switch statements which define a new variable in the guard
125+
* Note that type switch statements which declare a new variable in the guard
126126
* actually have a new variable (of the right type) implicitly declared at
127-
* the beginning of each case clause, and these do not have a declaration.
127+
* the beginning of each case clause, and these do not have a syntactic
128+
* declaration.
128129
*/
129130
Ident getDeclaration() { result.declares(this) }
130131

@@ -137,6 +138,15 @@ class Entity extends @object {
137138
/** Gets a textual representation of this entity. */
138139
string toString() { result = this.getName() }
139140

141+
private predicate hasRealLocationInfo(
142+
string filepath, int startline, int startcolumn, int endline, int endcolumn
143+
) {
144+
// take the location of the declaration if there is one
145+
this.getDeclaration().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) or
146+
any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable())
147+
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
148+
}
149+
140150
/**
141151
* Holds if this element is at the specified location.
142152
* The location spans column `startcolumn` of line `startline` to
@@ -147,23 +157,15 @@ class Entity extends @object {
147157
predicate hasLocationInfo(
148158
string filepath, int startline, int startcolumn, int endline, int endcolumn
149159
) {
150-
// take the location of the declaration if there is one
151-
if exists(this.getDeclaration())
152-
then this.getDeclaration().hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
153-
else
154-
// take the location of the implicit declaration if there is one
155-
if this = any(CaseClause cc).getImplicitlyDeclaredVariable()
156-
then
157-
any(CaseClause cc | this = cc.getImplicitlyDeclaredVariable())
158-
.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
159-
else (
160-
// otherwise fall back on dummy location
161-
filepath = "" and
162-
startline = 0 and
163-
startcolumn = 0 and
164-
endline = 0 and
165-
endcolumn = 0
166-
)
160+
this.hasRealLocationInfo(filepath, startline, startcolumn, endline, endcolumn)
161+
or
162+
// otherwise fall back on dummy location
163+
not this.hasRealLocationInfo(_, _, _, _, _) and
164+
filepath = "" and
165+
startline = 0 and
166+
startcolumn = 0 and
167+
endline = 0 and
168+
endcolumn = 0
167169
}
168170
}
169171

go/ql/lib/semmle/go/Stmt.qll

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,13 @@ class CaseClause extends @caseclause, Stmt, ScopeNode {
777777
/** Gets the number of statements of this `case` clause. */
778778
int getNumStmt() { result = this.getNumChildStmt() }
779779

780-
LocalVariable getImplicitlyDeclaredVariable() { implicitvardecl(this, result) }
780+
/**
781+
* Gets the implicitly declared variable for this `case` clause, if any.
782+
*
783+
* This exists for case clauses in type switch statements which declare a
784+
* variable in the guard.
785+
*/
786+
LocalVariable getImplicitlyDeclaredVariable() { implicitcasevardecl(this, result) }
781787

782788
override predicate mayHaveSideEffects() {
783789
this.getAnExpr().mayHaveSideEffects() or

go/ql/lib/semmle/go/controlflow/ControlFlowGraphImpl.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ newtype TControlFlowNode =
311311
* `cc` of type switch statement `ts`, and its assignment of the value from the guard.
312312
*/
313313
MkTypeSwitchImplicitVariable(CaseClause cc, Expr switchExpr) {
314-
exists(TypeSwitchStmt ts | ts.getAssign() instanceof DefineStmt |
314+
exists(TypeSwitchStmt ts, DefineStmt ds | ds = ts.getAssign() |
315315
cc = ts.getACase() and
316-
switchExpr = ts.getAssign().(DefineStmt).getRhs().(TypeAssertExpr).getExpr()
316+
switchExpr = ds.getRhs().(TypeAssertExpr).getExpr()
317317
)
318318
} or
319319
/**

go/ql/lib/semmle/go/controlflow/IR.qll

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,7 +1287,10 @@ module IR {
12871287
* }
12881288
* ```
12891289
*
1290-
* The `y` inside the case clause is actually a local variable that is implicitly declared.
1290+
* The `y` inside the case clause is actually a local variable with type `Type1` that
1291+
* is implicitly declared at the top of the case clause. In case clauses which list
1292+
* more than one type and default clauses, the type of the implicitly declared variable
1293+
* is the same as the expression which is being switched on.
12911294
*/
12921295
class TypeSwitchImplicitVariableInstruction extends Instruction, MkTypeSwitchImplicitVariable {
12931296
CaseClause cc;
@@ -1302,7 +1305,7 @@ module IR {
13021305

13031306
override ControlFlow::Root getRoot() { result.isRootOf(cc) }
13041307

1305-
override string toString() { result = "implicit variable declaration" }
1308+
override string toString() { result = "implicit type switch variable declaration" }
13061309

13071310
override predicate hasLocationInfo(
13081311
string filepath, int startline, int startcolumn, int endline, int endcolumn

0 commit comments

Comments
 (0)