[cfe-commits] r72608 - in /cfe/trunk: include/clang/AST/DeclBase.h include/clang/AST/PrettyPrinter.h lib/AST/DeclPrinter.cpp lib/AST/StmtPrinter.cpp lib/AST/Type.cpp

Douglas Gregor dgregor at apple.com
Fri May 29 22:51:03 PDT 2009


On May 29, 2009, at 9:20 PM, Eli Friedman wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=72608&view=rev
> Log:
> Expose an API to print a group of decls (like "int a,b;").
> Make StmtPrinter use DeclPrinter to print all declarations.  Merge
> declarations in the limited case of an unnamed TagDecl followed by one
> or more declarations using that TagDecl directly.  Change
> SuppressTypeSpecifiers to the more general SuppressSpecifiers, and
> use it to suppress stuff like "typedef" and "extern".  Replace
> OwnedTag with SuppressTag, since it's more convenient to print
> declarations from DeclPrinter at the moment.
> improvements to declaration printing.  Fix pretty-printing for K&R
> function definitions and __builtin_va_arg.
>
> We're now to the point where the pretty-printing output for non- 
> trivial
> programs can actually be piped back into clang.

Very cool.

> @@ -97,9 +168,38 @@
>   if (Indent)
>     Indentation += Policy.Indentation;
>
> +  llvm::SmallVector<Decl*, 2> Decls;
>   for (DeclContext::decl_iterator D = DC->decls_begin(Context),
>          DEnd = DC->decls_end(Context);
>        D != DEnd; ++D) {
> +    // The next bits of code handles stuff like "struct {int x;}  
> a,b"; we're
> +    // forced to merge the declarations because there's no other  
> way to
> +    // refer to the struct in question.  This limited merging is  
> safe without
> +    // a bunch of other checks because it only merges declarations  
> directly
> +    // referring to the tag, not typedefs.
> +    //
> +    // Check whether the current declaration should be grouped with  
> a previous
> +    // unnamed struct.
> +    QualType CurDeclType = getDeclType(*D);
> +    if (!Decls.empty() && !CurDeclType.isNull()) {
> +      QualType BaseType = GetBaseType(CurDeclType);
> +      if (!BaseType.isNull() && isa<TagType>(BaseType) &&
> +          cast<TagType>(BaseType)->getDecl() == Decls[0]) {
> +        Decls.push_back(*D);
> +        continue;
> +      }
> +    }
> +
> +    // If we have a merged group waiting to be handled, handle it  
> now.
> +    if (!Decls.empty())
> +      ProcessDeclGroup(Decls);
> +
> +    // If the current declaration is an unnamed tag type, save it
> +    // so we can merge it with the subsequent declaration(s) using  
> it.
> +    if (isa<TagDecl>(*D) && !cast<TagDecl>(*D)->getIdentifier()) {
> +      Decls.push_back(*D);
> +      continue;
> +    }

This makes me think that what we really want is for DeclContexts to  
store a list of DeclGroups, so that we can give a nice DeclGroup view  
for VisitDeclContext to walk over. I don't know how to actually  
implement this, but it seems like the right thing to do.

	- Doug



More information about the cfe-commits mailing list