Skip to content

Clang error expected unqualified-id in comment when switching on comment parsing #153673

@renebarto

Description

@renebarto

I'm using the following header file to parse:

#pragma once

// My namespace
namespace X {
namespace Y {

class A
{
public:
    A() = default;
    virtual ~A() = default;
    virtual void foo() const;
};

} // namespace Y
} // namespace X

I Include this header in a cpp file:

#include "A.h"

int main(int argc, char **argv)
{
    X::Y::A a;
    a.foo();
    return 0;
}

Of course the class has an implementation:

#include "A.h"

#include <iostream>

using namespace X;
using namespace Y;

void A::foo() const
{
    std::cout << "A::foo()" << std::endl;
}

This set is a simple set to try playing with the AST parser.

When running the parser and switching on comment parsing:

clang::Preprocessor &preProcessor = compilerInstance.getPreprocessor();
preProcessor.SetCommentRetentionState(true, true);

I'm getting errors on the commented lines:

Caught exception: Parsing failed due to: [Error] "code/applications/simpleMain/include/A.h":16: expected unqualified-id
[Error] "code/applications/simpleMain/include/A.h":3: expected unqualified-id
[Error] "code/applications/simpleMain/include/A.h":15: expected unqualified-id
[Error] "code/applications/simpleMain/include/A.h":16: expected unqualified-id

I have no idea why clang would generate errors in this case. It may be that I'm doing something wrong, but this seems incorrect.

When I change one of the comment lines to use three forward slashes instead of two, I do get a comment callback, but still the same errors.

I'm using llvm 20.0.0git

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:as-a-librarylibclang and C++ APIclang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions