[cfe-dev] clangd/libclang: how to emulate other compilers?

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Thu Apr 19 03:25:00 PDT 2018


On Wed, Apr 18, 2018 at 9:37 PM Milian Wolff <mail at milianw.de> wrote:

> On Mittwoch, 18. April 2018 12:25:40 CEST Manuel Klimek wrote:
> > On Tue, Apr 17, 2018 at 8:02 PM Milian Wolff via cfe-dev <
> >
> > cfe-dev at lists.llvm.org> wrote:
> > > Hey all,
> > >
> > > how does clangd or other users of the libclang handle situations where
> you
> > > want to parse code that is dependent on a certain other compiler or
> > > compiler
> > > environment? The most common scenario being embedded projects that
> rely on
> > > the
> > > compiler-builtin defines and include paths to find the sysroot include
> > > paths
> > > and such.
> >
> > I'm not sure I understand what you mean - do you mean the compiler has
> > builtins that clang doesn't provide and relies on their existence?
>
> Take this example code:
>
> ```
> #ifndef __arm__
> #error unsupported platform
> #endif
>
> #include <foobar.h>
>
> static_assert(sizeof(void*) == 4);
> ```
>
> How can I parse this with libclang, such that it emulates my arm-none-eabi-
> gcc?


> - __arm__ should be defined, but not __x86_64__
> - foobar.h should be found in the default include paths for the
> arm-none-eabi-
> gcc compiler, not in the default include paths of libclang
> - it should be 32bit by default
>

Using clang -target arm-eabi seems to do the trick?


> Now, we can get there to some degree via -nostdinc and -nostdinc++. But
> once
> you do that for all compilers, you get into nasty issues when you replace
> the
> libclang builtin headers with headers from a different clang version or
> even a
> different compiler like GCC. They will be compiler specific and not
> portable,
> thus not parsable by libclang.
>
> > Generally, you'll want to use the builtin defines and includes from clang
> > (at the point at which you compiled libclang), but the standard library
> and
> > so forth that the system is using. Clang should be able to find that
> given
> > the right flags.
>
> Can you tell us what the right flags would be? I just looked at the man
> page
> again and found -nostdlibinc, which may resolve this partially - I'll
> check.
>

Again, there are multiple levels of builtin includes. If you want the right
ones for a target platform, you'll need to select that target platform - if
that doesn't work with clangd, we need to fix it :)


>
> Cheers
>
> --
> Milian Wolff
> mail at milianw.de
> http://milianw.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180419/4813370a/attachment.html>


More information about the cfe-dev mailing list