how to generate reports for C code with clang - llvm

Following the directions from the main clang static analyzer web page (http://clang-analyzer.llvm.org/scan-build.html)...
I have a small C file that is heinously bug-ridden (badcode.c):
int main(int argc, char ** argv)
{
int j;
int a[4];
puts(a[j]);
return 'a';
}
In order to get a basic idea how the clang static analyzer (scan-build) words, I run:
scan-build -v clang badcode.c
It outputs:
scan-build: Emitting reports for this run to '/tmp/scan-build-2012-08-17-1'.
scan-build: 'clang' executable not found in '/usr/share/clang/scan-build/bin'.
scan-build: Using 'clang' from path: /usr/bin/clang
badcode.c:7:2: warning: implicit declaration of function 'puts' is invalid in C99 [-Wimplicit-function-declaration]
puts(a[j]);
^
1 warning generated.
scan-build: Removing directory '/tmp/scan-build-2012-08-17-1' because it contains no reports.
Ok, great, clang gives a little warning, but a.out is still produced. And how come it doesn't produce a report? The unititialized variable j should be a painfully obvious red flag to any static analyzer -- why isn't it reported?
Am I simply using the wrong command line arguments?

Try this:
scan-build -k -V -o scan-reports xcodebuild clean build -configuration Debug -sdk [sdk-version] -xcconfig=[xconfig-certificate-file]
E.g.:
scan-build -k -V -o scan-reports xcodebuild clean build -configuration Debug -sdk iphoneos5.0 -xcconfig="/Users/username/config.xcconfig"
This should create a html output.

Related

Cross compilation using clang for target aarch64-cros-linux-gnu

Maybe I've got a silly question, but I stuck with that issue. I'm trying to do cross compilation using clang-5.0 of simple test program for target aarch64-cros-linux-gnu:
int main(int argc, const char **argv)
{
return 0x2;
}
So, when I'm running clang with the next command:
/home/alex/Dev/CustomToolchains/Google/clang-5.0/aarch64/usr/bin/clang --sysroot=/home/alex/Dev/CustomToolchains/Google/clang-5.0/aarch64/usr/aarch64-cros-linux-gnu -Qunused-arguments -grecord-gcc-switches -fPIE -D_FORTIFY_SOURCE=2 -fstack-protector-strong -pie -fno-omit-frame-pointer main.cxx -o main3 -B/home/alex/Dev/CustomToolchains/Google/clang-5.0/aarch64/bin -target aarch64-cros-linux-gnu
I'm getting an error:
ld.bfd: no input files
clang: error: linker command failed with exit code 1 (use -v to see invocation)
But when I'm removing --sysroot and -target parameters everything is ok. I have checked --sysroot path and it's exists but I'm not sure if it correct. clang docs says that this folder must contain bin, lib, include directories, but in the sysroot directory there are only next folders:
lib lib64 NOTICE-eglibc NOTICE-gdb sbin sys-include usr var
So, how I can specify correct toolchain path to the clang for my target triple aarch64-cros-linux-gnu? or where I can get that toolchain?
Big thanks for answers.
PS. I'm a newbie with all linux/clang/cros compilation -related staff, so I'm sorry for mistakes in my question.

How to Enable C++11 Features in Codelite

The following code compiles and runs in Xcode 5 and in Visual Studio 2013. I am interested in trying out Codelite, but Codelite will not compile the following program (a problem since I am working with scoped enums in my project). As far as I understand it, Codelite is using the same compiler as Xcode.
Is the code valid per C++11? Why is Codelite unable to compile it?
#include <iostream>
namespace abc
{
namespace xyz
{
enum class SampleEnum
{
SomeValue = 0,
SomeOtherValue = 1
};
}
}
int main(int argc, char **argv)
{
abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
return 0;
}
Here is the build output from Codelite. In case it's garbled, it's pointing to the word "SampleEnum" in the instantiation of the variable and saying "expected a class or namespace".
/bin/sh -c 'make -j8 -e -f Makefile'
----------Building project:[ ClangTest - Debug ]----------
codelite-cc /usr/bin/clang++ -c "/Users/xxx/Desktop/Test/ClangTest/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I.
/Users/xxx/Desktop/Test/ClangTest/main.cpp:7:8: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class SampleEnum
^
/Users/xxx/Desktop/Test/ClangTest/main.cpp:17:40: error: expected a class or namespace
abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
~~~~~~~~~~^
1 warning and 1 error generated.
make[1]: *** [Debug/main.cpp.o] Error 1
make: *** [All] Error 2
2 errors, 1 warnings
It is necessary to pass -std=c++11 to the compiler to enable C++11 features. Here are the steps to do so in Codelite:
Right click on the project in the workspace view.
Select Settings near the bottom of this pop-up menu. Common Settings->Compiler->C++ Compiler Options
Click into the semicolon delimited list of compiler switches to reveal elipses and click on the elipses.
Click the checkbox for -std=c++11
If you are using C++11 extensions, compilers want it to be flagged. Without it they may throw warnings and errors. That's because some of C++11 changes are not backward-compatible, e.g. the use of auto.
For example, in gcc you should have
gcc -std=c++11
Check if your compiler shouldn't have such parameter as well!
I suppose this is because that your default std version is not c++11. To change to c++11, if you are using your terminal, you should type in the following command:
g++ yourfile.cpp -std=c++11

How to install clang in custom location from SVN, making it recognize c++ standard library, on OS X

I try to compile and use clang from svn trunk. I basically try to follow the directions here:
svn co -q http://llvm.org/svn/llvm-project/llvm/trunk llvm
svn co -q http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
svn co -q http://llvm.org/svn/llvm-project/clang-tools-extra/trunk llvm/tools/clang/tools/extra
svn co -q http://llvm.org/svn/llvm-project/compiler-rt/trunk llvm/projects/compiler-rt
mkdir llvm_build_release
cd llvm_build_release
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/usr/local -DLLVM_TARGETS_TO_BUILD=host ../llvm
make -j12
make install
Above, I configure clang to be installed in the custom location ~/usr/local since I want to be able to play with it without changing my default environment.
I then create a simple test.cpp:
#include <iostream>
int main(int argc, const char* argv[]){
std::cout << "Hello world\n";
return 0;
}
and try to compile it:
~/usr/local/bin/clang++ test.cpp -o test
but get the error message:
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
(using the system version of clang, the same compilation works fine).
If I manually enter which standard library to use, it does work
~/usr/local/bin/clang++ -std=c++11 -stdlib=libstdc++ -I/usr/include/c++/4.2.1/ -L/usr/lib test.cpp -o test
The question is: How do I configure, compile and install clang from source so that I do not have to enter these standard library settings, but instead can just enter the ordinary ~/usr/local/bin/clang++ test.cpp -o test? I have macports installed, with its version of the standard libraries and the include files, if that helps.

If I use g++ as my compiler, how do I scan a c++ file with the clang static analyzer?

I use g++ to compile my C++ project. When I try to use the clang static analyzer (scan-build) to check my code, I get an error:
>> scan-build g++ main.cpp
could not find clang line
How do I use the scan-build tool with g++?
It appears that scan-build is having trouble recognizing "g++" as the compiler command. It expects "clang" or "gcc". If you replace "g++" with "gcc -lstdc++" to build your project, the scan-build tool will work properly.
>> scan-build gcc -lstdc++ main.cpp
main.cpp:7:3: warning: Assigned value is garbage or undefined
int y = x;
^ ~
1 warning generated.
scan-build: 1 bugs found.
scan-build: Run 'scan-view /var/folders/2l/2l6vhCnVFNad-O8ryd5YO++++TI/-Tmp-/scan-build-2011-09-18-2' to examine bug reports.

Dealing with "C compiler cannot create executables" in Cygwin

Whatever I try to compile in Cygwin I get the following output:
checking for mingw32 environment... no
checking for EMX OS/2 environment... no
checking how to run the C preprocessor... gcc -E
checking for gcc... gcc
checking whether the C compiler (gcc ) works... no
configure: error: installation or configuration problem: C compiler cannot creat
e executables.
The last few lines of the logfile look like this:
configure:2810: checking for EMX OS/2 environment
configure:2822: gcc -c conftest.c 1>&5
configure: In function `main':
configure:2818: error: `__EMX__' undeclared (first use in this function)
configure:2818: error: (Each undeclared identifier is reported only once
configure:2818: error: for each function it appears in.)
configure: failed program was:
#line 2815 "configure"
#include "confdefs.h"
int main() {
return __EMX__;
; return 0; }
configure:2838: checking how to run the C preprocessor
configure:2859: gcc -E conftest.c >/dev/null 2>conftest.out
configure:2943: checking for gcc
configure:3056: checking whether the C compiler (gcc ) works
configure:3072: gcc -o conftest conftest.c -llib 1>&5
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find
-llib
collect2: ld returned 1 exit status
configure: failed program was:
#line 3067 "configure"
#include "confdefs.h"
main(){return(0);}
This is a fresh Cygwin install with G++ and a bunch of other devtools added. Any idea what I need to do to get this thing working?
Update 0: Nick, your link to http://www.geektimes.com/linux/troubleshooting/c-cant-create-executables.html was tried already - unfortunately this instructions are for redhat and do not seem to apply to cygwin.
Your Configure is wrong.
Usually autoreconf -f helps. If not you need to check the failing rule and fix it.
The '-llib' seems a bit unusual to me, but I'm far from an expert. Just out of curiosity is autoconf installed? I had some problems similar to this until I installed autoconf. It seems like the configure script is incorrectly generating a '-llib' value but it's hard to say why based on just the snippets posted.
When I've had this problem, it has been a link error caused by Cygwin looking for "library.o" instead of "library.obj" or "library.so" instead of "library.dll".