add namespaces to C (in my local clang) OR "tag" a couple of functions

Hi.

(In my local clang copy) I would like to add namespace support to C. I
want to use it to "tag" a couple of functions like this:

namespace tag1 {

(In my local clang copy) I need the ability to "tag" a bunch of C/C++
functions/globals with some "name". Something like:

Would it work to just compile your C sources with a C++ compiler?

/* Foo.c
*/

#ifdef __cplusplus
extern "C" {
#endif

#include "Foo.h" /* These function names will not be mangled */

#ifdef __cplusplus
}
#endif

#ifdef __cplusplus
namespace tag1{
#endif

void foo( int bar )
{
  getDownAndBoogie();

  return;
}

#ifdef __cplusplus
} /* namespace tag1 */
#endif

Some creative macro definitions could make that look less ugly.

Hi,

thank you for your reply. Personally, I would prefer switching to C++ too.
But it is unfortunately not possible :frowning:

I have been a little imprecise when I said "compability
with real world C does not matter in my case". I meant this on a
naming/linkage level (as we can handle this in our linker).

We have a big C code base and need source level compability (to the
greatest possible extent). Introducing two new keywords "namespace" and
"using" would not hurt much. But needing to fix tons of "void-pointer
assignments" like:

   char* x = malloc(100);
   char* y = malloc(100);
   char* z = malloc(100);

would hurt a lot.

Cheers,
Jonas

Hi,

thank you for your reply. Personally, I would prefer switching to C++ too.
But it is unfortunately not possible :frowning:

I have been a little imprecise when I said “compability
with real world C does not matter in my case”. I meant this on a
naming/linkage level (as we can handle this in our linker).

Would this not be a worthy extension to be supported? I know overloaded functions can be enabled in GNU mode. It would make this a bit more portable, if not very useful for other developers…

Ruben

Hi Ruben,

there has been a discussion one year ago about adding a namespace
extension to ObjC that I did not want to reiterate:
http://lists.cs.uiuc.edu/pipermail/cfe-dev/2010-November/012026.html

I only skimmed through it, but I think it boils down to:
"If we do it, then we want a more or less generic approach for C, ObjC,
C++, ObjC++. If someone writes a proposal, we can discuss it."
No one wrote a proposal.

Cheers,
Jonas

I don't fully understand what the problems really were, but many of
the C++ compilers that were released before the C++ 1998 ISO Standard
was ratified had some terrible problems with interactions between
namespaces and templates.

I'm sure someone here would remember what that was. At the time I was
such a C++ neophyte that I didn't ever write my own templates.

The C++ Scope Resolution Operator "::" is way overloaded. With
nothing on the left it means global scope. With a class name on the
left then it is class scope. There can be any number of namespaces
separated by "::" operators.

Making namespaces work for all those languages in one go would
probably be a huge problem, considering how many years passed before
we had reasonably standards-compliant C++ compilers alone.

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

But a shorter-term possibility is that I understand CLang can be used
to edit C source code in various ways, for example to add
instrumentation for profilers.

Suppose you had the following C+Namespaces source:

namespace Foo {

int getFive( void )
{
   return 5;
}

}

After being preprocessed somehow it would look like:

/* Nothing here - the namespace declaration is gone */

int Foo_getFive( void )
{
   return 5;
}

/* Nothing here either */

You could then compile that is if it was regular C and not C++.

If your namespace declarations were replaced by blank lines, the line
numbers would still work right for source debuggers.

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

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?

Cheers,
Jonas

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