1

Given c/cpp/.h files, I want to compile them and find the compilation errors (and warnings).

From the compilation error, I want to produce a structure or table like,

{
  level: (ERROR/WARNING)
  fileName: 'hello.cc'
  lineNumber: 24
  char: 5 (if available, or can be skipped)
  message: 'The description of the error'
}
  1. In Java, there is a Java Compiler API. Is there a similar API for C++?
  2. If the only option is to parse the output of gcc or g++, how to parse the error message, so that I can generate an array of the structure as defined above?

I looked at some error messages. For many error messages from gcc, I am looked at, all the error lines start with 'filename: In function main' or 'filename:lineNumber:column' And all the multi line error messages are indented by some whitespace

Is it safe to assume, lines starting with non-whitespace characters are the beginning of error lines?

Note: I had never written c++ programs in the last 10 years, I am building C/C++ support for Codiva.io online compiler (that only supports java at present). I feel, instead of giving a console output, parsing and showing at each line would be a good user experience and saves a ton of time for students. Will parsing error messages from clang compiler be easier?

7
  • You can use liblang for custom processing of parsed c++ code. Commented Nov 23, 2016 at 7:42
  • There is no standard for printing of compiler errors. They may differ for each compiler vendor. Commented Nov 23, 2016 at 7:43
  • The question "how to parse?" is probably too broad for SO. Commented Nov 23, 2016 at 7:53
  • With templates, the simple structure wouldn't suffice. You typically have an instantiation stack, which has a variable length. Commented Nov 23, 2016 at 8:10
  • @MSalters I didn't understand your comment. Commented Nov 24, 2016 at 7:52

1 Answer 1

4

With the upcoming gcc 9.0, there is an option to output JSON: -fdiagnostics-format=json

https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Message-Formatting-Options.html#index-fdiagnostics-format

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.