microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Goto Definition takes a couple of seconds each time to complete the jump

yg-i opened this issue · comments

commented

Environment

  • OS and Version: Win10 19045
  • VS Code Version: 1.89.0
  • C/C++ Extension Version: 1.20.5

Bug Summary and Steps to Reproduce

Bug Summary:

Looking up definitions are very slow in a simple Win32 C project.

I've made a gif demonstrating the issue (click the triangle in the upper right corner to play it). Also note the popup in the bottom right corner.

2024_0510_2042_02 (Shameless Nandine)

Steps to reproduce:

  1. Save the text at the bottom of this comment as a .c source file:
  2. Place cursor on PAINTSTRUCT. Hit the shortcut for Goto Definition

Expected behavior:
Jump should be reasonably fast. At least, if the first invocation is slow, subsequent invocations should be faster.

Observed behavior:
See gif above. Each invocation of the shortcut on the same symbol takes a few seconds to complete. Also note the messages that keeps popping up in the bottom right corner.

#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
			 PSTR szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("HelloWin");
	HWND hwnd;
	MSG msg;
	WNDCLASS wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("This program requires Windows NT!"),
			     szAppName, MB_ICONERROR);
		return 0;
	}
	hwnd = CreateWindow(szAppName,		     // window class name
				  TEXT("The Hello Program"), // window caption
				  WS_OVERLAPPEDWINDOW,	     // window style
				  CW_USEDEFAULT,		     // initial x position
				  CW_USEDEFAULT,		     // initial y position
				  CW_USEDEFAULT,		     // initial x size
				  CW_USEDEFAULT,		     // initial y size
				  NULL,			     // parent window handle
				  NULL,			     // window menu handle
				  hInstance,		     // program instance handle
				  NULL);			     // creation parameters

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC hdc;
	PAINTSTRUCT ps;
	RECT rect;

	switch (message)
	{
	case WM_CREATE:
		PlaySound(TEXT("test_audio.wav"), NULL, SND_FILENAME | SND_ASYNC);
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		GetClientRect(hwnd, &rect);

		DrawText(hdc, TEXT("Hello, Windows 98!"), -1, &rect,
			   DT_SINGLELINE | DT_CENTER | DT_VCENTER);
		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

Configuration and Logs

I have not modified c_cpp_properties.json

Debug outputs:

[Warning] Some references may be missing, because workspace parsing was incomplete when Find All References was started.

[Warning] Some references may be missing, because workspace parsing was incomplete when Find All References was started.

Other Extensions

No response

Additional context

No response

Thank you for opening this issue. I think there is something unusual going on, because this operation is nearly instantaneous for me after copying your code into a C file.

The main contributor to the slowness is that the extension is choosing to do "Find all References" instead of "Go to Definition." Would you be able to enable Debug logging, restart the extension, rerun your scenario and then share the logs with us?

2.c.-.simple.-.Visual.Studio.Code.2024-05-13.09-04-54.mp4
commented

Thank you for your reply. Please see the attached log.

I performed 4-5 "goto definition" actions this time, 3 of which are instantaneous, the other 2 suffer from the problem depicted in my original video (all 5 goto definitions jump to inside either winuser.h or windef.h).

I did not perform any "find all references" action, but the LSP seems to have received/invoked lots of cpptools/findAllReferences action.

debug.log

I'm going to try and see if I can get a cleaner log and if so I'll post it here.

Hi @yg-i,

I'm looking at your log and I see multiple "Find all References" requests. I had thought that maybe what was happening was that VS Code was translating your "Go To Definition" request into a "Find all References" request. This happens by default when we return the current location as the result which can happen when we haven't located the actual definition. However, when this happens, I should see a "Go To Definition" request in the log right before the "Find all References" request and I don't see that.

Do you happen to have any extensions installed that automatically invoke "Find all References" as you move the cursor around? Most of the calls to "Find all References" seem to be happening right after moving the cursor, or opening a file (which also sets the cursor position).

This issue has been closed because it needs more information and has not had recent activity.