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

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 : CVc7ProjectFileReaderImpl - implementation class to parse
 *                Visual C++ project files for Visual Studio .NET 2002 onwards.
 *
 *     (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/Applications/SourceVersioner/Development/Shared/Vc7ProjectFileReaderImpl.h $
 *   $Revision: 16 $
 *       $Date: 28/04/08 9:12 $
 *     $Author: Anna $
 * 
 * $Nokeywords: $
 ************************************************************************/

/// \file	
/// \brief Implementation class for CProjectFileReader.

#pragma once

#ifdef _UTILS_LOCAL_INCLUDE
	#include "StringUtils.h"
	#include "XmlUtils.h"
#else
	#include <Utils\Include\StringUtils.h>
	#include <Utils\Include\XmlUtils.h>
#endif

#include "ProjectFileReaderImpl.h"


namespace Riverblade
{
	namespace Libraries
	{
		namespace AddInSolutionModel
		{
			/// \brief Implementation class to read Visual C++ project files for Visual Studio NET 2002 onwards.
			///
			class CVc7ProjectFileReaderImpl : public CProjectFileReaderImpl
			{
				public:
					/// \brief Default constructor.
					///
					CVc7ProjectFileReaderImpl(void)
						:	CProjectFileReaderImpl()
					{
					}


					/// \brief Class destructor.
					///
					virtual ~CVc7ProjectFileReaderImpl(void)
					{
					}


					/// \brief Parse the given Visual C++ 7.0/7.1/8.0/9.0 project file contents.
					/// 
					/// \param	sFileText			The text of the project file.
					/// \return						\em true if the contents of the file was parsed successfully;
					///								\em false otherwise.
					///
					virtual bool Parse(const CString& sFileText)
					{
						USES_CONVERSION;

						Utils::CSplitPath split(m_sProjectPathName);

						try
						{
							MSXML2::IXMLDOMDocumentPtr ptrDOMDoc;

							// Create empty DOM
							const HRESULT hr = ptrDOMDoc.CreateInstance("Msxml2.DOMDocument.3.0");
							ATLASSERT(SUCCEEDED(hr) );
							DBG_UNREFERENCED_LOCAL_VARIABLE(hr);

							ATLVERIFY(VARIANT_FALSE != ptrDOMDoc->loadXML(T2COLE(sFileText) ) );

							const MSXML2::IXMLDOMElementPtr ptrRootElement = ptrDOMDoc->selectSingleNode(L"VisualStudioProject");

							//<VisualStudioProject
							//	ProjectType="Visual C++"
							//	Version="8.00"
							m_sProjectName = Utils::GetStringValue(ptrRootElement, L"Name");

							// Read configurations and files
							ReadConfigurations(ptrDOMDoc);
							ReadFiles(ptrDOMDoc);

							return true;
						}
						catch (const _com_error& e)
						{
							DBG_UNREFERENCED_PARAMETER(e);

							ATLTRACE(_T("Unexpected COM exception 0x%X (%s) caught in CVc7ProjectFileReaderImpl::Parse()\n"), e.Error(), e.ErrorMessage() );
							ATLASSERT(!"Unexpected COM exception caught in CVc7ProjectFileReaderImpl::Parse()");
						}
						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.
					///
					virtual CString GetConfigurationString(const CString& sConfig) const
					{
						CString sUnused;
						if (m_mapAvailableConfigurations.Lookup(sConfig, sUnused) )
						{
							return sConfig;
						}
						return _T("");
					}


				// Implementation
				private:
					/// \brief Read the configuration described within the given XML element
					///
					/// \param	ptrConfigElement	A reference to a MSXML2::IXMLDOMElementPtr object holding the XML data for the configuration.
					/// \param	rConfig				A reference to a CProjectConfiguration object which will receive details of the configuration.
					/// \return						\em true if the configuration was read successfully; \em false otherwise.
					///
					bool ReadConfiguration(const MSXML2::IXMLDOMElementPtr& ptrConfigElement, CProjectConfiguration& rConfig)
					{
						rConfig.m_sName = Utils::GetStringValue(ptrConfigElement, L"Name");

						// e.g.	<Configuration	Name="Debug|Win32"
						//						OutputDirectory=".\..\Debug"
						//						IntermediateDirectory=".\Debug"
						//						ConfigurationType="2"
						//						UseOfMFC="2"
						//						ATLMinimizesCRunTimeLibraryUsage="FALSE"
						//						CharacterSet="2">

						rConfig.m_sIntermediateFolder	= Utils::GetStringValue(ptrConfigElement, L"IntermediateDirectory");
						rConfig.m_eConfigurationType	= Utils::GetIntegerValue(ptrConfigElement, L"ConfigurationType");
						rConfig.m_eCharacterSet			= Utils::GetIntegerValue(ptrConfigElement, L"CharacterSet");
						rConfig.m_bMinimiseRTL			= Utils::GetBooleanValue(ptrConfigElement, L"ATLMinimizesCRunTimeLibraryUsage");
						rConfig.m_eUseOfMfc				= Utils::GetIntegerValue(ptrConfigElement, L"UseOfMFC");
						rConfig.m_eUseOfAtl				= Utils::GetIntegerValue(ptrConfigElement, L"UseOfAtl");

						if (0 == rConfig.m_sIntermediateFolder.Find(_T(".\\") ) )
						{
							// Remove ".\" from the start of any pathnames, as they can confuse PC-Lint [Anna 12.2.2008].
							rConfig.m_sIntermediateFolder = Utils::After(rConfig.m_sIntermediateFolder, _T(".\\") );
						}

						//<Tool	Name="VCCLCompilerTool"
						//		AdditionalOptions="/Zm200"
						//		Optimization="0"
						//		PreprocessorDefinitions="WIN32;_WINDOWS;_DEBUG;_USRDLL;_UNICODE"
						//		MinimalRebuild="TRUE"
						//		BasicRuntimeChecks="3"
						//		RuntimeLibrary="3"
						//		BufferSecurityCheck="TRUE"
						//		  TreatWChar_tAsBuiltInType="false"
						//		ForceConformanceInForLoopScope="TRUE"
						//		RuntimeTypeInfo="TRUE"
						//		UsePrecompiledHeader="0"
						//		WarningLevel="4"
						//		Detect64BitPortabilityProblems="TRUE"
						//		DebugInformationFormat="4"/>

						// N.B. Project files for the Intel compiler (.icproj files) have a "CppCmplrTool" element instead of "VCCLCompilerTool"
						MSXML2::IXMLDOMElementPtr ptrCompilerElement = ptrConfigElement->selectSingleNode(L"Tool[(@Name='VCCLCompilerTool') or (@Name='CppCmplrTool')]");

						if (NULL != ptrCompilerElement)
						{
							rConfig.m_sPreprocessorDefinitions			= Utils::GetStringValue(ptrCompilerElement, L"PreprocessorDefinitions");
							rConfig.m_sAdditionalIncludeDirectories		= Utils::GetStringValue(ptrCompilerElement, L"AdditionalIncludeDirectories");
							rConfig.m_eRuntimeLibrary					= Utils::GetIntegerValue(ptrCompilerElement, L"RuntimeLibrary");
							rConfig.m_bEnableRuntimeTypeInfo			= Utils::GetBooleanValue(ptrCompilerElement, L"RuntimeTypeInfo");
							rConfig.m_bForceConformanceInForLoopScope	= Utils::GetBooleanValue(ptrCompilerElement, L"ForceConformanceInForLoopScope", rConfig.m_bForceConformanceInForLoopScope);
							rConfig.m_bNativeWideChars					= Utils::GetBooleanValue(ptrCompilerElement, L"TreatWChar_tAsBuiltInType", rConfig.m_bNativeWideChars);
							rConfig.m_sAdditionalCommandLineOptions		= Utils::GetStringValue(ptrCompilerElement, L"AdditionalOptions");
						}

						// If there are any inherited property sheets, read the inherited properties
						const CString sInheritedProperties = Utils::GetStringValue(ptrConfigElement, L"InheritedPropertySheets");

						if (!sInheritedProperties.IsEmpty() )
						{
							ATLVERIFY(ReadInheritedProperties(sInheritedProperties, rConfig) );
						}
						return true;
					}


					/// \brief Read the contents of an inherited property sheet (.vsprops) file and merge it into the projects configurations.
					///
					/// \param	sPathName					The pathname of the .vsprops file.
					/// \param	rConfig						The corresponding project configuration
					/// \return								\em true if OK; \em false otherwise.
					///
					bool ReadInheritedPropertiesFile(const CString& sPathName, CProjectConfiguration& rConfig)
					{
						try
						{
							MSXML2::IXMLDOMDocumentPtr ptrDOMDoc;

							// Create empty DOM
							const HRESULT hr = ptrDOMDoc.CreateInstance("Msxml2.DOMDocument.3.0");
							ATLASSERT(SUCCEEDED(hr) );
							DBG_UNREFERENCED_LOCAL_VARIABLE(hr);

							if (VARIANT_FALSE != ptrDOMDoc->load(T2COLE(sPathName) ) )
							{
								const MSXML2::IXMLDOMElementPtr ptrRootElement = ptrDOMDoc->selectSingleNode(L"VisualStudioPropertySheet");
								ATLASSERT(NULL != ptrRootElement);

								const CString sInheritedPropertySheets = Utils::GetStringValue(ptrRootElement, L"InheritedPropertySheets");

								if (!sInheritedPropertySheets.IsEmpty() )
								{
									ATLVERIFY(ReadInheritedProperties(sInheritedPropertySheets, rConfig) );		// Recursive call
								}

								MSXML2::IXMLDOMNodeListPtr ptrUserMacros = ptrDOMDoc->selectNodes(L"VisualStudioPropertySheet/UserMacro");
								if (NULL != ptrUserMacros)
								{
									const long nLength = ptrUserMacros->length;

									for (long nItem = 0; nItem < nLength; nItem++)
									{
										MSXML2::IXMLDOMElementPtr ptrUserMacroElement = ptrUserMacros->Getitem(nItem);
										if (NULL != ptrUserMacroElement)
										{
											const CString sName	= Utils::GetStringValue(ptrUserMacroElement, L"Name");
											CString sValue		= Utils::GetStringValue(ptrUserMacroElement, L"Value");

											ATLVERIFY(ExpandEnvironmentVars(sValue) );

											if (!sName.IsEmpty() )
											{
												m_mapInternalEnvVars[sName] = sValue;
											}
										}
									}
								}

								const MSXML2::IXMLDOMElementPtr ptrToolElement = ptrDOMDoc->selectSingleNode(L"VisualStudioPropertySheet/Tool[(@Name='VCCLCompilerTool') or (@Name='CppCmplrTool')]");

								if (NULL != ptrToolElement)
								{
									// Read any additional include folder specifications within the InheritedPropertySheet configuration
									CString sAdditionalIncludes = Utils::GetStringValue(ptrToolElement, L"AdditionalIncludeDirectories");

									ATLVERIFY(ExpandEnvironmentVars(sAdditionalIncludes) );

									if (!sAdditionalIncludes.IsEmpty() )
									{
										sAdditionalIncludes.TrimRight(_T(";") );
										sAdditionalIncludes.Replace( _T("\\\\"), _T("\\") );		// Replace doubled occurrences of \ in the pathname
		
										rConfig.m_sAdditionalIncludeDirectories += (_T(";") + sAdditionalIncludes);
										rConfig.m_sAdditionalIncludeDirectories.TrimLeft(_T(";") );
									}

									// Read any preprocessor directives within the InheritedPropertySheet configuration
									const CString sPreprocessorDefinitions = OLE2CT(ptrToolElement->getAttribute(L"PreprocessorDefinitions").bstrVal);

									if (!sPreprocessorDefinitions.IsEmpty() )
									{
										rConfig.m_sPreprocessorDefinitions += (_T(";") +sPreprocessorDefinitions);
										rConfig.m_sPreprocessorDefinitions.TrimLeft(_T(";") );
									}
								}
								return true;
							}
						}
						catch (const _com_error& e)
						{
							DBG_UNREFERENCED_PARAMETER(e);

							ATLTRACE(_T("Unexpected COM exception 0x%X (%s) caught in CVc7ProjectFileReaderImpl::Parse()\n"), e.Error(), e.ErrorMessage() );
							ATLASSERT(!"Unexpected COM exception caught in CVc7ProjectFileReaderImpl::Parse()");
						}
						return false;
					}
					
					
					/// \brief Read the contents of an inherited property sheet (.vsprops) file and merge it into the projects configurations.
					///
					/// \param	sInheritedProperties		The "InheritedPropertySheet" property from the project configuration.
					/// \param	rConfig						The corresponding project configuration
					/// \return								\em true if OK; \em false otherwise.
					///
					bool ReadInheritedProperties(const CString& sInheritedProperties, CProjectConfiguration& rConfig)
					{
						int nPos = 0;

						do 
						{
							CString sFileName = sInheritedProperties.Tokenize(_T(";"), nPos);
							sFileName.Trim();

							if (!sFileName.IsEmpty() )
							{
								ATLVERIFY(ExpandEnvironmentVars(sFileName) );

								sFileName.Replace( _T("\\\\"), _T("\\") );							// Replace doubled occurrences of \ in the pathname

								(void)ReadInheritedPropertiesFile(GetPathName(sFileName), rConfig);	// May fail if the .vsprops file is no longer around
							}
						} while (nPos >= 0);

						return true;
					}


					/// \brief Implementation method to read data on the configurations within the project.
					///
					/// \param	ptrDOMDoc		A reference to the XML Document
					///
					void ReadConfigurations(const MSXML2::IXMLDOMDocumentPtr& ptrDOMDoc)
					{
						ATLASSERT(NULL != ptrDOMDoc);
						if (NULL != ptrDOMDoc)
						{
							const MSXML2::IXMLDOMElementPtr ptrRootElement = ptrDOMDoc->selectSingleNode(L"VisualStudioProject");
							ATLASSERT(NULL != ptrRootElement);

							const int nVersion = Utils::GetIntegerValue(ptrRootElement, L"Version");					// NB we don't care about the minor version - only the major version.

							const MSXML2::IXMLDOMNodeListPtr ptrConfigNodes = ptrDOMDoc->selectNodes(L"VisualStudioProject/Configurations/Configuration");
							ATLASSERT(NULL != ptrConfigNodes);

							if (NULL != ptrConfigNodes)
							{
								const long nCount = ptrConfigNodes->Getlength();

								for (long n = 0; n < nCount; ++n)
								{
									const MSXML2::IXMLDOMElementPtr ptrConfigElement = ptrConfigNodes->Getitem(n);

									CProjectConfiguration config;

									// VS2005 has native wide chars on by default [Anna 30.11.2007].
									config.m_bNativeWideChars = (nVersion >= 8) ? true : false;

									if (ReadConfiguration(ptrConfigElement, config) )
									{
										(void)m_mapAvailableConfigurations.SetAt(config.m_sName, config.m_sIntermediateFolder);
										m_mapConfigToSettings[config.m_sName] = config;
									}
								}
							}
						}
					}


					/// \brief Implementation method to read data on the files within the project.
					///
					/// \param	ptrDOMDoc		A reference to the XML Document
					///
					void ReadFiles(const MSXML2::IXMLDOMDocumentPtr& ptrDOMDoc)
					{
						ATLASSERT(NULL != ptrDOMDoc);
						if (NULL != ptrDOMDoc)
						{
							// Files
							const MSXML2::IXMLDOMNodeListPtr ptrFileNodes = ptrDOMDoc->selectNodes(L"VisualStudioProject/Files//File");

							const long nCount = ptrFileNodes->Getlength();

							for (long nFile = 0; nFile < nCount; ++nFile)
							{
								const MSXML2::IXMLDOMElementPtr ptrFileElement = ptrFileNodes->Getitem(nFile);

								const CString sRelativePathName = Utils::GetStringValue(ptrFileElement, L"RelativePath");
								ATLTRACE(_T("") );

								const CString sPathName = GetPathName(sRelativePathName);
								if (!sPathName.IsEmpty())
								{
									// Read any per-file configurations.
									ReadFileConfigurations(ptrFileElement, sPathName);

									(void)m_arrayFiles.Add(sPathName);
								}
							}
						}
					}


					/// \brief Implementation method to read the per-file configurations for the specified file
					///
					///	e.g.:
					/// \code
					///		<File
					///		RelativePath="LintProject.cpp">
					///			<FileConfiguration
					///				Name="Debug|Win32">
					///			<Tool
					///				Name="VCCLCompilerTool"
					///				Optimization="0"
					///				AdditionalIncludeDirectories=".\"
					///				PreprocessorDefinitions="FOOBLE"
					///				BasicRuntimeChecks="3"/>
					///			</FileConfiguration>
					///			<FileConfiguration
					///				Name="Release|Win32">
					///				<Tool
					///					Name="VCCLCompilerTool"
					///					Optimization="2"
					///				PreprocessorDefinitions=""/>
					///			</FileConfiguration>
					///		</File>
					///	\endcode
					///
					/// \param	ptrFileElement		A reference to an MSXML2::IXMLDOMElementPtr & object holding the "File" element
					/// \param	sPathName			A string holding the full pathname of the file.
					///
					void ReadFileConfigurations(const MSXML2::IXMLDOMElementPtr& ptrFileElement, const CString& sPathName)
					{
						ATLASSERT(NULL != ptrFileElement);
						if (NULL != ptrFileElement)
						{
							const MSXML2::IXMLDOMNodeListPtr ptrFileConfigNodes = ptrFileElement->selectNodes(L"FileConfiguration");

							const long nConfigCount = ptrFileConfigNodes->Getlength();

							for (long nConfig = 0; nConfig < nConfigCount; ++nConfig)
							{
								const MSXML2::IXMLDOMElementPtr ptrFileConfiguration = ptrFileConfigNodes->Getitem(nConfig);

								const CString sConfigName = Utils::GetStringValue(ptrFileConfiguration, L"Name");

								const MSXML2::IXMLDOMNodeListPtr ptrToolNodes = ptrFileConfiguration->selectNodes(L"Tool[(@Name='VCCLCompilerTool') or (@Name='CppCmplrTool')]");

								const long nToolCount = ptrToolNodes->Getlength();

								for (long nTool = 0; nTool < nToolCount; ++nTool)
								{
									const MSXML2::IXMLDOMElementPtr ptrTool = ptrToolNodes->Getitem(nTool);

									CFileConfiguration fileconfig;

									fileconfig.m_sAdditionalIncludeDirectories	= Utils::GetStringValue(ptrTool, L"AdditionalIncludeDirectories");
									fileconfig.m_sPreprocessorDefinitions		= Utils::GetStringValue(ptrTool, L"PreprocessorDefinitions");
									fileconfig.m_sAdditionalCommandLineOptions	= Utils::GetStringValue(ptrTool, L"AdditionalOptions");

									if (!fileconfig.IsEmpty() )
									{
										CProjectConfiguration projectconfig;
										(void)m_mapConfigToSettings.Lookup(sConfigName, projectconfig);

										ATLVERIFY(projectconfig.SetFileConfiguration(sPathName, fileconfig) );

										(void)m_mapConfigToSettings.SetAt(sConfigName, projectconfig);
									}
								}
							}
						}
					}

			};


		};	// 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)

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.140814.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