Clang Static Code Analyzer Output Format for Xcode project - objective-c

I am trying to run inbuilt Xcode Clang static analyzer on ObjectiveCProject. I want to know if it is possible to transform the Obj-C Analyzer output into a another format that sonar plugin can consume. Since there is no Sonar plugin yet for objectiveC that has full features.

This is the command you can use to run clang for the iOS application:
scan-build -o static_analyzer_results xcodebuild -configuration Debug -sdk iphonesimulator
This creates the html reports in static_analyzer directory. Check this for reference:
http://clang-analyzer.llvm.org/scan-build.html
Not sure if you can feed them into Sonar but they are well formatted.

Related

libclang_rt.asan_osx_dynamic.dylib in release builds

I just noticed that libclang_rt.asan_osx_dynamic.dylib is in the release build of my macOS app's Contents/Frameworks/ directory. I was under the impression that the address sanitizer is a debug feature, so I was surprised to see this. I'm using xcodebuild in a custom build script to generate the release build of the app.
2 questions:
Is it wrong for that dylib to be in release builds?
How do I prevent Xcode from including it?
You're right, you should not ship your app with runtime sanitizers enabled, they cost unnecessary performance and are often only targeting a small range of supported macOS versions.
But as far as I know, they're not included if you do the preferred release process of creating an archive (Product > Archive). I'm not aware of enabling them other than the schemes, and here they shouldn't affect your archive builds. So in case you're currently just building in Xcode and try to ship the resulting app, that's not what you should ship.
after investigation, it seems to be an issue with xcodebuild's caching. I have a script that analyzes, builds, bundles, and uploads the app to our content server. At the start of the script it would run the static analyzer:
xcodebuild analyze -project "$SCRIPT_DIR/Redacted.xcodeproj" -destination generic/platform=macOS -scheme Redacted -configuration Release CONFIGURATION_BUILD_DIR="$BUILD_DIRECTORY" ...etc...
later it would build the project:
xcodebuild -project "$SCRIPT_DIR/REDACTED.xcodeproj" -target REDACTED -target REDACTED -target REDACTED -parallelizeTargets -destination generic/platform=macOS -configuration Release CONFIGURATION_BUILD_DIR="$BUILD_DIRECTORY" build ...etc...
the second xcodebuild was reusing artifacts created by the first xcodebuild, which caused the address sanitizer dylib to be included in the release build of the app.
To fix it, I made the script delete the $BUILD_DIRECTORY after the static analysis.

"Use of undeclared identifier 'strchr'" when building in Xcode

My Objective-C project has compiled fine up til now either in the Xcode IDE or using the following script lines:
xcodebuild -scheme ObjCHelper -sdk iphonesimulator -configuration Debug -xcconfig ./ObjCHelper.xcconfig clean build
echo "Built ObjCHelper for simulator"
xcodebuild -scheme ObjCHelper -sdk iphoneos -configuration Debug -xcconfig ObjCHelper.xcconfig clean build
echo "Built ObjCHelper for iPhone"
(similar for Release).
After upgrading to Xcode 12.5 (12E262), I now get many errors of the above kind. Here is a sample from the build output:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:77:9: error: no member named 'strncmp' in the global namespace
using ::strncmp;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:78:9: error: no member named 'strcoll' in the global namespace
using ::strcoll;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:79:9: error: no member named 'strxfrm' in the global namespace
using ::strxfrm;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cstring:82:9: error: no member named 'strcspn' in the global namespace
using ::strcspn;
~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Both the simulator and iphoneos builds fail in the same way.
It seems that part of the C++ SDK can't be found since the upgrade - how can I best mend this?
I've tried deinstalling Xcode and reinstalling it, but the problem was the same.
Edit: It builds OK in Xcode 12.5 on a colleague's Mac. I tried taking the version back to 12.3 anyway, but the problem is still there.
The Framework and Header Search paths are set as follows:
When I uninstalled and reinstalled Xcode, I deleted Application/Xcode.app. As far as I understand, the toolchains are here, in Contents/Developer/Toolchains. If I've understood correctly, Xcode should not be searching for C++ files outside this location?

Why does xcodebuild fail when trying to link to test target?

I have an Objective-C Xcode project. Previously, there were no test targets in this project. We added a Swift-based test target and a few basic tests. The project continues to build from the Xcode GUI just fine. However, building from the command line fails when trying to link the test target.
% xcodebuild -project Foo.xcodeproj -scheme Foo -configuration AdHoc -sdk iphoneos build
...output...
The following build commands failed:
Ld /Users/mluton/Library/Developer/Xcode/DerivedData/Foo-hkljicipwnivteazylcxtobonsbc/Build/Intermediates/Foo.build/AdHoc-iphoneos/FooTests.build/Objects-normal/armv7/FooTests normal armv7
Ld /Users/mluton/Library/Developer/Xcode/DerivedData/Foo-hkljicipwnivteazylcxtobonsbc/Build/Intermediates/Foo.build/AdHoc-iphoneos/FooTests.build/Objects-normal/arm64/FooTests normal arm64
Running the Ld command manually results in this...
ld: file not found: /Users/mluton/Library/Developer/Xcode/DerivedData/Foo-hkljicipwnivteazylcxtobonsbc/Build/Intermediates/Foo.build/AdHoc-iphoneos/FooTests.build/Objects-normal/armv7/FooTests
If I navigate to that directory I see the file is indeed not there. I do see bunch of AFNetworking related files along with FooTest* files. Most have extensions like .d, .dia, and .o. But there's no FooTests which is what the linker is looking for.
If I edit the 'Foo' scheme I see there are two targets in the Build item. Foo and FooTests. If I uncheck everything associated with FooTests then the command line build will work fine.
Has anyone else tried this and run into the same problem? Am I missing a build setting somewhere? Am I asking for trouble by trying to do unit tests for an Objective-C project in Swift?

Build bundle using xcodebuild

I am building a static library. That library relies on a resource bundle that is another target in the same project. I am trying to create a Run Script, that will generate the resource bundle every time I am building the static library and puts it into same folder. With this script I managed to do that for iOS Simulator:
xcodebuild -target LibsResources -configuration ${CONFIGURATION} -sdk iphonesimulator BUILD_DIR="${BUILD_DIR}"
But for some reason this script also generates empty build folder ("build/MyLib.build") at the root of the project. Why and how I can disable it?
Bonus question: How I can specify the sdk(-sdk iphonesimulator or -sdk iphoneos) in the script so it changes depending if the library is built against iSO simulator or iOS Device?

Error "Couldn't discover the 'ccc-analyzer'" when running scan-build

I'm triying to find leaks in my project chibi-ORM using the tool of scan-build as suggested in other threads.
But when run from the terminal:
/Users/mamcx/Downloads/checker-0.138/scan-build -k -V xcodebuild
I get this:
009-01-13 10:33:18.296 xcodebuild[14025:4213] Warning: Couldn't discover the 'ccc-analyzer' compiler's built-in search paths and preprocessor definitions for language dialect 'objective-c'. This may lead to indexing issues.
Compiler: /Users/mamcx/Downloads/checker-0.138/ccc-analyzer
Reason: gcc-4.0: installation problem, cannot exec '/Developer/usr/bin/arm-apple-darwin9-gcc-4.0.1': No such file or directory
However, I can build & debug just fine from the XCode Ide. This is a problem with this tool or something wrong on my side?
There's no need to change the project, just add the -sdk flag to the xcodebuild command, for example:
scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator2.0
You can change the 2.0 to be 2.1, 2.2, 2.2.1, or 3.0 to match your target SDK.
the build is failing due to code signing reasons
the fix for iphone apps would be to just go to the project’s properties and set the “Base SDK” to “Simulator - iPhone OS 2.1″ rather than “Device”
check in your case what it would be
What I did to get this to work was to create a new build configuration (which was a dupe of my debug config), which I called Clang, then did the following in the project settings:
set "base SDK" to "Simulator - iPhone OS x.xx"
set Code signing Identity to - "Don't Code Sign"
then, when I run scan-build I do:
scan-build xcodebuild -configuration Clang
(obviously if you named your new build config something different use that name there).
Then it all worked fine and found no bugs in my code (except one false positive) :-)
Also, make sure you do a clean before each scan-build with:
xcodebuild -configuration Clang clean
Otherwise scan-build won't scan the files that have already been built.
Make sure that you can first run xcodebuild on the command-line for your project. In my case I discovered that my project was doing the release build by default and was trying to build for the device. Xcode must add some magic to make it work with certificates because I got the same error messages.
Once I explicitly told it to build with the Debug configuration all worked well. So you may have to run CLang/LLVM with scan-build xcodebuild -configuration Debug.