r202141 - Fix for Bug 18536 - Bad alignment in clang/AST/StmpOpenMP.h

Alexey Bataev a.bataev at hotmail.com
Tue Feb 25 03:25:38 PST 2014


Author: abataev
Date: Tue Feb 25 05:25:38 2014
New Revision: 202141

URL: http://llvm.org/viewvc/llvm-project?rev=202141&view=rev
Log:
Fix for Bug 18536 - Bad alignment in clang/AST/StmpOpenMP.h

Modified:
    cfe/trunk/include/clang/AST/OpenMPClause.h
    cfe/trunk/include/clang/AST/StmtOpenMP.h
    cfe/trunk/lib/AST/Stmt.cpp

Modified: cfe/trunk/include/clang/AST/OpenMPClause.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/OpenMPClause.h?rev=202141&r1=202140&r2=202141&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/OpenMPClause.h (original)
+++ cfe/trunk/include/clang/AST/OpenMPClause.h Tue Feb 25 05:25:38 2014
@@ -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 T> class OMPVarList {
+template <class T> 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<Expr *> getVarRefs() {
     return llvm::MutableArrayRef<Expr *>(
-        reinterpret_cast<Expr **>(static_cast<T *>(this) + 1), NumVars);
+        reinterpret_cast<Expr **>(
+            reinterpret_cast<char *>(this) +
+            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())),
+        NumVars);
   }
 
   /// \brief Sets the list of variables for this clause.
   void setVarRefs(ArrayRef<Expr *> VL) {
     assert(VL.size() == NumVars &&
            "Number of variables is not the same as the preallocated buffer");
-    std::copy(VL.begin(), VL.end(),
-              reinterpret_cast<Expr **>(static_cast<T *>(this) + 1));
+    std::copy(
+        VL.begin(), VL.end(),
+        reinterpret_cast<Expr **>(
+            reinterpret_cast<char *>(this) +
+            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())));
   }
 
   /// \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<Expr *>::iterator varlist_iterator;
@@ -115,7 +126,9 @@ public:
   /// \brief Fetches list of all variables in the clause.
   ArrayRef<const Expr *> getVarRefs() const {
     return ArrayRef<const Expr *>(
-        reinterpret_cast<const Expr *const *>(static_cast<const T *>(this) + 1),
+        reinterpret_cast<const Expr *const *>(
+            reinterpret_cast<const char *>(this) +
+            llvm::RoundUpToAlignment(sizeof(T), llvm::alignOf<Expr *>())),
         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<OMPPrivateClause> {
+class OMPPrivateClause : public OMPVarListClause<OMPPrivateClause> {
   /// \brief Build clause with number of variables \a N.
   ///
   /// \param StartLoc Starting location of the clause.
@@ -260,16 +273,17 @@ class OMPPrivateClause : public OMPClaus
   ///
   OMPPrivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
                    SourceLocation EndLoc, unsigned N)
-      : OMPClause(OMPC_private, StartLoc, EndLoc),
-        OMPVarList<OMPPrivateClause>(LParenLoc, N) {}
+      : OMPVarListClause<OMPPrivateClause>(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<OMPPrivateClause>(SourceLocation(), N) {}
+      : OMPVarListClause<OMPPrivateClause>(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<OMPFirstprivateClause> {
+class OMPFirstprivateClause : public OMPVarListClause<OMPFirstprivateClause> {
   /// \brief Build clause with number of variables \a N.
   ///
   /// \param StartLoc Starting location of the clause.
@@ -320,16 +333,17 @@ class OMPFirstprivateClause : public OMP
   ///
   OMPFirstprivateClause(SourceLocation StartLoc, SourceLocation LParenLoc,
                         SourceLocation EndLoc, unsigned N)
-      : OMPClause(OMPC_firstprivate, StartLoc, EndLoc),
-        OMPVarList<OMPFirstprivateClause>(LParenLoc, N) {}
+      : OMPVarListClause<OMPFirstprivateClause>(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<OMPFirstprivateClause>(SourceLocation(), N) {}
+      : OMPVarListClause<OMPFirstprivateClause>(
+            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<OMPSharedClause> {
+class OMPSharedClause : public OMPVarListClause<OMPSharedClause> {
   /// \brief Build clause with number of variables \a N.
   ///
   /// \param StartLoc Starting location of the clause.
@@ -378,16 +392,17 @@ class OMPSharedClause : public OMPClause
   ///
   OMPSharedClause(SourceLocation StartLoc, SourceLocation LParenLoc,
                   SourceLocation EndLoc, unsigned N)
-      : OMPClause(OMPC_shared, StartLoc, EndLoc),
-        OMPVarList<OMPSharedClause>(LParenLoc, N) {}
+      : OMPVarListClause<OMPSharedClause>(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<OMPSharedClause>(SourceLocation(), N) {}
+      : OMPVarListClause<OMPSharedClause>(OMPC_shared, SourceLocation(),
+                                          SourceLocation(), SourceLocation(),
+                                          N) {}
 
 public:
   /// \brief Creates clause with a list of variables \a VL.

Modified: cfe/trunk/include/clang/AST/StmtOpenMP.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/StmtOpenMP.h?rev=202141&r1=202140&r2=202141&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/StmtOpenMP.h (original)
+++ cfe/trunk/include/clang/AST/StmtOpenMP.h Tue Feb 25 05:25:38 2014
@@ -42,6 +42,7 @@ class OMPExecutableDirective : public St
   llvm::MutableArrayRef<OMPClause *> Clauses;
   /// \brief Associated statement (if any) and expressions.
   llvm::MutableArrayRef<Stmt *> 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<OMPClause **>(static_cast<T *>(this) + 1),
-              NumClauses),
-      StmtAndExpressions(reinterpret_cast<Stmt **>(Clauses.end()),
-                         NumberOfExpressions) { }
+      : Stmt(SC), Kind(K), StartLoc(StartLoc), EndLoc(EndLoc),
+        Clauses(reinterpret_cast<OMPClause **>(
+                    reinterpret_cast<char *>(this) +
+                    llvm::RoundUpToAlignment(sizeof(T),
+                                             llvm::alignOf<OMPClause *>())),
+                NumClauses),
+        StmtAndExpressions(reinterpret_cast<Stmt **>(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 OMPE
   ///
   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<OMPClause *> Clauses,
-                                      Stmt *AssociatedStmt);
+  static OMPParallelDirective *
+  Create(const ASTContext &C, SourceLocation StartLoc, SourceLocation EndLoc,
+         ArrayRef<OMPClause *> 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

Modified: cfe/trunk/lib/AST/Stmt.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Stmt.cpp?rev=202141&r1=202140&r2=202141&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Stmt.cpp (original)
+++ cfe/trunk/lib/AST/Stmt.cpp Tue Feb 25 05:25:38 2014
@@ -1130,8 +1130,9 @@ OMPPrivateClause *OMPPrivateClause::Crea
                                            SourceLocation LParenLoc,
                                            SourceLocation EndLoc,
                                            ArrayRef<Expr *> VL) {
-  void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * VL.size(),
-                         llvm::alignOf<OMPPrivateClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * VL.size());
   OMPPrivateClause *Clause = new (Mem) OMPPrivateClause(StartLoc, LParenLoc,
                                                         EndLoc, VL.size());
   Clause->setVarRefs(VL);
@@ -1140,8 +1141,9 @@ OMPPrivateClause *OMPPrivateClause::Crea
 
 OMPPrivateClause *OMPPrivateClause::CreateEmpty(const ASTContext &C,
                                                 unsigned N) {
-  void *Mem = C.Allocate(sizeof(OMPPrivateClause) + sizeof(Expr *) * N,
-                         llvm::alignOf<OMPPrivateClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPPrivateClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * N);
   return new (Mem) OMPPrivateClause(N);
 }
 
@@ -1150,9 +1152,9 @@ OMPFirstprivateClause *OMPFirstprivateCl
                                                      SourceLocation LParenLoc,
                                                      SourceLocation EndLoc,
                                                      ArrayRef<Expr *> VL) {
-  void *Mem = C.Allocate(sizeof(OMPFirstprivateClause) +
-                         sizeof(Expr *) * VL.size(),
-                         llvm::alignOf<OMPFirstprivateClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * VL.size());
   OMPFirstprivateClause *Clause = new (Mem) OMPFirstprivateClause(StartLoc,
                                                                   LParenLoc,
                                                                   EndLoc,
@@ -1163,8 +1165,9 @@ OMPFirstprivateClause *OMPFirstprivateCl
 
 OMPFirstprivateClause *OMPFirstprivateClause::CreateEmpty(const ASTContext &C,
                                                           unsigned N) {
-  void *Mem = C.Allocate(sizeof(OMPFirstprivateClause) + sizeof(Expr *) * N,
-                         llvm::alignOf<OMPFirstprivateClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPFirstprivateClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * N);
   return new (Mem) OMPFirstprivateClause(N);
 }
 
@@ -1173,8 +1176,9 @@ OMPSharedClause *OMPSharedClause::Create
                                          SourceLocation LParenLoc,
                                          SourceLocation EndLoc,
                                          ArrayRef<Expr *> VL) {
-  void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * VL.size(),
-                         llvm::alignOf<OMPSharedClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * VL.size());
   OMPSharedClause *Clause = new (Mem) OMPSharedClause(StartLoc, LParenLoc,
                                                       EndLoc, VL.size());
   Clause->setVarRefs(VL);
@@ -1183,8 +1187,9 @@ OMPSharedClause *OMPSharedClause::Create
 
 OMPSharedClause *OMPSharedClause::CreateEmpty(const ASTContext &C,
                                               unsigned N) {
-  void *Mem = C.Allocate(sizeof(OMPSharedClause) + sizeof(Expr *) * N,
-                         llvm::alignOf<OMPSharedClause>());
+  void *Mem = C.Allocate(llvm::RoundUpToAlignment(sizeof(OMPSharedClause),
+                                                  llvm::alignOf<Expr *>()) +
+                         sizeof(Expr *) * N);
   return new (Mem) OMPSharedClause(N);
 }
 
@@ -1200,9 +1205,10 @@ OMPParallelDirective *OMPParallelDirecti
                                               SourceLocation EndLoc,
                                               ArrayRef<OMPClause *> Clauses,
                                               Stmt *AssociatedStmt) {
-  void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
-                         sizeof(OMPClause *) * Clauses.size() + sizeof(Stmt *),
-                         llvm::alignOf<OMPParallelDirective>());
+  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
+                                           llvm::alignOf<OMPClause *>());
+  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 *OMPParallelDirecti
 OMPParallelDirective *OMPParallelDirective::CreateEmpty(const ASTContext &C,
                                                         unsigned N,
                                                         EmptyShell) {
-  void *Mem = C.Allocate(sizeof(OMPParallelDirective) +
-                         sizeof(OMPClause *) * N + sizeof(Stmt *),
-                         llvm::alignOf<OMPParallelDirective>());
+  unsigned Size = llvm::RoundUpToAlignment(sizeof(OMPParallelDirective),
+                                           llvm::alignOf<OMPClause *>());
+  void *Mem = C.Allocate(Size + sizeof(OMPClause *) * N + sizeof(Stmt *));
   return new (Mem) OMPParallelDirective(N);
 }





More information about the cfe-commits mailing list