]> granicus.if.org Git - clang/commitdiff
Handle dependent types/exprs in static_assert expressions.
authorAnders Carlsson <andersca@mac.com>
Sat, 14 Mar 2009 00:33:21 +0000 (00:33 +0000)
committerAnders Carlsson <andersca@mac.com>
Sat, 14 Mar 2009 00:33:21 +0000 (00:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@66997 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/Expr.cpp
lib/Sema/SemaDeclCXX.cpp
test/SemaCXX/static-assert.cpp

index 273b5ed72f797c761ab38634911f07fe992ad1af..e34412e19092cc57aa863004ca01435ea0834f97 100644 (file)
@@ -1044,6 +1044,7 @@ static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) {
 }
 
 static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) {
+  assert(!E->isValueDependent() && "Should not see value dependent exprs!");
   if (!E->getType()->isIntegralType()) {
     return ICEDiag(2, E->getLocStart());
   }
index 57a6c6246166340346ec013ec49cfc2f83d24425..37035b98c75ca47537b257465c5c4c840ffa631f 100644 (file)
@@ -2243,20 +2243,23 @@ Sema::DeclTy *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc,
   StringLiteral *AssertMessage = 
     cast<StringLiteral>((Expr *)assertmessageexpr.get());
 
-  llvm::APSInt Value(32);
-  if (!AssertExpr->isIntegerConstantExpr(Value, Context)) {
-    Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) <<
-      AssertExpr->getSourceRange();
-    return 0;
-  }
+  if (!AssertExpr->isTypeDependent() && !AssertExpr->isValueDependent()) {
+    llvm::APSInt Value(32);
+    if (!AssertExpr->isIntegerConstantExpr(Value, Context)) {
+      Diag(AssertLoc, diag::err_static_assert_expression_is_not_constant) <<
+        AssertExpr->getSourceRange();
+      return 0;
+    }
 
+    if (Value == 0) {
+      std::string str(AssertMessage->getStrData(), 
+                      AssertMessage->getByteLength());
+      Diag(AssertLoc, diag::err_static_assert_failed) << str;
+    }
+  }
+  
   Decl *Decl = StaticAssertDecl::Create(Context, CurContext, AssertLoc, 
                                         AssertExpr, AssertMessage);
-  if (Value == 0) {
-    std::string str(AssertMessage->getStrData(), 
-                    AssertMessage->getByteLength());
-    Diag(AssertLoc, diag::err_static_assert_failed) << str;
-  }
   
   CurContext->addDecl(Decl);
   return Decl;
index a2b0b52d5d1b7fa3f1fedb41e00b814b5afbe998..94ffff86cd29c4e1c6582383ca38639640a3bb51 100644 (file)
@@ -13,3 +13,12 @@ void g() {
 class C {
     static_assert(false, "false is false"); // expected-error {{static_assert failed "false is false"}}
 };
+
+template<int N> struct T {
+    static_assert(N == 2, "N is not 2!");
+};
+
+template<typename T> struct S {
+    static_assert(sizeof(T) > sizeof(char), "Type not big enough!");
+};
+