clangd / vscode-clangd

Visual Studio Code extension for clangd
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd
MIT License
594 stars 97 forks source link

How to make clangd provide intelligence for .cu files #558

Closed shadow150519 closed 7 months ago

shadow150519 commented 7 months ago

I have a very strange problem for my clangd. It can provide intellgence for my cpp files correctly, but when it comes to .cu files, it don't work. Here's my situation.I've add the torch lib in my project. when I include <torch/library.h>. clangd works correctly. memory_utils.cpp However, when I include <torch/library.h> in a .cu file. clangd doesn't seem to be working. memory_utils.cu But when I rename the .cu file to .cpp file. It works again. memory_utils_gpu.cpp The three files are in the same directory. I can make sure there is no problem with my cmake configuration.

Logs when I hover on the #include<torch/library.h> in the .cu file

V[15:53:33.482] <<< {
  "id": 73,
  "jsonrpc": "2.0",
  "method": "textDocument/hover",
  "params": {
    "position": {
      "character": 24,
      "line": 8
    },
    "textDocument": {
      "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu"
    }
  }
}

I[15:53:33.482] <-- textDocument/hover(73)
V[15:53:33.483] ASTWorker running Hover on version 5 of /home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu
I[15:53:33.483] --> reply:textDocument/hover(73) 0 ms
V[15:53:33.483] >>> {
  "id": 73,
  "jsonrpc": "2.0",
  "result": null
}

I[15:53:33.483] --> textDocument/clangd.fileStatus
V[15:53:33.483] >>> {
  "jsonrpc": "2.0",
  "method": "textDocument/clangd.fileStatus",
  "params": {
    "state": "idle",
    "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu"
  }
}

V[15:53:33.642] <<< {
  "id": 74,
  "jsonrpc": "2.0",
  "method": "textDocument/codeAction",
  "params": {
    "context": {
      "diagnostics": [
        {
          "code": "pp_file_not_found",
          "message": "'torch/library.h' file not found",
          "range": {
            "end": {
              "character": 26,
              "line": 8
            },
            "start": {
              "character": 9,
              "line": 8
            }
          },
          "relatedInformation": [],
          "severity": 1,
          "source": "clang"
        }
      ],
      "only": [
        "quickfix"
      ],
      "triggerKind": 1
    },
    "range": {
      "end": {
        "character": 26,
        "line": 8
      },
      "start": {
        "character": 9,
        "line": 8
      }
    },
    "textDocument": {
      "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu"
    }
  }
}

I[15:53:33.643] <-- textDocument/codeAction(74)
V[15:53:33.643] ASTWorker running EnumerateTweaks on version 5 of /home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu
I[15:53:33.643] --> reply:textDocument/codeAction(74) 0 ms
V[15:53:33.643] >>> {
  "id": 74,
  "jsonrpc": "2.0",
  "result": []
}

I[15:53:33.643] --> textDocument/clangd.fileStatus
V[15:53:33.643] >>> {
  "jsonrpc": "2.0",
  "method": "textDocument/clangd.fileStatus",
  "params": {
    "state": "idle",
    "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops.cu"
  }
}

when I hover on the #include<torch/library.h> in the .cpp file

V[15:54:34.829] <<< {
  "id": 81,
  "jsonrpc": "2.0",
  "method": "textDocument/hover",
  "params": {
    "position": {
      "character": 24,
      "line": 9
    },
    "textDocument": {
      "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops_gpu.cpp"
    }
  }
}

I[15:54:34.829] <-- textDocument/hover(81)
V[15:54:34.829] ASTWorker running Hover on version 1 of /home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops_gpu.cpp
I[15:54:34.830] --> reply:textDocument/hover(81) 0 ms
V[15:54:34.830] >>> {
  "id": 81,
  "jsonrpc": "2.0",
  "result": {
    "contents": {
      "kind": "markdown",
      "value": "### `library.h`  \n\n---\n```\n/home/wtx/miniconda3/envs/dgl/lib/python3.10/site-packages/torch/include/torch/library.h\n```"
    }
  }
}

I[15:54:34.830] --> textDocument/clangd.fileStatus
V[15:54:34.830] >>> {
  "jsonrpc": "2.0",
  "method": "textDocument/clangd.fileStatus",
  "params": {
    "state": "idle",
    "uri": "file:///home/wtx/workspace/cpp_project/uvm/memory_utils/memory_utils_ops_gpu.cpp"
  }
}

System information Clangd version (from the log, or clangd --version):

clangd version 15.0.6
Features: linux
Platform: x86_64-unknown-linux-gnu

clangd extension version: v0.1.25 Operating system: Ubuntu 20.04.5 LTS