[cfe-dev] Are there plans for a libDriver?

Daniel Dunbar daniel at zuster.org
Mon Aug 4 17:18:32 PDT 2008


Seems like a good idea to me.

One useful option may be allowing the clang driver to provide some facility
for loading plugin modules, a la the --load option of opt, which could then
provide additional ASTConsumer options.


 - Daniel


----- Original Message ----
From: Nico Weber <nicolasweber at gmx.de>
To: cfe-dev at cs.uiuc.edu
Sent: Monday, August 4, 2008 1:07:35 PM
Subject: [cfe-dev] Are there plans for a libDriver?

Hi,

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  
translation unit.

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  
directories.

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 :-)

Nico
_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list