[PATCH] D79473: [clang] Fix a crash on invalid auto.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 6 01:02:42 PDT 2020


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added a project: clang.

The crash is triggered on accessing a null InitExpr.

For group declaration, e.g. `auto c = a, &d = {a};`, what's happening:

1. each VarDecl is built separately during the parsing stage.
2. perform the semantic analysis (Sema::BuildDeclaratorGroup) to check

whether the type of the two VarDecl is the same, if not mark it as invalid.

in step 1, VarDecl c and d are built, both of them are valid (after D77395 <https://reviews.llvm.org/D77395>),
but d is without the InitExpr attached (under -fno-recovery-ast), crash
happens in step 2 when accessing the source range of d's InitExpr.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D79473

Files:
  clang/lib/Sema/SemaDecl.cpp
  clang/test/SemaCXX/auto-invalid-init-crash.cpp


Index: clang/test/SemaCXX/auto-invalid-init-crash.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/auto-invalid-init-crash.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -fno-recovery-ast -verify %s
+
+namespace std {
+template <typename>
+class initializer_list{};
+int a;
+auto c = a, &d = {a}; // expected-error {{'auto' deduced as 'int'}} \
+                         expected-error {{non-const lvalue reference to type}}
+} // namespace std
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -13208,13 +13208,15 @@
         DeducedDecl = D;
       } else if (!Context.hasSameType(DT->getDeducedType(), Deduced)) {
         auto *AT = dyn_cast<AutoType>(DT);
-        Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
-             diag::err_auto_different_deductions)
-          << (AT ? (unsigned)AT->getKeyword() : 3)
-          << Deduced << DeducedDecl->getDeclName()
-          << DT->getDeducedType() << D->getDeclName()
-          << DeducedDecl->getInit()->getSourceRange()
-          << D->getInit()->getSourceRange();
+        auto Dia = Diag(D->getTypeSourceInfo()->getTypeLoc().getBeginLoc(),
+                        diag::err_auto_different_deductions)
+                   << (AT ? (unsigned)AT->getKeyword() : 3) << Deduced
+                   << DeducedDecl->getDeclName() << DT->getDeducedType()
+                   << D->getDeclName();
+        if (DeducedDecl->hasInit())
+          Dia << DeducedDecl->getInit()->getSourceRange();
+        if (D->getInit())
+          Dia << D->getInit()->getSourceRange();
         D->setInvalidDecl();
         break;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D79473.262303.patch
Type: text/x-patch
Size: 1791 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200506/2682f1c3/attachment-0001.bin>


More information about the cfe-commits mailing list