microsoft / microsoft/vscode-cpptools

Incorrect Colorization for Custom Iterator Type

Open
#9,123 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug type: Language Service

Describe the bug

  • OS and Version: MacOS Big Sur 11.4
  • VS Code Version: 1.66.0
  • C/C++ Extension Version: 1.9.7
  • Other extensions you installed (and if the issue persists after disabling them): None

Workspace is a single C++ file containing a definition of a custom std::forward_iterator_tag implementation. Colorization of this is incorrect when used in a for loop as in below picture:

image

Steps to reproduce
Open MWE with setting "C_Cpp.enhancedColorization": "Enabled".

Expected behavior
Brackets are colorized as normal.

Code sample and logs

  • Code sample
#include <iostream>

class Foo {
    public:
        int a;
        
        Foo(int a) : a( a ) {}
};

class FooIterator : public std::forward_iterator_tag {
    public:
        int a;
        const Foo* f;
        
        FooIterator(int a, const Foo& f) : a( a ), f( &f ) {}
        
        FooIterator operator++() {
            advance();
            return *this;
        }
        
        bool operator==(const FooIterator& f) const {
            return this->a == f.a;
        }
        
        bool operator!=(const FooIterator& f) const {
            return this->a != f.a;
        }

        int operator*() const {
            return a;
        }
        
        void advance() {
            a++;
        }
};

FooIterator begin(const Foo& f) {
    return FooIterator(0, f);
}

FooIterator end(const Foo& f) {
    return FooIterator(f.a, f);
}

int main() {
    Foo f(10);
    
    for (int a : f) {
        std::cout << a << std::endl;
    }
    
    return 0;
}
  • Configurations in c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/System/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c17",
            "cppStandard": "c++20"
        }
    ],
    "version": 4
}
  • Logs from running C/C++: Log Diagnostics from the VS Code command palette
-------- Diagnostics - 3/31/2022, 3:25:00 PM
Version: 1.9.7
Current Configuration:
{
    "name": "Mac",
    "includePath": [
        "${workspaceFolder}/**"
    ],
    "defines": [],
    "macFrameworkPath": [
        "/Library/Developer/CommandLineTools/SDKs/MacOSX11.sdk/System/Library/Frameworks"
    ],
    "compilerPath": "/usr/bin/g++",
    "cStandard": "c17",
    "cppStandard": "c++20",
    "compilerPathIsExplicit": true,
    "cStandardIsExplicit": true,
    "cppStandardIsExplicit": true,
    "intelliSenseModeIsExplicit": false,
    "compilerArgs": [],
    "mergeConfigurations": false,
    "browse": {
        "path": [
            "${workspaceFolder}/**"
        ],
        "limitSymbolsToIncludedHeaders": true
    }
}
Translation Unit Mappings:
[ /Users/Lars/Desktop/ColoringIssue/main.cpp ]:
    /Users/Lars/Desktop/ColoringIssue/main.cpp
Translation Unit Configurations:
[ /Users/Lars/Desktop/ColoringIssue/main.cpp ]:
    Process ID: 34495
    Memory Usage: 139 MB
    Compiler Path: /usr/bin/g++
    Includes:
        /usr/local/include
        /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include/c++/v1
        /Library/Developer/CommandLineTools/usr/lib/clang/12.0.5/include
        /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/usr/include
        /Library/Developer/CommandLineTools/usr/include
    Frameworks:
        /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Frameworks
    Standard Version: c++20
    IntelliSense Mode: macos-clang-x64
    Other Flags:
        --clang
        --clang_version=110000
Total Memory Usage: 139 MB

------- Workspace parsing diagnostics -------
Number of files discovered (not excluded): 30097
loggingLevel: Debug
loggingLevel has changed to: Debug
cpptools/didChangeCppProperties
$/setTraceNotification
cpptools/getCodeActions: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 82)
cpptools/textEditorSelectionChange
cpptools/textEditorSelectionChange
cpptools/activeDocumentChange: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/getFoldingRanges: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 83)
cpptools/textEditorSelectionChange
cpptools/textEditorSelectionChange
cpptools/activeDocumentChange: /Users/Lars/Desktop/ColoringIssue/main.cpp
willSaveWaitUntil: 0ms
textDocument/didSave: /Users/Lars/Desktop/ColoringIssue/main.cpp
  tag parsing file: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/fileChanged: /Users/Lars/Desktop/ColoringIssue/main.cpp
idle loop: reparsing the active document
Checking for syntax errors: /Users/Lars/Desktop/ColoringIssue/main.cpp
Queueing IntelliSense update for files in translation unit of: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/finishUpdateSquiggles
Error squiggle count: 0
Update IntelliSense time (sec): 0.258
cpptools/getSemanticTokens: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 85)
textDocument/didClose: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/getCodeActions: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 86)
textDocument/didOpen: /Users/Lars/Desktop/ColoringIssue/main.cpp
Checking for syntax errors: /Users/Lars/Desktop/ColoringIssue/main.cpp
Queueing IntelliSense update for files in translation unit of: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/textEditorSelectionChange
cpptools/getDocumentSymbols: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 87)
cpptools/textEditorSelectionChange
cpptools/getSemanticTokens: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 88)
cpptools/activeDocumentChange: /Users/Lars/Desktop/ColoringIssue/main.cpp
cpptools/getDocumentSymbols
cpptools/getFoldingRanges: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 89)
textDocument/hover: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 90)
Request canceled: 90
cpptools/finishUpdateSquiggles
Error squiggle count: 0
terminating child process: 34560
Update IntelliSense time (sec): 1.05
cpptools/getFoldingRanges: /Users/Lars/Desktop/ColoringIssue/main.cpp (id: 91)

Screenshots

Looks like the tokens given to the bracket are very wrong:
Screen Shot 2022-03-31 at 15 27 00

as compared to a normal for loop:
Screen Shot 2022-03-31 at 15 27 31

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

Start with the supplied main.cpp and reproduce the issue with C_Cpp.enhancedColorization set to Enabled. Inspect the semantic tokens reported for the custom iterator's range-based for loop and compare them with the normal loop; done means the brackets receive the expected colorization without introducing syntax errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, vscode
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.