clangd / vscode-clangd

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

Clangd showing error even when member is properly initialized #697

Closed austinbennett69420 closed 1 month ago

austinbennett69420 commented 1 month ago

When making a class constructor, if i initialize a class member with a parameter, rather than that classes constructor, it shows the red wiggly line underneath

EX:

class foo {

private:
    bar member;
public
    foo(bar object) {
        member = object;//even the the variable is initialized, it will still mark it as an error.
    }

};
HighCommander4 commented 1 month ago

Could you give a complete code example please?

austinbennett69420 commented 1 month ago

this is where i was encountering the error in my own code:

class surface {
    private:
    SDL_Texture* t;
    renderer rend;

    public:

    surface(renderer& r, int w, int h) {
        t = SDL_CreateTexture(r.getSdlRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h);
        rend = r;
    }
}
HighCommander4 commented 1 month ago

"Complete" means including definitions of other symbols that the code depends on (e.g. here SDL_Texture), and any relevant configuration (e.g. .clangd file, .clang-tidy, compile_commands.json).

Basically, enough information for someone else to reproduce the behaviour you're seeing.

austinbennett69420 commented 1 month ago

so sorry thats my fault!

heres a better full-code example

class surface {
    private:
    SDL_Texture* t;
    renderer rend;

    public:

    surface(renderer& r, int w, int h) {
        t = SDL_CreateTexture(r.getSdlRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h);
        rend = r;
    }
};
//SDL_Texture definition via https://github.com/libsdl-org/SDL/blob/main/src/render/SDL_sysrender.h
struct SDL_Texture
{
    // Public API definition
    SDL_PixelFormat format;     /**< The format of the texture, read-only */
    int w;                      /**< The width of the texture, read-only. */
    int h;                      /**< The height of the texture, read-only. */

    int refcount;               /**< Application reference count, used when freeing texture */

    // Private API definition
    SDL_Colorspace colorspace;  // The colorspace of the texture
    float SDR_white_point;      // The SDR white point for this content
    float HDR_headroom;         // The HDR headroom needed by this content
    SDL_TextureAccess access;   // The texture access mode
    SDL_BlendMode blendMode;    // The texture blend mode
    SDL_ScaleMode scaleMode;    // The texture scale mode
    SDL_FColor color;           // Texture modulation values
    SDL_RenderViewState view;   // Target texture view state

    SDL_Renderer *renderer;

    // Support for formats not supported directly by the renderer
    SDL_Texture *native;
    SDL_SW_YUVTexture *yuv;
    void *pixels;
    int pitch;
    SDL_Rect locked_rect;
    SDL_Surface *locked_surface; // Locked region exposed as a SDL surface

    Uint32 last_command_generation; // last command queue generation this texture was in.

    SDL_PropertiesID props;

    void *internal;             // Driver specific texture representation

    SDL_Texture *prev;
    SDL_Texture *next;
};

and heres the definition for my renderer class:

class renderer {
    private:

    SDL_Renderer* rend = nullptr;

    public:

    renderer() {}

    renderer(screen& s) {
        rend = SDL_CreateRenderer(s.get_sdl_window(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
        SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
    }

    SDL_Renderer* getSdlRenderer() {
        return rend;
    }
};

early in on this project so most things are a little bare-bones in terms of methods
HighCommander4 commented 1 month ago

That's getting there, but there are still a couple of things you could do to make it easier for me to reproduce the issue:

austinbennett69420 commented 1 month ago

Oh yeah of course Btw thank you so much for replying and taking the time to deal with my clumsy self I really do appreciate it!

Im using the version 2.30.7 although it should be noted i have encountered this issue when not using SDL

to make it easy for you to install here are the steps: First of all im using MinGW on windows, you can get that here

Now to install SDL:

I cannot disclose the full source file due to it containing a lot of sensitive information but that should be all the code you need to replicate the issue, again thank you for all of your help!

HighCommander4 commented 1 month ago

Based on the info you gave, I put together the following file to try and reproduce what you're seeing:

#include <SDL2/SDL.h>

class renderer {
private:
  SDL_Renderer *rend = nullptr;

public:
  renderer() {}

  // renderer(screen& s) {
  //     rend = SDL_CreateRenderer(s.get_sdl_window(), -1,
  //     SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE);
  //     SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND);
  // }

  SDL_Renderer *getSdlRenderer() { return rend; }
};

class surface {
private:
  SDL_Texture *t;
  renderer rend;

public:
  surface(renderer &r, int w, int h) {
    t = SDL_CreateTexture(r.getSdlRenderer(), SDL_PIXELFORMAT_ARGB8888,
                          SDL_TEXTUREACCESS_TARGET, w, h);
    rend = r;
  }
};

(Note, I had to comment out the renderer(screen& s) constructor because screen wasn't defined.)

No errors for me:

image

austinbennett69420 commented 1 month ago

Okay the reason is because of the default constructor, usually I get the error when I don’t have a default constructor, if I do I get no errors but when I don’t then even if I instantiate the member I see the error

On Sat, Oct 5, 2024 at 7:13 PM Nathan Ridge @.***> wrote:

Based on the info you gave, I put together the following file to try and reproduce what you're seeing:

include <SDL2/SDL.h>

class renderer {private: SDL_Renderer *rend = nullptr; public: renderer() {}

// renderer(screen& s) { // rend = SDL_CreateRenderer(s.get_sdl_window(), -1, // SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE); // SDL_SetRenderDrawBlendMode(rend, SDL_BLENDMODE_BLEND); // }

SDL_Renderer getSdlRenderer() { return rend; } }; class surface {private: SDL_Texture t; renderer rend; public: surface(renderer &r, int w, int h) { t = SDL_CreateTexture(r.getSdlRenderer(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, w, h); rend = r; } };

(Note, I had to comment out the renderer(screen& s) constructor because screen wasn't defined.)

No errors for me:

image.png (view on web) https://github.com/user-attachments/assets/48fc91a4-22cc-45e5-81e4-2ec31fdc814a

— Reply to this email directly, view it on GitHub https://github.com/clangd/vscode-clangd/issues/697#issuecomment-2395234342, or unsubscribe https://github.com/notifications/unsubscribe-auth/AT2KE7F23C2M2WHODVAF4KDZ2B6BBAVCNFSM6AAAAABPMVHJJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJVGIZTIMZUGI . You are receiving this because you authored the thread.Message ID: @.***>

HighCommander4 commented 1 month ago

Note, I'm testing on Linux rather than Windows, but that's unlikely to be the relevant difference.

What's more likely is that there is something needed to reproduce the problem that you haven't shared yet.

Let me remind you of something I wrote earlier:

"Complete" means including definitions of other symbols that the code depends on (e.g. here SDL_Texture), and any relevant configuration (e.g. .clangd file, .clang-tidy, compile_commands.json).

Maybe you're seeing a diagnostic I'm not because you have a clang-tidy checker enabled in .clang-tidy that I don't.

Or maybe there's just more to the code itself than the pieces you've shared.

This is why investigating issues like these really requires a complete example.

I cannot disclose the full source file due to it containing a lot of sensitive information but that should be all the code you need to replicate the issue, again thank you for all of your help!

The usual practice in such situations is for you to prepare and share a smaller (ideally, minimal) source file that still reproduces the problem but has sensitive information removed.

austinbennett69420 commented 1 month ago

I understand, I don’t have any .clang files or anything like that, I’m saying the problem is the default constructor as i specifically see the bug when there is no default constructor

On Sat, Oct 5, 2024 at 7:16 PM Nathan Ridge @.***> wrote:

Note, I'm testing on Linux rather than Windows, but that's unlikely to be the relevant difference.

What's more likely is that there is something needed to reproduce the problem that you haven't shared yet.

Let me remind you of something I wrote earlier:

"Complete" means including definitions of other symbols that the code depends on (e.g. here SDL_Texture), and any relevant configuration (e.g. .clangd file, .clang-tidy, compile_commands.json).

Maybe you're seeing a diagnostic I'm not because you have a clang-tidy checker enabled in .clang-tidy that I don't.

Or maybe there's just more to the code itself than the pieces you've shared.

This is why investigating bugs like these really requires a complete example.

I cannot disclose the full source file due to it containing a lot of sensitive information but that should be all the code you need to replicate the issue, again thank you for all of your help!

The usual practice in such situations is for you to prepare and share a smaller (ideally, minimal) source file that still reproduces the problem but has sensitive information removed.

— Reply to this email directly, view it on GitHub https://github.com/clangd/vscode-clangd/issues/697#issuecomment-2395235455, or unsubscribe https://github.com/notifications/unsubscribe-auth/AT2KE7DAZ5TT2UE7AMU2VG3Z2B6OXAVCNFSM6AAAAABPMVHJJWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGOJVGIZTKNBVGU . You are receiving this because you authored the thread.Message ID: @.***>

HighCommander4 commented 1 month ago

Feel free to share an example without a default constructor where the code compiles but clangd gives you an error.

austinbennett69420 commented 1 month ago

Probably should have started with this, heres screenshots of the exact problem: Screenshot 2024-10-07 171319 Screenshot 2024-10-07 171610

HighCommander4 commented 1 month ago

That looks like it wouldn't compile, and clangd is just accurately telling you about the compiler error that will also get when building.

austinbennett69420 commented 1 month ago

No it will compile just fine, if i did something like Font = " " or i didnt set the value of Font to anything then it wouldnt, but referring to the screenshot, i set it the the parameter f in the constructor, it only does this with objects that have no default constructor and sometimes its valid to not have a default constructor for some of my classes

austinbennett69420 commented 1 month ago

The problem is that it seems to me that Clangd is only checking if i instantiate the member with its constructor, or if the member has a default constructor, not if i set the member with a constructor parameter

HighCommander4 commented 1 month ago

No it will compile just fine

Ok; I can't really verify this though without the example being complete.

austinbennett69420 commented 1 month ago

Okay after messing with it a bit, i see that apparently it does not compile correctly, unless i do it like constructor(foo val) : member(val) {}, i think its a bit dumb but now i have passed my frustration and taken your precious minutes maybe even hours away because of my thoughtlessness, I sincerely apologize.

HighCommander4 commented 1 month ago

No worries :)