Click here to Skip to main content
15,883,938 members
Articles / Desktop Programming / MFC

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

,
Rate me:
Please Sign up or sign in to vote.
4.83/5 (36 votes)
29 Jan 2009CPOL13 min read 279.5K   1.7K   52  
Utility to run PC-Lint on Visual C++ solutions and projects, generating XML and HTML reports of the results.
/************************************************************************
 *
 *  Description : CProjectFileReader - class to parse Visual Studio
 *                project files.
 *
 *     (c) Copyright 2000-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/AddIns/Visual Lint/Development/Libraries/AddInSolutionModel/src/ProjectFileReader.cpp $
 *   $Revision: 43 $
 *       $Date: 5/03/08 16:49 $
 *     $Author: Anna $
 * 
 * $Nokeywords: $
 ************************************************************************/

/// \file	
/// \brief CProjectFileReader implementation.

//lint -esym(749, *::ConfigurationTypes::*, *::useOfMfc::*, *::useOfATL::*, *::charSet::*)	 (Information -- local enumeration constant not referenced)

#include "StdAfx.h"


// _UTILS_LOCAL_INCLUDE is set if copies of the include files we
// need from the Utils library are compiled locally.
#ifdef _UTILS_LOCAL_INCLUDE
	#include "SplitPath.h"
#else
	#include <Utils\Include\SplitPath.h>
#endif

#include "ProjectFileReader.h"
#include "ProjectFileReaderImpl.h"
#include "Vc6ProjectFileReaderImpl.h"
#include "Vc7ProjectFileReaderImpl.h"
#include "CSharpProjectFileReaderImpl.h"



namespace Riverblade
{
	namespace Libraries
	{
		namespace AddInSolutionModel
		{
			/////////////////////////////////////////////////////////////////////////////
			// CProjectFileReader

			/// \brief Default constructor
			///
			CProjectFileReader::CProjectFileReader(void)
				:	m_pImpl(NULL)
			{
			}


			/// \brief Class destructor
			///
			CProjectFileReader::~CProjectFileReader(void)
			{
				try
				{
					if (NULL != m_pImpl)
					{
						delete m_pImpl;
					}
				}
				catch (...)
				{
					ATLASSERT(!"Unexpected exception caught in CProjectFileReader destructor");
				}

				m_pImpl = NULL;
			}


			/////////////////////////////////////////////////////////////////////////////
			// CProjectFileReader operations

			/// \brief Return the pathname of the folder containing the project file.
			///
			/// \return					The pathname of the folder containing the project file.
			///
			CString CProjectFileReader::GetProjectFolder(void) const
			{
				ATLASSERT(NULL != m_pImpl);
				if (NULL != m_pImpl)
				{
					return m_pImpl->GetProjectFolder();
				}
				return _T("");
			}


			/// \brief Read and parse the project file with the given pathname.
			/// 
			/// \param	sPathName			The pathname of the project file
			/// \return						\em true if the project file was parsed successfully;
			///								\em false otherwise.
			///
			bool CProjectFileReader::Read(const CString& sPathName, const CString& sSolutionPathName /*= _T("")*/ )
			{
				if (NULL != m_pImpl)
				{
					delete m_pImpl;
					m_pImpl = NULL;
				}

				m_pImpl = CreateImplementation(sPathName);

				if (NULL != m_pImpl)
				{
					if (!sSolutionPathName.IsEmpty() )
					{
						ATLVERIFY(m_pImpl->SetSolutionPathName(sSolutionPathName) );
					}
					return m_pImpl->Read(sPathName);
				}
				return false;
			}


			/////////////////////////////////////////////////////////////////////////////
			// Operations

			/// \brief Return the names of the files within the project
			///
			/// \param	rarrayFiles					An array which will be filled with the filenames of all files in the project.
			/// \param	sExt						An optional extension, which will be used to filter the filenames placed in rarrayFiles.
			/// \return								The number of filenames returned in the array.
			///
			int CProjectFileReader::GetFiles(CAtlArray<CString>& rarrayFiles, const CString& sExt /*= _T("")*/ ) const
			{
				rarrayFiles.RemoveAll();

				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->GetFiles(rarrayFiles, sExt);
				}
				return static_cast<int>(rarrayFiles.GetCount() );
			}


			/// \brief Return the names of the configurations within the project.
			///
			/// \param	rarrayConfigurations		An array which will be filled with the names of the configurations within the project.
			/// \return								The number of configurations returned in the array.
			///
			int CProjectFileReader::GetConfigurations(CAtlArray<CString>& rarrayConfigurations) const
			{
				rarrayConfigurations.RemoveAll();

				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->GetConfigurations(rarrayConfigurations);
				}
				return static_cast<int>(rarrayConfigurations.GetCount() );
			}


			/// \brief Determine whether the given configuration name is valid.
			///
			///	\param	sConfig						A configuration string identifying the project configuration.
			/// \return								\em true if the configuration is valid; \em false otherwise.
			///
			bool CProjectFileReader::IsValidConfiguration(const CString& sConfig) const
			{
				ATLASSERT(-1 == sConfig.Find(_T(" - ") ) );		// Check that a VC6 full config string has not been passed in [Anna 5.3.2008].
				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->IsValidConfiguration(StripProjectNameFromConfigString(sConfig) );
				}
				return false;
			}


			/// \brief Return the "configuration string" for a given configuration within the project.
			///
			/// \param	sConfig						The name of the configuration for which the configuration string should be returned.
			/// \return								The corresponding configuration string, or an empty string if the configuration name
			///										is not recognised.
			///
			CString CProjectFileReader::GetConfigurationString(const CString& sConfig) const
			{
				ATLASSERT(-1 == sConfig.Find(_T(" - ") ) );		// Check that a VC6 full config string has not been passed in [Anna 5.3.2008].
				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->GetConfigurationString(StripProjectNameFromConfigString(sConfig) );
				}
				return _T("");
			}


			/// \brief Return the "Intermediate Files" folder for a given configuration within the project.
			///
			/// \param	sConfig						The name of the configuration for which the folder path should be returned.
			/// \return								The corresponding "Intermediate Files" folder path. This is likely to be a relative path.
			///
			CString CProjectFileReader::GetIntermediateFilesFolder(const CString& sConfig) const
			{
				ATLASSERT(-1 == sConfig.Find(_T(" - ") ) );		// Check that a VC6 full config string has not been passed in [Anna 5.3.2008].
				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->GetIntermediateFilesFolder(StripProjectNameFromConfigString(sConfig) );
				}
				return _T("");
			}


			/// \brief Return the properties of a given configuration within the project.
			///
			/// \param	sConfig						The name of the configuration for which the properties should be returned.
			/// \return								A CProjectConfiguration object containing the
			///                                     properties of the configuration.
			///
			CProjectConfiguration CProjectFileReader::GetConfigurationProperties(const CString& sConfig) const
			{
				ATLASSERT(-1 == sConfig.Find(_T(" - ") ) );		// Check that a VC6 full config string has not been passed in [Anna 5.3.2008].
				ATLASSERT(NULL != m_pImpl);

				if (NULL != m_pImpl)
				{
					return m_pImpl->GetConfigurationProperties(StripProjectNameFromConfigString(sConfig) );
				}
				return CProjectConfiguration();
			}


			/// \brief Expand any Visual Studio environment variables in the given string.
			///
			/// The string may be a pathname, sequence of pathnames or just a delimited string containing
			/// environment variables. Note that only Windows environment vars are currently supported;
			/// Visual Studio specific environment variables such as "$(TargetPath)" are not supported
			/// and will be ignored.
			///
			/// Please note that once full environment var support is added this method will
			/// be moved to CProjectConfiguration.
			///
			///
			/// \param	rsText			A string containing environment variables in Visual Studio
			///							format (e.g. "$(varname)" )
			/// \return					\em true if all environment vars found were expanded; \em false
			///							if any remain unexpanded.
			///
			bool CProjectFileReader::ExpandEnvironmentVars(CString& rsText) const
			{
				ATLASSERT(NULL != m_pImpl);
				if (NULL != m_pImpl)
				{
					return m_pImpl->ExpandEnvironmentVars(rsText);
				}
				return false;
			}


			////////////////////////////////////////////////////////////////////////////////
			// CProjectFileReader implementation

			/// \brief Create an implementation class to read the specified file.
			///
			/// \param	sPathName			A string containing the pathname of the project file.
			/// \return						A pointer to a CProjectFileReaderImpl object constructed
			///								on the heap to read the file. Note that the caller is 
			///								responsible for deleting this object.
			///
			CProjectFileReaderImpl* CProjectFileReader::CreateImplementation(const CString& sPathName) const
			{
				CProjectFileReaderImpl* pImpl = NULL;

				Utils::CSplitPath splitpath(sPathName);
				CString sExt = splitpath.GetExtension();
				(void)sExt.MakeLower();

				if ( (_T(".dsp") == sExt) || (_T(".vcp") == sExt) )
				{
					pImpl = new CVc6ProjectFileReaderImpl;		// VS6 or eVC4 project
				}
				else if (_T(".csproj") == sExt)
				{
					pImpl = new CCSharpProjectFileReaderImpl;;	// C# project
				}
				else if ( (_T(".vcproj") == sExt) || (_T(".icproj") == sExt) )
				{
					pImpl = new CVc7ProjectFileReaderImpl;;		// VS2002, 2003, 2005 or 2008 C++ project
				}
				return pImpl;
			}


			/// \brief Return the pathname corresponding to the given filename within the project.
			/// 
			/// \param	sFileName			The filename.
			/// \return						The corresponding to the filename.
			///
			CString CProjectFileReader::GetPathName(const CString& sFileName) const
			{
				ATLASSERT(NULL != m_pImpl);
				if (NULL != m_pImpl)
				{
					return m_pImpl->GetPathName(sFileName);
				}
				return sFileName;
			}



			/// \brief Return a copy of the given configuration name/string, minus any project name.
			///
			///	This method is provided primarily for ease of interfacing to legacy Visual C++ (VC6, eVC4 etc.) systems,
			/// where configuration names and strings are easily confused.
			///
			/// \param	sConfig				The configuration name or project configuration string (e.g.: "LintProject - Win32 Debug").
			/// \return						The configuration name, minus the project name (if any).
			///
			CString CProjectFileReader::StripProjectNameFromConfigString(const CString& sConfig)
			{
				if (-1 != sConfig.Find(_T(" - ") ) )
				{
					// Handle any cases where a VC6 full configuration string is erroneously passed in [Anna 5.3.2008].
					return Utils::After(sConfig, _T(" - ") );	
				}
				return sConfig;
			}


		};	// namespace AddInSolutionModel
	};	// namespace Libraries
};	//namespace Riverblade

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)


Written By
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!

Written By
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.

Comments and Discussions