[clang] [clang][AST] Fix printing `TagDecl`s. (PR #69971)

via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 24 10:31:57 PDT 2023


isuckatcs wrote:

> I think this might be working around the issue rather than fixing it. It looks to me like the bug is in `Decl::printGroup`:
> 
> https://github.com/llvm/llvm-project/blob/f4023d4d5d6a6f35ef2315f1b1ce7dd35f24e901/clang/lib/AST/DeclPrinter.cpp#L223
> 
> We are printing the tag _definition_ here if the tag _declaration_ is part of the `DeclGroup`. I think that the `if(TD)` on the line above that one should instead be something like:
> 
> ```c++
>       if (TD && (TD->isThisDeclarationADefinition() || TD->isThisDeclarationADemotedDefinition()))
> ```
> 
> (That is: print the tag definition if the tag declaration in the decl group was written as a definition.)

Initially I was thinking about tweaking `Decl::printGroup` but I wasn't entirely sure about it. Now that I'm experimenting with your proposed solution, it seems there is an edge case, which we need to handle explicitly.

```c++
enum __attribute__((aligned(16))) T *p0;
```
```c++
EnumDecl ... T
`-AlignedAttr ... aligned
  `-ConstantExpr ... 'int'
    |-value: Int 16
    `-IntegerLiteral ... 'int' 16
```
This enum doesn't have a definition but has attributes, which are not printed unless `SubPolicy.IncludeTagDefinition` is set to `true`.

If we explicitly check for `TD->hasAttrs()`, we are back at the original issue.
```c++
struct A {
  int x;
};

struct Class {
  enum __attribute__((aligned(16))) T *(*next)(struct A *q);
};
```
```c++
struct A {
    int x;
};
struct Class {
    enum __attribute__((aligned(16))) T *(*next)(struct A {
        int x;
    } *);
};
```

At this point we either ignore the implicitly generated `TagDecl`, or we can tweak the attribute printing, so that they are printed even if we only print the declaration, though when I tried that 10 test cases failed, so I'm not sure we want to pursue that solution.

https://github.com/llvm/llvm-project/pull/69971


More information about the cfe-commits mailing list