Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Fix display of shadowed variables while debugging (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhendrixMSFT authored and ramya-rao-a committed Jan 23, 2019
1 parent cee927d commit e07df5a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ install:
- go get -u -v github.com/cweill/gotests/...
- go get -u -v github.com/haya14busa/goplay/cmd/goplay
- go get -u -v github.com/davidrjenni/reftools/cmd/fillstruct
- go get -u -v github.com/alecthomas/gometalinter
- gometalinter --install
- curl -fsSL https://git.io/vp6lP | sh -s -- -b $GOPATH/bin

script:
- npm run lint
Expand Down
44 changes: 43 additions & 1 deletion src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,22 @@ interface EvalOut {
Variable: DebugVariable;
}

enum GoVariableFlags {
VariableEscaped = 1,
VariableShadowed = 2,
VariableConstant = 4,
VariableArgument = 8,
VariableReturnArgument = 16
}

interface DebugVariable {
name: string;
addr: number;
type: string;
realType: string;
kind: GoReflectKind;
flags: GoVariableFlags;
DeclLine: number;
value: string;
len: number;
cap: number;
Expand Down Expand Up @@ -843,14 +853,44 @@ class GoDebugSession extends LoggingDebugSession {
log('functionArgs', args);
this.addFullyQualifiedName(args);
let vars = args.concat(locals);

// annotate shadowed variables in parentheses
const shadowedVars = new Map<string, Array<number>>();
for (let i = 0; i < vars.length; ++i) {
if ((vars[i].flags & GoVariableFlags.VariableShadowed) === 0) {
continue;
}
const varName = vars[i].name;
if (!shadowedVars.has(varName)) {
const indices = new Array<number>();
indices.push(i);
shadowedVars.set(varName, indices);
} else {
shadowedVars.get(varName).push(i);
}
}
for (const svIndices of shadowedVars.values()) {
// sort by declared line number in descending order
svIndices.sort((lhs: number, rhs: number) => {
return vars[rhs].DeclLine - vars[lhs].DeclLine;
});
// enclose in parentheses, one pair per scope
for (let scope = 0; scope < svIndices.length; ++scope) {
const svIndex = svIndices[scope];
// start at -1 so scope of 0 has one pair of parens
for (let count = -1; count < scope; ++count) {
vars[svIndex].name = `(${vars[svIndex].name})`;
}
}
}
let scopes = new Array<Scope>();
let localVariables = {
name: 'Local',
addr: 0,
type: '',
realType: '',
kind: 0,
flags: 0,
DeclLine: 0,
value: '',
len: 0,
cap: 0,
Expand Down Expand Up @@ -899,6 +939,8 @@ class GoDebugSession extends LoggingDebugSession {
type: '',
realType: '',
kind: 0,
flags: 0,
DeclLine: 0,
value: '',
len: 0,
cap: 0,
Expand Down

0 comments on commit e07df5a

Please sign in to comment.