MochiLibraries / Biohazrd

A framework for automatically generating binding wrappers for C/C++ libraries
MIT License
61 stars 10 forks source link

Synthesized cursors which are promoted to be in-scope results in a confusing warning. #216

Closed PathogenDavid closed 3 years ago

PathogenDavid commented 3 years ago

This was observed with a generator for the Windows API. In particular on the _GUID struct in guiddef.h:

// From line 22 of guiddef.h:
typedef struct _GUID {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    unsigned char  Data4[ 8 ];
} GUID;

For whatever reason, Clang 12.0.1 is synthesizing a TypeVisibilityAttr associated with this record, (this didn't happen in Clang 10) and the attribute has no associated file. This results in bogus, confusing diagnostics:

Warning: Out-of-scope file '<>Synthesized' was included inside a declaration from in-scope file 'C:\Program Files (x86)\Windows Kits\10\Include\10.0.14393.0\shared\guiddef.h'. The file was implicitly promoted to be in-scope in the context of the containing declaration.
Warning at : Attribute of unrecognized kind: CX_AttrKind_TypeVisibility

The first warning mentions <>Synthesized because we replace null files from Clang with the synthesized declaration file:

https://github.com/InfectedLibraries/Biohazrd/blob/d7361c408508a607cf5d7b525b0eeff6667a4f88/Biohazrd/TranslationUnitParser.cs#L166-L169

The second warning mentions no file at all because Clang isn't returning a file name since the declaration is null.


For the first warning, I think the easiest solution is to simply not promote the synthesized file placeholder.

For the second warning we probably shouldn't be warning on visibility attributes as it is. Ignoring that, I also tried to modify the diagnostic builders for Clang cursors to use the parent of the cursor for location info if it's missing:

https://github.com/InfectedLibraries/Biohazrd/blob/d7361c408508a607cf5d7b525b0eeff6667a4f88/Biohazrd/DiagnosticCollectionExtensions.cs#L34-L38

Unfortunately these synthesized cursors don't even have parents (lexical or semantic), so that's not an option. Let's wait to fix that until a more legitimate case comes up. (And maybe keep it in mind for whenever we revamp diagnostics, assuming that still happens.)