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

Fix display of shadowed variables while debugging #2254

Merged
merged 3 commits into from
Jan 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its weird that this doesnt have struct tag at https://github.com/go-delve/delve/blame/4c9a72e486f1f0d0c90ecede8415a871dced8117/service/api/types.go#L264 to change it to camelcasing.
Should we log a bug for delve?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it odd as well. We could open an issue however given that JSON is case-sensitive it would be a breaking change for consumers.

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