[PATCH] D112374: [clang] Implement ElaboratedType sugaring for types written bare

Abramo Bagnara via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 15 11:01:46 PDT 2022


Abramo-Bagnara added a comment.

I have to doubly apologize:

1. my reference to refersInstantiatedDecl is completely wrong and I have been mislead by an old patch on my machine.
2. the problem despite being very real is independent by your changes

If you are still interested this is the repro followed by the explaination:

abramo at igor:/tmp$ cat a.c
template <typename>
void p() {

  typedef int x;
  sizeof(x);

}

int main() {

  p<int>();

}
abramo at igor:/tmp$ clang++-16 -cc1 -ast-dump -xc++ a.c
a.c:4:3: warning: expression result unused [-Wunused-value]

  sizeof(x);
  ^~~~~~~~~

a.c:4:3: warning: expression result unused [-Wunused-value]

  sizeof(x);
  ^~~~~~~~~

a.c:8:3: note: in instantiation of function template specialization 'p<int>' requested here

  p<int>();
  ^

TranslationUnitDecl 0x55df356fc8c8 <<invalid sloc>> <invalid sloc>

| -TypedefDecl 0x55df356fd130 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'                       |
| `-BuiltinType 0x55df356fce90 '__int128'                                                                          |
| -TypedefDecl 0x55df356fd1a0 <<invalid sloc>> <invalid sloc> implicit __uint128_t 'unsigned __int128'             |
| `-BuiltinType 0x55df356fceb0 'unsigned __int128'                                                                 |
| -TypedefDecl 0x55df356fd518 <<invalid sloc>> <invalid sloc> implicit __NSConstantString '__NSConstantString_tag' |
| `-RecordType 0x55df356fd290 '__NSConstantString_tag'                                                             |
| `-CXXRecord 0x55df356fd1f8 '__NSConstantString_tag'                                                              |
| -TypedefDecl 0x55df356fd5b0 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'               |
| `-PointerType 0x55df356fd570 'char *'                                                                            |
| `-BuiltinType 0x55df356fc970 'char'                                                                              |
| -TypedefDecl 0x55df35742aa8 <<invalid sloc>> <invalid sloc> implicit __builtin_va_list '__va_list_tag[1]'        |
| `-ConstantArrayType 0x55df35742a50 '__va_list_tag[1]' 1                                                          |
| `-RecordType 0x55df356fd6a0 '__va_list_tag'                                                                      |
| `-CXXRecord 0x55df356fd608 '__va_list_tag'                                                                       |
| -FunctionTemplateDecl 0x55df35742ca8 <a.c:1:1, line:5:1> line:2:6 p                                              |
|                                                                                                                  | -TemplateTypeParmDecl 0x55df35742b00 <line:1:11> col:19 typename depth 0 index 0              |
|                                                                                                                  | -FunctionDecl 0x55df35742c08 <line:2:1, line:5:1> line:2:6 p 'void ()'                        |
|                                                                                                                  | `-CompoundStmt 0x55df35742ed0 <col:10, line:5:1>                                              |
|                                                                                                                  |                                                                                               | -DeclStmt 0x55df35742e30 <line:3:3, col:16>                            |
|                                                                                                                  |                                                                                               | `-TypedefDecl 0x55df35742dd8 <col:3, col:15> col:15 referenced x 'int' |
|                                                                                                                  |                                                                                               | `-BuiltinType 0x55df356fc9d0 'int'                                     |
|                                                                                                                  | `-UnaryExprOrTypeTraitExpr 0x55df35742eb0 <line:4:3, col:11> 'unsigned long' sizeof 'x':'int' |
| `-FunctionDecl 0x55df357430e8 <line:2:1, line:5:1> line:2:6 used p 'void ()'                                     |
|                                                                                                                  | -TemplateArgument type 'int'                                                                  |
|                                                                                                                  | `-BuiltinType 0x55df356fc9d0 'int'                                                            |
| `-CompoundStmt 0x55df35743328 <col:10, line:5:1>                                                                 |
|                                                                                                                  | -DeclStmt 0x55df35743310 <line:3:3, col:16>                                                   |
|                                                                                                                  | `-TypedefDecl 0x55df357432b8 <col:3, col:15> col:15 referenced x 'int'                        |
|                                                                                                                  | `-BuiltinType 0x55df356fc9d0 'int'                                                            |
| `-UnaryExprOrTypeTraitExpr 0x55df35742eb0 <line:4:3, col:11> 'unsigned long' sizeof 'x':'int'                    |
|

`-FunctionDecl 0x55df35742f40 <line:7:1, line:9:1> line:7:5 main 'int ()'

  `-CompoundStmt 0x55df357432a0 <col:12, line:9:1>
    `-CallExpr 0x55df35743280 <line:8:3, col:10> 'void'
      `-ImplicitCastExpr 0x55df35743268 <col:3, col:8> 'void (*)()' <FunctionToPointerDecay>
        `-DeclRefExpr 0x55df357431e0 <col:3, col:8> 'void ()' lvalue Function 0x55df357430e8 'p' 'void ()' (FunctionTemplate 0x55df35742ca8 'p')

2 warnings generated.
abramo at igor:/tmp$

If you compare the UnaryExprOrTypeTraitExpr in the template and the one in the instantation you will see that they have wrongly the same pointer.

This is due to the fact that the two inner types are not distinct despite referring two distinct TypedefDecl.

The bug is in bool TemplateInstantiator::AlreadyTransformed(QualType T) where it is returned true also for types that when instantiated would refer distinct (but not dependent) declarations.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112374/new/

https://reviews.llvm.org/D112374



More information about the llvm-commits mailing list