1

I am writing a simple C editor. I have to validate a code to highlight any misspellings, missing semicolons, uses of non-existing functions/variables/methods, assigments in if condition and so on and so forth.

Parsing and validating C is a very complex problem so I have decided to use CDT. However, I have no idea how to do so. I have only found informations about method org.eclipse.cdt.core.dom.ast.gnu.c.GCCLanguage.getASTTranslationUnit(...) but this is not helping very much, because it allows to find only basic syntax errors. (Am I right?)

I need a function which gets a C code or an object of the class IASTTranslationUnit. It has to return list of all problems (errors and warnings). How can I do that, using the CDT API?

Community
  • 1
  • 1
Piotr Siupa
  • 3,929
  • 2
  • 29
  • 65
  • I don't think you will get AST in case of parsing error, so this approach won't work – qrdl Jul 29 '16 at 11:27
  • validate C code, is to 1) run the compiler with all warnings enabled, and when all warnings fixed, then run a tool, such as valgrind over the source code. – user3629249 Jul 29 '16 at 19:03
  • @user3629249 I'll consider this as a last resort. However, I am looking for portable solution. It is possible that the code will be running on server on other weird machine. And even if not, I would not want to force every user to install and configure a compiler. – Piotr Siupa Jul 29 '16 at 19:12
  • @NO_NAME, without a compiler, any user is not going to be generating any C code into executables. So perhaps I do not understand what you are trying to accomplish. – user3629249 Jul 30 '16 at 11:25
  • @user3629249 User won't be generating any executables. A code will be sent to server and stored there until the target machine will download it. They won't be long programs but kind of compilable scripts. – Piotr Siupa Jul 30 '16 at 12:26

1 Answers1

1

Many categories of errors can be checked by resolving names found in the AST (calling IASTName.resolveBinding()), and seeing whether the resulting binding is an IProblemBinding.

See how CDT's ProblemBindingChecker does this to show many errors in the editor.

Note that this wonn't catch all errors; you can look at CDT's other checkers for ideas about how to catch other categories of errors (some of the checkers also produce warnings).

However, even all of CDT's checkers put together won't diagnose of all the errors and warnings that a compiler would. If that is a requirement, I would suggest using an actual compiler's internals, such as libclang.

HighCommander4
  • 50,428
  • 24
  • 122
  • 194