Language Service: Unable to resolve type when managing namespaces via header and preprocessor

Open
#8,324 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Reproduce the behavior using main.cpp and namespace.hpp from the issue, then compare resolution for b::Foo with a:🅱️:Foo and Foo. Investigate the language service's handling of preprocessor-managed nested namespaces and verify that main.cpp has no error squiggles and that b::Foo resolves to its declaration.

Written by the indexing model from the issue text.

Description

bug Language Service Visual Studio

Bug type: Language Service

Describe the bug

  • OS and Version: Windows 10 Pro 21H1 19043.1288
  • VS Code Version: 1.61.2 (user setup)
  • C/C++ Extension Version: v1.7.1
  • Other extensions you installed (and if the issue persists after disabling them):
    • Local
      • jeff-hykin.better-cpp-syntax (disabled)
      • usernamehw.errorlens (disabled)
      • ms-vscode-remote.remote-ssh
      • ms-vscode-remote.remote-ssh-edit (disabled)
      • ms-vscode-remote.remote-wsl (disabled)
    • SSH
      • ms-vscode.cpptools
      • twxs.cmake (disabled)
      • ms-vscode.cmake-tools (disabled)
      • mhutchie.git-graph (disabled)
      • eamodio.gitlens (disabled)
      • ms-python.vscode-pylance (disabled)
      • ms-python.python (disabled)
  • If using SSH remote, specify OS of remote machine: Ubuntu 20.04.3 LTS
  • When managing a nested namespace with the preprocessor via a header file, a type inside the nested namespace is not recognized from inside the namespace when specifying the inner namespace explicitly. See Steps to reproduce for clarification.

Steps to reproduce

  1. Open a folder with the following two files:

    // main.cpp
    
    #include "namespace.hpp"
    
    class Foo {};
    
    b::Foo *foo;
    
    #include "namespace.hpp"
    
    int main() {}
    
    // namespace.hpp
    
    #ifndef NAMESPACE_A_B_OPEN
        #define NAMESPACE_A_B_OPEN
        namespace a {
            namespace b {
    #else
            }
        }
    #endif
    
  2. See the following errors:

    • b:: has error squiggles with the following two problems:
    this declaration has no storage class or type specifier C/C++(77)
    
    expected a ';'C/C++(65)
    
    • b::Foo does not correctly link to its declaration above when pressing CTRL+CLICK and shows no information when hovering the mouse over it.

    • endif has error squiggles with the following two problems (this one might be unrelated to the other errors - I don't really care about this one):

    PCH warning: header stop not at file scope.  An IntelliSense PCH file was not generated. C/C++(2923)
    
    expected a '}' C/C++(67)
    

Expected behavior

No error squiggles in main.cpp and correctly resolving b::Foo.

Additional context

The following changes to the code circumvent the problem:

  • Specify the outer namespace as well:

    -b::Foo *foo;
    +a::b::Foo *foo;
    
  • Don't specify the inner namespace:

    -b::Foo *foo;
    +Foo *foo;
    
  • Manually including namespace.hpp by copying the content of namespace.hpp to the two lines with #include "namespace.hpp".

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

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.

More from microsoft/vscode-cpptools

All issues in microsoft/vscode-cpptools

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.