Menu

1 / 3

Simulating Madness: How We Forced a C++ Parser to Lie for Backward Compatibility

DEV Community: cpp·Flude team·2 days ago
#xs5V3D7R
#devtocpp#devto#dev#anonymous#struct
Long article (674 words) — no AI summary yet.
Reading 0:00
15s threshold

In C++, entities without a name legally exist—anonymous namespaces and structs. For example, a nested struct class Widget { struct { int x, y; } coords; }; .

At first, we encountered this while parsing the XML from the old generator: it output empty <compoundname> tags for anonymous namespaces, causing our parser to crash with a scream. We taught it to painlessly collapse them into the global scope. But the real war began when we reached anonymous structs in our new native AST parser based on libclang .

The Chaos of Absolute Paths

When the native libclang parser encounters an anonymous struct, it needs to name it somehow inside the Abstract Syntax Tree (AST). And the compiler honestly generates a purely technical name for it: (unnamed struct at /home/user/workspace/repo/include/widget.h:42:5) .

The problem is that this name includes the absolute path to the file on the hard drive. On one developer's machine, the path is one thing; on the CI server, it's another.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.