microsoft / microsoft/vscode-cpptools

False positive declaration-is-incompatible in C on static function that takes a tagged struct type defined within another struct

Open
#13,536 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Language Service Visual Studio
Dominant language
TypeScript
Stars
6.2k
Forks
1.7k
Avg merge
14h 46m
Merged PRs (30d)
61

Description

Environment
  • OS and Version: Arch Linux x64, fully up-to-date
  • VS Code Version: 1.99.3
  • C/C++ Extension Version: 1.24.5
  • If using SSH remote, specify OS of remote machine: N/A
Bug Summary and Steps to Reproduce

Bug Summary:
In C, if a static function takes a struct that was defined within another struct, a false positive declaration-is-incompatible problem appears.

Regression of #8751

Steps to reproduce:

  1. With the C/C++ extension installed and running, open a C file that contains a tagged structure defined within another tagged structure, and a static function which accepts a parameter of that nested tagged structure type. (example included below):
  2. Validate that file compiles and executes as expected.

Expected behavior:
No problem is noted

Actual behavior:
A "declaration is incompatible" problem appears.

declaration is incompatible with "int print_foo_bar_static(const struct foo_bar *fbp)" (declared at line 33)
Configuration and Logs

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

Diagnostics:

-------- Diagnostics - 4/19/2025, 11:10:46 AM
Version: 1.24.5
Current Configuration:
{
    "name": "Linux",
    "includePath": [
        "/home/tony/src/asdf/**"
    ],
    "defines": [],
    "compilerPath": "/usr/bin/gcc",
    "cStandard": "c17",
    "cppStandard": "c++17",
    "intelliSenseMode": "linux-gcc-x64",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": true,
    "compilerPathInCppPropertiesJson": "/usr/bin/gcc",
    "mergeConfigurations": false,
    "recursiveIncludes": {
        "reduce": "default",
        "priority": "afterSystemIncludes",
        "order": "depthFirst"
    },
    "browse": {
        "limitSymbolsToIncludedHeaders": true
    }
}
Modified Settings:
{
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "newLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "sameLine",
    "C_Cpp.clang_format_fallbackStyle": "GNU"
}
Additional Tracked Settings:
{
    "editorTabSize": 8,
    "editorInsertSpaces": false,
    "editorAutoClosingBrackets": "languageDefined",
    "filesEncoding": "utf8",
    "filesAssociations": {
        "*.h": "c"
    },
    "filesExclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/.DS_Store": true,
        "**/Thumbs.db": true
    },
    "filesAutoSaveAfterDelay": false,
    "editorInlayHintsEnabled": true,
    "editorParameterHintsEnabled": true,
    "searchExclude": {
        "**/node_modules": true,
        "**/bower_components": true,
        "**/*.code-search": true
    },
    "workbenchSettingsEditor": "ui"
}
cpptools version (native): 1.24.5.0
Current database path: /home/tony/.cache/vscode-cpptools/3c2dde7ab04cc03895f85c14d63a05eb/.browse.VC.db
Translation Unit Mappings:
[ /home/tony/src/asdf/main.c - source TU]:
    /usr/include/bits/types/FILE.h
    /usr/include/bits/typesizes.h
    /usr/include/stdio.h
    /usr/include/stdlib.h
    /usr/include/sys/cdefs.h
    /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include/stddef.h
Translation Unit Configurations:
[ /home/tony/src/asdf/main.c ]
    Process ID: 29641
    Memory Usage: 23 MB
    Compiler Path: /usr/bin/gcc
    Include paths:
        system include: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include
        system include: /usr/local/include
        system include: /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include-fixed
        system include: /usr/include
    Standard Version: c17
    IntelliSense Mode: linux-gcc-x64
    Other Flags:
        --gcc
        --gnu_version=140201
Total Memory Usage: 23 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 34569

C/C++ Extension Debug logs: attached, see "Additional context"

Other Extensions

No response

Additional context
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct foo {
	struct foo_bar {
		int x, y;
	} bar;
};

int print_foo_bar(const struct foo_bar *fbp);
static int print_foo_bar_static(const struct foo_bar *fbp); /* problem appears here */

int main(void)
{
	struct foo foo = { {1, 1} };

	if (print_foo_bar(&foo.bar) < 0) {
		exit(EXIT_FAILURE);
	}

	if (print_foo_bar_static(&foo.bar) < 0) {
		exit(EXIT_FAILURE);
	}
}

int print_foo_bar(const struct foo_bar *fbp)
{
	assert(fbp != NULL);
	return printf("%p->%d.%d\n", fbp, fbp->x, fbp->y);
}

static int print_foo_bar_static(const struct foo_bar *fbp)
{
	assert(fbp != NULL);
	return printf("%p->%d.%d\n", fbp, fbp->x, fbp->y);
}
[tony@framework asdf]$ cc -std=c99 -o asdf main.c && ./asdf; echo $?
0x7fff78f88d70->1.1
0x7fff78f88d70->1.1
0

Image

extension_logs.txt

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the diagnostic with the embedded main.c example, using c_cpp_properties.json with C17 and GCC, and review the attached extension_logs.txt. Start by tracing how IntelliSense handles the nested tagged struct in the declaration-incompatible diagnostic; done means the valid static declaration no longer produces a problem while the example still compiles and runs.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.