clang_parseTranslationUnit2 passes on incomplete type error

hi,
when using clang_parseTranslationUnit2 to compile uncomplete
struct declaration, like:

  struct a {
    int a;
    int b;
    struct c c;
  };

I'm getting just warning on terminal but no error on the call itself:

  $ ./test
  definitions.h:1:35: error: field has incomplete type 'struct c'
  definitions.h:1:33: note: forward declaration of 'struct c'
  Clang error 0

Is there a way (some flag perhaps) to make this fail on this input,
or do I need to call clang_visitChildren and check the fields offsets
and sizes, because those seem mangled, offsets are zero and size -2.

I'm new to this, so I might be missing something obvious :wink:

thanks,
jirka

hi,
trying it once more.. any idea? :wink:

thanks,
jirka

hi,
when using clang_parseTranslationUnit2 to compile uncomplete
struct declaration, like:

  struct a {
    int a;
    int b;
    struct c c;
  };

I'm getting just warning on terminal but no error on the call itself:

  $ ./test
  definitions.h:1:35: error: field has incomplete type 'struct c'
  definitions.h:1:33: note: forward declaration of 'struct c'
  Clang error 0

Is there a way (some flag perhaps) to make this fail on this input,
or do I need to call clang_visitChildren and check the fields offsets
and sizes, because those seem mangled, offsets are zero and size -2.

I'm new to this, so I might be missing something obvious :wink:

Hey Jirka,

I think clang_parseTranslationUnit2 only returns an error when something
seriously failed. But it won't return an error on "normal" source input
errors. Instead, you'll get diagnostics for those. So in your case, I suggest
you iterate over the diagnostics (cf. clang_getNumDiagnostics and
clang_getDiagnostic), then check clang_getDiagnosticSeverity for every
diagnostic and error-our when you encounter an error.

Hope that helps

---
#include <clang-c/Index.h>
#include <iostream>
#include <vector>

using namespace std;

int main(int argc, char **argv)
{
  CXIndex index = clang_createIndex(1, 1);
  CXErrorCode error;
  CXTranslationUnit translation_unit;
  std::string input = "struct a { int a; int b; struct c c; };";

  CXUnsavedFile unsaved_files[] =
  {
    {
      .Filename = "definitions.h",
      .Contents = input.c_str(),
      .Length = input.size(),
    },
  };

  error = clang_parseTranslationUnit2(
      index,
      "definitions.h",
      NULL, 0,
      unsaved_files, sizeof(unsaved_files)/

sizeof(CXUnsavedFile),

      

CXTranslationUnit_DetailedPreprocessingRecord,

ok, I was hoping for some magical flag to force it that I overlooked :wink:
I'll check on the diagnostics iteration then

thanks,
jirka