r232568 - Fix a crash when the size of an 'auto' is needed and its initalizer

Kaelyn Takata rikka at google.com
Tue Mar 17 16:50:13 PDT 2015


Author: rikka
Date: Tue Mar 17 18:50:12 2015
New Revision: 232568

URL: http://llvm.org/viewvc/llvm-project?rev=232568&view=rev
Log:
Fix a crash when the size of an 'auto' is needed and its initalizer
contained a typo correction (the auto decl was being marked as dependent
unnecessarily, which triggered an assertion in cases where the size of
the type is needed).

Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/SemaCXX/cxx11-crashes.cpp

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=232568&r1=232567&r2=232568&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Mar 17 18:50:12 2015
@@ -8688,6 +8688,20 @@ void Sema::AddInitializerToDecl(Decl *Re
 
   // C++11 [decl.spec.auto]p6. Deduce the type which 'auto' stands in for.
   if (TypeMayContainAuto && VDecl->getType()->isUndeducedType()) {
+    // Attempt typo correction early so that the type of the init expression can
+    // be deduced based on the chosen correction:if the original init contains a
+    // TypoExpr.
+    ExprResult Res = CorrectDelayedTyposInExpr(Init);
+    if (!Res.isUsable()) {
+      RealDecl->setInvalidDecl();
+      return;
+    }
+    if (Res.get() != Init) {
+      Init = Res.get();
+      if (CXXDirectInit)
+        CXXDirectInit = dyn_cast<ParenListExpr>(Init);
+    }
+
     Expr *DeduceInit = Init;
     // Initializer could be a C++ direct-initializer. Deduction only works if it
     // contains exactly one expression.

Modified: cfe/trunk/test/SemaCXX/cxx11-crashes.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx11-crashes.cpp?rev=232568&r1=232567&r2=232568&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx11-crashes.cpp (original)
+++ cfe/trunk/test/SemaCXX/cxx11-crashes.cpp Tue Mar 17 18:50:12 2015
@@ -74,3 +74,20 @@ namespace b6981007 {
     }
   }
 }
+
+namespace incorrect_auto_type_deduction_for_typo {
+struct S {
+  template <typename T> S(T t) {
+    (void)sizeof(t);
+    (void)new auto(t);
+  }
+};
+
+void Foo(S);
+
+void test(int some_number) {  // expected-note {{'some_number' declared here}}
+  auto x = sum_number;  // expected-error {{use of undeclared identifier 'sum_number'; did you mean 'some_number'?}}
+  auto lambda = [x] {};
+  Foo(lambda);
+}
+}





More information about the cfe-commits mailing list