Clang CMS


The clang project is a frontend to the llvm-compiler for C/C++/Objective-C. Part of this project is a static source code analyzer which checks for various issues in the source code, like invalid memory access, memory leaks or dead assignments. Furthermore, there are specific CMS clang static analyzer checkers to crawl C/C++ code for constructs which may become problematic when running multi-threaded code or do violate CMS Framework Rules.

Available Checks

Dedicated Checkers exist for each of this code constructs:

Non-const local statics

int foo()
  static int myEvilLocalState;
  return 0;

Non-const global statics

static g_myGlobalStatic;

use of the mutable keyword ( breaks const-correctness )

struct Foo{
    mutable int myEvilValue;
use of const_cast to remove const-ness
std::string s = "23";
std::string const& r_const = s;
std::string & r = const_cast< std::string & >( r_const );

every explicit cast statement that removes const-ness

std::string s = "23";
std::string const& r_const = s;
std::string & r = (std::string &) ( r_const );  

Compile LLVM / clang with this extensions

Follow the directions to obtain and compile LLVM/clang here:

Stick to the directory structure suggested by this website, but run configure with the option --enable-optimized which will speed-up llvm/clang by some factors. Compile LLVM/clang and see if this is working.

For the impatient user

There is a cut and paste recipe which works out of the box for lxplus (slc5 nodes):
mkdir StaticAnalysis; cd StaticAnalysis
source /afs/ 
svn co llvm
cd llvm/tools
svn co clang
cd ../../llvm/projects
svn co compiler-rt
cd ../../
mkdir build; cd build
../llvm/configure --enable-optimized 
make -j 8

The root path of the LLVM subversion folder will in the following be aliased by . The folder where you built llvm is aliased

Export the path to the new clang binary ( Bash example ):

export PATH=<llvm_bin>/Release+Asserts/bin/:$PATH

Now, checkout the repository which contains the CMS extensions into the same folder as resides (CERN account needed):

svn co 

and run

cmake .
inside the clang_cms folder. If you need to disable the CMS-specific filters, you can to so in CMakeLists.txt. If you encounter problems with missing files or directories, you may need to edit the file CMakeLists.txt to adapt it to your specific build configuration.

The CMS specific checkers have now been compiled into an external library in clang_cms/lib.

Test on a small example (non-CMSSW)

To see a listing of all available checkers, also the CMS-specific ones, you can run the scan-build command:

<llvm_src>/tools/clang/tools/scan-build/scan-build -load-plugin lib/

Test out the newly compiled and modified clang, cd into the clang_cms/test folder and run:

<llvm_src>/tools/clang/tools/scan-build/scan-build -load-plugin ../lib/ -enable-checker threadsafety make -B

This wil produce a clang static analyzer html your can open in your favorite browser. You can find the location in the output line, something along the lines:

scan-build: 6 bugs found.
scan-build: Run 'scan-view /tmp/scan-build-2012-04-26-13' to examine bug reports.

You then call:

firefox /tmp/scan-build-2012-04-26-13/index.html

Run within a SCRAM-based build

Create a project area with arch slc5_amd64_gcc470
export SCRAM_ARCH=slc5_amd64_gcc470
scram pro CMSSW CMSSW_6_0_0_pre3
In the project area edit
and change these lines
#      <environment name="CXX" value="$GCCBINDIR/c++"/>
      <environment name="CXX" value="(llvm src path)/tools/clang/tools/scan-build/c++-analyzer"/>
Then setup the project area with the new cxxcompiler settings
scram setup cxxcompiler
Now you can run a build as usual expect that you will run scan-build to collect the results and then run scan-view to view them. The output by default goes to /tmp/scan-view-date-1
cd src/(your package dir)
scan-build scram b -v -k -j 4
scan-view /tmp/scan-view-(date)-(##)
You will need to include the paths to clang, scan-build and scan-view in your path
export PATH=$PATH\:(llvm install path)/bin/\:(llvm src path)/tools/clang/tools/scan-build/\:(llvm src path)/tools/clang/tools/scan-view/
If you also want to generate the reports for thread-safety, you also need to add the additional parameters to scan-build.

More read on clang Checkers

Constructs to implement checks for

Globals in C++ anonymous namespace
namespace {
int myNonConstGlobal;

Constructs where not to produce a warning ( not implemented yet )

  • non-const static in EVENTSETUP_RECORD_REG framework macro (can this be made const at one point ?)
  • non-const static in DEFINE_FWK_INPUT_SOURCE ( uses edmplugin::PluginFactory )
  • non-const static in REGISTER_PLUGIN framework macro
  • non-const static in DEFINE_FWK_MODULE framework macro
  • non-const static in TYPELOOKUP_DATA_REG framework macro
  • non-const static local EDM_REGISTER_PLUGINFACTORY
  • all warnings originating from "/test/" folders
  • all warnings originating from auto-generated files ( for example /CMSSW_6_0_0_pre3 /tmp /slc5_amd64_gcc470 /src /PhysicsTools /MVATrainer /src /PhysicsToolsMVATrainer /a / )
  • your entry goes here

-- DaniloPiparo - 21-Jun-2012

Edit | Attach | Watch | Print version | History: r16 < r15 < r14 < r13 < r12 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r16 - 2012-06-25 - ThomasHauth
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Main All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback