Extending homework testing platform to include code analysis (C/C++)

I'm maintaining/developing a platform for homework testing. It's mostly automatic. What I need to add now is code analysis. I need to check the code for specific constructs.

For example:

Does the file main.cpp contain a class named user with a const method get_name() ?

Is there some tool out there that would allow me to do such stuff (ideal would be something that can be scripted). Linux only.


One possibility might be to run the code through GCC and use the GCC-XML extension to produce XML descriptions of the program's internal structure. You could then use your favourite XML library to parse the document, or apply an XSLT to it if all you need to do is display it as HTML or whatever.


您可能可以将一些使用GCC-XML框架的东西拼凑起来,而不会有太多困难。


How does this apply to C ? :)

Does the file main.cpp contain a class named user with a const method get_name()?

Create another file (test.cpp) with

void test(void) {
  const user x;
  x.get_name();
}

compile test.cpp and main.cpp together. If there's an error (exit code != 0) then NO!, the file main.cpp does not define a (public) class named user with the specific method.

Caveat: I know no C++ , so excuse any major (or minor) errors in the above.


Edit script added

#! /bin/sh

cat main.c test.c > 3710532.c
if gcc 3710532.c > /dev/null 2>&1
then echo OK
else echo BZZZT
fi
rm 3710532.c

I don't have a ready-to-use C++ compiler installed on this machine I'm on, so my test was with a C compiler and C files. My compiler didn't "work with" gcc -combine main.c test.c so I tweaked that part.

Running this script with a working combination of main.c and test.c outputs 'OK', otherwise it outputs 'BZZZT'.


For the tests I used this main.c

typedef int user;

int get_name(void) {
  return 0;
}

int main(void) {
  return 0;
}

and this test.c

void test(void) {
  const user x;
  get_name();
}

Sample run

$ ./3710532.sh
OK
链接地址: http://www.djcxy.com/p/47846.html

上一篇: Android应用程序的最大尺寸

下一篇: 扩展作业测试平台以包含代码分析(C / C ++)