CppSharp icon indicating copy to clipboard operation
CppSharp copied to clipboard

IgnoreHeadersWithName does not work

Open Rosentti opened this issue 3 years ago • 2 comments

Brief Description

This library doesn't seem to support cstdlib or ostream. I will need to ignore these headers to get output. The errors with these headers are:

/usr/include/stdlib.h(579,5): error: '__malloc__' attribute takes no arguments
/usr/include/stdlib.h(583,14): error: '__malloc__' attribute takes no arguments
/usr/include/stdlib.h(812,6): error: '__malloc__' attribute takes no arguments
/usr/include/wchar.h(155,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(189,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(201,25): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(223,39): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(260,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(285,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(294,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(303,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(309,32): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(315,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(322,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(830,24): error: '__malloc__' attribute takes no arguments

I have set the following into Preprocess:

 ctx.IgnoreHeadersWithName("cstdlib");
 ctx.IgnoreHeadersWithName("ostream");

But this does not work. The compile errors persist. If I manually remove these includes from the file, the compile succeeds.

OS: Windows / OS X / Linux (include version and/or distro) Arch Linux

Used headers
/* Generated with cbindgen:0.24.3 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>


namespace charcoal {

struct User;

struct FFI_User {
  const char *id;
  const char *username;
};


extern "C" {

void data_free(User *ptr);

FFI_User *get_sample_userlite_from_c();

} // extern "C"

} // namespace charcoal

Used settings

Target: MSVC/GCC/Clang C#

Other settings CppSharp installed from NuGet

Stack trace or incompilable generated code
Parsing libraries...
Parsed 'libcharcoal.so'
Parsing code...
Error parsing 'generated_bindings.h'
/usr/include/stdlib.h(579,5): error: '__malloc__' attribute takes no arguments
/usr/include/stdlib.h(583,14): error: '__malloc__' attribute takes no arguments
/usr/include/stdlib.h(812,6): error: '__malloc__' attribute takes no arguments
/usr/include/wchar.h(155,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(189,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(201,25): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(223,39): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(260,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(285,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(294,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(303,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(309,32): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(315,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(322,24): error: '__malloc__' attribute takes no arguments
/usr/include/stdio.h(830,24): error: '__malloc__' attribute takes no arguments
CppSharp has encountered an error while parsing code

Rosentti avatar Sep 25 '22 12:09 Rosentti

IgnoreHeadersWithName is only for parsed headers, given in your case the headers themselves are not being parsed due to the errors then it will make no difference.

We definitely support cstdlib and ostreambut this looks like a version mismatch issue. From a quick check, it looks like GCC11 added support for arguments in __malloc__:

The existing malloc attribute has been extended so that it can be used to identify allocator/deallocator API pairs. A pair of new -Wmismatched-dealloc and -Wmismatched-new-delete warnings will complain about mismatched calls, and -Wfree-nonheap-object about deallocation calls with pointers not obtained from allocation functions. Additionally, the static analyzer will use these attributes when checking for leaks, double-frees, use-after-frees, and similar issues.

So the issue is that your system headers are too recent for our Clang version, looks like there is a bug tracking this already: https://bugs.llvm.org/show_bug.cgi?id=52265

I would advise you to use a set of older system includes (you could copy them out of a Docker container for instance), you can see how to setup CppSharp here: https://github.com/mono/CppSharp/blob/main/src/CppParser/ParserGen/ParserGen.cs#L93

tritao avatar Sep 25 '22 12:09 tritao

Thanks for the quick answer! I removed the includes manually, but it only outputted an Std.cs file with the following contents:

// ----------------------------------------------------------------------------
// <auto-generated>
// This is autogenerated code by CppSharp.
// Do not edit this file or all your changes will be lost after re-generation.
// </auto-generated>
// ----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Security;
using __CallingConvention = global::System.Runtime.InteropServices.CallingConvention;
using __IntPtr = global::System.IntPtr;

Rosentti avatar Sep 25 '22 13:09 Rosentti