[cfe-dev] C++ typedef merging

Chris Lattner clattner at apple.com
Sun Dec 2 10:42:33 PST 2007


On Dec 1, 2007, at 10:02 AM, Cédric Venet wrote:
> Small patch for the merging of typedef as defined in C++ std. So  
> this become
> valid in c++ mode:
>
> ===============
> typedef int I;
> typedef int J;
> typedef J L;
> typedef int I;
> typedef L I;
> typedef I I;
> ===============

Interesting, I didn't realize this was valid in C++.

> However this code should produce an error (and don't):
>
> ===============
> struct complex { /* ... */ };
> typedef int complex; // error: redefinition
> ===============
>

Okay, lets handle this as a later piece.  There is also the  
strangeness in C++ where you have:


struct stat { .. };
struct stat stat();

"stat" is a function, but "struct stat" is the type, even though the  
tag namespace is normally searched for normal identifiers.

About the patch:

+  // FIXME: Does CPlusPlus0x langoption inplie CPlusPlus?
+  if(PP.getLangOptions().CPlusPlus ||  
PP.getLangOptions().CPlusPlus0x) {
+    // see C++ 7.1.3 [dcl.typedef]

Yes, it does, you should just check CPlusPlus.  Also, please change  
the comment above the 'if' to be something like:

   // Redefinitions of typedef are ok in C++ as long as the subsequent
   // definition is the same type as the first definition: C++ 7.1.3  
[dcl.typedef]



+    if( TagDecl* oldTag = dyn_cast<TagDecl>(OldD) ) {
+      oldQT = Context.getTagDeclType( oldTag );

Please avoid the extraneous spaces, use something like:

+    if(TagDecl *oldTag = dyn_cast<TagDecl>(OldD)) {
+      oldQT = Context.getTagDeclType(oldTag);


Overall, the algorithm seems like a fine starting point.  I'd suggest  
restructuring the code to look like this though:

+    QualType oldQT;
+    if( TagDecl* oldTag = dyn_cast<TagDecl>(OldD) ) {
+      oldQT = Context.getTagDeclType( oldTag );
+    } else if( TypedefDecl* oldTypeDef =  
dyn_cast<TypedefDecl>(OldD) ) {
+      oldQT = oldTypeDef->getUnderlyingType();
+    }

      if (!oldQT.isNull() &&
          oldQT.getCanonicalType() == New- 
 >getUnderlyingType().getCanonicalType())
         return New;
      // otherwise fall through to the code that already emits  
diagnostics.

which seems a bit simpler.  Also, can you come up with an example  
which would hit the TagDecl case here yet?

-Chris



More information about the cfe-dev mailing list