diff options
| author | Anders Carlsson <andersca@mac.com> | 2009-03-14 00:25:26 +0000 |
|---|---|---|
| committer | Anders Carlsson <andersca@mac.com> | 2009-03-14 00:25:26 +0000 |
| commit | 5bbe1d7ba74ed004b5f297185bf8f08e313b0c58 (patch) | |
| tree | 93511ab04b94614acb6d436a8c1766b666566ebd /clang/lib | |
| parent | c86715631cf936da5d94be15a07d14f0153f5b4c (diff) | |
| download | bcm5719-llvm-5bbe1d7ba74ed004b5f297185bf8f08e313b0c58.tar.gz bcm5719-llvm-5bbe1d7ba74ed004b5f297185bf8f08e313b0c58.zip | |
More static_assert work. Check that the assert expr is valid and show an error if it's false. Create the declaration and add it to the current context.
llvm-svn: 66995
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/AST/DeclCXX.cpp | 17 | ||||
| -rw-r--r-- | clang/lib/Sema/Sema.h | 5 | ||||
| -rw-r--r-- | clang/lib/Sema/SemaDeclCXX.cpp | 27 |
3 files changed, 49 insertions, 0 deletions
diff --git a/clang/lib/AST/DeclCXX.cpp b/clang/lib/AST/DeclCXX.cpp index 4776ce58778..fd900834caf 100644 --- a/clang/lib/AST/DeclCXX.cpp +++ b/clang/lib/AST/DeclCXX.cpp @@ -13,6 +13,7 @@ #include "clang/AST/DeclCXX.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/Expr.h" #include "clang/Basic/IdentifierTable.h" #include "llvm/ADT/STLExtras.h" using namespace clang; @@ -339,3 +340,19 @@ UsingDirectiveDecl *UsingDirectiveDecl::Create(ASTContext &C, DeclContext *DC, Used, CommonAncestor); } +StaticAssertDecl *StaticAssertDecl::Create(ASTContext &C, DeclContext *DC, + SourceLocation L, Expr *AssertExpr, + StringLiteral *Message) { + return new (C) StaticAssertDecl(DC, L, AssertExpr, Message); +} + +void StaticAssertDecl::Destroy(ASTContext& C) { + AssertExpr->Destroy(C); + Message->Destroy(C); + this->~StaticAssertDecl(); + C.Deallocate((void *)this); +} + +StaticAssertDecl::~StaticAssertDecl() { +} + diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index a86aa65aff2..54b3d197fa0 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -1554,6 +1554,11 @@ public: virtual void ActOnDelayedCXXMethodParameter(Scope *S, DeclTy *Param); virtual void ActOnFinishDelayedCXXMethodDeclaration(Scope *S, DeclTy *Method); + virtual DeclTy *ActOnStaticAssertDeclaration(SourceLocation AssertLoc, + ExprArg AssertExpr, + ExprArg AssertMessageExpr, + SourceLocation RParenLoc); + bool CheckConstructorDeclarator(Declarator &D, QualType &R, FunctionDecl::StorageClass& SC); bool CheckConstructor(CXXConstructorDecl *Constructor); diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index f5d8f6b5e12..57a6c624616 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -2234,3 +2234,30 @@ Sema::DeclTy *Sema::ActOnExceptionDeclarator(Scope *S, Declarator &D) ProcessDeclAttributes(ExDecl, D); return ExDecl; } + +Sema::DeclTy *Sema::ActOnStaticAssertDeclaration(SourceLocation AssertLoc, + ExprArg assertexpr, + ExprArg assertmessageexpr, + SourceLocation RParenLoc) { + Expr *AssertExpr = (Expr *)assertexpr.get(); + 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; + } + + 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; +} |

