Linting Without a Plugin
Join the DZone community and get the full member experience.
Join For Freewith “eclipse and pc-lint: linticator” i have a plugin to lint my sources in a comfortable way. but i can do this as well without a plugin. for this i use a batch file with a build configuration, plus settings to get the pc-lint messages into the problems view. yes, this does not sound easy, but is very doable and straight forward once i have set it up. it gives me complete control on every little detail. here is how i do it… pc-lint is like any other compiler: it compiles my source files. but, it is not producing object code: it is producing messages. so i need to set up the compiler to compile my sources, and to produce the messages so i can click and jump to the offending source files and lines. to run pc-lint with a project in eclipse means solving three problems:
- how to set up pc-lint as compiler for my project?
- how to pass options to the pc-lint compiler?
- how to configure the messages for the problems view?
i’m using a joint approach with eclipse build configuration , batch file and lint option files . the solution presented here is using the following steps:
- using a managed make build configuration
- setting up a batch file to call pc-lint
- defining the list of files
- specifying the options
- defining the message format
- lint it !
step 1: build configuration
to my existing project i add a managed make build configuration for pc-lint. the build configuration is set up to call a batch file. i select the project and use the menu project > build configuration > manage… and create a new configuration using the new button:
for the new configuration, i copy the settings from the existing configuration:
this new configuration would just use my normal compiler. instead, i want to use the lint compiler. in project > properties > c/c++ build > builder settings , i disable ‘ use default build command ‘ and use instead:
${projdirpath}\lint\do_lint.bat "${projdirpath}" "${mcutoolsbasedir}"
i’m pointing to a do_lint.bat file inside a lint folder of my project which i will create in the next step. additionally i disable ‘ generate makefiles automatically ‘:
codewarrior for mcu10.2 uses parallel builds by default. this would add -j6 as option to the command line. in order to disable this, i configure project specific settings in the build behaviour tab:
step 2: batch file
to separate the lint files from the rest of my build and project, i create a lint sub-folder inside my project root with a do_lint.bat batch file:
the do_lint.bat has following content:
@rem the arguments for this batch file: @rem %1: the path to the project folder @rem %2: the path to the codewarrior installation folder @rem ------------------------------------------------------ @rem path to my project folder set proj_path=%1 @rem path to codewarrior installation folder (which is e.g. "c:\freescale\cw mcu v10.2\eclipse\..\mcu") set cw_path=%2 @rem path to lint-nt.exe set lint_exe=c:\lint\lint-nt.exe @rem path to my lint configuration files set local_lnt_files=c:\freescale\pc-lint\fsl_lnt @rem path to my local lint folder inside the project set proj_lint_path=%proj_path%\lint @rem lint configuration files and includes set lnt_includes=-i"%local_lnt_files%" "%local_lnt_files%\co-mwhc08.lnt" -i%local_lnt_files% @rem --------------- run pc-lint --------------------------- %lint_exe% %lnt_includes% %proj_lint_path%\proj_options.lnt %proj_lint_path%\proj_files.lnt -vf
the batch file is called from eclipse with two arguments (%1 and %2): with the path to the project folder and the path to the codewarrior installation folder. i assign them to local variables (proj_path and cw_path) so i can use them inside the .lnt files. to know where my lint compiler is, i use lint_exe. i store my lint configuration files outside of the pc-lint installation folder, that’s why i have defined a path variable for this: local_lint_files. proj_lint_path contains the project sub-folder with all my batch and lint files for the project. in lnt_includes i specify my compiler lint configuration file, plus where lint shall search for my lint configuration files. finally it calls the lint executable with the lint include files, plus two files: the project options (proj_options.lnt) and the project files (proj_files.lnt) . i explain these files in step 4.
step 3: list of files
i have created a file proj_files.lnt file inside my project:
this file has all my source files listed. and because i have defined environment variables like proj_path, i can use it here:
%proj_path%\sources\main.c %proj_path%\sources\events.c
step 4: passing the options
what is missing are the project specific options: i add them to the proj_options.lnt file:
in this file i add with the -i pc-lint option all the paths where it can find my files:
// include paths used -i%proj_path% -i%proj_path%\sources -i%proj_path%\generated_code -i%cw_path%\lib\hc08c\include
additionally i specify all the global options, e.g. to inhibit messages:
// inhibit messages for processor expert libraries -elib(19, 10) -e766 +libh(events.h, cpu.h)
step 5: defining the message format
last but not least: i need to tell pc-lint how to format the messages so they end up properly in the eclipse problems view. i add them as well to the proj_options.lnt:
// coerce messages for eclipse -hf1 +ffn // normally my format is defined as follows: //-"format=%(\q%f\q %l %c%) %t %n: %m" // for eclipse-usage, the gcc error format is necessary, // since we have only the default eclipse error parser available. -"format=%(%f:%l:%c:%) %t %n: %m" // enable warning 831 if you are interested. -frl // do not break lines -width(0) // and make sure no foreign includes change the format +flm
step 5: linting in action
time to see how this works! as i have set up a separate build configuration to lint my files, i can run it like any other build configuration:
the example below shows several lint errors for main.c. the messages show up in the problems view, and are listed as well in the console view:
summary
the approach presented here does not need any other eclipse plugin. it is using a batch file, and uses a brute force approach: it will lint all files regardless if they have changed or not. this could be improved with a make file approach as outlined here . the approach presented here requires some setup, is pretty simple, and routes all lint messages to the problems view. if i want to use more of a plugin approach, then the linticator plugin is the alternative.
thanks to catherine for providing a lot of good ideas used in this article!
happy linting
Published at DZone with permission of Erich Styger, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments