[cfe-dev] clang_parseTranslationUnit2 passes on incomplete type error

Milian Wolff via cfe-dev cfe-dev at lists.llvm.org
Mon Apr 29 04:48:42 PDT 2019


On Thursday, April 18, 2019 3:14:46 PM CEST Jiri Olsa via cfe-dev wrote:
> 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 ;-)

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,
> 			&translation_unit);
> 
> 	cerr << "Clang error " << error << endl;
> 	return 0;
> }
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


-- 
Milian Wolff
mail at milianw.de
http://milianw.de





More information about the cfe-dev mailing list