[all-commits] [llvm/llvm-project] d200db: [clang] template / auto deduction deduces common s...

Matheus Izvekov via All-commits all-commits at lists.llvm.org
Thu Sep 8 10:18:43 PDT 2022


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: d200db38637884fd0b421802c6094b2a03ceb29e
      https://github.com/llvm/llvm-project/commit/d200db38637884fd0b421802c6094b2a03ceb29e
  Author: Matheus Izvekov <mizvekov at gmail.com>
  Date:   2022-09-08 (Thu, 08 Sep 2022)

  Changed paths:
    M clang-tools-extra/clangd/unittests/ASTTests.cpp
    M clang-tools-extra/clangd/unittests/HoverTests.cpp
    M clang/include/clang/AST/ASTContext.h
    M clang/include/clang/AST/Type.h
    M clang/include/clang/Sema/Sema.h
    M clang/lib/AST/ASTContext.cpp
    M clang/lib/Sema/SemaDecl.cpp
    M clang/lib/Sema/SemaDeclCXX.cpp
    M clang/lib/Sema/SemaExprCXX.cpp
    M clang/lib/Sema/SemaOverload.cpp
    M clang/lib/Sema/SemaStmt.cpp
    M clang/lib/Sema/SemaTemplate.cpp
    M clang/lib/Sema/SemaTemplateDeduction.cpp
    M clang/test/SemaCXX/cxx0x-initializer-stdinitializerlist.cpp
    M clang/test/SemaCXX/deduced-return-void.cpp
    M clang/test/SemaCXX/sugared-auto.cpp
    M clang/test/SemaTemplate/deduction.cpp

  Log Message:
  -----------
  [clang] template / auto deduction deduces common sugar

After upgrading the type deduction machinery to retain type sugar in
D110216, we were left with a situation where there is no general
well behaved mechanism in Clang to unify the type sugar of multiple
deductions of the same type parameter.

So we ended up making an arbitrary choice: keep the sugar of the first
deduction, ignore subsequent ones.

In general, we already had this problem, but in a smaller scale.
The result of the conditional operator and many other binary ops
could benefit from such a mechanism.

This patch implements such a type sugar unification mechanism.

The basics:

This patch introduces a `getCommonSugaredType(QualType X, QualType Y)`
method to ASTContext which implements this functionality, and uses it
for unifying the results of type deduction and return type deduction.
This will return the most derived type sugar which occurs in both X and
Y.

Example:

Suppose we have these types:
```
using Animal = int;
using Cat = Animal;
using Dog = Animal;

using Tom = Cat;
using Spike = Dog;
using Tyke = Dog;
```
For `X = Tom, Y = Spike`, this will result in `Animal`.
For `X = Spike, Y = Tyke`, this will result in `Dog`.

How it works:

We take two types, X and Y, which we wish to unify as input.
These types must have the same (qualified or unqualified) canonical
type.

We dive down fast through top-level type sugar nodes, to the
underlying canonical node. If these canonical nodes differ, we
build a common one out of the two, unifying any sugar they had.
Note that this might involve a recursive call to unify any children
of those. We then return that canonical node, handling any qualifiers.

If they don't differ, we walk up the list of sugar type nodes we dived
through, finding the last identical pair, and returning that as the
result, again handling qualifiers.

Note that this patch will not unify sugar nodes if they are not
identical already. We will simply strip off top-level sugar nodes that
differ between X and Y. This sugar node unification will instead be
implemented in a subsequent patch.

This patch also implements a few users of this mechanism:
* Template argument deduction.
* Auto deduction, for functions returning auto / decltype(auto), with
  special handling for initializer_list as well.

Further users will be implemented in a subsequent patch.

Signed-off-by: Matheus Izvekov <mizvekov at gmail.com>

Differential Revision: https://reviews.llvm.org/D111283


  Commit: d42122cd5db021e6b14a90a98ad1dd09412efb4c
      https://github.com/llvm/llvm-project/commit/d42122cd5db021e6b14a90a98ad1dd09412efb4c
  Author: Matheus Izvekov <mizvekov at gmail.com>
  Date:   2022-09-08 (Thu, 08 Sep 2022)

  Changed paths:
    M clang-tools-extra/clangd/unittests/tweaks/ExtractVariableTests.cpp
    M clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/narrowing-conversions-ignoreconversionfromtypes-option.cpp
    M clang/lib/Sema/SemaExpr.cpp
    M clang/lib/Sema/SemaExprCXX.cpp
    M clang/test/AST/ast-dump-fpfeatures.cpp
    M clang/test/CodeGen/compound-assign-overflow.c
    M clang/test/Sema/matrix-type-operators.c
    M clang/test/Sema/nullability.c
    A clang/test/Sema/sugar-common-types.c
    M clang/test/SemaCXX/matrix-type-operators.cpp
    A clang/test/SemaCXX/sugar-common-types.cpp
    M clang/test/SemaCXX/sugared-auto.cpp
    M clang/test/SemaObjC/format-strings-objc.m
    M compiler-rt/test/ubsan/TestCases/Integer/add-overflow.cpp
    M compiler-rt/test/ubsan/TestCases/Integer/no-recover.cpp
    M compiler-rt/test/ubsan/TestCases/Integer/sub-overflow.cpp
    M compiler-rt/test/ubsan/TestCases/Integer/uadd-overflow.cpp
    M compiler-rt/test/ubsan/TestCases/Integer/umul-overflow.cpp
    M compiler-rt/test/ubsan/TestCases/Integer/usub-overflow.cpp
    M lldb/test/API/commands/expression/rdar42038760/main.c
    M lldb/test/API/commands/expression/rdar44436068/main.c

  Log Message:
  -----------
  [clang] use getCommonSugar in an assortment of places

For this patch, a simple search was performed for patterns where there are
two types (usually an LHS and an RHS) which are structurally the same, and there
is some result type which is resolved as either one of them (typically LHS for
consistency).

We change those cases to resolve as the common sugared type between those two,
utilizing the new infrastructure created for this purpose.

Depends on D111283

Signed-off-by: Matheus Izvekov <mizvekov at gmail.com>

Differential Revision: https://reviews.llvm.org/D111509


  Commit: 16e5d6d7f98f1119aab3d10ec4f9e59b5aacd359
      https://github.com/llvm/llvm-project/commit/16e5d6d7f98f1119aab3d10ec4f9e59b5aacd359
  Author: Matheus Izvekov <mizvekov at gmail.com>
  Date:   2022-09-08 (Thu, 08 Sep 2022)

  Changed paths:
    M clang/include/clang/AST/ASTContext.h
    M clang/lib/AST/ASTContext.cpp
    M clang/lib/Sema/SemaTemplateDeduction.cpp
    M clang/test/SemaCXX/sugar-common-types.cpp

  Log Message:
  -----------
  [clang] extend getCommonSugaredType to merge sugar nodes

This continues D111283 by extending the getCommonSugaredType
implementation to also merge non-canonical type nodes.

We merge these nodes by going up starting from the canonical
node, calculating their merged properties on the way.

If we reach a pair that is too different, or which we could not
otherwise unify, we bail out and don't try to keep going on to
the next pair, in effect striping out all the remaining top-level
sugar nodes. This avoids mismatching 'companion' nodes, such as
ElaboratedType, so that they don't end up elaborating some other
unrelated thing.

Depends on D111509

Signed-off-by: Matheus Izvekov <mizvekov at gmail.com>

Differential Revision: https://reviews.llvm.org/D130308


Compare: https://github.com/llvm/llvm-project/compare/b34006dd0a51...16e5d6d7f98f


More information about the All-commits mailing list