Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Clang Static Analyzer from within Xcode

Tags:

xcode

clang

Since there is no Xcode script variable for "current project directory," how can you create a script menu item to run the Clang Static Analyzer on your current project from Xcode?

like image 847
mmc Avatar asked Jun 07 '09 13:06

mmc


People also ask

How do I run static analyzer in Xcode?

It's very easy to run. Just go to Product and choose Analyze, or use the keyboard shortcut Command-Shift-B. You can see the analyzer running in the status bar of Xcode. Analyzing your project is very similar to building it.

What is clang static analyzer?

The Clang Static Analyzer is a source code analysis tool that finds bugs in C, C++, and Objective-C programs. It implements path-sensitive, inter-procedural analysis based on symbolic execution technique.

Is clang tidy a static analysis tool?

clang-tidy is a clang-based C++ “linter” tool. Its purpose is to provide an extensible framework for diagnosing and fixing typical programming errors, like style violations, interface misuse, or bugs that can be deduced via static analysis.


2 Answers

From the XCode script menu item, "Edit User Scripts" enter the following script:

#!/bin/bash
result=$( osascript << END
tell application "Xcode"
 tell active project document
  set projectPath to path as string
 end tell 
end tell 
return projectPath
END
)

cd "$result"

/Developer/clangchecker/scan-build -k -V xcodebuild -configuration Debug -sdk iphonesimulator3.0

Obviously, you will need to adjust the path to your install of Clang, and adjust to the version of the SDK you are using.

Remember to do a "Clean All" immediately before using scan-build, or the results may be incomplete.

like image 137
mmc Avatar answered Oct 03 '22 13:10

mmc


FYI, Xcode 3.2 (Snow Leopard only I believe) includes the Clang Static Analyzer in the "Build and Analyze" menu option.

http://iosdevelopertips.com/xcode/static-code-analysis-clang-and-xcode-3-2.html

One downside of Xcode 3.2 (aside from it only working on Snow Leopard) is that the v2.x Simulators don't seem to work - in fact, I've seen posts indicating that v2.x builds are not supported at all.

like image 42
Dan J Avatar answered Oct 03 '22 12:10

Dan J