From: Alexey Bataev Date: Tue, 25 Feb 2014 11:25:38 +0000 (+0000) Subject: Fix for Bug 18536 - Bad alignment in clang/AST/StmpOpenMP.h X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=271504f86c5b97c62d2fc378b26ece3709f473f1;p=clang Fix for Bug 18536 - Bad alignment in clang/AST/StmpOpenMP.h git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@202141 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/OpenMPClause.h b/include/clang/AST/OpenMPClause.h index bc22479bd5..4a3c9e0a49 100644 --- a/include/clang/AST/OpenMPClause.h +++ b/include/clang/AST/OpenMPClause.h @@ -67,7 +67,7 @@ public: /// \brief This represents clauses with the list of variables like 'private', /// 'firstprivate', 'copyin', 'shared', or 'reduction' clauses in the /// '#pragma omp ...' directives. -template class OMPVarList { +template class OMPVarListClause : public OMPClause { friend class OMPClauseReader; /// \brief Location of '('. SourceLocation LParenLoc; @@ -78,23 +78,34 @@ protected: /// \brief Fetches list of variables associated with this clause. llvm::MutableArrayRef getVarRefs() { return llvm::MutableArrayRef( - reinterpret_cast(static_cast(this) + 1), NumVars); + reinterpret_cast( + reinterpret_cast(this) + + llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf())), + NumVars); } /// \brief Sets the list of variables for this clause. void setVarRefs(ArrayRef VL) { assert(VL.size() == NumVars && "Number of variables is not the same as the preallocated buffer"); - std::copy(VL.begin(), VL.end(), - reinterpret_cast(static_cast(this) + 1)); + std::copy( + VL.begin(), VL.end(), + reinterpret_cast( + reinterpret_cast(this) + + llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf()))); } /// \brief Build clause with number of variables \a N. /// + /// \param K Kind of the clause. + /// \param StartLoc Starting location of the clause (the clause keyword). + /// \param LParenLoc Location of '('. + /// \param EndLoc Ending location of the clause. /// \param N Number of the variables in the clause. /// - OMPVarList(SourceLocation LParenLoc, unsigned N) - : LParenLoc(LParenLoc), NumVars(N) {} + OMPVarListClause(OpenMPClauseKind K, SourceLocation StartLoc, + SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N) + : OMPClause(K, StartLoc, EndLoc), LParenLoc(LParenLoc), NumVars(N) {} public: typedef llvm::MutableArrayRef::iterator varlist_iterator; @@ -115,7 +126,9 @@ public: /// \brief Fetches list of all variables in the clause. ArrayRef getVarRefs() const { return ArrayRef( - reinterpret_cast(static_cast(this) + 1), + reinterpret_cast( + reinterpret_cast(this) + + llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf())), NumVars); } }; @@ -250,7 +263,7 @@ public: /// In this example directive '#pragma omp parallel' has clause 'private' /// with the variables 'a' and 'b'. /// -class OMPPrivateClause : public OMPClause, public OMPVarList { +class OMPPrivateClause : public OMPVarListClause { /// \brief Build clause with number of variables \a N. /// /// \param StartLoc Starting location of the clause. @@ -260,16 +273,17 @@ class OMPPrivateClause : public OMPClause, public OMPVarList { /// OMPPrivateClause(SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N) - : OMPClause(OMPC_private, StartLoc, EndLoc), - OMPVarList(LParenLoc, N) {} + : OMPVarListClause(OMPC_private, StartLoc, LParenLoc, + EndLoc, N) {} /// \brief Build an empty clause. /// /// \param N Number of variables. /// explicit OMPPrivateClause(unsigned N) - : OMPClause(OMPC_private, SourceLocation(), SourceLocation()), - OMPVarList(SourceLocation(), N) {} + : OMPVarListClause(OMPC_private, SourceLocation(), + SourceLocation(), SourceLocation(), + N) {} public: /// \brief Creates clause with a list of variables \a VL. @@ -309,8 +323,7 @@ public: /// In this example directive '#pragma omp parallel' has clause 'firstprivate' /// with the variables 'a' and 'b'. /// -class OMPFirstprivateClause : public OMPClause, - public OMPVarList { +class OMPFirstprivateClause : public OMPVarListClause { /// \brief Build clause with number of variables \a N. /// /// \param StartLoc Starting location of the clause. @@ -320,16 +333,17 @@ class OMPFirstprivateClause : public OMPClause, /// OMPFirstprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N) - : OMPClause(OMPC_firstprivate, StartLoc, EndLoc), - OMPVarList(LParenLoc, N) {} + : OMPVarListClause(OMPC_firstprivate, StartLoc, + LParenLoc, EndLoc, N) {} /// \brief Build an empty clause. /// /// \param N Number of variables. /// explicit OMPFirstprivateClause(unsigned N) - : OMPClause(OMPC_firstprivate, SourceLocation(), SourceLocation()), - OMPVarList(SourceLocation(), N) {} + : OMPVarListClause( + OMPC_firstprivate, SourceLocation(), SourceLocation(), + SourceLocation(), N) {} public: /// \brief Creates clause with a list of variables \a VL. @@ -368,7 +382,7 @@ public: /// In this example directive '#pragma omp parallel' has clause 'shared' /// with the variables 'a' and 'b'. /// -class OMPSharedClause : public OMPClause, public OMPVarList { +class OMPSharedClause : public OMPVarListClause { /// \brief Build clause with number of variables \a N. /// /// \param StartLoc Starting location of the clause. @@ -378,16 +392,17 @@ class OMPSharedClause : public OMPClause, public OMPVarList { /// OMPSharedClause(SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc, unsigned N) - : OMPClause(OMPC_shared, StartLoc, EndLoc), - OMPVarList(LParenLoc, N) {} + : OMPVarListClause(OMPC_shared, StartLoc, LParenLoc, + EndLoc, N) {} /// \brief Build an empty clause. /// /// \param N Number of variables. /// explicit OMPSharedClause(unsigned N) - : OMPClause(OMPC_shared, SourceLocation(), SourceLocation()), - OMPVarList(SourceLocation(), N) {} + : OMPVarListClause(OMPC_shared, SourceLocation(), + SourceLocation(), SourceLocation(), + N) {} public: /// \brief Creates clause with a list of variables \a VL. diff --git a/include/clang/AST/StmtOpenMP.h b/include/clang/AST/StmtOpenMP.h index 6982960592..a9e2855a6a 100644 --- a/include/clang/AST/StmtOpenMP.h +++ b/include/clang/AST/StmtOpenMP.h @@ -42,6 +42,7 @@ class OMPExecutableDirective : public Stmt { llvm::MutableArrayRef Clauses; /// \brief Associated statement (if any) and expressions. llvm::MutableArrayRef StmtAndExpressions; + protected: /// \brief Build instance of directive of class \a K. /// @@ -54,11 +55,14 @@ protected: OMPExecutableDirective(const T *, StmtClass SC, OpenMPDirectiveKind K, SourceLocation StartLoc, SourceLocation EndLoc, unsigned NumClauses, unsigned NumberOfExpressions) - : Stmt(SC), Kind(K), StartLoc(StartLoc), EndLoc(EndLoc), - Clauses(reinterpret_cast(static_cast(this) + 1), - NumClauses), - StmtAndExpressions(reinterpret_cast(Clauses.end()), - NumberOfExpressions) { } + : Stmt(SC), Kind(K), StartLoc(StartLoc), EndLoc(EndLoc), + Clauses(reinterpret_cast( + reinterpret_cast(this) + + llvm::RoundUpToAlignment(sizeof(T), + llvm::alignOf())), + NumClauses), + StmtAndExpressions(reinterpret_cast(Clauses.end()), + NumberOfExpressions) {} /// \brief Sets the list of variables for this clause. /// @@ -70,9 +74,7 @@ protected: /// /// /param S Associated statement. /// - void setAssociatedStmt(Stmt *S) { - StmtAndExpressions[0] = S; - } + void setAssociatedStmt(Stmt *S) { StmtAndExpressions[0] = S; } public: /// \brief Returns starting location of directive kind. @@ -104,9 +106,7 @@ public: } /// \brief Returns statement associated with the directive. - Stmt *getAssociatedStmt() const { - return StmtAndExpressions[0]; - } + Stmt *getAssociatedStmt() const { return StmtAndExpressions[0]; } OpenMPDirectiveKind getDirectiveKind() const { return Kind; } @@ -141,16 +141,17 @@ class OMPParallelDirective : public OMPExecutableDirective { /// OMPParallelDirective(SourceLocation StartLoc, SourceLocation EndLoc, unsigned N) - : OMPExecutableDirective(this, OMPParallelDirectiveClass, OMPD_parallel, - StartLoc, EndLoc, N, 1) { } + : OMPExecutableDirective(this, OMPParallelDirectiveClass, OMPD_parallel, + StartLoc, EndLoc, N, 1) {} /// \brief Build an empty directive. /// /// \param N Number of clauses. /// explicit OMPParallelDirective(unsigned N) - : OMPExecutableDirective(this, OMPParallelDirectiveClass, OMPD_parallel, - SourceLocation(), SourceLocation(), N, 1) { } + : OMPExecutableDirective(this, OMPParallelDirectiveClass, OMPD_parallel, + SourceLocation(), SourceLocation(), N, 1) {} + public: /// \brief Creates directive with a list of \a Clauses. /// @@ -160,11 +161,9 @@ public: /// \param Clauses List of clauses. /// \param AssociatedStmt Statement associated with the directive. /// - static OMPParallelDirective *Create(const ASTContext &C, - SourceLocation StartLoc, - SourceLocation EndLoc, - ArrayRef Clauses, - Stmt *AssociatedStmt); + static OMPParallelDirective * + Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc, + ArrayRef Clauses, Stmt *AssociatedStmt); /// \brief Creates an empty directive with the place for \a N clauses. /// @@ -179,6 +178,6 @@ public: } }; -} // end namespace clang +} // end namespace clang #endif diff --git a/lib/AST/Stmt.cpp b/lib/AST/Stmt.cpp index de851615cb..f4d90eaded 100644 --- a/lib/AST/Stmt.cpp +++ b/lib/AST/Stmt.cpp @@ -1130,8 +1130,9 @@ OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C, SourceLocation LParenLoc, SourceLocation EndLoc, ArrayRef VL) { - void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(), - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause), + llvm::alignOf()) + + sizeof(Expr *) * VL.size()); OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc, EndLoc, VL.size()); Clause->setVarRefs(VL); @@ -1140,8 +1141,9 @@ OMPPrivateClause *OMPPrivateClause::Create(const ASTContext &C, OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C, unsigned N) { - void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N, - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause), + llvm::alignOf()) + + sizeof(Expr *) * N); return new (Mem) OMPPrivateClause(N); } @@ -1150,9 +1152,9 @@ OMPFirstprivateClause *OMPFirstprivateClause::Create(const ASTContext &C, SourceLocation LParenLoc, SourceLocation EndLoc, ArrayRef VL) { - void *Mem = C.Allocate(sizeof(OMPFirstprivateClause) + - sizeof(Expr *) * VL.size(), - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause), + llvm::alignOf()) + + sizeof(Expr *) * VL.size()); OMPFirstprivateClause *Clause = new (Mem) OMPFirstprivateClause(StartLoc, LParenLoc, EndLoc, @@ -1163,8 +1165,9 @@ OMPFirstprivateClause *OMPFirstprivateClause::Create(const ASTContext &C, OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C, unsigned N) { - void *Mem = C.Allocate(sizeof(OMPFirstprivateClause) + sizeof(Expr *) * N, - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause), + llvm::alignOf()) + + sizeof(Expr *) * N); return new (Mem) OMPFirstprivateClause(N); } @@ -1173,8 +1176,9 @@ OMPSharedClause *OMPSharedClause::Create(const ASTContext &C, SourceLocation LParenLoc, SourceLocation EndLoc, ArrayRef VL) { - void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * VL.size(), - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause), + llvm::alignOf()) + + sizeof(Expr *) * VL.size()); OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc, EndLoc, VL.size()); Clause->setVarRefs(VL); @@ -1183,8 +1187,9 @@ OMPSharedClause *OMPSharedClause::Create(const ASTContext &C, OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C, unsigned N) { - void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * N, - llvm::alignOf()); + void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause), + llvm::alignOf()) + + sizeof(Expr *) * N); return new (Mem) OMPSharedClause(N); } @@ -1200,9 +1205,10 @@ OMPParallelDirective *OMPParallelDirective::Create( SourceLocation EndLoc, ArrayRef Clauses, Stmt *AssociatedStmt) { - void *Mem = C.Allocate(sizeof(OMPParallelDirective) + - sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *), - llvm::alignOf()); + unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective), + llvm::alignOf()); + void *Mem = C.Allocate(Size + sizeof(OMPClause *) * Clauses.size() + + sizeof(Stmt *)); OMPParallelDirective *Dir = new (Mem) OMPParallelDirective(StartLoc, EndLoc, Clauses.size()); Dir->setClauses(Clauses); @@ -1213,8 +1219,8 @@ OMPParallelDirective *OMPParallelDirective::Create( OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C, unsigned N, EmptyShell) { - void *Mem = C.Allocate(sizeof(OMPParallelDirective) + - sizeof(OMPClause *) * N + sizeof(Stmt *), - llvm::alignOf()); + unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective), + llvm::alignOf()); + void *Mem = C.Allocate(Size + sizeof(OMPClause *) * N + sizeof(Stmt *)); return new (Mem) OMPParallelDirective(N); }