[clang] [Sema] Fast-path simple plain auto deduction in DeduceAutoType (PR #188196)

via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 25 08:08:56 PDT 2026


================
@@ -5262,7 +5260,41 @@ Sema::DeduceAutoType(TypeLoc Type, Expr *Init, QualType &Result,
 
     DeducedType = getDecltypeForExpr(Init);
     assert(!DeducedType.isNull());
-  } else {
+  } else if (CanTryFastPath) {
+    // Fast-path a subset of `auto` deduction for non-init-list cases in
+    // non-ObjC and non-OpenCL language modes. For these cases, the deduced
+    // type can be computed directly from the initializer type by removing
+    // references, applying array/function decay, and dropping top-level
+    // cv-qualifiers. For single-level `auto*` declarators, the deduced type
+    // is the pointee type of the processed initializer type.
+
+    QualType TypeTy = Type.getType();
+    bool IsPlainOrTopLevelCvAuto = Context.hasSameType(
+        TypeTy.getLocalUnqualifiedType(), Context.getAutoDeductType());
+    bool IsSimpleAutoStar =
+        TypeTy->isPointerType() &&
+        Context.hasSameType(TypeTy->getPointeeType().getLocalUnqualifiedType(),
+                            Context.getAutoDeductType());
+
+    QualType ProcessedInitTy = Init->getType().getNonReferenceType();
+    if (ProcessedInitTy->isArrayType() || ProcessedInitTy->isFunctionType())
+      ProcessedInitTy = Context.getDecayedType(ProcessedInitTy);
+
+    ProcessedInitTy = ProcessedInitTy.getUnqualifiedType();
+    bool CanUsePointerFastPath =
+        IsSimpleAutoStar && ProcessedInitTy->isPointerType();
+
+    if (IsPlainOrTopLevelCvAuto) {
+      DeducedType = ProcessedInitTy;
+      assert(!DeducedType.isNull());
+    } else if (CanUsePointerFastPath) {
+      DeducedType = ProcessedInitTy->getPointeeType();
+      assert(!DeducedType.isNull());
+    }
----------------
Sirraide wrote:

No need for the assertions here; if `ProcessedInitTy` was somehow null, we would have already crashed before getting here because you dereference it above

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


More information about the cfe-commits mailing list