[llvm-dev] how to access certain kinds of diagnostics from libclang API?

Andrew Kelley via llvm-dev llvm-dev at lists.llvm.org
Sat Feb 15 22:07:27 PST 2020


Here's an example diagnostic:

andy at ark ~/tmp> clang -c test.c -march=bogus
error: unknown target CPU 'bogus'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom,
      silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7,
      westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell,
      core-avx2, broadwell, skylake, skylake-avx512, skx, cannonlake,
      icelake-client, icelake-server, knl, knm, k8, athlon64, athlon-fx,
      opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona,
      btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, x86-64


When using clang::ASTUnit::LoadFromCommandLine, both the clang::ASTUnit*
return value and the clang::ASTUnit* ErrAST out parameter are NULL. This
makes it impossible to access diagnostics with

    if (diags->hasErrorOccurred()) {
        clang::ASTUnit *unit = ast_unit ? ast_unit : err_unit.release();
        ZigList<Stage2ErrorMsg> errors = {};

        for (clang::ASTUnit::stored_diag_iterator it =
unit->stored_diag_begin(),
             it_end = unit->stored_diag_end(); it != it_end; ++it)


whereas for normal C compilation errors, this does work. Is there some
other way to access the diagnostics?

Here's a downstream issue report with more context:
https://github.com/ziglang/zig/issues/4455
See specifically the discussion starting here
https://github.com/ziglang/zig/issues/3210#issuecomment-586085463 which
may point to a bug or flaw in libclang's API.

Thanks for the help,
Andrew

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200216/45e7f874/attachment.sig>


More information about the llvm-dev mailing list