[cfe-dev] clang_parseTranslationUnit2 passes on incomplete type error

Jiri Olsa via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 18 06:14:46 PDT 2019

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 ;-)


#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(
			NULL, 0,
			unsaved_files, sizeof(unsaved_files)/sizeof(CXUnsavedFile),

	cerr << "Clang error " << error << endl;
	return 0;

More information about the cfe-dev mailing list