JackDaniels
JackDaniels

Reputation: 1071

Parsing C/C++ compilation errors printed by GCC

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?

Upvotes: 1

Views: 2227

Answers (1)

Laurent
Laurent

Reputation: 2999

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

Upvotes: 4

Related Questions