Using clang static analyzer on linux with Objective C for arm-apple-darwin9-gcc

Hello,
I’m wondering if this is the right forum (mailing list) to ask for help with using Clang static analyzer ccc-analyzer on Linux with Objective C code for arm-apple-darwin9-gcc.

I have an Objective C project which compiles fine under Fedora 8 on x86 gcc-4.2.1 cross compiler for arm-apple-darwin9.

When I use scan-build with ccc-analyzer it fails to compile. The first compiler error is
/Projects/iphone/toolchain/toolchain/sys/usr/include/Foundation/NSObject.h:105: error: expected ‘;’ before ‘attribute

I can provide more info, please let me know what kind of info is needed.

Thanks

Hello,
I'm wondering if this is the right forum (mailing list) to ask for help with
using Clang static analyzer ccc-analyzer on Linux with Objective C code for
arm-apple-darwin9-gcc.

This is the right place to ask.

I have an Objective C project which compiles fine under Fedora 8 on x86
gcc-4.2.1 cross compiler for arm-apple-darwin9.

When I use scan-build with ccc-analyzer it fails to compile. The first
compiler error is
/Projects/iphone/toolchain/toolchain/sys/usr/include/Foundation/NSObject.h:105:
error: expected ‘;’ before ‘__attribute__’

I can provide more info, please let me know what kind of info is needed.

Is that error coming out of gcc? (The complete console output might
be useful here.) If it is, it's probably a bug in the scan-build
script. Also, if you could show a few lines around the error, it
might be useful for figuring out what exactly the error is complaining
about.

-Eli

I don't think it's a scan-build bug, but just a limitation of what the scan-build script can do. It doesn't know what version of gcc you want to use, so it just uses the system default gcc. It sounds like it isn't calling Jay's cross compiler.

scan-build "interposes" on a build by overriding the CC environment variable to point to ccc-analyzer. ccc-analyzer then calls 'gcc' to do the compilation, and 'clang-cc' to do the analysis.

Jay: To have scan-build use your cross-compiler, check out the '-use-cc' option. 'scan-build' with no options will tell you more.

Thanks Ted and Eli for the replies.

I’ve just tried --use-cc, I get about the same result as when I tried hacking around setting CCC_CC in my env to point to my cross compiler. (After digging through the perl code in ccc-analyzer)

When I use this method, I get a bunch of errors about not finding header files. This is because my cross compiler was compiled with --with-sysroot

Now I can change my Makefile to pass --sysroot=/home/foo/Projects/iphone/toolchain/toolchain/sys to cc,
which is the --with-sysroot parameter my compiler was compiled with. But it’s not clear to me why this needs to be done. If it’s using my cross compiler, shouldn’t it find these?

Anyway to workaround that I can alter my Makefile to pass --sysroot and -I/path/to/header/location to cc.

After doing that I get output like this below.

/home/foo/Projects/iphone/clang/llvm/tools/clang/utils/ccc-analyzer -c --sysroot=/home/foo/Projects/iphone/toolchain/toolchain/sys -I/home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include src/BrowseUITableViewController.m -o BrowseUITableViewController.o
In file included from src/BrowseUITableViewController.m:1:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIKit.h:10:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIActivityIndicatorView.h:8:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIView.h:10:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIInterface.h:10:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIColor.h:9:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/CoreGraphics/CoreGraphics.h:25:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/CoreGraphics/CGEvent.h:17:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/CoreServices/CoreServices.h:21:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/AE/AE.h:20:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/CarbonCore/CarbonCore.h:160:
/home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/fenv.h:41:6: error: #error This fenv header and set of APIs is intended for the ARM (with VFP) architecture only.
#error This fenv header and set of APIs is intended for the ARM (with VFP) architecture only.
^
In file included from src/BrowseUITableViewController.m:1:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIKit.h:46:
/home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UITableView.h:158:58: warning: property type ‘id’ is incompatible with type ‘id’ inherited from ‘UIScrollView’
@property(nonatomic,assign) id delegate;
^
In file included from src/BrowseUITableViewController.m:1:
In file included from /home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UIKit.h:51:
/home/foo/Projects/iphone/toolchain/toolchain/sys/usr/include/UIKit/UITextView.h:50:52: warning: property type ‘id’ is incompatible with type ‘id’ inherited from ‘UIScrollView’
@property(nonatomic,assign) id delegate;
^
3 diagnostics generated.

.^^ And the same for all of the other files in my project.

At the end I see:
scan-build: Removing directory ‘/tmp/scan-build-2009-05-28-1’ because it contains no reports.

The files do actually build–I see generated .o object files and the linked binary, but I don’t see what I’m looking for, or at least what I think I am looking for, which is warnings about memory allocation/deallocation.

Thanks again for your help with this.

Probably clang is using the wrong target triple; try setting CLANG to
"clang -ccc-host-triple arm-apple-darwin"

-Eli

Adding this to my Makefile
CLANG=‘clang -ccc-host-triple arm-apple-darwin’
(Tried with and without single quotes if that matters)

I get

cannot find ‘clang-cc’ in ‘clang’ command
make: *** [GridRow.o] Error 9

-J

That's a (recently introduced) ccc-analyzer issue. We plan on fixing it soon by rewriting ccc-analyzer to reuse the clang driver internals directly because the current implementation is really brittle. Unfortunately we won't start working on that for at least a couple weeks.

Ah, bleh, I didn't actually test this before I suggested it. Try r72538.

-Eli

Probably clang is using the wrong target triple; try setting CLANG to
“clang -ccc-host-triple arm-apple-darwin”

-Eli

Adding this to my Makefile
CLANG=‘clang -ccc-host-triple arm-apple-darwin’
(Tried with and without single quotes if that matters)

I get

cannot find ‘clang-cc’ in ‘clang’ command
make: *** [GridRow.o] Error 9

Ah, bleh, I didn’t actually test this before I suggested it. Try r72538.

-Eli

Sorry if this is a dumb question but I’m really new to Objective C and Clang.
What is r72538?

About Ted’s comment

That’s a (recently introduced) ccc-analyzer issue. We plan on fixing it soon by rewriting
ccc-analyzer to reuse the clang driver internals directly because the current
implementation is really brittle. Unfortunately we won’t start working on that for at
least a couple weeks.

Is there a quick/dirty workaround I could patch the perl code with?

Or some earlier revision I could revert to? (Would this mean checking out from svn and rebuilding everything again?)

-J

Sorry if this is a dumb question but I'm really new to Objective C and Clang.
What is r72538?

subversion revision. i.e. update your sources more or back to some particular reversion.

Or some earlier revision I could revert to? (Would this mean checking out from svn and rebuilding everything again?)

Yes, yes and yes.

-eric

Okay, so I’ve checked out two things here per the instructions at http://clang.llvm.org/get_started.html

One is
svn co [http://llvm.org/svn/llvm-project/llvm/trunk](http://llvm.org/svn/llvm-project/llvm/trunk) llvm
The other is
svn co [http://llvm.org/svn/llvm-project/cfe/trunk](http://llvm.org/svn/llvm-project/cfe/trunk) clang

Which one needs to be r72538 (clang?) and will the other work with that version using the latest on the trunk or do I also need to revert the other repo?

-J

They'll likely need to be the same version so you may want to get llvm to be approximately the same day as well.

-eric

It's the SVN revision; just run "svn update" in the LLVM and clang
directories, then run "make" again.

-Eli

Sorry if this is a dumb question but I'm really new to Objective C and
Clang.
What is r72538?

It's the SVN revision; just run "svn update" in the LLVM and clang
directories, then run "make" again.

Or you can just say 'make update' in the LLVM and everything gets updated.

- Fariborz

Probably clang is using the wrong target triple; try setting CLANG to
“clang -ccc-host-triple arm-apple-darwin”

-Eli

Adding this to my Makefile
CLANG=‘clang -ccc-host-triple arm-apple-darwin’
(Tried with and without single quotes if that matters)

I get

cannot find ‘clang-cc’ in ‘clang’ command
make: *** [GridRow.o] Error 9

Ah, bleh, I didn’t actually test this before I suggested it. Try r72538.

So I’ve fixed my header problems (I think) by supplying scan-build with --use-cc to point to my cross compiler, and I’ve hacked clang/lib/Frontend/InitHeaderSearch.cpp to remove the system default paths and then AddPath() paths to my alternate headers used by my cross compiler which was built with --with-sysroot.

I’m still stuck on this
#error This fenv header and set of APIs is intended for the ARM (with VFP) architecture only.

Setting CLANG to
CLANG=clang -ccc-host-triple arm-apple-darwin
Gives me the above error:
cannot find ‘clang-cc’ in ‘clang’ command

Trying the older revision r72538.did not help solve the problem.

Is there some way I could hack the source directly to force the host triple to arm-apple-darwin? Where would I do that?
Can I hard code this directly in Driver.cpp
const HostInfo *Driver::GetHostInfo(const char *TripleStr)

Thanks

-J

Bleh, try CLANG="clang -ccc-host-triple arm-apple-darwin -ccc-clang-archs arm".

-Eli

Setting CLANG to
CLANG=clang -ccc-host-triple arm-apple-darwin
Gives me the above error:
cannot find ‘clang-cc’ in ‘clang’ command

Bleh, try CLANG=“clang -ccc-host-triple arm-apple-darwin -ccc-clang-archs arm”.

Still get:
cannot find ‘clang-cc’ in ‘clang’ command

This comes from a die message in the perl code in
sub GetCCArgs()
I’m having a hard time figuring out what this function is trying to do, but it’s looking for the regex /clang-cc/ in some exec’ed clang command.

-J

Fantastic. Got things working.

Okay here’s the deal:
I’d really like to thank everyone here for your help. The prompt responses on this list have been amazing. Everyone is extremely polite and helpful. Much more than I expected from a mailing list.

When using a cross compiler compiled with --with-sysroot we need to use:
scan-build --use-cc
To point to the location of our cross compiler.

We need to hack lib/Frontend/InitHeaderSearch.cpp to remove system default include paths and add paths to our alternate sys root used by the cross compiler using AddPath()

We need to add -D__VFP_FP__ to our cc compiler args. (I call these CFLAGS in my Makefiles) This fixes the error:
#error This fenv header and set of APIs is intended for the ARM (with VFP) architecture only.

Now I have a beautiful html report with all of my many memory leaks that I can now fix.

Thanks again everyone for your help with this. You folks are great.

-J

When using a cross compiler compiled with --with-sysroot we need to use:
scan-build --use-cc
To point to the location of our cross compiler.

We need to hack lib/Frontend/InitHeaderSearch.cpp to remove system default
include paths and add paths to our alternate sys root used by the cross
compiler using AddPath()

Hopefully, we're going to have better cross-compilation support and
better documentation for how to use it in the near future.

We need to add -D__VFP_FP__ to our cc compiler args. (I call these CFLAGS in
my Makefiles) This fixes the error:
#error This fenv header and set of APIs is intended for the ARM (with VFP)
architecture only.

That sounds like it might be a bug in clang's ARM support. Would you
mind attaching the output of "gcc -dM -E -o - -x c /dev/null" with
your version of gcc?

-Eli