扩展作业测试平台以包括代码分析 (C/C++)
我正在维护/开发一个用于作业测试的平台。它大部分是自动的。我现在需要补充的是代码分析。我需要检查特定构造的代码。
例如:
文件
main.cpp
是否包含 带有 const 方法的名为user
的类get_name()
?
有没有一些工具可以让我做这样的事情(理想的是可以编写脚本的东西)。仅限 Linux。
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 nameduser
with a const methodget_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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一种可能是通过 GCC 运行代码并使用 GCC-XML 扩展,用于生成程序内部结构的 XML 描述。然后,您可以使用您最喜欢的 XML 库来解析文档,或者如果您需要做的只是将其显示为 HTML 或其他内容,则可以对其应用 XSLT。
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 框架 的东西组合在一起。
You could probably hack together something that used the GCC-XML framework without too much difficulty.
这如何应用于
C
? :)创建另一个文件(test.cpp),将
test.cpp 和 main.cpp 一起编译。如果出现错误(退出代码!= 0),则否!,文件 main.cpp 未使用特定方法定义名为 user 的(公共)类。
警告:我不懂 C++,所以请原谅上面的任何主要(或次要)错误。
添加了编辑脚本
我在这台机器上没有安装即用型
C++
编译器,所以我的测试是使用C< /code> 编译器和
C
文件。我的编译器无法“使用”gcc -combine main.c test.c
,因此我调整了该部分。使用
main.c
和test.c
的工作组合运行此脚本会输出“OK”,否则会输出“BZZZT”。对于测试,我使用了这个
main.c
和这个
test.c
示例运行
How does this apply to
C
? :)Create another file (test.cpp) with
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
I don't have a ready-to-use
C++
compiler installed on this machine I'm on, so my test was with aC
compiler andC
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
andtest.c
outputs 'OK', otherwise it outputs 'BZZZT'.For the tests I used this
main.c
and this
test.c
Sample run
我从 Mozilla 发现了 deHydra 工具。它似乎主要是为了内部目的而编写的,但它可能正是我正在寻找的。
DeHydra
编辑: 太棒了。它缺少一些小功能,例如确定 const 方法,但其他方面都很棒。
I have discovered dehydra tool from Mozilla. It seems to be mostly written for internal purposes, but it may just be exactly what I was looking for.
https://developer.mozilla.org/En/Dehydra/Using_Dehydra
Edit: Dehydra is great. It is missing some minor features like determining const methods, but otherwise great.
如果你想做任意代码分析,你需要任意解析/匹配等。
GCC-XML 将为您提供声明信息,但不提供方法的内容。
我们的 DMS 软件重新工程工具包将提供与 GCC-XML 相同的抽象信息,但
另外包括定义内容的完整细节(例如,方法主体
信息),由其 C++ 前端 支持。这将允许您访问声明和内容来检查您的学生计划。
DMS 提供对 AST、符号表和源模式匹配的通用解析。 C++ 前端提供完整的 C++ 解析、构建 C++ AST 和相应的符号信息。之后您要做什么来进行识别取决于您,但您的示例似乎是在寻找特定的模式。
你的示例的一半将由几个 C++ 的 DMS 源模式处理:(
请原谅我的 C++ 语法,我不会写很多)它将匹配任何 AST,
分别对应命名的用户类和所需的 const 方法。
引号是元引号,里面的内容是带转义的 C++ 语法
\p、\m 和 \s 代表元变量 p、m 和 s,其语法必须为
分别是参数列表、方法列表和语句列表以匹配模式。自动导出参数列表等的定义
来自 C++ 前端的 C++ 语法部分。
另一半是通过之后执行的一些 DMS PARLANSE 代码来实现的
调用 C++ 解析器和名称/类型解析器:
采取一些自由措施来简化表示。
这是一个非常简单的检查;您可以从 PARLANSE 代码访问符号表,以执行更复杂的检查是否有意义。
虽然 DMS 不能直接在 Linux 下运行,但它似乎可以在 Wine 下运行。
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++:
(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:
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.