Is there a CLI flag for clang that will go through the entire compilation process, including assembling and linking, printing all errors or warnings, but will not create files on the disk?
In particular, I don't need any .out files, nor object (.o) files.
When you're generating a single file, you can use the -o option to choose the output file, so -o /dev/null will just get rid of the output without generating any file. You'll still get warnings and errors printed to stderr.
This works in both gcc and clang.
Related
I want to create a software-generated control flow diagram for this code using LLVM CFG https://github.com/inmcm/Simon_Speck_Ciphers/blob/master/C/speck.c
But when I followed the instructions given on the LLVM webpage (https://www.programmersought.com/article/61364910575/), and typed the following command: clang -S -emit-llvm simon.c -o g.ll
Terminal shows this error message:
clang: error: -emit-llvm cannot be used when linking
I am new to this tool and Linux, can someone please help?
I was able to generate the llvm IR and the CFG dot files for the above mentioned speck.c file. The message cannot be used when linking appears only when you haven't passed any special flags for output like -S, -c, etc. so I'm guessing you have made some typo in the command.
You can try using clang -save-temps speck.c command, which would generate the bitcode (.bc) file for the LLVM IR, which can be used in any place where the .ll file is used.
llvm-ar archives several LLVM bitcode files into a single archive library that can be linked into a program. The archive operation works as expected. Aslo, it is possible to use llvm-nm to show all the symbols in the generated archive.
However, I noticed the generated archive can not be used by the other standard llvm tools, for example, llc, lli and llvm-link directly.
In order to use the generated archived file, I have to extract all the bitcode files from the archived file and then use them directly by llc.
Questions:
Is there some more elegant or efficient way to use the generated archived file by llvm-ar?
Could clang use bitcode archived file directly as below:
// assumes hello.bc is valid bitcode file and it is built successfully
clang-3.8 -o main main.c hello.bc
// ar hello.bc into lib libhello.bca
llvm-ar-3.8 rcs libhello.bca hello.bc
llvm-nm libhello.bca // has below output
hello.bc:
---------------- T outupt
U puts
clang-3.8 -o main main.c libhello.bca // has below error
libhello.bca: error adding symbols: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)
LLVM tools like llc, lli, opt and others can't operate on bitcode archives directly. You have to unpack it before running them. Alternatively, you can link archive items into one single bitcode file, but that's not the same as having the archive, so it depends if that suits you.
As for clang, you can pass bitcode archives along with source files and libraries, and it will do the correct thing - unpack it, run passes, compile to object file and link into the final binary.
After solving this Omitted code blocks from clang AST for ObjectiveC, I've tried it on a small Objective C .m file along with an appropriate compile_commands.json and it works properly and I get the entire syntax tree.
Now I'm trying to see if it's possible to run it on the entire xcodebuild
[
{
"directory" : "/Users/xx/Desktop/iOSApplication",
"command" : "xcodebuild clean build CODE_SIGN_IDENTITY=\"\" CODE_SIGNING_REQUIRED=NO -project /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj/",
"file" : "iOSApplication.xcodeproj"
}
]
When I tried to then run clang-check -ast-dump /Users/xx/Desktop/iOSApplication/iOSApplication.xcodeproj it gives me the errors error: unknown argument: '-project' and error: unable to handle compilation, expected exactly one compiler job in ''
Is it actually possible to run the AST based checker on the entire xcodeproject? Or how should I go about compiling the files 1 at a time?
I've managed to generate the compile_commands.json by following this guide here http://docs.oclint.org/en/stable/guide/xcodebuild.html
However, I'd still like to be able to run my RecursiveASTVisitor on the entire projects. Or alternatively, pass in the xcode project and enumerate all the source files would probably work too.
Anyone has ideas how to go about passing entire xcodebuild project as parameter for RecursiveASTVisitor?
Right now I'm running my ASTVisitor like this ./MyASTChecker ~/Desktop/directory/sample1.m but I'd like to make it do something like ./MyASTChecker ~/Desktop/directory/sampleproject.xcodeproj
The way I do it is at compile time using scan-build. This works for me with cmake/make based projects.
scan-build --use-analyzer=clang -enable-checker <checker_name> make
This will read the makefile and build everything in there while running the specified checker on each file as it's compiled. If you only want to build with some flags or a specific target, you can do this:
scan-build --use-analyzer=clang -enable-checker <checker_name> make <build_options>
If you instead have a cmake based project. You might first want to generate a makefile in a build directory. I do this for that:
cmake <path_to_cmakelists> -DCMAKE_CXX_COMPILER=c++-analyzer -DCMAKE_CC_COMPILER=ccc-analyzer
This followed by scan-build from above will compile and run checker on the source files while each file is being compiled.
I have only tried this with CMAKE / MAKE but should work with xcode like this:
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild
and with build options
scan-build --use-analyzer=clang -enable-checker <checker_name> xcodebuild <build_options>
You can read more about scan-build here
I'm compiling my project while treating all the warnings as error. Some of my files cannot be changed therefore i would like the complier to ignore the warnings in them. I used the -w compiler flag on those file, however when I compiling Xcode he ignores the -w and treats the warnings as errors. Is it possible to make him ignore those files? (Xcode ver 5.1.1)
Check this https://stackoverflow.com/a/1150400/3800154
Here you can disable the checkboxes for the various warnings you have active.
I have installed scan-build/clang version 2.9 on Ubuntu desktop. I build my C++ source code there using make . As it said scan-build would analyze a project which is built using make if you give
scan-build make
to
but after the make i see a message
scan-build: Removing '/tmp/scan-build-2013-10-16-1' because it contains no reports.
Also tried
scan-build --use-c++=/use/bin/clang++ make
Q1 - What am i doing wrong/missing here. How to use scan-build to analyze all source files.
Q2 - Is there any option to use clang++ --analyze myfile.cpp
to analyze single source file. But it gives an error about a header file included not found 'fatal' error' my.h
what is the option to clang analyze to point it to the folder having header files.
As for Q2, you should be able to use:
scan-build clang++ -c myfile.cpp
or what you suggested:
clang++ --analyze myfile.cpp
but you need to make sure that the compiler knows about all the includes and libraries (you should be able to successfully compile myfile.cpp to an object file without analysis). That includes especially the -I directories.
There is also the -o option to scan-build, which specifies the target directory for HTML report files. Subdirectories will be created as needed to represent separate "runs" of the analyzer. If this option is not specified, a directory is created in /tmp to store the reports, as you already know.
Another useful option would be -v (verbose), which should print any errors that the analyzer might run into.
Last but not least, you should use the analysis with debug builds where the optimization is disabled, but more importantly where the symbols are not stripped.
Not sure if it helps, let me know ...