If you want to do arbitrary code analysis, you need arbitrary parsing/matching/etc.
GCC-XML will give you declaration information, but not the content of the methods.
Our DMS Software Reengineering Toolkit will provide the same abstract information as GCC-XML, but
additionally include complete detail for the content of definitions (e.g., method body
information), supported by its C++ Front End. THis will let you access delcarations and content to check your student programs.
DMS provides general purpose parsing to ASTs, symbol tables and source-pattern matching. The C++ front end provides full C++ parsing, building C++ ASTs and corresponding symbol information. What you do after that for recognition is up to you, but your example seems to be about looking for a specific pattern.
Half of your example would be handled by few DMS source patterns for C++:
pattern is_correct_student_class(m:members):class =
" class user { \m } ".
pattern is_correct_student_method_present(p:parameters,s:statements):method =
" const get_name(\p) { \s } "
(forgive my C++ syntax, I don't write a lot of it) which will match any AST,
respectively, corresponding the named user class and the desired const method.
The quotes are meta-quotes, with the stuff inside being C++ syntax with escapes
\p, \m and \s representing the metavariables p, m, and s, which must syntactically be
a parameter list, a method list and statement lists respectively in order to match the pattern. The definitions of the parameter list, etc. are automatically derived
from the C++ grammar portion of the C++ Front End.
The other half is implemented by a bit of DMS PARLANSE code executed after
invoking the C++ parser and name/type resolver:
(define has_student_code (lamdba (function boolean [tree AST]))
(AST:IsInTree tree
(lambda (function boolean [tree1 AST]
(&& (Registry:MatchPattern tree1 "is_correct_student_class")
(AST:IsInList (AST:GetNthGrammarChild tree1 4) ; the member list
(lambda (function boolean [tree2 AST])
(Registry:MatchPattern tree2 ; a member
"is_correct_student_method_present")
)lambda
)lambda
)
)define
with some liberties taken to simplify the presentation.
This is a pretty simple check; you can access the symbol table from the PARLANSE code to do more sophisticated checking if that makes sense.
While DMS doesn't run directly under Linux, it does seem to run under Wine.