Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

LintProject - Improving the Usability of PC-Lint with Visual C++ Solutions and Projects

, , 29 Jan 2009
Utility to run PC-Lint on Visual C++ solutions and projects, generating XML and HTML reports of the results.
/************************************************************************
 *
 *  Description : CFileLintAnalyser - class for analysing Visual C++
 *                implementation files using the PC-Lint code analysis tool.
 *
 *     (c) Copyright 2004-2008 by Anna-Jayne Metcalfe (anna@riverblade.co.uk)
 *     and Beth Mackenzie (beth@riverblade.co.uk) / Riverblade Limited
 *
 *  Licence Terms:
 *
 *     This code may be freely reused, subject to the licence terms below.
 *     Please do let us know of any bugs you find or improvements you make,
 *     so that we can pass them on to the rest of the development community.
 *
 *     This code is free software; you can redistribute it and/or
 *     modify it under the terms of the Code Project Open License (CPOL)
 *     version 1.0 (http://www.codeproject.com/info/cpol10.aspx).
 *
 *     This code is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     Code Project Open Licence for further details.
 *
 ************************************************************************
 *    $Archive: /Projects/Applications/LintProject/1.4/FileLintAnalyser.cpp $
 *   $Revision: 22 $
 *       $Date: 7/01/09 12:52 $
 *     $Author: Anna $
 * 
 * $Nokeywords: $
 ************************************************************************/

/// \file	
/// \brief CFileLintAnalyser class implementation.

#include "StdAfx.h"
#include "Shared/StringUtils.h"
#include "Shared/FileUtils.h"
#include "Shared/PathUtils.h"
#include "Shared/SplitPath.h"

#include "FileLintAnalyser.h"


using namespace std;


#define ENVVAR_CONFIG_NAME	_T("$(ConfigurationName)") 

// [Alex McCarthy 21.10.2008]
#define ENVVAR_PROJECT_NAME	_T("$(ProjectName)") 
// [Alex McCarthy 21.10.2008]
#define ENVVAR_SOLUTION_DIR	_T("$(SolutionDir)") 
// [Alex McCarthy 23.12.2008]
#define ENVVAR_PLATFORM_NAME _T("$(PlatformName)") 


CFileLintAnalyser::CFileLintAnalyser(void)
	:	m_sLintFolder( _T("") ),
		m_sPathName( _T("") ),
		m_sFileName( _T("") ),
		m_sProjectFilePathName( _T("") ),
		m_sProjectFileFolder( _T("") ),
		m_sProjectName( _T("") ),
		m_sSolutionFileFolder( _T("") ), // [Alex McCarthy 21.10.2008]
		m_sPlatformName( _T("") ), // [Alex McCarthy 23.12.2008]
		m_sResultsFileFolder( _T("") ),
		m_sStdtLntFileName( _T("") ),
		m_sAdditionalParams( _T("") ),
		m_sConfiguration( _T("") ),
		m_sIntermediateFilesFolder( _T("") ),
		m_nWarningCount(0),
		m_sResultsFilePathName( _T("") ),
		m_bAnalysed(false)
{
}

	
CFileLintAnalyser::CFileLintAnalyser(	const CString& sPathName,
										const CString& sProjectFilePathName,
										const CString& sSolutionFileFolder, // [Alex McCarthy 21.10.2008]
										const CString& sPlatformName, // [Alex McCarthy 23.12.2008]
										const CString& sResultsFileFolder,
										const CString& sLintConfigFile,
										const CString& sAdditionalParams,
										const CString& sConfiguration,
										const CString& sIntermediateFilesFolder)
	:	m_sLintFolder( _T("") ),
		m_sPathName(sPathName),
		m_sFileName( _T("") ),
		m_sProjectFilePathName(sProjectFilePathName),
		m_sProjectFileFolder( _T("") ),
		m_sProjectName( _T("") ),
		m_sSolutionFileFolder(sSolutionFileFolder), // [Alex McCarthy 21.10.2008]
		m_sPlatformName(sPlatformName), // [Alex McCarthy 23.12.2008]
		m_sResultsFileFolder(sResultsFileFolder),
		m_sStdtLntFileName(sLintConfigFile),
		m_sAdditionalParams(sAdditionalParams),
		m_sConfiguration(sConfiguration),
		m_sIntermediateFilesFolder(sIntermediateFilesFolder),
		m_nWarningCount(0),
		m_sResultsFilePathName( _T("") ),
		m_bAnalysed(false)
{
	// Strip leading/trailing quotes - just in  case
	m_sPathName.TrimLeft( _T("\"") );
	m_sPathName.TrimRight( _T("\"") );

	m_sProjectFilePathName.TrimLeft( _T("\"") );
	m_sProjectFilePathName.TrimRight( _T("\"") );

	m_sResultsFileFolder.TrimLeft( _T("\"") );
	m_sResultsFileFolder.TrimRight( _T("\"") );

	Utils::CSplitPath splittarget(m_sPathName);

	m_sFileName					= splittarget.GetFileName() + splittarget.GetExtension();

	Utils::CSplitPath splitproject(m_sProjectFilePathName);

	m_sProjectFileFolder		= splitproject.GetDrive() + splitproject.GetDirectory();
	m_sProjectName				= splitproject.GetFileName();
}


CFileLintAnalyser::CFileLintAnalyser(const CFileLintAnalyser& src)
	:	m_sLintFolder(src.m_sLintFolder),
		m_sPathName(src.m_sPathName),
		m_sFileName(src.m_sFileName),
		m_sProjectFilePathName(src.m_sProjectFilePathName),
		m_sProjectFileFolder(src.m_sProjectFileFolder),
		m_sProjectName(src.m_sProjectName),
		m_sSolutionFileFolder(src.m_sSolutionFileFolder), // [Alex McCarthy 21.10.2008]
		m_sPlatformName(src.m_sPlatformName), // [Alex McCarthy 23.12.2008]
		m_sResultsFileFolder(src.m_sResultsFileFolder),
		m_sStdtLntFileName(src.m_sStdtLntFileName),
		m_sAdditionalParams(src.m_sAdditionalParams),
		m_sConfiguration(src.m_sConfiguration),
		m_sIntermediateFilesFolder(src.m_sIntermediateFilesFolder),
		m_nWarningCount(src.m_nWarningCount),
		m_sResultsFilePathName(src.m_sResultsFilePathName),
		m_bAnalysed(src.m_bAnalysed)
{
}


CFileLintAnalyser::~CFileLintAnalyser(void)
{
}


bool CFileLintAnalyser::SetLintFolder(const CString& sFolder)
{
	m_sLintFolder = sFolder;
	ATLASSERT(!m_sLintFolder.IsEmpty() );

	return !m_sLintFolder.IsEmpty();
}


int CFileLintAnalyser::GetWarningCount(void) const
{
	return m_nWarningCount;
}


CFileLintAnalyser& CFileLintAnalyser::operator=(const CFileLintAnalyser& src)
{
	if (this != &src)
	{
 		m_sLintFolder				= src.m_sLintFolder;
		m_sPathName					= src.m_sPathName;
		m_sFileName					= src.m_sFileName;

		m_sStdtLntFileName			= src.m_sStdtLntFileName;
		m_sAdditionalParams			= src.m_sAdditionalParams;
		m_sConfiguration			= src.m_sConfiguration;
		m_sIntermediateFilesFolder	= src.m_sIntermediateFilesFolder;
		m_sProjectFilePathName		= src.m_sProjectFilePathName;
		m_sProjectFileFolder		= src.m_sProjectFileFolder;
		m_sProjectName				= src.m_sProjectName;
		m_sSolutionFileFolder		= src.m_sSolutionFileFolder; // [Alex McCarthy 21.10.2008]
		m_sPlatformName				= src.m_sPlatformName; // [Alex McCarthy 23.12.2008]
		m_sResultsFileFolder		= src.m_sResultsFileFolder;

		m_nWarningCount				= src.m_nWarningCount;
		m_sResultsFilePathName		= src.m_sResultsFilePathName;

		m_bAnalysed					= src.m_bAnalysed;
	}
	return *this;
}


bool CFileLintAnalyser::IsVSDotNetProject(void) const
{
	Utils::CSplitPath split(m_sProjectFilePathName);

	CString sExtension = split.GetExtension();

	sExtension.MakeLower();

	return ( _T(".vcproj") == sExtension);
}


CString CFileLintAnalyser::GetResultsFilePathName(void) const
{
	return m_sResultsFilePathName;
}


CString CFileLintAnalyser::GetResultsFileName(void) const
{
	const int nPos = m_sResultsFilePathName.ReverseFind( _T('\\') );
	if (nPos > 0)
	{
		CString sResultsFileName = m_sResultsFilePathName.Mid(nPos);

		sResultsFileName.TrimLeft( _T("\\") );

		return sResultsFileName;
	}
	return _T("");
}


CString CFileLintAnalyser::ChooseResultsFilePathName(void) const
{
	ATLASSERT(!m_sPathName.IsEmpty() );

	CString sResultsFilePathName;

	if (!m_sPathName.IsEmpty() )
	{
		Utils::CSplitPath split(m_sPathName);
		const CString sFileName = split.GetFileName() + split.GetExtension();

		const CString sResultsFileName = sFileName + _T(".txt");

		sResultsFilePathName = m_sResultsFileFolder + _T("\\") + sResultsFileName;
	}
	return sResultsFilePathName;
}


bool CFileLintAnalyser::Analyse(void)
{
	ATLASSERT(!m_sPathName.IsEmpty() );
	ATLASSERT(!m_sProjectFilePathName.IsEmpty() );

	if (!m_sPathName.IsEmpty() && !m_sProjectFilePathName.IsEmpty() )
	{
		CString sRelativePathName = Utils::CreateRelativePath(m_sProjectFileFolder, m_sPathName);

		if (sRelativePathName.IsEmpty() )
		{
			sRelativePathName = m_sPathName;
		}

		Utils::CSplitPath split(m_sPathName);
		const CString sFileName = split.GetFileName() + split.GetExtension();

		CString sMsg = _T("Analysing file: ") + sFileName + _T("...");

		cout << CStringA(sMsg).GetString();

		m_sResultsFilePathName = ChooseResultsFilePathName();

		CString sRelativeOutputPath = Utils::CreateRelativePath(m_sProjectFileFolder, m_sResultsFilePathName);

		// If the relative path comes out empty, it's probably because the project and results
		// files are on diferent drives
		if (sRelativeOutputPath.IsEmpty() )
		{
			sRelativeOutputPath = m_sResultsFilePathName;
		}

		(void)SetCurrentDirectory(m_sProjectFileFolder);

		// Generate a .lnt file for the project
		CString sProjectLntFileName;
		
		if (!m_sConfiguration.IsEmpty() )
		{
			CString sConfiguration = m_sConfiguration;

			sConfiguration.Replace( _T("|"), _T("_") );
			sConfiguration.Replace( _T(" "), _T("_") );

			sProjectLntFileName.Format( _T("%s_%s.lnt"), m_sProjectName, sConfiguration);

			// If the intermediate files folder contains $(ConfigurationName), expand it [Anna 15.11.2007].
			if (-1 != m_sIntermediateFilesFolder.Find(ENVVAR_CONFIG_NAME) )
			{
				const CString sProjectConfiguration = Utils::Before(m_sConfiguration, _T("|") );

				m_sIntermediateFilesFolder.Replace(ENVVAR_CONFIG_NAME, sProjectConfiguration);
			}

			// If the intermediate files folder contains $(ProjectName), expand it [Alex McCarthy 21.10.2008].
			if (-1 != m_sIntermediateFilesFolder.Find(ENVVAR_PROJECT_NAME) )
			{
				const CString sProjectName = Utils::Before(m_sProjectName, _T("|") );

				m_sIntermediateFilesFolder.Replace(ENVVAR_PROJECT_NAME, sProjectName);
			}

			// If the intermediate files folder contains $(SolutionDir), expand it [Alex McCarthy 21.10.2008].
			if (!m_sSolutionFileFolder.IsEmpty() && -1 != m_sIntermediateFilesFolder.Find(ENVVAR_SOLUTION_DIR) )
			{
				const CString sSolutionFileFolder = Utils::Before(m_sSolutionFileFolder, _T("|") );

				m_sIntermediateFilesFolder.Replace(ENVVAR_SOLUTION_DIR, sSolutionFileFolder);
			}
			// If the intermediate files folder contains $(PlatformName), expand it [Alex McCarthy 23.12.2008].
			if (!m_sPlatformName.IsEmpty() && -1 != m_sIntermediateFilesFolder.Find(ENVVAR_PLATFORM_NAME) )
			{
				const CString sPlatformName = Utils::Before(m_sPlatformName, _T("|") );

				m_sIntermediateFilesFolder.Replace(ENVVAR_PLATFORM_NAME, sPlatformName);
			}
		}
		else
		{
			sProjectLntFileName.Format( _T("%s.lnt"), m_sProjectName);
		}

		CString sIntermediateFilesInclude;
		sIntermediateFilesInclude.Format(	_T("-i\"%s\""),
											!m_sIntermediateFilesFolder.IsEmpty() ? m_sIntermediateFilesFolder : CString(_T("Debug") ) );

		const CString sFrameworkOptionsFile = IsVSDotNetProject() ? _T("env-vc7.lnt") : _T("env-vc6.lnt");

		CString sParams;
		if (Utils::FileExists(sProjectLntFileName) )
		{
			// e.g.	c:\lint\lint-nt.exe -i"c:\lint" -b <user parameters> -u std.lnt env-vc6.lnt -i"Debug" --u %1.lnt %2.cpp >Lint\%2.cpp.txt
			ATLASSERT(!m_sLintFolder.IsEmpty() );
			
			// Additional project specific file properties specified by the user
			CString sUserLintOptionsFileName;
			sUserLintOptionsFileName.Format( _T("%s.options.lnt"), m_sProjectName);
			
			if (Utils::FileExists(sUserLintOptionsFileName) )
			{
				//TO TEST
				// e.g.	c:\lint\lint-nt.exe -i"c:\lint" -b -u -i"Debug" --u project.lnt project.options.lnt std.lnt env-vc6.lnt <user parameters> filename.cpp >Lint\filename.cpp.txt
				sParams.Format( _T("-i%s -b -u %s --u %s %s %s %s %s %s >%s"),
								Utils::PathAddQuotes(m_sLintFolder),
								sIntermediateFilesInclude,
								Utils::PathAddQuotes(sProjectLntFileName),
								Utils::PathAddQuotes(sUserLintOptionsFileName),
								Utils::PathAddQuotes(m_sStdtLntFileName),
								Utils::PathAddQuotes(sFrameworkOptionsFile),
								m_sAdditionalParams,
								Utils::PathAddQuotes(sRelativePathName),
								Utils::PathAddQuotes(sRelativeOutputPath) );
			}
			else
			{		
				//TO TEST
				// e.g.	c:\lint\lint-nt.exe -i"c:\lint" -b -u -i"Debug" --u project.lnt std.lnt env-vc6.lnt <user parameters> filename.cpp >Lint\filename.cpp.txt
				sParams.Format( _T("-i%s -b -u %s --u %s %s %s %s %s >%s"),
								Utils::PathAddQuotes(m_sLintFolder),
								sIntermediateFilesInclude,
								Utils::PathAddQuotes(sProjectLntFileName),
								Utils::PathAddQuotes(m_sStdtLntFileName),
								Utils::PathAddQuotes(sFrameworkOptionsFile),
								m_sAdditionalParams,
								Utils::PathAddQuotes(sRelativePathName),
								Utils::PathAddQuotes(sRelativeOutputPath) );
			}
		}
		else
		{
			//TO TEST
			// e.g.	c:\lint\lint-nt.exe -i"c:\lint" -b -u -i"Debug" std.lnt env-vc6.lnt <user parameters> filename.cpp >Lint\filename.cpp.txt
			ATLASSERT(!m_sLintFolder.IsEmpty() );
			sParams.Format( _T("-i%s -b -u %s %s %s %s %s >%s"),
							Utils::PathAddQuotes(m_sLintFolder),
							sIntermediateFilesInclude,
							Utils::PathAddQuotes(m_sStdtLntFileName),
							Utils::PathAddQuotes(sFrameworkOptionsFile),
							m_sAdditionalParams,
							Utils::PathAddQuotes(sRelativePathName),
							Utils::PathAddQuotes(sRelativeOutputPath) );
		}

		// Run PC-Lint to perform the analysis. We use _tsystem() for this as its the simplest way to run
		// a command line process with redirected output.
		//
		// Note that if PC-Lint is installed in a folder with a pathname containing spaces, _tsystem()
		// will fail as it doesn't understand long filenames. To work around this, we obtain the short
		// pathname of the PC-Lint executable and pass that to the command interpreter instead. 
		CString sShortPathName;
		(void)GetShortPathName(m_sLintFolder + _T("\\lint-nt.exe"), sShortPathName.GetBuffer(_MAX_PATH + 1), _MAX_PATH);
		sShortPathName.ReleaseBuffer();

		m_nWarningCount = _tsystem(sShortPathName + _T(" ") + sParams);

		if (255 == m_nWarningCount)
		{
			// If a warning count of 255 is returned, lint-nt.exe has maxed out its errorlevel,
			// so we'll have to try to parse the results file to get the true warning count.
			CAtlArray<CString> arrayLines;
			ATLVERIFY(SUCCEEDED(Utils::ReadTextFile(sRelativeOutputPath, arrayLines) ) );

			for (size_t n = 0; n < arrayLines.GetCount(); n++)
			{
				CString sLine = arrayLines[n];

				sLine.MakeLower();

				// Look for: "error 900: (Note -- Successful completion, nnn messages produced"
				if ( (sLine.Find( _T("error 900:") ) >= 0) || (sLine.Find( _T("note 900:") ) >= 0) )
				{
					CString sCount = Utils::After(sLine, _T("completion") );
					sCount.TrimLeft( _T(" ,") );

					m_nWarningCount = _ttoi(sCount);
				}
			}
		}

		sMsg.Format( _T("%d issues"), m_nWarningCount);
			
		cout << CStringA(sMsg).GetString() << endl;

		m_bAnalysed = true;
		
		return true;
	}
	return false;
}



HRESULT CFileLintAnalyser::GenerateXml(	MSXML2::IXMLDOMDocument* pDOMDoc,
										MSXML2::IXMLDOMElement* pParentElement,
										CString* psMsg /*= NULL*/) const
{
	USES_CONVERSION;

	HRESULT hr = S_OK;

	MSXML2::IXMLDOMDocumentPtr ptrDOMDoc(pDOMDoc);	
	MSXML2::IXMLDOMElementPtr ptrParentElement(pParentElement);	

	try
	{
		MSXML2::IXMLDOMElementPtr const ptrFile = ptrDOMDoc->createElement( _T("File") );

		MSXML2::IXMLDOMAttributePtr const ptrFileNameAttr = ptrDOMDoc->createAttribute(_T("name"));
		ptrFileNameAttr->value = T2COLE(m_sFileName);
		(void)ptrFile->setAttributeNode(ptrFileNameAttr);
		
		(void)ptrParentElement->appendChild(ptrFile);

		MSXML2::IXMLDOMAttributePtr const ptrResultsFileAttr = ptrDOMDoc->createAttribute( _T("resultsfile") );
		ptrResultsFileAttr->value = T2COLE(GetResultsFileName() );
		(void)ptrFile->setAttributeNode(ptrResultsFileAttr);

		MSXML2::IXMLDOMElementPtr const ptrWarnings = ptrDOMDoc->createElement( _T("Warnings") );
		ptrWarnings->text = T2COLE(Utils::IntToStr(m_nWarningCount) );
		(void)ptrFile->appendChild(ptrWarnings);

		MSXML2::IXMLDOMElementPtr const ptrStatus = ptrDOMDoc->createElement( _T("Status") );
		ptrStatus->text = m_bAnalysed ? _T("Complete") : _T("Pending");
		(void)ptrFile->appendChild(ptrStatus);
	}
	catch (const _com_error& e)
	{
		hr = e.Error();

		if (NULL != psMsg)
		{
			psMsg->Format(	_T("Exception 0x%08x in CFileLintAnalyser::GenerateXml()\n"),
							hr);
		}
	}
	return hr;

}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Authors

Anna-Jayne Metcalfe
Founder Riverblade Limited
United Kingdom United Kingdom
I haven't always written software for a living. When I graduated from Surrey University in 1989, it was with an Electronic Engineering degree, but unfortunately that never really gave me the opportunity to do anything particularly interesting (with the possible exception of designing Darth Vader's Codpiece * for the UK Army in 1990).
    * Also known as the Standard Army Bootswitch. But that's another story...
Since the opportunity arose to lead a software team developing C++ software for Avionic Test Systems in 1996, I've not looked back. More recently I've been involved in the development of subsea acoustic navigation systems, digital TV broadcast systems, port security/tracking systems, and most recently software development tools with my own company, Riverblade Ltd.
 
One of my personal specialities is IDE plug-in development. ResOrg was my first attempt at a plug-in, but my day to day work is with Visual Lint, an interactive code analysis tool environment with works within the Visual Studio and Eclipse IDEs or on build servers.
 
I love lots of things, but particularly music, photography and anything connected with history or engineering. I despise ignorant, intolerant and obstructive people - and it shows...I can be a bolshy cow if you wind me up the wrong way...Laugh | :laugh:
 
I'm currently based 15 minutes walk from the beach in Bournemouth on the south coast of England. Since I moved here I've grown to love the place - even if it is full of grockles in Summer!
Follow on   Twitter

-+- Beth Mackenzie -+-
Software Developer Riverblade Ltd
United Kingdom United Kingdom
I'm a software developer and/or tester with Riverblade Ltd (www.riverblade.co.uk) developing our core product range including our Visual Lint integration product and Lint Project Professional.
 
I incorporate a number of technologies into a daily basis including Windows API, C++ (VS2008), Managed C++, CLI, Databases, Java, JNI, Eclipse Framework, CDT and of course Visual Studio Extensibility (VSIP VSX).
 
In my spare time I enjoy cooking (prepping ingredients from scratch!), running, cycling, swimming, reading, interested in experimental electronic music (such as ClockDVA), movies, volunteering my IT skills where I can.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 29 Jan 2009
Article Copyright 2004 by Anna-Jayne Metcalfe, -+- Beth Mackenzie -+-
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid