microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.42k stars 1.52k forks source link

Make it possible to define custom compiler keywords #6642

Open denis-shienkov opened 3 years ago

denis-shienkov commented 3 years ago

Type: LanguageService

Right now the intellisence fails at highliting of a custom compiler keywords, like __data, data, __xdata and so forth, which are used, e.g. for 8051 (aka MCS51) architecture:

изображение

изображение

and so forth.

BTW, similar keywords also are exists and in many other compilers with different MCU architectures.

So, it would be good to allow to define the custom keywords in some VSCode API.

Colengms commented 3 years ago

Hi @denis-shienkov . Thanks for suggesting this. We will consider this for a future milestone.

denis-shienkov commented 3 years ago

Guys,

1) as I understand, I can inject the required compiler keywords, using the grammars entry in the package.json file: https://code.visualstudio.com/api/language-extensions/syntax-highlight-guide

Someting like this:

== package.json ==

    "grammars": [
      {
        "scopeName": "iar-keywords.injection",
        "path": "./syntaxes/iar-c-cpp.tmLanguage.json",
        "injectTo": [
          "source.c",
          "source.cpp"
        ]
      }
    ],

== iar-c-cpp.tmLanguage.json ==

{
    "scopeName": "iar-keywords.injection",
    "injectionSelector": "L:source.c, L:source.cpp",
    "patterns": [
        {
            "name": "keyword.c",
            "match": "(__aapcs_core)\\s"
        },
}

But, here I'm have a problem is that I need to do it dynamycally, e.g. I need to inject the appropriate keywords only for a specified compiler/architecture.

For example, I will have a set of *.tmLanguage.json files for a different toolchains. But, seems, it is impossible to use the concrete *.tmLanguage.json file, because the grammars field has not any condition property, e.g. as when.

Also, I can't to specify all *.tmLanguage.json files, because it is wrong.

2) I found out a some additional API, called DocumentSemanticTokensProvider : https://code.visualstudio.com/api/language-extensions/semantic-highlight-guide#semantic-token-provider

But, here also it is unclear, how to use this provider to inject some additional compiler keywords to the document. F.e. I don't want to re-implement whole semantic features for every toolchain.

I need only in ability to inject some additional compiler keywords depending on the current toolchain. Let's say, that I can detect the current used toolchain, it is not a problem here. A problem here is in injection...

Guys, could you please point me, is it possible todo dynamically ?

sean-mcmanus commented 3 years ago

FYI, those grammars will only affect lexical colorization, which is not implemented by our C/C++ extension, but by https://github.com/jeff-hykin/cpp-textmate-grammar (that might be a better place to file an issue like this). The IntelliSense parser won't have knowledge of the extra keywords and might generate error squiggles, which might be worked around via using something like

#ifdef __INTELLISENSE__
#define __bdata
#endif

in some file specified by the forcedInclude setting.

denis-shienkov commented 3 years ago

Hi @sean-mcmanus ,

will only affect lexical colorization

Ohh.. ok, clear, thanks.

which might be worked around via using something like

I have checked it already, and this does not work too. The intelliSence marks this keywords (with all expression) as error.

Is there are other right way to insert a new keywords to the IntelliSense? Because this #ifdef is not soo good way...

UPD: So, as I understand, is it not enough just to use the TextMate feature? I need also implement an additional steps and for the IntelliSense? Maybe is it possible to add some new API to the intelliSence, e.g. a new property which is an array of additional keywords or something else?

sean-mcmanus commented 3 years ago

Can you give an example of IntelliSense giving an error with the keyword used? I'm not seeing an error: image

Another alternative is to add the keywords to the "defines" section, like image

Our IntelliSense engine doesn't currently have an extensibility model for adding keywords.

denis-shienkov commented 3 years ago

Can you give an example of IntelliSense giving an error with the keyword used? I'm not seeing an error:

Yes, of course:

изображение

изображение

Anyway, it is not an option to add the fake defines for that, because then the compilation can fails (and using the additional #ifdef __INTELLISENSE__ in many places is not good also.

Also, how to define the @ symbol?

Another alternative is to add the keywords to the "defines" section, like

Could you please point me, what is a file with this section?

Our IntelliSense engine doesn't currently have an extensibility model for adding keywords.

That's a bad, bad, bad. ;) It would be good to have this feature (even better if it was possible to change dynamically from the typescript code.)

denis-shienkov commented 3 years ago

UPD: Yes, many thanks, for some compilers it will work:

изображение

But for other - will not work... Because there are impossible to use a fake defines for that (e.g. previous example with the @ symbol).

denis-shienkov commented 3 years ago

Also, it fails for the following combination:

изображение

изображение

sean-mcmanus commented 3 years ago

The "@" case isn't part of the C++ standard language. Our compiler is currently only scoped to handle compilers based on gcc, clang, cl.exe, and EDG compilers that support the standard C++ language features (e.g. https://en.cppreference.com/w/cpp/compiler_support ). You would have to replace all @ occurrences with a macro that defines nothing if __INTELLISENSE__ is defined and uses @ otherwise. Our IntelliSense parser is based on https://www.edg.com/c -- so support for Keil extensions would need to originate from them, and from the list of customers at https://www.edg.com/customers, it looks like the Keil compiler is implemented using EDG as well, so the Keil team would need to somehow push their compiler changes back to the EDG team so other uses of EDG could benefit.

The "defines" section I'm referring to is in c_cpp_properties.json or C_Cpp.default.defines.

For the srf case, you would need to define it to something like "int" that would compile.

We had a request to add Keil support, but it got closed due to not enough votes: https://github.com/microsoft/vscode-cpptools/issues/3222 ...you could file another request and we could see if it gets more votes.

denis-shienkov commented 3 years ago

@sean-mcmanus ,

Ok, clear. Many thanks for your time and explanation.

github-actions[bot] commented 3 years ago

This feature request is being closed due to insufficient upvotes. When enough upvotes are received, this issue will be eligible for our backlog.

github-actions[bot] commented 2 years ago

This feature request has received enough votes to be added to our backlog.

slav4ocom commented 2 years ago

Helpfull information :) But in: void Uart2Isr() __interrupt (2) 2 gets squiggless. How to avoid this ?

slav4ocom commented 2 years ago

I found the way :)

#define UART2_INT 8
#ifdef __INTELLISENSE__
#define UART2_INT
#endif

and using is:

void Uart2Isr() __interrupt UART2_INT
{

}
mrx23dot commented 2 weeks ago

try to make it empty

#ifdef _MSC_VER
#define __data
#endif

or more sophisticated

// Macro to check if we are in VS Code
#if defined(_MSC_VER) || defined(__GNUC__)
    #define IN_VSCODE (getenv("TERM_PROGRAM") && strcmp(getenv("TERM_PROGRAM"), "vscode") == 0)
#else
    #define IN_VSCODE 0
#endif