]> granicus.if.org Git - clang/commitdiff
[NFC] Modernize MacroArgs using TrailingObjects
authorFaisal Vali <faisalv@yahoo.com>
Thu, 28 Sep 2017 01:50:23 +0000 (01:50 +0000)
committerFaisal Vali <faisalv@yahoo.com>
Thu, 28 Sep 2017 01:50:23 +0000 (01:50 +0000)
Refactor MacroArgs to use TrailingObjects when creating a variably sized object on the heap to store the unexpanded tokens immediately after the MacroArgs object.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314372 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Lex/MacroArgs.h
lib/Lex/MacroArgs.cpp

index cfe46ceb0979768f3a364b1fffc1915cd1baee65..ac02748191f8b6a84541b374aff93eed1f4da047 100644 (file)
@@ -17,6 +17,7 @@
 #include "clang/Basic/LLVM.h"
 #include "clang/Lex/Token.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/TrailingObjects.h"
 #include <vector>
 
 namespace clang {
@@ -26,7 +27,10 @@ namespace clang {
 
 /// MacroArgs - An instance of this class captures information about
 /// the formal arguments specified to a function-like macro invocation.
-class MacroArgs {
+class MacroArgs final 
+    : private llvm::TrailingObjects<MacroArgs, Token> {
+
+  friend TrailingObjects;
   /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the
   /// arguments.  All of the actual argument tokens are allocated immediately
   /// after the MacroArgs object in memory.  This is all of the arguments
index f791d8d4bacc213aa5d31be70cdc05d48fd2042b..c816fee085d65900ac2c02657c8c8ec8ca716c4e 100644 (file)
@@ -33,7 +33,7 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI,
   // See if we have an entry with a big enough argument list to reuse on the
   // free list.  If so, reuse it.
   for (MacroArgs **Entry = &PP.MacroArgCache; *Entry;
-       Entry = &(*Entry)->ArgCache)
+       Entry = &(*Entry)->ArgCache) {
     if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
         (*Entry)->NumUnexpArgTokens < ClosestMatch) {
       ResultEnt = Entry;
@@ -44,14 +44,12 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI,
       // Otherwise, use the best fit.
       ClosestMatch = (*Entry)->NumUnexpArgTokens;
     }
-
+  }
   MacroArgs *Result;
   if (!ResultEnt) {
-    // Allocate memory for a MacroArgs object with the lexer tokens at the end.
-    Result = (MacroArgs *)malloc(sizeof(MacroArgs) +
-                                 UnexpArgTokens.size() * sizeof(Token));
-    // Construct the MacroArgs object.
-    new (Result)
+    // Allocate memory for a MacroArgs object with the lexer tokens at the end,
+    // and construct the MacroArgs object.
+    Result = new (std::malloc(totalSizeToAlloc<Token>(UnexpArgTokens.size())))
         MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
   } else {
     Result = *ResultEnt;
@@ -63,9 +61,14 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI,
   }
 
   // Copy the actual unexpanded tokens to immediately after the result ptr.
-  if (!UnexpArgTokens.empty())
+  if (!UnexpArgTokens.empty()) {
+    static_assert(std::is_trivially_copyable_v<Token>,
+                  "assume trivial copyability if copying into the "
+                  "uninitialized array (as opposed to reusing a cached "
+                  "MacroArgs)");
     std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(), 
-              const_cast<Token*>(Result->getUnexpArgument(0)));
+              Result->getTrailingObjects<Token>());
+  }
 
   return Result;
 }
@@ -93,6 +96,8 @@ MacroArgs *MacroArgs::deallocate() {
   // Run the dtor to deallocate the vectors.
   this->~MacroArgs();
   // Release the memory for the object.
+  static_assert(std::is_trivially_destructible_v<Token>,
+                "assume trivially destructible and forego destructors");
   free(this);
   
   return Next;
@@ -115,7 +120,7 @@ unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
 const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
   // The unexpanded argument tokens start immediately after the MacroArgs object
   // in memory.
-  const Token *Start = (const Token *)(this+1);
+  const Token *Start = getTrailingObjects<Token>();
   const Token *Result = Start;
   // Scan to find Arg.
   for (; Arg; ++Result) {