Friday, September 27, 2013

Embedding C++ code analysis into Visual Studio

Once needed to perform C++ code analysis in order to find multibyte-unfriendly code. Perl with regular expressions helped me a much. Then I've realized that asking everybody in my team to work from DOS console would be inconvenient, thus I've started thinking how to embed this stuff into my lovely IDE (*)

I even asked the community a question: How do I modify View.FindResults1 contents in Visual Studio? 
and got am advice to my own version of the "Find Results" window by creating a Visual Studio extension myself. We can start by having a look at Extending Visual Studio > Tool Windows


Probably writing VisualStudio extensions is a deal of my future self-development. For now I've stopped on calling the script from Post-Build process:
perl $(SolutionDir)\Localization\test_resource_funs.pl  $(InputDir)
thus having its output in Compile Output window. No additional efforts are needed. I can even use "Errors List" to sort and observe all of script warnings.

"Output" window:

"Error List" window:


* - I bet Visual Studio is the best one IDE.

PS see: Formatting the Output of a Custom Build Step or Build Event 

Wednesday, September 25, 2013

Line count with .bat

When it comes to routine, some can think of wasted hours, yearning, many cups of cofee trying to to fall asleep during your work. In that case it's better to think about robots. Let robots do their staff.

I have a perl script which has to be called 100 times for each of VS solution subdirectory. I don't want to write another Perl script to automate it, so let's use .BAT file:
@echo off

if "%1" == "" GOTO HELP

for /D %%D in (%1*) do perl the_script.pl %%D > %%D.txt

goto END

:HELP
echo test_resource_funs_loop.bat - calls test_resource_funs.pl with each subdirectory
echo Usage example: test_resource_funs_loop.bat C:\Dev-9.1\hyp_src\

:END
Thanks to Brett Batie I've written Linux wc command analogue to count # of lines in the archived 100 .txt files:
@echo off
findstr /R /N "^" %1  |find /C ":"

Tuesday, September 17, 2013

Suggest using of Visual Assist X



I’d highly recommend using of the subject since it significantly improves code traversing, understanding of code written by others, code refactoring, etc.
Visual Assist is a Productivity and code refactoring plugin an is much more effective than Visual Studio Intellisence. You can switch off Intellisence and use Visual Assist X instead.

Sweets:

  • Its “Go to definition” never fails and works even if you have compile errors;

  • Find references finds much more precisely then of Intellisence
    For example, it can find all usages of CString::operator+() rather than searching for ‘+’ with “Find-In-Files”, which might be very useful;

  • “Call tree” and “Caller tree” gives you opportunity to investigate without running the code. Sometimes you even don’t have idea how to run/debug this particular function.

  • “Rename” and “Extract function” speeds up any rewriting.

  • It highlights the same variables/words on a screen that cursor is currently staying on. Helps a lot finding all ‘i’-s in a function.

  • It speeds up navigation among tons of sources an classes/function names. To look for a function named either “FillDirectory” or “DirectoryFill”, just type “Directory” and choose from the list.

 Read more about Visual Assist Features, try its 30-days trial, if interested

This is my proposal, it’s up to you whether to ask for purchasing it. But it has good discount policy: 5 copies costs as 4 separate purchases.

PS Note that it is not a static analysis tool like Klockwork or Coverity. Static analysis tools goal is to warn you about potential errors, thus code is written not according to Code Complete

Thursday, September 12, 2013

Dual personality of while

At a first glance nothing special. Then you see dual personality
   while(1)
   {
      ch = ::CharPrev(m_str1, ch);
      TestString s(ch);
   } while (ch!=m_str1);

Friday, September 6, 2013

Cure for broken TEXTINCLUDE section of VC++ resource files


Today I've configured VC++ project to allow editing Chinese resources from Visual Studio, which ended up in SVN Commit error “Inconsistent line ending style”.

Pic 1.  After each resource modification Visual Studio was writing incorrect end of line sequence to “Read-Only Symbol Directives” area

Thursday, September 5, 2013

Why is UTF-8 treated as not multibyte?



It’s a long discussion and object of terminology. Multibyte is a slippery term and is not the best one.
Visual Studio has 3 options for characters sets:

a) No characters set, which means it works OK with single byte characters sets (SBCS) like CP1251 (ru-RU) or CP1252 (en-US)
characters take 1 byte

b) MBCS, which means it works OK with multibyte Character sets like CP936,
characters take 1 or 2 bytes, GUI accepts such characters if appropriate locale is selected in Control Panel

c) Unicode, which means working with UTF-16BE,
characters take 2 bytes, selected locale doesn’t make any sense

Note that there is no option to work with utf-8.
There are conversion functions utf8 <-> MBCS.
In Microsoft documentation term “multibyte” is related to MBCS. It was hard for me to achieve, and I suppose there could be misunderstanding among the team regarding this term.

Even having the same way of coding and having floating amount of bytes, utf8 is a way of encoding Unicode characters, it is not related to MBCS at all.

In order to have our virare and maina (it.) we’ve agreed to call MBCS multibyte