[clang] 386f95e - [Parser] Fix the assertion crash in ActOnStartOfSwitch stmt.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 24 07:17:16 PDT 2020


Author: Haojian Wu
Date: 2020-03-24T15:17:04+01:00
New Revision: 386f95e168b09603595864a5956624792ccb59c4

URL: https://github.com/llvm/llvm-project/commit/386f95e168b09603595864a5956624792ccb59c4
DIFF: https://github.com/llvm/llvm-project/commit/386f95e168b09603595864a5956624792ccb59c4.diff

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

Summary:
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

Reviewers: sammccall

Subscribers: ilya-biryukov, kadircet, usaxena95, cfe-commits

Tags: #clang

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

Added: 
    clang/test/Parser/switch-typo-correction.cpp

Modified: 
    clang/lib/Sema/SemaStmt.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 2104add400e0..68de776e83e6 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -730,11 +730,11 @@ StmtResult Sema::ActOnStartOfSwitchStmt(SourceLocation SwitchLoc,
 
   if (CondExpr && !CondExpr->isTypeDependent()) {
     // We have already converted the expression to an integral or enumeration
-    // type, when we parsed the switch condition. If we don't have an
-    // appropriate type now, enter the switch scope but remember that it's
-    // invalid.
-    assert(CondExpr->getType()->isIntegralOrEnumerationType() &&
-           "invalid condition type");
+    // type, when we parsed the switch condition. There are cases where we don't
+    // have an appropriate type, e.g. a typo-expr Cond was corrected to an
+    // inappropriate-type expr, we just return an error.
+    if (!CondExpr->getType()->isIntegralOrEnumerationType())
+      return StmtError();
     if (CondExpr->isKnownToHaveBooleanValue()) {
       // switch(bool_expr) {...} is often a programmer error, e.g.
       //   switch(n && mask) { ... }  // Doh - should be "n & mask".

diff  --git a/clang/test/Parser/switch-typo-correction.cpp b/clang/test/Parser/switch-typo-correction.cpp
new file mode 100644
index 000000000000..ebf1c18f2b86
--- /dev/null
+++ b/clang/test/Parser/switch-typo-correction.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+namespace c { double xxx; } // expected-note{{'c::xxx' declared here}}
+namespace d { float xxx; }
+namespace z { namespace xxx {} }
+
+void crash() {
+  switch (xxx) {} // expected-error{{use of undeclared identifier 'xxx'; did you mean }}
+}


        


More information about the cfe-commits mailing list