Writing custom checkers for Clang Static Analyzer - c++

I've just finished following the "Getting Started" instructions from the clang analyzer page.
My XCode is currently using the checker build 278
Right now I'm trying to follow some guides I can find to write my own custom checkers like
http://blog.trailofbits.com/2014/04/27/using-static-analysis-and-clang-to-find-heartbleed/
http://bbannier.github.io/blog/2015/05/02/Writing-a-basic-clang-static-analysis-check.html
Was hoping that someone can point me in the right direction and I'm not very familiar with building clang projects.
Are there any IDE available that would help?
How should I add the custom checker I wrote to the build 278?

I have recently started using clang checker's and here's how I got my custom checker to work with clang.
You have to modify the Checkers.td to register your checker.
<path-to-llvm>/llvm/tools/clang/lib/StaticAnalyzer/Checkers/Checkers.td
I made a debug checker to I put it under debug group and added these lines:
def MyCustomChecker : Checker<"DebugUsingMyChecker">,
HelpText<"Print results of my custom checker">,
DescFile<"DebugCheckers.cpp">; //this is the file where we define the class file of our checker
Then edit the DebugCheckers.cpp to add your checker's class to be invoked by the newly registered checker.
<path-to-llvm>/llvm/tools/clang/lib/StaticAnalyzer/Checkers/DebugCheckers.cpp
And add under debug checkers tag
namespace
{
class MyCustomChecker : public Checker<check::ASTCodeBody> {
public:
void checkASTCodeBody(const Decl *D, AnalysisManager& mgr,
BugReporter &BR) const {
if (MyCustomChecker* CC = mgr.getAnalysis<CustomChecker>(D)) {
CC->dump(mgr.getSourceManager());
}
}
};
}
void ento::registerMyCustomChecker(CheckerManager &mgr) {
mgr.registerChecker<MyCustomChecker>();
}
After this, you can place the relevant class files in the folder:
<path-to-llvm>/llvm/tools/clang/lib/Analysis/
And edit the CMakeList.txt in that folder to include your class file.
Hope that clarifies the process. You can also read this link for more: http://clang-analyzer.llvm.org/checker_dev_manual.html
As far as IDE's are concerned, you can use any IDE which supports's CMake based projects (like CLion). You can look at this link for more: http://llvm.org/docs/CMake.html

Related

Qt creator could not parse stddef.h --> incorrect code completion and highlighting

I am developing a QT GUI for my application using QT Creator (4.11.0).
Recently, our IT updated my OS to Ubuntu 20.04 (from 18.04) - maybe the error is related to that.
I have not touched to project in some month but yesterday wanted to continue developing it.
However, within the IDE there are now thousands of errors highlighted at almost every line of my code. (with highlighted I mean that there is this red background and a red dot beside the line number)
On the very top, it says:
Warning: The code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
cstddef:50:10: fatal error: 'stddef.h' file not found
...
The errors that are listed in the IDE are very wired like:
type `QMainWindow`is not a direct or virtual base of `MainWindow` (my class is called `MainWindow`)
I'm pretty sure it is not related to my code:
the code compiles and works fine - it is really just the IDE that is highlighting errors at every line of my code.
I have the same code on a Windows Computer and there no errors are listed in the IDE.
even if I start a brand new QT Widget project the errors appear within the template code provided by the QT Creator.
Since the GCC version changed with the update of the OS, I thought maybe I have to define a new KIT but this also did not help.
Is there anything I can do to fix the issue?
Do I have to reinstall the QT Creator?
I know, pictures are not very well-liked but here I think it might help to understand what I mean with "IDE is highlighting errors":
1. First
~/.profile :
CPATH="/usr/include/clang/10/include:$CPATH"
C_INCLUDE_PATH="/usr/include/clang/10/include:$C_INCLUDE_PATH"
CPLUS_INCLUDE_PATH="/usr/include/clang/10/include:$CPLUS_INCLUDE_PATH"
export CPATH
export C_INCLUDE_PATH
export CPLUS_INCLUDE_PATH
maybe /usr/include/clang/10/include see in you file system.
2. Second
Logout / login.
3. Third
Specifying Build Settings (Projects -> Build -> in every config build) in Build Environment section: [unset] CPATH, C_INCLUDE_PATH and CPLUS_INCLUDE_PATH
or [edit] replace this variable with the necessary values
I fixed this issue by sudo apt install clang-8.
Source: https://bugs.launchpad.net/ubuntu/+source/qtcreator/+bug/1890434

Eclipse Neon build errors despite successful build

I'm trying to use Eclipse to do the development for a project that involves Gazebo (a popular robotics simulator). Gazebo provides a plugin system to allow external interaction with the simulator and a series of tutorials on how to write plugins.
Having followed the tutorials successfully, I tried migrating the code to Eclipse, using cmake -G "Eclipse CDT4 - Unix Makefiles" [buildpath] to generate an eclipse rpoject, then importing it into my Eclipse workspace.
Everything generally went well, but I've run into a problem that is a bit odd:
When I compile my project, Eclipse comes back with "Member declaration not found" error referring to an SDFormat data type used in the signature to the ModelPush::Load function (see code snippets below). SDFormat, incidnetally is a robotics XML used for describing how a robot is put together.
Despite this error (which should result in nothing being built), the resulting shared library is built anyway.
I guess I can live with it, but I'd obviously like to resolve this issue, which appears to be internal to Eclipse / CDT...
TO CLARIFY:
I'm trying to determine why Eclipse gives me the error: "Member declaration not found" on the Load() function signature in model_push.cc. The guilty party is the sdf::ElementPtr _sdf parameter. Something's wrong with the SDFormat library or with the way that Eclipse / CDT looks at it. This isn't an include issue. And, even though Eclipse gives me the error, it still builds the .so file. Running make from the command line also generates the file, but without any errors.
Again, I can live with it, but I'd rather not. I just don't know where to start looking for a solution since this isn't a problem finding an include or the sdf library file.
Here's the class declaration (mode_push.hh):
#ifndef MODEL_PUSH_HH_
#define MODEL_PUSH_HH_
#include <boost/bind.hpp>
#include <gazebo/gazebo.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include <sdf/sdf.hh>
namespace gazebo
{
class ModelPush : public ModelPlugin
{
public:
void Load (physics::ModelPtr _parent, sdf::ElementPtr _sdf);
//Called by the world update start event
void OnUpdate (const common::UpdateInfo & /*_info*/);
//Pointer to the model
private:
physics::ModelPtr model;
//Pointer to the update event connection
private:
event::ConnectionPtr updateConnection;
};
}
#endif /* MODEL_PUSH_HH_ */
Here's the implementation file (model_push.cc):
#include "model_push.hh"
namespace gazebo
{
void ModelPush::Load(physics::ModelPtr _parent, sdf::ElementPtr _sdf)
//void ModelPush::Load (physics::ModelPtr _parent, sdf::ElementPtr /*sdf*/)
{
//Store the pointer to the model
this -> model = _parent;
//Listen to the update event. This event is broadcast every
//simulation iteration.
this -> updateConnection = event::Events::ConnectWorldUpdateBegin(
boost::bind (&ModelPush::OnUpdate, this, _1));
}
//Called by the world update start event
void ModelPush::OnUpdate (const common::UpdateInfo & /*_info*/)
{
//Apply a small linear velocity to the model.
this -> model -> SetLinearVel (math::Vector3 (0.03, 0.0, 0.0));
}
//Register this plugin with the simulator
//GZ_REGISTER_MODEL_PLUGIN(ModelPush)
}
I've been struggling with this exact problem. I've found a solution that works, but I still don't think is ideal. Instead of generating the eclipse project using cmake (or catkin_make) I'm generating it using the CDT project builder. Here's the process I'm using in Eclipse 2018-09.
Create a New C/C++ Project of type C++ Managed Build (A C++ Project build using the CDT's managed build system.)
Project name: ROSWorkspace
Location: /home/username/eclipse-workspace/ROSWorkspace
Project type: Makefile project / Empty Project
Toolchain: Linux GCC
Finish.
Right click on the project and select Properties.
C/C++ Build / Builder Stetings:
Uncheck "Use default build command"
Build command: catkin_make
Build directory: ${workspace_loc:/../catkin_ws}/
C/C++ General / Paths and Symbols / Includes tab
Add /usr/include/gazebo-8
Add /usr/include/sdformat-5.3
C/C++ General / Preprocessor Includes / Providers tab
CDT GCC Built-in Compiler Settings / Command to get compiler specs: ${COMMAND} ${FLAGS} -E -P -v -dD "${INPUTS}" -std=c++11
Click Ok, then from the drop down menu choose:
Project / C/C++ Index / Freshen all files
Ideally I'd make the time to dig in to figure out how to get the preprocessor to properly work with the generated project, but I just don't have the time right now. I hope this helps.

Using Reflection Causing MissingMethodException in Xamarin UITest

I have a class in my Xamarin PCL which makes a call to System.Reflection.GetRuntimeProperties. For an example, let's say my PCL class has this method:
public string ExampleMethod(string arg) {
if(arg == null) return null;
IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
return infos[0].Name;
}
I then have a Xamarin.UITest project which references the PCL project and tests this class. I have two test cases in my TestFixture so far, which for our example would be the following:
[Test]
public void TestExampleMethod_ArgNull_Null(){
Assert.That (exampleInstance.ExampleMethod(null), Is.Null);
}
[Test]
public void TestExampleMethod_ArgNotNull_NotNull(){
Assert.That (exampleInstance.ExampleMethod("testValue"), Is.NotNull);
}
When I run the Xamarin.UITest project, it compiles, runs the tests, and completes fine on both Android and iOS platforms. The TestExampleMethod_ArgNull_Null test passes since it returns early. However, the TestExampleMethod_ArgNotNull_NotNull test fails with:
System.MissingMethodException : Method 'RuntimeReflectionExtensions.GetRuntimeProperties' not found.
So it appears that even though everything is compiling just fine, and I am able to run other test cases fine, the Xamarin.UITest project is not able to use anything in System.Reflection. Does anyone know how I go about debugging this?
On my end, using the following failed to build:
IEnumerable<PropertyInfo> infos = this.GetType().GetRuntimeProperties();
return infos[0].Name;
due to not being able to do bracket indexes on and IEnumerable. I changed to this:
List<PropertyInfo> infos = this.GetType().GetRuntimeProperties().ToList();
return infos[0].Name;
And the project built and the tests ran successfully.
The class with the method using Reflection was in a PCL which was referenced from a UI Test project.
So basically I am not able to reproduce the error you got.
I posted this to Xamarin Support as well (thanks #jgoldberger) and we were able to figure out that it was due to a project setup issue. This is a project which uses Couchbase Lite which requires a specific version of Json.Net (6.0.4 as of this post). I must have accidentally updated the packages on some of the projects since the same version of Json.Net was not being used across all the projects (PCL, Android, iOS, and UITest). I ended up re-creating the project from scratch and that took care of it.

vs 2013 : breakpoint into cppunittest test throws exception

I am testing my c++11 static lib in vs 2013 environment.
I followed this nice tutorial and testing + code coverage are working fine.
Now I need step-by-step : I added breakpoints and executed the "Debug Selected Test" command and I get the following message : vstest.executionengine.exe has triggered a breakpoint and the callstack brings me at CppUnitTest.h:465 : (static_cast<ThisClass *>(this)->*method2)();, eg at the root call of the method I want to break into. No way to see the code inside this call.
My question : how to break into my code during debugging Ms cpp unit test ?
I found the problem. Some breakpoints were activated inside the static lib used by the test dll. It seems breakpoints inside static lib generates something wrong for the test framework.
To reproduce :
create c++ static lib project with a simple function, int foo(){ return 0;}
create a test-dll project
add a test function which calls foo
set a breakpoint in foo
execute "run selected test" : it will work
execute "debug selected test" : it will block just before calling the test-method test-generated class.
I am still interested in explaining this non-intuitive behaviour.

How to disable Eclipse CDT code formatter for a code block

The CDT code formatter has a pretty decent selection of options, but it doesn't seem to have to a feature that allows one to tell it to ignore a block of code. This feature exists in the Java code formatter:
// #formatter:off
... // code that should not be formatted
// #formatter:on
Does this feature exist and I just don't know about it, or does anyone know of any decent work-arounds?
In my particular case, I'm trying to define data structures (enum types and arrays of strings) that I want to have specific layouts.
Use Astyle (Artistic Style) formatter, it's far superior to the Eclipse CDT built-in formatter and has the feature you require:
http://astyle.sourceforge.net/astyle.html#_Disable_Formatting
Example:
#include <iostream>
int main(int argc, char** argv)
{
// *INDENT-OFF*
std::cout<<"hello world"<<'\n';
// *INDENT-ON*
}
Formatting this using astyle won't indent the code between // INDENT-OFF and // INDENT-ON but it will also disable any other formatting features astyle does, like the spacing of the instructions in this case.
I use it myself configured as an external tool.
The only problem, external tools don't have hotkeys, but there is one hotkey to "Run Last Launched External Tool", and if you only use one external tool it works the same.
More details about the configuration (linux):
Astyle:
You can get it easily from your distribution repositories or via the official site.
To setup a configuration file with the formatting settings:
http://astyle.sourceforge.net/astyle.html#_Options_File
I use the home folder variant, just create a .astylerc in your $HOME, mine contains:
--suffix=none
--style=allman
--indent=tab=4
--max-code-length=70
--close-templates
--keep-one-line-blocks
--break-elseifs
--break-closing-brackets
--align-reference=type
--align-pointer=type
--indent-classes
--indent-modifiers
--indent-switches
--indent-cases
--indent-labels
--indent-col1-comments
--min-conditional-indent=0
--pad-oper
--pad-header
--unpad-paren
Eclipse:
"Run" menu --> External tools --> External tools Configurations... Add a new "Program" and in the configuration window:
Location: /usr/bin/astyle (use whereis or locate to check this)
Working Directory: ${project_loc}
Arguments: ${selected_resource_loc}
In the same window, refresh tab:
Tick Refresh resources upon completion.
Tick "The selected resource"
Same window, common tab:
Display in favorites menu, Tick "External tools"
Yes, you can do it since CDT supports this feature starting from version 9.7. The behavior is exactly the same of JDT.
If you are using OS X or Linux (I haven't checked Windows, but it may be supported), you can use clang-format and CppStyle instead.
clang-format is a formatter utility which is provided with Clang, and it supports on/off comments // clang-format on and // clang-format off in C/C++/ObjC code. An introduction to build Clang and its utility tools can be found here.
http://clang.llvm.org/get_started.html
You do not need to install whole Clang and LLVM files on your system. Because clang-format is a standalone program which works without Clang. The on/off comments are not supported in old versions, so please use ver 3.7 (available from SVN as of Feb 2015).
CppStyle is an Eclipse plugin which enables us to use clang-format from Eclipse CDT.
https://github.com/wangzw/cppstyle
FYI. Here is the same feature request in the CDT Bugzilla. The functionality might be officially supported in future, but using clang-format or Astyle seems to be a better solution at the moment. https://bugs.eclipse.org/bugs/show_bug.cgi?id=453926
I guess I could stick these in a file with an extension ignored by the formatter and include this file where appropriate. I tried this out and it works - the data structure gets picked up the indexer (i.e. autocomplete works). Still, it would be nice to have an equivalent to the Java "#formatter:..." syntax.
As far as I know the answer is simply no, such a feature does not exist. You might be able to implement such a feature using the SDK though. Beware that in my experience the documentation is very incomplete and it's very hard to find an Eclipse developer who would be willing to help you fill in the holes. But since the feature exists in the Java formatter and it is an open source product, perhaps you could port over the logic to the C++ formatter.
You could also avoid formatting the whole file, and instead format only by selection.