[cfe-dev] Are there plans for a libDriver?
nicolasweber at gmx.de
Mon Aug 4 15:07:35 CDT 2008
so I've been playing around with using clang as a library the last few
days. Here's an early program I wrote: http://codepad.org/PPhjkyli
The program is intended to be a drop-in replacement for gcc, so that
it can be used like `CC=myprogram make`. The -I and -D options of gcc
are processed, the rest of the options are silently ignored. All the
program does is to list all global variables defined in the current
Even though the program can parse really complex C, it is only 236
lines thanks to clang. However, it still could be a lot shorter.
28 lines of the program define a diagnostic client. I use a simplified
(and less useful) version of TextDiagnosticPrinter. I'd be happy to
use TextDiagnosticPrinter directly, but it is defined in Driver, so I
can't use it.
35 lines define a function that can add directories to the include
path (addIncludePath()) or that can define built-in macros
(DefineBuiltinMacro()). Both functions are stolen from the Driver
directory (DefineBuiltinMacro() is actually defined twice in clang:
once in clang.cpp, once in Preprocessor.cpp, both times as a static
function. Both versions are identical -- so there's obviously a need
for this function in other programs ;-) ). Again, functions like this
should already be available.
Creating a Preprocessor object takes 14 lines in this program. I need
to create a DiagnosticsClient object, a Diagnostics object, a
LangOptions object, a TargetInfo, a SourceManager, FileManager, and a
HeaderSearch before I can create a Preprocessor. There should be a
(say) PreprocessorContext that contains all those classes and that
uses reasonable defaults for everything, so that creating a
Preprocessor with friends is just one or two lines.
Creating a Preprocessor object is in my eyes the "hello world" of
clang, so this should be simpler.
Another 12 lines are used for adding the default header search paths.
The PreprocessorContext (or whatever) should already add those include
Are there plans for a libDriver, which makes writing simple, "one-off"
clang clients easier? With this library, the amount of code of this
program could be reduced by a whole order of magnitude (only the
ASTConsumer, two short loops for the -D and -I options, and the call
to ParseAST would be left).
Thanks a lot for clang :-)
More information about the cfe-dev