I think every thing is in the title^^ in fact, I'm developping a tool using libtooling, but I'd like to suppress every errors (the tool is aimed to be used only on correct source, so error output polutes stderr...).
Asked
Active
Viewed 905 times
1
-
I didn't find answer on google, so I ask the question here so that even if the answer is obvious, it will be indexed... – hl037_ Apr 28 '14 at 14:22
-
2I couldn't even parse the text of your question, much less your title. Care to explain better? If your tool is aimed to be used only on correct source, why would it emit error messages? – Massa Apr 28 '14 at 14:31
-
because the source is correct in its process build, however, in the tool, errors can occurs. Fortunately, I don't care about these errors because the purpose of this tool is limited to detect some symbols etc. Then the question is not "why" but "how turn off any error outputs" – hl037_ Apr 28 '14 at 14:39
2 Answers
4
The title is libclang/libtooling, so here is the answer for libclang. Create your CXIndex
like this:
bool excludeDeclarationsFromPCH = false;
bool displayDiagnostics = false;
CXIndex index = clang_createIndex((int)excludeDeclarationsFromPCH, (int)displayDiagnostics);
See the documentation.

cubuspl42
- 7,833
- 4
- 41
- 65
-
The linked documentation does not mention what does displaydiagnostics do... after digging a little bit more, I found the `if (CXXIdx->getDisplayDiagnostics()) printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get());` :) – Massa Apr 28 '14 at 15:36
-
@Massa You're right, actually the argument name was probably considered self-explaining by Clang devs. – cubuspl42 Apr 28 '14 at 15:56
0
Do you want to redirect your std::cerr
output? Or the stderr
for every child process? If it's the latter case, you can do something like:
#include <unistd.h>
int fd = dup(2);
int n = open("/dev/null", O_WRONLY);
dup2(n, 2);
close(n);
//... do your thing ...
dup2(fd, 2); // put the stderr back where it belongs :D
close(fd);

Massa
- 8,647
- 2
- 25
- 26
-
In fact, I don't want to redirect output (this lead to a bad design in my case because I still have to use stderr, and I don't want to switch on/off each time) Actually, I'm looking for a way in the libclang library to handle the errors or to disable any diagnostic messages. – hl037_ Apr 28 '14 at 14:52
-
@hl037_, Taking a look at libclang's source code, I found that it prints its diagnostics directly to `stderr` with a `fprintf` (look at `./clang/tools/libclang/CIndex.cpp:6707` if you want); this means that if you want to turn off all diagnostics (including errors), you'll have to redirect stderr; but you can use the saved `fd` and write your own diagnostics to it... – Massa Apr 28 '14 at 15:20