vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Parse #define from included files like autoconf.h

Open jankapte opened this issue 9 months ago • 3 comments

Feature Request

Looks like the extension does not pick up #define or other preprocessor directives from header files:

autoconf.h: Image

c_cpp_properties.json: Image

(full config below) Results in grayed out section:

Image

using "defines": ["CONFIG_ENG"] does work:

Image

I'd prefer not to use the "defines" array, because there are many defines and they change from build config to build config and instead I'd love to parse the defines from the autoconf.h.

Is that possible already or would that be a new feature?

Thanks.

full config:

{
    "configurations": [
        {
            "name": "GCC DevContainer",
            "cStandard": "c17",
            "cppStandard": "gnu++20",
            "compilerPath": "/opt/arm-gnu-toolchain-x86_64-arm-none-eabi/13.3.rel1/bin/arm-none-eabi-gcc",
            "intelliSenseMode": "linux-gcc-arm",
            "compilerArgs": [
                "-mcpu=cortex-m4",
                "-mthumb",
                "-mfloat-abi=hard",
                "-mfpu=fpv4-sp-d16",
                "-mabi=aapcs",
                "-DARM_MATH_CM4",
                "-D__FPU_PRESENT=1",
                "-ffunction-sections",
                "-fdata-sections",
                "-fno-strict-aliasing",
                "-fno-builtin",
                "-fshort-enums",
                "-Wno-unused-function",
                "-fmerge-all-constants",
                "-fomit-frame-pointer",
                "-Og",
                "-fno-unroll-loops",
                "-fno-inline",
                "-fno-code-hoisting",
                "-fno-strict-aliasing",
                "-fno-schedule-insns",
                "-fno-schedule-insns2",
                "-g"
            ],
            // "defines": ["CONFIG_ENG"],
            "forcedInclude": [
                "out/autoconf.h"
            ],
            "mergeConfigurations": true,
            "includePath": [
                "out/"
            ]
        }
    ],
    "version": 4,
    "enableConfigurationSquiggles": true
}

jankapte avatar Feb 12 '26 11:02 jankapte

@jankapte We do pick up defines from header files and forced include files as long as they're being included in the TU. The relative path out/autoconf.h needs to be absolute via something like ${workspaceFolder}/out/autoconf.h, same with the includePath. You can run C/C++: Log Diagnostics to see how the particular TU is getting configured to see if it's as expected or not.

sean-mcmanus avatar Feb 12 '26 21:02 sean-mcmanus

Using an absolute path should work, though cpptools should be trying to resolve forcedInclude's against include paths. If the include path is out, the forcedInclude should probably be just autoconf.h, unless it's under out\out\. I think includePath supports relative paths, considering them to be under ${workspaceFolder}, so shouldn't need to be an absolute path.

C/C++: Log Diagnotics output would tell us more. A simple self-contained repro example, in the form of a zip file or GitHub repo, would be the easiest to diagnose.

Colengms avatar Feb 12 '26 21:02 Colengms

Dear both, thank you so much for your very fast reply.

I can confirm that both explanations are correct.

  1. The include paths "nest": Image

Although VSC complains that it can't find the file, it's properly parsed by cpptools and the define is shown correctly.

  1. Using an absolute path always works always, regardless of other includes "forcedInclude": ["${workspaceFolder}/out/autoconf.h"]

PS, I chose the third option: Create the compilation database via bear and use it via "compileCommands": "${workspaceFolder}/out/compile_commands.json"

jankapte avatar Feb 13 '26 11:02 jankapte