How do I create rules for CPPCHECK - pcre

I have created the following rule file for cpp check:
<?xml version="1.0"?>
<rule version="1">
<pattern>virtual .* \( .*dword .* \)</pattern>
<message>
<id>virtual function</id>
<summary>Possible error </summary>
</message>
</rule>
This rule is detecting only the first matching item in the code
what will be the problem.

I am a Cppcheck developer.
I am not sure .. but if pcre is greedy then the match will contain all code from the first virtual function, until the very last ')' in your code.
don't use .* , maybe [^)]* works better.
If you use --rule on the command line you can see what it matches.

Related

Search and Replace within XML tag via sed over multiple lines

i want to change the value of a specific XML tag property.
There are many questions about how to handle sed, but the problem here is the newline within the tag.
I want to change the value after name= and it must be searched in the <package ... > tag
XMLStarlet is not an option.
Coverage.xml
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="include">
<classes>
<class branch-rate="0.0" complexity="0.0"
My best try so far:
sed -n '/<package/ {
:a
n
/<classes>/q
s/name=/xxxx/g
}' coverage.xml
Do you have an idea?
UPDATE 2: More of coverage.xml with approach of #RavinderSingh13
<package branch-rate="0.031746031746" complexity="0.0"
line-rate="0.159420289855" name="NEW_VALUE">
<classes>
<class branch-rate="0.0" complexity="0.0"
filename="NEW_VALUE"
name="NEW_VALUE">
If you are ok with awk, then as per your shown samples could you please try following once.(this will look only for package tag and its name value and for rest tags it will not do anything)
awk '/^>/{flag=""} /<package/{flag=1} flag && /name=/{sub(/name=.*\"/,"name=\"NEW_VALUE\"")} 1' Input_file
In case you want to save output into Input_file itself append > temp_file && mv temp_file Input_file to above code too.
I just did a little tweak using a for loop.
LINES=`awk '/<package /{print NR+1}' coverage.xml`
for i in ${LINES};
do
echo $i
sed -i ''"${i}"'s/name=.*/name="NEW_VALUE"/' coverage.xml;
done
The NR+1 helps to reach the second line in the package tag.

Powershell: XPath cannot select when element has "xmlns" tag?

I've got a very simple xml, as below:
<?xml version="1.0" encoding="utf-8"?>
<First>
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then I have a powershell script to select "Folder" element:
[xml]$xml=Get-Content "D:\m.xml"
$xml.SelectNodes("//Folder")
It outputs:
#text
-----
today
tomorrow
yesterday
No problem. But if I change the xml file to add "xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to "First" like below:
<?xml version="1.0" encoding="utf-8"?>
<First xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Second>
<Folder>today</Folder>
<FileCount>10</FileCount>
</Second>
<Second>
<Folder>tomorrow</Folder>
<FileCount>90</FileCount>
</Second>
<Second>
<Folder>yesterday</Folder>
<FileCount>22</FileCount>
</Second>
</First>
Then, my powershell script outputs nothing.
Why? How to change my powershell script to support this xmlns?
Thanks a lot.
What you added is default namespace. Unlike prefixed namespace, descendant elements inherit ancestor default namespace implicitly, unless otherwise specified (using explicit prefix or local default namespace that points to different URI).
To select element in namespace, you'll need to define prefix that point to the namespace URI and use that prefix properly in your XPath, for example :
$ns = New-Object System.Xml.XmlNamespaceManager($xml.NameTable)
$ns.AddNamespace("d", $xml.DocumentElement.NamespaceURI)
$xml.SelectNodes("//d:Folder", $ns)

How to write AssemblyVersion to file using MSBuild?

FinalEdit: Despite relative directories not working in the first post, it worked if I simply removed the $(MsBuildThisFileDirectory) from the Exec line.
Edit2: I added the new targets to the DefaultTargets. Which now runs them by default. However, timing was now off with the postbuild command. I added <Exec Command="call $(MsBuildThisFileDirectory)documentation\tools\GenerateDocumentation.bat" IgnoreExitCode="false" /> to the target, but it gives an error that C:\Users\my is not a valid batch file because of the space which is actually C:\Users\my program\documentation\tools\GenerateDocumentation.bat. Putting quotes around the path gives me error MSB4025 that Name cannot begin with $.
Edit: I have tried stijn's code and it works when I explicitly run it from the command line using /t:RetrieveIdentities, but for some reason it doesn't seem to run otherwise.
I have been using Doxygen to generate documentation for my source code, however, I would like to be able to do it automatically. I wrote a simple .bat script to run Doxygen with my desired config file and compile the output into a .chm help file, but I have been unable to change the revision number automatically in Doxygen.
I was attempting to simply update the config file by adding a new line to the config file with the new revision number using MSBuild, but I have been unable to get anything to print or even create a new file when none is present.
The code I have so far I have gotten from other similar questions, but I cannot seem to get it to work.
<ItemGroup>
<MyTextFile Include="\documentation\DoxygenConfigFile.doxyconfig"/>
<MyItems Include="PROJECT_NUMBER = %(MyAssemblyIdentitiesAssemblyInfo.Version)"/>
</ItemGroup>
<Target Name="RetrieveIdentities">
<GetAssemblyIdentity AssemblyFiles="bin\foo.exe">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
<WriteLinesToFile File="#(MyTextFile)" Lines="#(MyItems)" Overwrite="false" Encoding="UTF8" />
</Target>
Encoding is wrong, it should be UTF-8
When working with items/properties, the % and # and $ must come right before the (, no spacing in between: %(MyAssemblyIdentitiesAssemblyInfo.Version)
MyAssemblyIdentitiesAssemblyInfo does not exist, you probably meant MyAssemblyIdentities
Look up how msbuild evaluates properties and items. Basically what it will do in your script is evaluate MyItems, but at that time MyAssemblyIdentities does not yet exist so is empty, and only afterwards the GetAssemblyIdentity gets executed. Fix this by enforcing correct evaluation order: put your items inside the target and make it depend on another target that creates MyAssemblyIdentities before evaluating your items.
To summarize:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="GetAssemblyIdentities">
<GetAssemblyIdentity AssemblyFiles="bin\foo.exe">
<Output TaskParameter="Assemblies" ItemName="MyAssemblyIdentities"/>
</GetAssemblyIdentity>
</Target>
<Target Name="RetrieveIdentities" DependsOnTargets="GetAssemblyIdentities">
<ItemGroup>
<MyTextFile Include="\documentation\DoxygenConfigFile.doxyconfig"/>
<MyItems Include="PROJECT_NUMBER = %(MyAssemblyIdentities.Version)"/>
</ItemGroup>
<WriteLinesToFile File="#(MyTextFile)" Lines="#(MyItems)"
Overwrite="false" Encoding="UTF-8" />
</Target>
</Project>
Note this will only work if you invoke msbuild in the directory where the script is, else the paths (documentation/foo) will be wrong. That could be fixed by using eg $(MsBuildThisFileDirectory)\bin\foo.exe)

How do I config xml namespace for a smooks input file using WSO2?

I couldn't find any advices on how to setup Smooks (on WSO2 Developer Studio 3.1) in order to it properly read a xml input which has a namespace declaration.
Without the namespace the transformation works just fine!
By informing the xmlns in the input, I get this exception:
Error on line 5, column 19 in free-marker-template
Expecting a string, date or number here, Expression .vars["order"]["order-items/order-item/#id"] is instead a freemarker.ext.dom.NodeListModel
The problematic instruction:
----------
==> ${.vars["order"]["order-items/order-item/#id"]} [on line 5, column 17 in free-marker-template]
These are both my input and output:
<order id='444' xmlns="http://example.com">
<header>
<customer number="555">Amila</customer>
</header>
<order-items>
<order-item id='1'>
<product>1</product>
<quantity>2</quantity>
<price>400</price>
</order-item>
</order-items>
</order>
<salesorder>
<details>
<orderid></orderid>
<customer>
<id></id>
<name></name>
</customer>
</details>
<itemList>
<item>
<id></id>
<productId></productId>
<quantity></quantity>
<price></price>
</item>
</itemList>
</salesorder>
I've also tried Smooks coreĀ“s namespace declarations as per Smooks site:
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd"
>
<core:namespaces>
<core:namespace prefix="ex" uri="http://example.com/"/>
</core:namespaces>
But it seems not to be supported in the IDE since the configuration editor raises this exception:
-Value 'org.eclipse.emf.ecore.xml.type.impl.AnyTypeImpl#53abd5a9 (mixed: null, anyAttribute: null)' is not legal. (platform:/resource/Corp/smooks-config.xml,6,20)
Well, any idea?
Instead of Smooks, can't you use a XSLT? It will avoid the namespace issue you have.

NAnt IF task doesn't seem to work

I'm trying the example from the NAnt documentation for the if task at:
http://nant.sourceforge.net/release/0.85/help/tasks/if.html
Specifically the following code...
<if test="${build.configuration='release'}">
<echo>Build release configuration</echo>
</if>
where build.configuration has been defined beforehand as
<property name="build.configuration" value="debug" overwrite="false" />
When I run it using nant.exe (version 0.91.3881.0), I get the following error:
'}' expected
Expression: ${build.configuration='release'}
^
I'm guessing I'm missing something simple?
You need to double the = symbol as per your web page.
When programming, = is an assignment operator in most languages, whereas == is the boolean comparison operator.
Maybe the NAnt reference should be changed then....
<if test="${build.configuration='release'}">
<echo>Build release configuration</echo>
</if>
See the manual.