Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to automatically analyse a C/C++ source file and find out which indent parameters does suit more (for example, produce minimum of changes when filtered though indent)?

share|improve this question

1 Answer 1

There isn't a commonly distributed tool to do this, so you will have to write your own. I would write a Bash script that calls indent with different parameters to create a large number of alternate versions of the original source file, then finds the "best" diff output for each alternate version, where "best" might be simply the least number of changed lines.

My experience is that you can partition the problem if you know who wrote the original code. Programmers tend to adopt long-term formatting habits that differ from programmer to programmer.

You should expect that even in the best cases, the diffs that indent introduces will render svn/git blame useless. This will probably force you to either adopt one specific indent policy for all of the code and start a new repository with automatically formatted code, or else to just abandon the whole idea. A repository with several different indent policies will probably cause more confusion and aggravation than benefit.

If you are working on new code and not just making small changes to existing code, then best practice would be to require all contributors to use a specific indent or astyle policy before submitting code. You could also do this automatically using hooks on svn/git commit but this can cause compile errors or even bugs in rare cases. If you are working in an organization and not running an on-line project where you are the gatekeeper, then you will need a manager with sufficient authority to enforce the policy as most programmers already know from years of experience that their own formatting is the only correct way and all other formatting is invalid.

share|improve this answer
    
The intended sequence is: 1. Find out current code's style policy using this tool; 2. Introduce a little bit of change with crappy text editor that (for example) breaks whitespace, indenting, line endings and removes trailing \n from files; 3. indent with pre-discovered parameters that fix it back into good compliant source code. I.e. outsource all worries about low-level codestyle (indents, whitespace, etc.) from brain to computer. –  Vi. Apr 12 '13 at 9:54
    
"You should expect that even in the best cases, the diffs that indent introduces will render svn/git blame useless." -> I expect after using indent with proper parameters there should introduce zero to few changes, if the code already have some strict style. The question is how to auto-discover that style. –  Vi. Apr 12 '13 at 9:57
    
@Vi.: If the code is consistently using a style, the best way to discover it is by manual inspection. Try doing od -c some_file.c to see if there is consistent use of tabs and spaces, or just search for tabs or spaces in vi to see of there is something consistent. I wouldn't bother trying to automate this. If the original coder followed some known style such as descibed in en.wikipedia.org/wiki/Indent_style then it should be easy to discover by eyeball and test with astyle. –  Jonathan Ben-Avraham Apr 12 '13 at 10:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.