[PATCH] D76592: [Parser] Fix the assertion crash in ActOnStartOfSwitch stmt.

Haojian Wu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 23 01:06:15 PDT 2020


hokein created this revision.
hokein added a reviewer: sammccall.
Herald added subscribers: usaxena95, kadircet, ilya-biryukov.
Herald added a project: clang.

After we parse the switch condition, we don't do the type check for
type-dependent expr (e.g. TypoExpr) (in Sema::CheckSwitchCondition), then the
TypoExpr is corrected to an invalid-type expr (in Sema::MakeFullExpr) and passed
to the ActOnStartOfSwitchStmt, which triggers the assertion.

Fix https://github.com/clangd/clangd/issues/311


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D76592

Files:
  clang/lib/Sema/SemaStmt.cpp
  clang/test/Parser/switch-typo-correction.cpp


Index: clang/test/Parser/switch-typo-correction.cpp
===================================================================
--- /dev/null
+++ clang/test/Parser/switch-typo-correction.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace c { double xxx; }
+namespace d { float xxx; }
+namespace z { namespace xxx {} }
+
+void crash() {
+  switch (xxx) {} // expected-error{{use of undeclared identifier 'xxx'}}
+}
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -706,6 +706,12 @@
       llvm_unreachable("conversion functions are permitted");
     }
   } SwitchDiagnoser(Cond);
+  // The TypoExpr might be corrected to a non-intergral-or-enum type in the
+  // later stage without the proper type check, which is invalid for switch
+  // condition, so we fail the check here (this would avoid emitting incorrect
+  // fixit).
+  if (llvm::isa<TypoExpr>(Cond))
+    return ExprError();
 
   ExprResult CondResult =
       PerformContextualImplicitConversion(SwitchLoc, Cond, SwitchDiagnoser);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D76592.251957.patch
Type: text/x-patch
Size: 1133 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200323/9d021c1c/attachment-0001.bin>


More information about the cfe-commits mailing list