[cfe-dev] add namespaces to C (in my local clang) OR "tag" a couple of functions

Douglas Gregor dgregor at apple.com
Mon Oct 24 08:08:07 PDT 2011

On Oct 23, 2011, at 11:14 PM, Jonas Gefele wrote:

> Hi,
>> I can understand now why you don't want to compile as C++.  But I
>> suggest you do it anyway from time to time, "When No One Is Looking".
>> Adding C-Style Casts won't hurt your C.  Several years down the road
>> you could surprise everyone that your C code suddenly builds as C++.
> Unfortunately, I maintain the compiler but not the sources the compiler
> should compile :)
>> But a shorter-term possibility is that I understand CLang can be used
>> to edit C source code in various ways
> ...
>> Suppose you had the following C+Namespaces source:
> ...
>> After being preprocessed somehow it would look like:
> Sure, but I would already have clang to be able to parse "C+Namespaces"
> which leads me back to my original problem:
> Is it possible to change some "if(LangOpts.CPlusPlus)" into
> "if(LangOpts.CPlusPlus || LangOpts.CWithNameSpace)" in the right places to
> enable a "C+namespaces mode" or would this be a more complex problem?

It's a more complex problem than that. With namespaces come a lot of baggage in the area of name lookup. You start to need to handle the scoping operator (::, which isn't even a token in C), and the simple, lexical name lookup used in C has to be replaced by much more complicated lookups that know how to look across redefinitions of namespaces. Soon you realize that you want something like Argument Dependent Lookup because you're having to write qualified names everywhere.

Naturally, Clang supports all of these features for C++, but the code that implements them really assumes we're in C++, and there are various fast-paths for C (such as in name lookup) that are C-specific and much-simplified, which you would have to disable.

Something that merely tags a set of functions would be much, much easier. Check out, e.g., the #pragma clang arc_cf_code_audited begin/end stuff that was recently added to Clang trunk for some ideas on how to tag a set of declarations.

	- Doug

More information about the cfe-dev mailing list