12

We have students submit exercises in a course while they have to adhere to some code conventions. For example, functions names should be in camelCase. Function length should not exceed 50 lines (The tasks are simple enough to divide) etc. I'm looking for a tool that can automatically check that for C/C++ (Both are needed). That is, I would like the tool to complain when something is wrong so the student can fix it. So far I've been unable to find something that fits well. If the thing is open source and can be easily configured to our needs that will be o.k.

5
  • 2
    You're not likely to find a tool to enforce camelCase function names in C++ code, because the dominant style is lowercase_with_underscores, with a substantial number of deviants who prefer PascalCasing. Your best hope is something configurable.
    – Ben Voigt
    Jan 11, 2012 at 21:45
  • 1
    Why do you want to do that automatically? You could at least read and correct the homework manually, that is your job. You cannot quantify style. You can quantify compliance, but that is something completely different. Jan 11, 2012 at 21:45
  • 4
    We are quantifying compliance. We want to do it automatically so when a students has a mistake on one of those issues he can get an automatic email to fix it and leave the time of the human checker for more serious business.
    – Shiroko
    Jan 11, 2012 at 21:52
  • 3
    @wildplasser it this can be done automatically there is no point to do it manually.
    – log0
    Jan 11, 2012 at 21:58
  • I think that is actually a very good idea. Our code was automatically checked when I was at the university. Things like function of limited size are almost mandatory if you want your code to be a minimum readable. Now, I can instantly recognize code written by someone from my school. I eventually adapted some of the rules, but still do my best to write high quality code. tsunanet.net/~tsuna/cxxcodingstyle/cxxcodingstyle.pdf
    – log0
    Jan 11, 2012 at 22:08

8 Answers 8

2

You can use the unix tool indent to force some C coding style.

See

man 1 indent

when it is installed.

2
  • and run the results through a diff with the original... Jan 11, 2012 at 21:47
  • Indent cannot "force" the maximum number of lines of a function and any rules involving line breaks would destroy the diff output ...
    – log0
    Jan 12, 2012 at 8:39
1

QA-CPP might help here. It's been over a year since I used it but you can configure it to do that sort of thing.

You might want to give the guys at Programming Research a call and check if you can do that.

However the question remains why? If my lecturer started berating me for code style over functionality I would think he had a screw loose. As someone one who has gone through the University system you should be preparing them for working in the real world - heaven knows the stuff I see coming through now leave a lot to be desired.

2
  • 1
    In my experience, in the real world, you have to adhere to coding standards. Jul 12, 2012 at 16:07
  • 1
    He's not in the real world, he works in a university. Jul 12, 2012 at 18:45
1

There is EditorConfig, which looks like it's aiming to be a tool to enforce coding standards across various editors. Support and functionality are limited, but it's a pretty good idea. http://editorconfig.org/

1

One of these tools, with a minimum of customization, will do the job :

Programmable verification and analysis tool for C++: https://bitbucket.org/verateam/vera/wiki/Home
Google C++ norm checker: http://google-styleguide.googlecode.com/svn/trunk/cpplint/cpplint.py
EPITA University Norm Checker (student project): http://code.google.com/p/norme-checker/source/browse/

0

AFAIK visual studio premium and ultimate has such advanced option trough analyze and test tools. but those are not free :(

2
  • Yes, but unfortunately that's not useful for our case.
    – Shiroko
    Jan 11, 2012 at 21:53
  • /analyze performs static code analysis. This has nothing to do with style conventions.
    – pmr
    Jan 17, 2012 at 23:37
0

Doxygen produces XML output which would be very easy to process... Well, at least as easy as finding word boundaries in identifiers ever is.

0

You can use TIDY in debug mode, with a bit of shell/perl scripts.

Basically, the idea is to let tidy run and provide the output in another .txt file, which can then be parsed to provide only the error outputs and you can get the original lines of code and run a DIFF :-)

Might require a bit of coding and scripting to get right. We were using something similar, but for an entirely different project and objective.

Otherwise you can try and use these:

http://gcgreatcode.sourceforge.net/
http://universalindent.sourceforge.net/

It might help out somewhat with the problem, with a bit of coding to get to your specific automated checking.

0

Somewhat depending on the level of detail and precisions needed and the willingness to invest (either time to do it yourself or money to pay someone), you might want to a look clang: It is a C, C++, and Object-C compiler written C++ which exposes its internal data structures. I have used a plug-in to check code for various blunders, mostly smaller, nitpicking things, though. Since the compiler exposes an AST it was, for example, trivial to spit out warnings when C-style casts are found (it may get a bit noisy on C code).

Although I haven't used it, clang can apparently also emit an XML representation of its internal data structures. If you don't need to ask questions like "is class A a publicly accessible base class of B" (i.e. non-trivial questions requiring semantic inspection of the AST), this may be an easier road to a tool you described. Actually, in either case you can make your students implement the checks: if you have enough students and assign the same checks more than once to make sure you get back at least one good implementation you could gather a neat collection quite quickly. ... and if your students are anything like me (well, I don't really wish you fate as terrible as this) they would even enjoy the exercise.

One thing, however: don't mark any C++ programmer down for not using CamelCase! HereIsASimpleAndImpressiveExampleOfWhyUseOfCamelCaseIsReallyBad: you_have_a_much_easier_time_to_read_this! C++ programmers tend to do the Right Thing. Thus, silly abominations like CamelCase and Hungarian Notation are not used.

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