[PATCH] D105498: [clang] Remove assumption about SourceLocation alignment.

Simon Tatham via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 20 06:08:20 PDT 2021


simon_tatham updated this revision to Diff 360104.
simon_tatham added a comment.

... and removed an unused function from the previous version.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105498

Files:
  clang/include/clang/AST/DeclObjC.h
  clang/lib/AST/DeclObjC.cpp


Index: clang/lib/AST/DeclObjC.cpp
===================================================================
--- clang/lib/AST/DeclObjC.cpp
+++ clang/lib/AST/DeclObjC.cpp
@@ -28,6 +28,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
 #include <cassert>
@@ -867,21 +868,21 @@
 }
 
 void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
-                                         ArrayRef<ParmVarDecl*> Params,
-                                         ArrayRef<SourceLocation> SelLocs) {
-  ParamsAndSelLocs = nullptr;
-  NumParams = Params.size();
-  if (Params.empty() && SelLocs.empty())
-    return;
-
-  static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
-                "Alignment not sufficient for SourceLocation");
+                                         ArrayRef<ParmVarDecl *> ParamsIn,
+                                         ArrayRef<SourceLocation> SelLocsIn) {
+  Params = nullptr;
+  NumParams = ParamsIn.size();
+  SelLocs = nullptr;
+
+  if (!ParamsIn.empty()) {
+    Params = C.Allocate<ParmVarDecl *>(ParamsIn.size());
+    std::copy(ParamsIn.begin(), ParamsIn.end(), Params);
+  }
 
-  unsigned Size = sizeof(ParmVarDecl *) * NumParams +
-                  sizeof(SourceLocation) * SelLocs.size();
-  ParamsAndSelLocs = C.Allocate(Size);
-  std::copy(Params.begin(), Params.end(), getParams());
-  std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
+  if (!SelLocsIn.empty()) {
+    SelLocs = C.Allocate<SourceLocation>(SelLocsIn.size());
+    std::copy(SelLocsIn.begin(), SelLocsIn.end(), SelLocs);
+  }
 }
 
 void ObjCMethodDecl::getSelectorLocs(
Index: clang/include/clang/AST/DeclObjC.h
===================================================================
--- clang/include/clang/AST/DeclObjC.h
+++ clang/include/clang/AST/DeclObjC.h
@@ -150,11 +150,13 @@
   /// Type source information for the return type.
   TypeSourceInfo *ReturnTInfo;
 
-  /// Array of ParmVarDecls for the formal parameters of this method
-  /// and optionally followed by selector locations.
-  void *ParamsAndSelLocs = nullptr;
+  /// Array of the formal parameters of this method.
+  ParmVarDecl **Params = nullptr;
   unsigned NumParams = 0;
 
+  /// Source locations for the selector identifiers.
+  SourceLocation *SelLocs = nullptr;
+
   /// List of attributes for this method declaration.
   SourceLocation DeclEndLoc; // the location of the ';' or '{'.
 
@@ -192,20 +194,13 @@
 
   /// Get a pointer to the stored selector identifiers locations array.
   /// No locations will be stored if HasStandardSelLocs is true.
-  SourceLocation *getStoredSelLocs() {
-    return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
-  }
-  const SourceLocation *getStoredSelLocs() const {
-    return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
-  }
+  SourceLocation *getStoredSelLocs() { return SelLocs; }
+  const SourceLocation *getStoredSelLocs() const { return SelLocs; }
 
-  /// Get a pointer to the stored selector identifiers locations array.
-  /// No locations will be stored if HasStandardSelLocs is true.
-  ParmVarDecl **getParams() {
-    return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
-  }
+  /// Get a pointer to the parameter array.
+  ParmVarDecl **getParams() { return Params; }
   const ParmVarDecl *const *getParams() const {
-    return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
+    return const_cast<const ParmVarDecl *const *>(Params);
   }
 
   /// Get the number of stored selector identifiers locations.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105498.360104.patch
Type: text/x-patch
Size: 3686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210720/43692aff/attachment.bin>


More information about the cfe-commits mailing list