[prev in list] [next in list] [prev in thread] [next in thread] 

List:       cfe-dev
Subject:    Re: [cfe-dev] standalone tool: best way to find built-in includes?
From:       Sean Silva <chisophugis () gmail ! com>
Date:       2014-11-25 0:15:27
Message-ID: CAHnXoanDBwJ5qv1v3KsyP6yaMUx74fr8T+c-Ga0AYpSSDOHoqQ () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


On Fri, Nov 21, 2014 at 5:59 PM, Christian Convey <
christian.convey@gmail.com> wrote:

> Hi Sean:
> > It's actually possible for it to fail to build with the wrong builtin
> headers. E.g. an intrinsic was changed from using an __builtin_* function
> to using a different one that is not present in the other compiler.
>
> Are you basically reinforcing Manuel's warning, by saying there's a
> real-world example of this happening?
>

Yes.

-- Sean Silva


>
> Thanks,
> Christian
>
> On Fri, Nov 21, 2014 at 7:50 PM, Sean Silva <chisophugis@gmail.com> wrote:
> >
> >
> > On Fri, Nov 21, 2014 at 8:00 AM, Manuel Klimek <klimek@google.com>
> wrote:
> >>
> >> On Fri Nov 21 2014 at 4:40:44 PM Christian Convey
> >> <christian.convey@gmail.com> wrote:
> >>>
> >>> Hi Manuel,
> >>>
> >>> What really matters is that when my tool analyzes the target program's
> >>> source, it assumes the same standard headers that are normally used to
> >>> build that target program.
> >>>
> >>> For example, the target program might normally be built with clang
> >>> 3.4, but my analysis tool is built on clang 3.6.  When my tool
> >>> analyzes the source code of the target program, I want the analysis to
> >>> be as though the target program #include'd the 3.4 builtins, not the
> >>> 3.6 builtins.  Because my goal is to obtain the same AST as the one
> >>> created during the target program's normal (3.4) build process.
> >>
> >>
> >> The builtin headers are implementation details of the compiler. If you
> >> compile a file with a tool based on clang 3.6 and builtin headers of
> clang
> >> 3.4 the probability that you *don't* get a correct AST is higher.
> >
> >
> > It's actually possible for it to fail to build with the wrong builtin
> > headers. E.g. an intrinsic was changed from using an __builtin_*
> function to
> > using a different one that is not present in the other compiler.
> >
> > -- Sean Silva
> >
> >>
> >>
> >> If you want to get the same AST, you have to use the same version of
> >> clang. The question is, why is it important to you whether it's the same
> >> AST?
> >>
> >>>
> >>>
> >>> If I understand your suggestion, my analysis would give me an AST
> >>> based on the clang 3.6 builtins, not based on the 3.4 builtins.  Is
> >>> that correct?
> >>>
> >>> Thanks,
> >>> Christian
> >>>
> >>>
> >>>
> >>>
> >>>
> >>> On Fri, Nov 21, 2014 at 4:35 AM, Manuel Klimek <klimek@google.com>
> wrote:
> >>> > +1 to needs some refactoring, but generally the trick is: you don't
> >>> > want to
> >>> > use the builtin includes that are used by the clang you used to
> produce
> >>> > the
> >>> > compilation database, but the one that is current at the version at
> >>> > which
> >>> > you built your *tool*.
> >>> >
> >>> > Thus, optimally you'll install your tool into some/bin and the
> builtin
> >>> > headers into some/lib/clang/<version>.
> >>> >
> >>> > Cheers,
> >>> > /Manuel
> >>> >
> >>> > On Fri Nov 21 2014 at 5:23:16 AM Sean Silva <chisophugis@gmail.com>
> >>> > wrote:
> >>> >>
> >>> >> You can set CLANG_RESOURCE_DIR in CMake, or pass -resource-dir (the
> >>> >> default resource dir is created by
> >>> >> `llvm::sys::path::append(<exe-path>,
> >>> >> "..", "lib", "clang", CLANG_VERSION_STRING)`).
> >>> >>
> >>> >> This logic is actually duplicated in more than one place
> >>> >> unfortunately.
> >>> >> e.g. in CompilerInvocation::GetResourcesPath
> >>> >> (lib/Frontend/CompilerInvocation.cpp) and Driver::Driver
> >>> >> (lib/Driver/Driver.cpp); there's also some nastiness in
> >>> >> CIndexer::getClangResourcesPath. Needs some refactoring.
> >>> >>
> >>> >> -- Sean Silva
> >>> >>
> >>> >> On Thu, Nov 20, 2014 at 6:32 PM, Christian Convey
> >>> >> <christian.convey@gmail.com> wrote:
> >>> >>>
> >>> >>> Hi guys,
> >>> >>>
> >>> >>> I'm developed a standalone tool for analyzing source code.  It uses
> >>> >>> CommonOptionParser and ClangTool in what I think are the standard
> >>> >>> ways.
> >>> >>>
> >>> >>> My tool analyzes some other code ("foo.c") we have.  We build foo.c
> >>> >>> with clang the system-wide installed version of clang.  Thanks to
> >>> >>> cmake, we also produce a compilation database for that build of
> >>> >>> foo.c.
> >>> >>>
> >>> >>> I'd really like to ensure that when my analysis tool runs, I'd like
> >>> >>> it
> >>> >>> to simulate, as closely as possible, the way we normally build
> foo.c.
> >>> >>> In particular, I'd like to be sure it's using the same builtin
> >>> >>> headers
> >>> >>> and gcc-provided headers.
> >>> >>>
> >>> >>> Unfortunately, I can't easily copy my analysis tools executable
> into
> >>> >>> the same directory as the clang which we use to build foo.c.
> >>> >>>
> >>> >>> So here's my question: Is there a good way for me to force my tool
> to
> >>> >>> search the same include directories, in the same order, as our
> normal
> >>> >>> copy of clang does when it's building foo.c?
> >>> >>>
> >>> >>> I've tried running "clang -### ..." in the build system for foo.c,
> so
> >>> >>> that (I think) I get explicit information about the flags being
> >>> >>> passed
> >>> >>> to the front-end.  However, I haven't found a way to pass those
> flags
> >>> >>> to my analysis tool in a way that CommonOptionsParser and/or
> >>> >>> ClangTool
> >>> >>> find acceptable.  For example, they reject "-cc1".
> >>> >>>
> >>> >>> Thanks,
> >>> >>> Christian
> >>> >>> _______________________________________________
> >>> >>> cfe-dev mailing list
> >>> >>> cfe-dev@cs.uiuc.edu
> >>> >>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >>> >>
> >>> >>
> >>> >> _______________________________________________
> >>> >> cfe-dev mailing list
> >>> >> cfe-dev@cs.uiuc.edu
> >>> >> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
> >
> >
>

[Attachment #5 (text/html)]

<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov \
21, 2014 at 5:59 PM, Christian Convey <span dir="ltr">&lt;<a \
href="mailto:christian.convey@gmail.com" \
target="_blank">christian.convey@gmail.com</a>&gt;</span> wrote:<br><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex">Hi Sean:<br> <span class="">&gt; It&#39;s actually possible \
for it to fail to build with the wrong builtin headers. E.g. an intrinsic was changed \
from using an __builtin_* function to using a different one that is not present in \
the other compiler.<br> <br>
</span>Are you basically reinforcing Manuel&#39;s warning, by saying there&#39;s \
a<br> real-world example of this \
happening?<br></blockquote><div><br></div><div>Yes.</div><div><br></div><div>-- Sean \
Silva</div><div>  </div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"> <br>
Thanks,<br>
Christian<br>
<div class="HOEnZb"><div class="h5"><br>
On Fri, Nov 21, 2014 at 7:50 PM, Sean Silva &lt;<a \
href="mailto:chisophugis@gmail.com">chisophugis@gmail.com</a>&gt; wrote:<br> &gt;<br>
&gt;<br>
&gt; On Fri, Nov 21, 2014 at 8:00 AM, Manuel Klimek &lt;<a \
href="mailto:klimek@google.com">klimek@google.com</a>&gt; wrote:<br> &gt;&gt;<br>
&gt;&gt; On Fri Nov 21 2014 at 4:40:44 PM Christian Convey<br>
&gt;&gt; &lt;<a href="mailto:christian.convey@gmail.com">christian.convey@gmail.com</a>&gt; \
wrote:<br> &gt;&gt;&gt;<br>
&gt;&gt;&gt; Hi Manuel,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; What really matters is that when my tool analyzes the target \
program&#39;s<br> &gt;&gt;&gt; source, it assumes the same standard headers that are \
normally used to<br> &gt;&gt;&gt; build that target program.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; For example, the target program might normally be built with clang<br>
&gt;&gt;&gt; 3.4, but my analysis tool is built on clang 3.6.   When my tool<br>
&gt;&gt;&gt; analyzes the source code of the target program, I want the analysis \
to<br> &gt;&gt;&gt; be as though the target program #include&#39;d the 3.4 builtins, \
not the<br> &gt;&gt;&gt; 3.6 builtins.   Because my goal is to obtain the same AST as \
the one<br> &gt;&gt;&gt; created during the target program&#39;s normal (3.4) build \
process.<br> &gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; The builtin headers are implementation details of the compiler. If you<br>
&gt;&gt; compile a file with a tool based on clang 3.6 and builtin headers of \
clang<br> &gt;&gt; 3.4 the probability that you *don&#39;t* get a correct AST is \
higher.<br> &gt;<br>
&gt;<br>
&gt; It&#39;s actually possible for it to fail to build with the wrong builtin<br>
&gt; headers. E.g. an intrinsic was changed from using an __builtin_* function to<br>
&gt; using a different one that is not present in the other compiler.<br>
&gt;<br>
&gt; -- Sean Silva<br>
&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; If you want to get the same AST, you have to use the same version of<br>
&gt;&gt; clang. The question is, why is it important to you whether it&#39;s the \
same<br> &gt;&gt; AST?<br>
&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; If I understand your suggestion, my analysis would give me an AST<br>
&gt;&gt;&gt; based on the clang 3.6 builtins, not based on the 3.4 builtins.   Is<br>
&gt;&gt;&gt; that correct?<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Thanks,<br>
&gt;&gt;&gt; Christian<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; On Fri, Nov 21, 2014 at 4:35 AM, Manuel Klimek &lt;<a \
href="mailto:klimek@google.com">klimek@google.com</a>&gt; wrote:<br> &gt;&gt;&gt; \
&gt; +1 to needs some refactoring, but generally the trick is: you don&#39;t<br> \
&gt;&gt;&gt; &gt; want to<br> &gt;&gt;&gt; &gt; use the builtin includes that are \
used by the clang you used to produce<br> &gt;&gt;&gt; &gt; the<br>
&gt;&gt;&gt; &gt; compilation database, but the one that is current at the version \
at<br> &gt;&gt;&gt; &gt; which<br>
&gt;&gt;&gt; &gt; you built your *tool*.<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; Thus, optimally you&#39;ll install your tool into some/bin and the \
builtin<br> &gt;&gt;&gt; &gt; headers into some/lib/clang/&lt;version&gt;.<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; Cheers,<br>
&gt;&gt;&gt; &gt; /Manuel<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; On Fri Nov 21 2014 at 5:23:16 AM Sean Silva &lt;<a \
href="mailto:chisophugis@gmail.com">chisophugis@gmail.com</a>&gt;<br> &gt;&gt;&gt; \
&gt; wrote:<br> &gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; You can set CLANG_RESOURCE_DIR in CMake, or pass -resource-dir \
(the<br> &gt;&gt;&gt; &gt;&gt; default resource dir is created by<br>
&gt;&gt;&gt; &gt;&gt; `llvm::sys::path::append(&lt;exe-path&gt;,<br>
&gt;&gt;&gt; &gt;&gt; &quot;..&quot;, &quot;lib&quot;, &quot;clang&quot;, \
CLANG_VERSION_STRING)`).<br> &gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; This logic is actually duplicated in more than one place<br>
&gt;&gt;&gt; &gt;&gt; unfortunately.<br>
&gt;&gt;&gt; &gt;&gt; e.g. in CompilerInvocation::GetResourcesPath<br>
&gt;&gt;&gt; &gt;&gt; (lib/Frontend/CompilerInvocation.cpp) and Driver::Driver<br>
&gt;&gt;&gt; &gt;&gt; (lib/Driver/Driver.cpp); there&#39;s also some nastiness in<br>
&gt;&gt;&gt; &gt;&gt; CIndexer::getClangResourcesPath. Needs some refactoring.<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; -- Sean Silva<br>
&gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; On Thu, Nov 20, 2014 at 6:32 PM, Christian Convey<br>
&gt;&gt;&gt; &gt;&gt; &lt;<a \
href="mailto:christian.convey@gmail.com">christian.convey@gmail.com</a>&gt; \
wrote:<br> &gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Hi guys,<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; I&#39;m developed a standalone tool for analyzing source \
code.   It uses<br> &gt;&gt;&gt; &gt;&gt;&gt; CommonOptionParser and ClangTool in \
what I think are the standard<br> &gt;&gt;&gt; &gt;&gt;&gt; ways.<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; My tool analyzes some other code (&quot;foo.c&quot;) we \
have.   We build foo.c<br> &gt;&gt;&gt; &gt;&gt;&gt; with clang the system-wide \
installed version of clang.   Thanks to<br> &gt;&gt;&gt; &gt;&gt;&gt; cmake, we also \
produce a compilation database for that build of<br> &gt;&gt;&gt; &gt;&gt;&gt; \
foo.c.<br> &gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; I&#39;d really like to ensure that when my analysis tool \
runs, I&#39;d like<br> &gt;&gt;&gt; &gt;&gt;&gt; it<br>
&gt;&gt;&gt; &gt;&gt;&gt; to simulate, as closely as possible, the way we normally \
build foo.c.<br> &gt;&gt;&gt; &gt;&gt;&gt; In particular, I&#39;d like to be sure \
it&#39;s using the same builtin<br> &gt;&gt;&gt; &gt;&gt;&gt; headers<br>
&gt;&gt;&gt; &gt;&gt;&gt; and gcc-provided headers.<br>
&gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Unfortunately, I can&#39;t easily copy my analysis tools \
executable into<br> &gt;&gt;&gt; &gt;&gt;&gt; the same directory as the clang which \
we use to build foo.c.<br> &gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; So here&#39;s my question: Is there a good way for me to \
force my tool to<br> &gt;&gt;&gt; &gt;&gt;&gt; search the same include directories, \
in the same order, as our normal<br> &gt;&gt;&gt; &gt;&gt;&gt; copy of clang does \
when it&#39;s building foo.c?<br> &gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; I&#39;ve tried running &quot;clang -### ...&quot; in the \
build system for foo.c, so<br> &gt;&gt;&gt; &gt;&gt;&gt; that (I think) I get \
explicit information about the flags being<br> &gt;&gt;&gt; &gt;&gt;&gt; passed<br>
&gt;&gt;&gt; &gt;&gt;&gt; to the front-end.   However, I haven&#39;t found a way to \
pass those flags<br> &gt;&gt;&gt; &gt;&gt;&gt; to my analysis tool in a way that \
CommonOptionsParser and/or<br> &gt;&gt;&gt; &gt;&gt;&gt; ClangTool<br>
&gt;&gt;&gt; &gt;&gt;&gt; find acceptable.   For example, they reject \
&quot;-cc1&quot;.<br> &gt;&gt;&gt; &gt;&gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; Thanks,<br>
&gt;&gt;&gt; &gt;&gt;&gt; Christian<br>
&gt;&gt;&gt; &gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; &gt;&gt;&gt; cfe-dev mailing list<br>
&gt;&gt;&gt; &gt;&gt;&gt; <a \
href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br> &gt;&gt;&gt; \
&gt;&gt;&gt; <a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" \
target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br> \
&gt;&gt;&gt; &gt;&gt;<br> &gt;&gt;&gt; &gt;&gt;<br>
&gt;&gt;&gt; &gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; &gt;&gt; cfe-dev mailing list<br>
&gt;&gt;&gt; &gt;&gt; <a \
href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br> &gt;&gt;&gt; &gt;&gt; \
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" \
target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br> &gt;<br>
&gt;<br>
</div></div></blockquote></div><br></div></div>



_______________________________________________
cfe-dev mailing list
cfe-dev@cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic