[PATCH] D72242: Fix crash on value dependent bitfields in if conditions in templates

Elizabeth Andrews via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 9 11:39:06 PST 2020


eandrews updated this revision to Diff 237132.
eandrews edited the summary of this revision.
eandrews added a comment.

Semantic analysis for value dependent conditions is now skipped as per Erich's comment.  Patch adds an argument to CheckBooleanCondition to still do the required analysis for noexcept expressions.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72242/new/

https://reviews.llvm.org/D72242

Files:
  clang/include/clang/Sema/Sema.h
  clang/lib/Sema/SemaExceptionSpec.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/SemaTemplate/value-dependent-bitfield-cond.cpp


Index: clang/test/SemaTemplate/value-dependent-bitfield-cond.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaTemplate/value-dependent-bitfield-cond.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s -ast-dump | FileCheck %s --check-prefix=CHECK-AST
+// expected-no-diagnostics
+
+template <int b>
+class A {
+  int c : b;
+
+public:
+  void f() {
+    if (c)
+      ;
+  }
+};
+
+void foo() {
+
+  // CHECK-AST: ClassTemplateSpecializationDecl{{.*}}class A definition
+  // CHECK-AST: TemplateArgument integral 3
+  // CHECK-AST: FieldDecl{{.*}}c 'int'
+  // CHECK-AST-NEXT: ConstantExpr{{.*}}'int' Int: 3
+  // CHECK-AST-NEXT: SubstNonTypeTemplateParmExpr{{.*}}'int'
+  // CHECK-AST-NEXT: IntegerLiteral{{.*}}'int' 3
+  A<3> a;
+
+  // CHECK-AST: IfStmt
+  // CHECK-AST-NEXT: ImplicitCastExpr{{.*}}'bool' <IntegralToBoolean>
+  // CHECK-AST-NEXT: ImplicitCastExpr{{.*}}'int' <LValueToRValue>
+  // CHECK-AST-NEXT: MemberExpr{{.*}}'int' lvalue bitfield ->c
+  // CHECK-AST-NEXT: CXXThisExpr{{.*}}'A<3> *' implicit this
+  a.f();
+}
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -17362,7 +17362,7 @@
 }
 
 ExprResult Sema::CheckBooleanCondition(SourceLocation Loc, Expr *E,
-                                       bool IsConstexpr) {
+                                       bool IsConstexpr, bool IsNoexceptExpr) {
   DiagnoseAssignmentAsCondition(E);
   if (ParenExpr *parenE = dyn_cast<ParenExpr>(E))
     DiagnoseEqualityWithExtraParens(parenE);
@@ -17372,8 +17372,11 @@
   E = result.get();
 
   if (!E->isTypeDependent()) {
-    if (getLangOpts().CPlusPlus)
+    if (getLangOpts().CPlusPlus) {
+      if (E->isValueDependent() && !IsNoexceptExpr)
+        return E;
       return CheckCXXBooleanCondition(E, IsConstexpr); // C++ 6.4p4
+    }
 
     ExprResult ERes = DefaultFunctionArrayLvalueConversion(E);
     if (ERes.isInvalid())
Index: clang/lib/Sema/SemaExceptionSpec.cpp
===================================================================
--- clang/lib/Sema/SemaExceptionSpec.cpp
+++ clang/lib/Sema/SemaExceptionSpec.cpp
@@ -80,7 +80,9 @@
                                    Expr *NoexceptExpr,
                                    ExceptionSpecificationType &EST) {
   // FIXME: This is bogus, a noexcept expression is not a condition.
-  ExprResult Converted = CheckBooleanCondition(NoexceptLoc, NoexceptExpr);
+  ExprResult Converted = CheckBooleanCondition(NoexceptLoc, NoexceptExpr,
+                                               /*IsConstexpr*/ false,
+                                               /*IsNoexceptExpr*/ true);
   if (Converted.isInvalid())
     return Converted;
 
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -10885,7 +10885,8 @@
   /// 'if' keyword.
   /// \return true iff there were any errors
   ExprResult CheckBooleanCondition(SourceLocation Loc, Expr *E,
-                                   bool IsConstexpr = false);
+                                   bool IsConstexpr = false,
+                                   bool IsNoexceptExpr = false);
 
   /// ActOnExplicitBoolSpecifier - Build an ExplicitSpecifier from an expression
   /// found in an explicit(bool) specifier.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72242.237132.patch
Type: text/x-patch
Size: 3450 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200109/d6ba8d07/attachment.bin>


More information about the cfe-commits mailing list