Package: iwyu
Version: 3.3-1

Let's consider the following example:

  $ cat test.c
  #include <glib.h>
  #include <stdio.h>

  int main() {
    char* f = g_strdup_printf("%s", "test");
    printf("%s\n", f);
    g_free(f);
    return 0;
  }

Running iwyu tells me to replace glib.h:

  $ iwyu `pkg-config --cflags glib-2.0` test.c
  In file included from test.c:1:
  In file included from /usr/include/glib-2.0/glib.h:32:
  In file included from /usr/include/glib-2.0/glib/galloca.h:34:
  In file included from /usr/include/glib-2.0/glib/gtypes.h:34:
  In file included from 
/usr/lib/x86_64-linux-gnu/glib-2.0/include/glibconfig.h:9:
  /usr/include/glib-2.0/glib/gmacros.h:40:10: fatal error: 'stddef.h' file not 
found
  #include <stddef.h>
           ^

  test.c should add these lines:
  #include "glib/gmem.h"                  // for g_free
  #include "glib/gstrfuncs.h"             // for g_strdup_printf

  test.c should remove these lines:
  - #include <glib.h>  // lines 1-1

  The full include-list for test.c:
  #include <stdio.h>                      // for printf
  #include "glib/gmem.h"                  // for g_free
  #include "glib/gstrfuncs.h"             // for g_strdup_printf
  ---

Following the recommendation of iwyu:

  $ cat test2.c
  #include "glib/gmem.h"                  // for g_free
  #include "glib/gstrfuncs.h"             // for g_strdup_printf
  #include <stdio.h>

  int main() {
    char* f = g_strdup_printf("%s", "lala");
    printf("%s\n", f);
    g_free(f);
    return 0;
  }

However, this is wrong and not permited by the glib headers. Compiling
it fails:

  $ gcc `pkg-config --cflags glib-2.0` test2.c
  In file included from test2.c:1:0:
  /usr/include/glib-2.0/glib/gmem.h:31:2: error: #error "Only <glib.h> can be 
included directly."
   #error "Only <glib.h> can be included directly."
    ^
  In file included from /usr/include/glib-2.0/glib/gmem.h:34:0,
                   from test2.c:1:
  /usr/include/glib-2.0/glib/gtypes.h:31:2: error: #error "Only <glib.h> can be 
included directly."
   #error "Only <glib.h> can be included directly."
    ^
  In file included from 
/usr/lib/x86_64-linux-gnu/glib-2.0/include/glibconfig.h:9:0,
                   from /usr/include/glib-2.0/glib/gtypes.h:34,
                   from /usr/include/glib-2.0/glib/gmem.h:34,
                   from test2.c:1:
  /usr/include/glib-2.0/glib/gmacros.h:35:2: error: #error "Only <glib.h> can 
be included directly."
   #error "Only <glib.h> can be included directly."
    ^
  In file included from /usr/include/glib-2.0/glib/gtypes.h:36:0,
                   from /usr/include/glib-2.0/glib/gmem.h:34,
                   from test2.c:1:
  /usr/include/glib-2.0/glib/gversionmacros.h:31:2: error: #error "Only 
<glib.h> can be included directly."
   #error "Only <glib.h> can be included directly."
    ^
  In file included from test2.c:2:0:
  /usr/include/glib-2.0/glib/gstrfuncs.h:31:2: error: #error "Only <glib.h> can 
be included directly."
   #error "Only <glib.h> can be included directly."
    ^

Also running iwyu on it will also display the errors:

  $ iwyu `pkg-config --cflags glib-2.0` test2.c
  In file included from test2.c:1:
  /usr/include/glib-2.0/glib/gmem.h:31:2: error: "Only <glib.h> can be included 
directly."
  #error "Only <glib.h> can be included directly."
   ^
  In file included from test2.c:1:
  In file included from /usr/include/glib-2.0/glib/gmem.h:34:
  /usr/include/glib-2.0/glib/gtypes.h:31:2: error: "Only <glib.h> can be 
included directly."
  #error "Only <glib.h> can be included directly."
   ^
  In file included from test2.c:1:
  In file included from /usr/include/glib-2.0/glib/gmem.h:34:
  In file included from /usr/include/glib-2.0/glib/gtypes.h:34:
  In file included from 
/usr/lib/x86_64-linux-gnu/glib-2.0/include/glibconfig.h:9:
  /usr/include/glib-2.0/glib/gmacros.h:35:2: error: "Only <glib.h> can be 
included directly."
  #error "Only <glib.h> can be included directly."
   ^
  /usr/include/glib-2.0/glib/gmacros.h:40:10: fatal error: 'stddef.h' file not 
found
  #include <stddef.h>
           ^

  (test2.c has correct #includes/fwd-decls)

It would be nice if iwyu could double-check its suggestions. Maybe it'd
be enough to check if running iwyu on it again doesn't produce any new
errors.

Regards
-- 
Sebastian Ramacher

Attachment: signature.asc
Description: Digital signature

Reply via email to